-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
RegionCommand.java
133 lines (104 loc) · 4.99 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
124
125
126
127
128
129
130
131
132
133
package com.morphanone.depenizenbukkit.commands.worldguard;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import com.morphanone.depenizenbukkit.support.Support;
import com.morphanone.depenizenbukkit.support.plugins.WorldGuardSupport;
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());
}
else {
arg.reportUnhandled();
}
}
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("world") && scriptEntry.hasObject("action")
&& scriptEntry.getElement("action").asString().equalsIgnoreCase("REMOVE")) {
throw new InvalidArgumentsException("Must specify a valid world!");
}
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.getdObject("cuboid");
dWorld w = scriptEntry.getdObject("world");
World world = w != null ? w.getWorld() : cuboid != null ? cuboid.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 = Support.getPlugin(WorldGuardSupport.class);
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 = Support.getPlugin(WorldGuardSupport.class);
worldGuard.getRegionManager(cuboid.getWorld()).addRegion(region);
}
}