-
-
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
Changes from 5 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,129 @@ | ||||||
package org.skriptlang.skriptworldguard.elements.expressions; | ||||||
|
||||||
|
||||||
import ch.njol.skript.Skript; | ||||||
import ch.njol.skript.classes.Changer; | ||||||
import ch.njol.skript.lang.Expression; | ||||||
import ch.njol.skript.lang.ExpressionType; | ||||||
import ch.njol.skript.lang.SkriptParser; | ||||||
import ch.njol.skript.lang.util.SimpleExpression; | ||||||
import ch.njol.util.Kleenean; | ||||||
import ch.njol.util.coll.CollectionUtils; | ||||||
import com.sk89q.worldedit.bukkit.BukkitAdapter; | ||||||
import com.sk89q.worldguard.WorldGuard; | ||||||
import com.sk89q.worldguard.protection.flags.*; | ||||||
import com.sk89q.worldguard.protection.managers.RegionManager; | ||||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion; | ||||||
import com.sk89q.worldguard.protection.regions.RegionContainer; | ||||||
import org.bukkit.World; | ||||||
import org.bukkit.event.Event; | ||||||
import org.skriptlang.skriptworldguard.SkriptWorldGuard; | ||||||
|
||||||
public class ExprRegionFlag extends SimpleExpression<String> { | ||||||
|
||||||
static { | ||||||
|
||||||
Skript.registerExpression(ExprRegionFlag.class, String.class, ExpressionType.COMBINED, "[the] flag %string% of region %string% in [world] %world%"); | ||||||
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 can use the worldguardregion classinfo here |
||||||
|
||||||
} | ||||||
|
||||||
private Expression<String> flag; | ||||||
private Expression<String> region; | ||||||
private Expression<World> world; | ||||||
|
||||||
@SuppressWarnings("unchecked") | ||||||
@Override | ||||||
public boolean init(Expression<?>[] expression, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { | ||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
flag = (Expression<String>) expression[0]; | ||||||
region = (Expression<String>) expression[1]; | ||||||
world = (Expression<World>) expression[2]; | ||||||
return true; | ||||||
|
||||||
} | ||||||
|
||||||
@Override | ||||||
protected String[] get(Event event) { | ||||||
Flag<?> fl = null; | ||||||
|
||||||
WorldGuard wg = WorldGuard.getInstance(); | ||||||
RegionContainer container = wg.getPlatform().getRegionContainer(); | ||||||
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. Can these be put in static fields instead of getting them each time? Not sure the details on WG's API, so just a question. |
||||||
RegionManager regions = container.get(BukkitAdapter.adapt(world.getSingle(event))); | ||||||
ProtectedRegion rg = regions.getRegion(region.getSingle(event)); | ||||||
|
||||||
fl = Flags.fuzzyMatchFlag(wg.getFlagRegistry(), flag.getSingle(event)); | ||||||
|
||||||
Object value = rg.getFlag(fl); | ||||||
|
||||||
return new String[]{value.toString()}; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public String toString(Event arg0, boolean arg1) { | ||||||
return "WorldGuard region flag"; | ||||||
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. the toString should represent the actual input of the user |
||||||
} | ||||||
|
||||||
@Override | ||||||
public Class<? extends String> getReturnType() { | ||||||
return String.class; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean isSingle() { | ||||||
return true; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Class<?>[] acceptChange(final 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.
Suggested change
|
||||||
if (mode == Changer.ChangeMode.SET || mode == Changer.ChangeMode.DELETE) { return CollectionUtils.array(String.class);} | ||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return null; | ||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
public void change(Event e, Object[] delta, Changer.ChangeMode mode){ | ||||||
|
||||||
Flag<?> fl = null; | ||||||
|
||||||
WorldGuard wg = WorldGuard.getInstance(); | ||||||
RegionContainer container = wg.getPlatform().getRegionContainer(); | ||||||
RegionManager regions = container.get(BukkitAdapter.adapt(world.getSingle(e))); | ||||||
|
||||||
fl = Flags.fuzzyMatchFlag(wg.getFlagRegistry(), flag.getSingle(e)); | ||||||
ProtectedRegion rg = regions.getRegion(region.getSingle(e)); | ||||||
|
||||||
if (rg != null && fl != null){ | ||||||
if (mode == Changer.ChangeMode.SET && delta != null){ | ||||||
if (delta[0] instanceof Boolean) { | ||||||
if ((Boolean) delta[0]) { | ||||||
rg.setFlag((StateFlag) fl, StateFlag.State.ALLOW); | ||||||
} else { | ||||||
rg.setFlag((StateFlag) fl, StateFlag.State.DENY); | ||||||
} | ||||||
} | ||||||
else if (delta[0] instanceof String) { | ||||||
rg.setFlag((StringFlag) fl, (String) delta[0]); | ||||||
} else if (delta[0] instanceof Integer) { | ||||||
rg.setFlag((IntegerFlag) fl, (int) delta[0]); | ||||||
} else if (delta[0] instanceof Double) { | ||||||
rg.setFlag((DoubleFlag) fl, (double) delta[0]); | ||||||
} else { | ||||||
SkriptWorldGuard.getInstance().getLogger().warning("Region flag " + "\"" + fl.getName() + "\"" + " cannot be set to: " + delta[0]); | ||||||
} | ||||||
} | ||||||
else if(mode == Changer.ChangeMode.DELETE){ | ||||||
rg.setFlag(fl, null); | ||||||
} | ||||||
else{ | ||||||
SkriptWorldGuard.getInstance().getLogger().warning("A flag can only be set or cleared."); | ||||||
} | ||||||
}else{ | ||||||
if (rg == null) { | ||||||
SkriptWorldGuard.getInstance().getLogger().warning("Could not find region " + "\"" + rg.getId() +"\"."); | ||||||
} | ||||||
if (flag == null){ | ||||||
SkriptWorldGuard.getInstance().getLogger().warning("Could not find flag " + "\"" + flag.getSingle(e) +"\"."); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,95 @@ | ||||||||
package org.skriptlang.skriptworldguard.elements.expressions; | ||||||||
|
||||||||
|
||||||||
import ch.njol.skript.Skript; | ||||||||
import ch.njol.skript.classes.Changer; | ||||||||
import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||||||||
import ch.njol.skript.lang.Expression; | ||||||||
import ch.njol.skript.lang.ExpressionType; | ||||||||
import ch.njol.skript.lang.SkriptParser; | ||||||||
import ch.njol.skript.lang.util.SimpleExpression; | ||||||||
import ch.njol.util.Kleenean; | ||||||||
import ch.njol.util.coll.CollectionUtils; | ||||||||
|
||||||||
import com.sk89q.worldedit.bukkit.BukkitAdapter; | ||||||||
import com.sk89q.worldguard.WorldGuard; | ||||||||
import com.sk89q.worldguard.protection.flags.*; | ||||||||
import com.sk89q.worldguard.protection.managers.RegionManager; | ||||||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion; | ||||||||
import com.sk89q.worldguard.protection.regions.RegionContainer; | ||||||||
import org.bukkit.World; | ||||||||
import org.bukkit.event.Event; | ||||||||
import org.skriptlang.skriptworldguard.SkriptWorldGuard; | ||||||||
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"); | ||||||||
|
||||||||
} | ||||||||
|
||||||||
|
||||||||
private Expression<WorldGuardRegion> region; | ||||||||
|
||||||||
@SuppressWarnings("unchecked") | ||||||||
@Override | ||||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
public boolean init(Expression<?>[] expression, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { | ||||||||
region = (Expression<WorldGuardRegion>) expression[0]; | ||||||||
return true; | ||||||||
|
||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
public Number convert(WorldGuardRegion rg) { | ||||||||
|
||||||||
Number value = rg.getRegion().getPriority(); | ||||||||
|
||||||||
return value; | ||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
protected String getPropertyName() { | ||||||||
return "priority"; | ||||||||
} | ||||||||
|
||||||||
|
||||||||
@Override | ||||||||
public String toString(Event arg0, boolean arg1) { | ||||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return "WorldGuard region priority"; | ||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
public Class<? extends Number> getReturnType() { | ||||||||
return Number.class; | ||||||||
} | ||||||||
|
||||||||
|
||||||||
|
||||||||
@Override | ||||||||
public Class<?>[] acceptChange(final Changer.ChangeMode mode){ | ||||||||
if (mode == Changer.ChangeMode.SET) { return CollectionUtils.array(Number.class);} | ||||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return null; | ||||||||
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.
Suggested change
|
||||||||
|
||||||||
public void change(Event e, Object[] delta, Changer.ChangeMode mode){ | ||||||||
|
||||||||
|
||||||||
ProtectedRegion rg = region.getSingle(e).getRegion(); | ||||||||
|
||||||||
if (rg != null){ | ||||||||
DereWah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if (mode == Changer.ChangeMode.SET && delta != null){ | ||||||||
rg.setPriority(((Number) delta[0]).intValue()); | ||||||||
} | ||||||||
else{ | ||||||||
SkriptWorldGuard.getInstance().getLogger().warning("A region priority can only be set."); | ||||||||
} | ||||||||
}else{ | ||||||||
SkriptWorldGuard.getInstance().getLogger().warning("Could not find region " + "\"" + region.toString() +"\"."); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
||||||||
|
||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.