forked from SimpleServer/SimpleServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyAreaCommand.java
167 lines (150 loc) · 6.61 KB
/
MyAreaCommand.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (c) 2010 SimpleServer authors (see CONTRIBUTORS)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package simpleserver.command;
import static simpleserver.lang.Translations.t;
import java.util.Set;
import simpleserver.Color;
import simpleserver.Player;
import simpleserver.config.xml.AllBlocks;
import simpleserver.config.xml.Area;
import simpleserver.config.xml.Chests;
import simpleserver.config.xml.Config;
import simpleserver.config.xml.DimensionConfig;
import simpleserver.config.xml.Permission;
import simpleserver.config.xml.Config.AreaStoragePair;
public class MyAreaCommand extends AbstractCommand implements PlayerCommand {
private static final byte DEFAULT_SIZE = 50;
public MyAreaCommand() {
super("myarea [start|end|save|unsave|rename]",
"Manage your personal area");
}
private boolean areaSizeOk(Player player, int[] size) {
return (Math.abs(player.areastart.x() - player.areaend.x()) < size[0])
&& (Math.abs(player.areastart.z() - player.areaend.z()) < size[1])
&& player.areaend.dimension() == player.areastart.dimension();
}
private int[] getAreaMax(Player player) {
// Get the maximum area sizes from config.xml
int[] size = { Math.abs(player.getServer().config.properties.getInt("areaMaxX")),
Math.abs(player.getServer().config.properties.getInt("areaMaxZ")) };
// Check to make sure the configuration is valid
// If not, reset to default size
for (byte i = 0; i < size.length; i++) {
if (size[i] <= 0 || Integer.valueOf(size[i]) == null) {
size[i] = DEFAULT_SIZE;
}
}
return size;
}
public void execute(Player player, String message) {
// Set up an integer array to hold the maximum area size
int[] maxSize = getAreaMax(player); // X, Z
Config config = player.getServer().config;
String arguments[] = extractArguments(message);
if (arguments.length == 0) {
player.addTCaptionedMessage("Usage", commandPrefix() + "myarea [start|end|save|unsave|rename]");
return;
}
if (arguments[0].equals("start")) {
player.areastart = player.position();
player.areastart = player.areastart.setY((byte) 0); // no height limit
player.addTMessage(Color.GRAY, "Start coordinate set.");
} else if (arguments[0].equals("end")) {
player.areaend = player.position();
player.areaend = player.areaend.setY((byte) 127); // no height limit
player.addTMessage(Color.GRAY, "End coordinate set.");
} else if (arguments[0].equals("save")) {
if (player.areastart == null || player.areaend == null) {
player.addTMessage(Color.RED, "Define start and end coordinates for your area first!");
return;
}
if (!areaSizeOk(player, maxSize)) {
player.addTMessage(Color.RED, "Your area is allowed to have a maximum size of " +
maxSize[0] + "x" + maxSize[1] + "!");
return;
}
if (player.getServer().config.playerArea(player) != null) {
player.addTMessage(Color.RED, "New area can not be saved before you remove your old one!");
return;
}
Area area = createPlayerArea(player);
Set<Area> overlaps = config.dimensions.overlaps(area);
if (!overlaps.isEmpty()) {
player.addTMessage(Color.RED, "Your area overlaps with other areas and could therefore not be saved!");
StringBuilder str = new StringBuilder();
for (Area overlap : overlaps) {
str.append(overlap.name);
str.append(", ");
}
str.delete(str.length() - 2, str.length() - 1);
player.addTCaptionedMessage("Overlapping areas", "%s", str);
return;
}
saveArea(area, player);
player.addTMessage(Color.GRAY, "Your area has been saved!");
} else if (arguments[0].equals("unsave") || arguments[0].equals("remove")) {
AreaStoragePair area = config.playerArea(player);
if (area == null) {
player.addTMessage(Color.RED, "You currently have no personal area which can be removed!");
return;
}
area.storage.remove(area.area);
player.addTMessage(Color.GRAY, "Your area has been removed!");
player.getServer().saveConfig();
} else if (arguments[0].equals("rename")) {
AreaStoragePair area = config.playerArea(player);
if (area == null) {
player.addTMessage(Color.RED, "You currently have no personal area which can be renamed!");
return;
}
String label = extractArgument(message, 1);
if (label != null) {
area.area.name = label;
player.addTMessage(Color.GRAY, "Your area has been renamed!");
player.getServer().saveConfig();
} else {
player.addTMessage(Color.RED, "Please supply an area name.");
}
} else {
player.addTMessage(Color.RED, "You entered an invalid argument.");
}
}
private Area createPlayerArea(Player player) {
Area area = new Area(t("%s's area", player.getName()), player.areastart, player.areaend);
area.owner = player.getName().toLowerCase();
Permission perm = new Permission(player);
AllBlocks blocks = new AllBlocks();
blocks.destroy = perm;
blocks.place = perm;
blocks.use = perm;
area.allblocks.blocks = blocks;
area.chests.chests = new Chests(perm);
return area;
}
private void saveArea(Area area, Player player) {
DimensionConfig dimension = player.getServer().config.dimensions.get(area.start.dimension());
if (dimension == null) {
dimension = player.getServer().config.dimensions.add(area.start.dimension());
}
dimension.add(area);
player.getServer().saveConfig();
}
}