-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
RegionCommand.java
132 lines (101 loc) · 4.78 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
package com.denizenscript.depenizen.bukkit.commands.worldguard;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.denizenscript.denizen.objects.dCuboid;
import com.denizenscript.denizen.objects.dWorld;
import com.denizenscript.denizen.utilities.debugging.dB;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.aH;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
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 Depenizen, WorldGuard
// @Required 2
// @Short Adds or removes a protected region.
// @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());
}
else if (!scriptEntry.hasObject("cuboid")
&& arg.matchesArgumentType(dCuboid.class)) {
scriptEntry.addObject("cuboid", arg.asType(dCuboid.class));
}
else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class)) {
scriptEntry.addObject("world", arg.asType(dWorld.class));
}
else 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) {
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) {
dB.echoError("No valid world found!");
return;
}
dB.report(scriptEntry, getName(), region_id.debug() + (cuboid != null ? cuboid.debug() : "")
+ aH.debugObj("world", world.getName()) + action.debug());
RegionManager regionManager = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world));
if (action.asString().equalsIgnoreCase("REMOVE")) {
regionManager.removeRegion(region_id.asString());
return;
}
Location low = cuboid.getLow(0);
Location high = cuboid.getHigh(0);
ProtectedCuboidRegion region = new ProtectedCuboidRegion(region_id.asString(),
BlockVector3.at(low.getX(), low.getY(), low.getZ()),
BlockVector3.at(high.getX(), high.getY(), high.getZ()));
regionManager.addRegion(region);
}
}