-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
RegionCommand.java
123 lines (94 loc) · 4.89 KB
/
RegionCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package net.gnomeffinway.depenizen.commands.worldguard;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.gnomeffinway.depenizen.Depenizen;
import net.gnomeffinway.depenizen.support.Support;
import net.gnomeffinway.depenizen.support.Supported;
import org.bukkit.Location;
import org.bukkit.World;
public class RegionCommand extends AbstractCommand {
// <--[command]
// @Name region
// @Syntax region [{add} <cuboid>/remove <world>] [id:<name>]
// @Group Depenizen
// @Plugin WorldGuard
// @Required 2
// @Stable untested
// @Short Adds or removes a protected region.
// @Author Morphan1
// @Description
// Adds a protected region to a region manager based on the specified cuboid,
// or removes a protected region from a region manager based on the specified
// world. Currently, this command only supports cuboid-shaped regions.
// @Tags None
// @Usage
// Use to add a region based on a cuboid.
// - region add cu@l@123,0,321,world|l@321,256,123,world id:MyRegion
// @Usage
// Use to remove a region from a world.
// - region remove w@world id:MyRegion
// -->
private enum Action { ADD, REMOVE }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("region_id")
&& arg.matchesPrefix("id"))
scriptEntry.addObject("region_id", arg.asElement());
if (!scriptEntry.hasObject("cuboid")
&& arg.matchesArgumentType(dCuboid.class))
scriptEntry.addObject("cuboid", arg.asType(dCuboid.class));
if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class))
scriptEntry.addObject("world", arg.asType(dWorld.class));
if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values()))
scriptEntry.addObject("action", arg.asElement());
}
if (!scriptEntry.hasObject("region_id"))
throw new InvalidArgumentsException("Must specify a region id!");
if (!scriptEntry.hasObject("cuboid") && (!scriptEntry.hasObject("action")
|| scriptEntry.getElement("action").asString().equalsIgnoreCase("ADD")))
throw new InvalidArgumentsException("Must specify a valid cuboid!");
if (!scriptEntry.hasObject("action"))
scriptEntry.addObject("action", new Element("ADD"));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element region_id = scriptEntry.getElement("region_id");
dCuboid cuboid = scriptEntry.hasObject("cuboid") ? scriptEntry.getdObjectAs("cuboid", dCuboid.class) : null;
World world = scriptEntry.hasObject("world") ? scriptEntry.getdObjectAs("world", dWorld.class).getWorld()
: cuboid != null ? cuboid.getWorld()
: scriptEntry.hasPlayer() ? scriptEntry.getPlayer().getWorld()
: scriptEntry.hasNPC() ? scriptEntry.getNPC().getWorld() : null;
Element action = scriptEntry.getElement("action");
if (world == null)
throw new CommandExecutionException("No valid world found!");
dB.report(scriptEntry, getName(), region_id.debug() + (cuboid != null ? cuboid.debug() : "")
+ aH.debugObj("world", world.getName()) + action.debug());
if (action.asString().equalsIgnoreCase("REMOVE")) {
WorldGuardPlugin worldGuard = Supported.get("WORLDGUARD").getPlugin();
worldGuard.getRegionManager(world).removeRegion(region_id.asString());
return;
}
Location low = cuboid.getLow(0);
Location high = cuboid.getHigh(0);
ProtectedCuboidRegion region = new ProtectedCuboidRegion(region_id.asString(),
new BlockVector(low.getX(), low.getY(), low.getZ()),
new BlockVector(high.getX(), high.getY(), high.getZ()));
WorldGuardPlugin worldGuard = Supported.get("WORLDGUARD").getPlugin();
worldGuard.getRegionManager(cuboid.getWorld()).addRegion(region);
}
}