-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: main
Are you sure you want to change the base?
Changes from all commits
53991d6
c791ad5
090e3ac
aef2dca
7c3c9e9
f8fb772
5781493
8c398e3
ea2cbd8
4ce6e37
acfb6af
608d261
10b10c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> { | ||||||||||||||
|
||||||||||||||
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) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||
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: | ||||||||||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
case RESET: | ||||||||||||||
return CollectionUtils.array(String.class, Number.class, Boolean.class); | ||||||||||||||
default: | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public void change(Event event, Object[] delta, Changer.ChangeMode mode){ | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public String toString(Event event, boolean debug) { | ||||||||||||||
return "flag " + exprFlag.toString(event, debug) + " of region " + getExpr().toString(event, debug) ; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
} | ||||||||||||||
} |
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> { | ||||||||||||||||||||||||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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; | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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"; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs documentation annotations