Skip to content

Add expressions Region Flag and Region Priority #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.skriptlang.skriptworldguard.elements.expressions;

import ch.njol.skript.classes.Changer;
import ch.njol.skript.expressions.base.PropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.flags.DoubleFlag;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.IntegerFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.StringFlag;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.event.Event;
import org.skriptlang.skriptworldguard.SkriptWorldGuard;
import org.skriptlang.skriptworldguard.worldguard.WorldGuardRegion;
import java.util.ArrayList;
import java.util.List;

public class ExprRegionFlag extends PropertyExpression<WorldGuardRegion, Object> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs documentation annotations


private static final WorldGuard WORLD_GUARD = WorldGuard.getInstance();

static {
register(ExprRegionFlag.class, Object.class, "[worldguard] flag[s] %strings%", "worldguardregions");
}

private Expression<String> exprFlag;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] expression, int matchedPattern, Kleenean kleenean, ParseResult parseResult) {
exprFlag = (Expression<String>) expression[0];
setExpr((Expression<? extends WorldGuardRegion>) expression[1]);
return true;
}

@Override
protected Object[] get(Event event, WorldGuardRegion[] regions) {
String flag = exprFlag.getSingle(event);
if(flag != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should prefer to exit if the condition fails. Essentially, this helps prevent code from flying off into space with a bunch of indents. Example:

if (flag == null)
    return;
flag.whatever();

Also, your if statements should have a space between the f and opening parenthesis )

Flag<?> flagMatch = Flags.fuzzyMatchFlag(WORLD_GUARD.getFlagRegistry(), flag);
List<Object> result = new ArrayList<>();
for (WorldGuardRegion region : regions) {
ProtectedRegion rg = region.getRegion();
result.add(rg.getFlag(flagMatch));
}
return result.toArray(new Object[0]);
}
return null;
}

@Override
public Class<?>[] acceptChange(ChangeMode mode){
switch (mode){
case SET:
case DELETE:
case RESET:
return CollectionUtils.array(String.class, Number.class, Boolean.class);
default:
return null;
}
}

public void change(Event event, Object[] delta, Changer.ChangeMode mode){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to worry about warnings IMO. You can just silently fail.

Flag<?> flagMatch;
String flag = exprFlag.getSingle(event);
WorldGuardRegion[] regions = getExpr().getArray(event);
if(flag == null || regions == null){
return;
}
WorldGuard wg = WorldGuard.getInstance();
flagMatch = Flags.fuzzyMatchFlag(wg.getFlagRegistry(), flag);
if(flagMatch != null) {
for (WorldGuardRegion region : regions) {
ProtectedRegion rg = region.getRegion();
switch (mode) {
case SET:
if (delta != null) {
if (delta[0] instanceof Boolean) {
if ((Boolean) delta[0]) {
rg.setFlag((StateFlag) flagMatch, StateFlag.State.ALLOW);
} else {
rg.setFlag((StateFlag) flagMatch, StateFlag.State.DENY);
}
} else if (delta[0] instanceof String) {
rg.setFlag((StringFlag) flagMatch, (String) delta[0]);
} else if (delta[0] instanceof Integer) {
rg.setFlag((IntegerFlag) flagMatch, (int) delta[0]);
} else if (delta[0] instanceof Double) {
rg.setFlag((DoubleFlag) flagMatch, (double) delta[0]);
} else {
SkriptWorldGuard.getInstance().getLogger().warning("Region flag " + flagMatch.getName() + " cannot be set to: " + delta[0]);
}
}
break;
case RESET:
case DELETE:
rg.setFlag(flagMatch, null);
break;
default:
assert false;
}
}
}else{
SkriptWorldGuard.getInstance().getLogger().warning("Could not find flag " + flagMatch.getName());
}
}

@Override
public Class<? extends Flag> getReturnType() {
return Flag.class;
}
Comment on lines +115 to +117
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Class<? extends Flag> getReturnType() {
return Flag.class;
}
public Class<Object> getReturnType() {
return Object.class;
}


@Override
public String toString(Event event, boolean debug) {
return "flag " + exprFlag.toString(event, debug) + " of region " + getExpr().toString(event, debug) ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "flag " + exprFlag.toString(event, debug) + " of region " + getExpr().toString(event, debug) ;
return "the worldguard flag " + exprFlag.toString(event, debug) + " of " + getExpr().toString(event, debug) ;

region is not part of the syntax and thus not necessary

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.skriptlang.skriptworldguard.elements.expressions;

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.util.coll.CollectionUtils;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.event.Event;
import org.skriptlang.skriptworldguard.worldguard.WorldGuardRegion;

public class ExprRegionPriority extends SimplePropertyExpression<WorldGuardRegion, Number> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs documentation annotations


static {
register(ExprRegionPriority.class, Number.class, "priority", "worldguardregions");
}

@Override
public Number convert(WorldGuardRegion region) {
return region.getRegion().getPriority();
}

@Override
public Class<?>[] acceptChange(final ChangeMode mode){
switch(mode){
case SET:
case ADD:
case REMOVE:
return CollectionUtils.array(Number.class);
case RESET:
case DELETE:
return CollectionUtils.array();
Comment on lines +24 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case SET:
case ADD:
case REMOVE:
return CollectionUtils.array(Number.class);
case RESET:
case DELETE:
return CollectionUtils.array();
case SET:
case ADD:
case REMOVE:
case RESET:
case DELETE:
return CollectionUtils.array(Number.class);

you can just combine them :)

default:
return null;
}
}

public void change(Event event, Object[] delta, ChangeMode mode) {
WorldGuardRegion[] regions = getExpr().getArray(event);
assert regions != null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert regions != null;

shouldn't need to assert here

for (WorldGuardRegion region : regions) {
if (region != null) {
ProtectedRegion protectedRegion = region.getRegion();
if (delta != null) {
switch (mode) {
case SET:
protectedRegion.setPriority(((Number) delta[0]).intValue());
break;
case ADD:
protectedRegion.setPriority(protectedRegion.getPriority() + ((Number) delta[0]).intValue());
break;
case REMOVE:
protectedRegion.setPriority(protectedRegion.getPriority() - ((Number) delta[0]).intValue());
break;
}
} else {
switch (mode) {
case RESET:
case DELETE:
protectedRegion.setPriority(0);
}
}
}
}
}

@Override
public Class<? extends Number> getReturnType() {
return Number.class;
}

@Override
protected String getPropertyName() {
return "priority";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

}