-
Notifications
You must be signed in to change notification settings - Fork 1
/
RegionFlag.java
295 lines (274 loc) · 12.7 KB
/
RegionFlag.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package com.kicas.rp.data;
import com.kicas.rp.data.flagdata.*;
import com.kicas.rp.data.flagdata.EnumFilter.EntityFilter;
import com.kicas.rp.data.flagdata.EnumFilter.MaterialFilter;
import com.kicas.rp.util.Pair;
import com.kicas.rp.util.ReflectionHelper;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* All of the flags which can be set for a specific region.
*/
public enum RegionFlag {
TRUST(TrustMeta.class),
DENY_SPAWN(EntityFilter.class),
DENY_BREAK(MaterialFilter.class),
DENY_PLACE(MaterialFilter.class),
ANIMAL_GRIEF_BLOCKS(true), // block damage caused by non-player, non-hostile mobs
TNT(true),
OVERLAP, // Regions containing the same locations
INVINCIBLE,
GREETING(TextMeta.class),
FAREWELL(TextMeta.class),
HOSTILE_DAMAGE, // Allow players to damage hostiles
ANIMAL_DAMAGE, // Allow players to damage animals
POTION_SPLASH, // The actual splash part
FORCE_CHEST_ACCESS, // Just chests, trapped chests, and ender chests. Only checked if it's explicitly set.
PVP(true),
BED_ENTER,
WATER_FLOW,
LAVA_FLOW,
SNOW_CHANGE,
ICE_CHANGE,
CORAL_DEATH,
LEAF_DECAY,
LIGHTNING_MOB_DAMAGE,
PORTAL_PAIR_FORMATION,
ENTER_COMMAND(CommandMeta.class),
EXIT_COMMAND(CommandMeta.class),
DENY_COMMAND(StringFilter.class),
FOLLOW, // prevent pet tp
DENY_AGGRO(EntityFilter.class), // prevent certain mobs from targeting the player
DENY_GROWTH(MaterialFilter.class), // vine growth grass spread etc
DENY_BLOCK_USE(MaterialFilter.class),
KEEP_INVENTORY,
KEEP_XP,
RESPAWN_LOCATION(LocationMeta.class),
DENY_ENTITY_USE(EntityFilter.class),
DENY_ITEM_USE(MaterialFilter.class),
DENY_WEAPON_USE(MaterialFilter.class),
FLIGHT,
HOSTILE_GRIEF_BLOCKS(true), // block damage caused by hostile mobs
HOSTILE_GRIEF_ENTITIES(true), // entity damage caused by hostile mobs
LIGHTNING_STRIKES,
DENY_ITEM_CONSUMPTION(MaterialFilter.class),
TNT_IGNITION,
TNT_ENTITY_DAMAGE,
TNT_BLOCK_DAMAGE,
FALL_DAMAGE,
ELYTRA_FLIGHT,
DENY_ENTITY_TELEPORT(EntityFilter.class),
ANIMAL_CONTAINERS,
ITEM_DAMAGE,
RIPTIDE,
FARMLAND_MOISTURE_CHANGE,
ENTRANCE_RESTRICTION(BorderPolicy.class),
FIRE_TICK(true),
ENTRY_GAMEMODE(GameModeMeta.class),
EXIT_GAMEMODE(GameModeMeta.class),
PLAYER_COLLISIONS,
MODIFY_ARMOR_STANDS,
DENY_ENTITY_PICKUP(MaterialFilter.class); // Prevent entities from picking up certain items
public static final RegionFlag[] VALUES = values();
private static final Map<RegionFlag, Pair<Object, Function<World, Object>>> DEFAULT_VALUES = new HashMap<>();
private final boolean playerToggleable;
private final Class<? extends FlagMeta> metaClass;
RegionFlag(boolean playerToggleable, Class<? extends FlagMeta> metaClass) {
this.playerToggleable = playerToggleable;
this.metaClass = metaClass;
}
RegionFlag(Class<? extends FlagMeta> metaClass) {
this(false, metaClass);
}
RegionFlag(boolean playerToggleable) {
this(playerToggleable, null);
}
RegionFlag() {
this(false, null);
}
/**
* @return true if this flag can be modified using the /claimtoggle command.
*/
public boolean isPlayerToggleable() {
return playerToggleable;
}
/**
* @return the metadata class associated with this flag.
*/
public Class<? extends FlagMeta> getMetaClass() {
return metaClass;
}
/**
* @return true if this flag is a boolean flag, meaning the meta class is equal to <code>boolean.class</code>.
*/
public boolean isBoolean() {
return metaClass == null;
}
/**
* Returns this flag's default value for regions. This value returned by this method can by modified in the config.
*
* @param <T> the meta type.
* @return this flag's default value for regions.
*/
@SuppressWarnings("unchecked")
public <T> T getRegionDefaultValue() {
return (T) DEFAULT_VALUES.get(this).getFirst();
}
/**
* Returns this flag's default value for the world. This value will be equal to the functional vanilla value so that
* the global flag container does not cause conflicts based on configured defaults.
*
* @param world the world to get the default value for.
* @param <T> the meta type.
* @return this flag's default value for the world.
*/
@SuppressWarnings("unchecked")
public <T> T getWorldDefaultValue(World world) {
Pair<Object, Function<World, Object>> def = DEFAULT_VALUES.get(this);
return def.getSecond() == null ? (T) def.getFirst() : (T) def.getSecond().apply(world);
}
/**
* Converts the given meta to a human-readable string based on the given flag the meta is associated with.
*
* @param flag the flag.
* @param meta the associated metadata.
* @return the given meta in human-readable string from.
*/
public static String toString(RegionFlag flag, Object meta) {
return flag.isBoolean() ? ((boolean) meta ? "allow" : "deny") : meta.toString();
}
/**
* Parses the given meta string based on the associated flag, and returns the flag metadata derived from the given
* string.
*
* @param flag the flag associated with the given metadata in string form.
* @param metaString the string to parse.
* @return the flag metadata resulting from the given string.
* @throws IllegalArgumentException if the given metadata string is invalid in some way. The message of this
* exception will give details as to what the error was.
*/
public static Object metaFromString(RegionFlag flag, String metaString) throws IllegalArgumentException {
if (flag.isBoolean()) {
metaString = metaString.trim();
if ("allow".equalsIgnoreCase(metaString) || "yes".equalsIgnoreCase(metaString) ||
"true".equalsIgnoreCase(metaString)) {
return true;
} else if ("deny".equalsIgnoreCase(metaString) || "no".equalsIgnoreCase(metaString) ||
"false".equalsIgnoreCase(metaString)) {
return false;
} else
throw new IllegalArgumentException("Invalid flag value: " + metaString);
} else {
FlagMeta meta = ReflectionHelper.instantiateWithDefaultParams(flag.getMetaClass());
// This should never happen
if (meta == null)
throw new InternalError();
meta.readMetaString(metaString);
return meta;
}
}
/**
* This method is called upon the enabling of the plugin. Calling this method again while the plugin is enabled
* could cause a concurrent modification exception, but otherwise would not affect the functionality of the plugin.
*
* @param config the config to get default values from.
*/
public static void registerDefaults(FileConfiguration config) {
registerDefault(TRUST, TrustMeta.FULL_TRUST);
registerDefault(DENY_SPAWN, EntityFilter.EMPTY_FILTER);
registerDefault(DENY_BREAK, MaterialFilter.EMPTY_FILTER);
registerDefault(DENY_PLACE, MaterialFilter.EMPTY_FILTER);
registerDefault(ANIMAL_GRIEF_BLOCKS, config.getBoolean("entity.animal-grief-blocks"),
world -> world.getGameRuleValue(GameRule.MOB_GRIEFING));
registerDefault(TNT, config.getBoolean("world.tnt-explosions"));
registerDefault(OVERLAP, false);
registerDefault(INVINCIBLE, config.getBoolean("region.invincible"), world -> false);
registerDefault(GREETING, TextMeta.EMPTY_TEXT);
registerDefault(FAREWELL, TextMeta.EMPTY_TEXT);
registerDefault(HOSTILE_DAMAGE, config.getBoolean("player.hostile-damage"), world -> true);
registerDefault(ANIMAL_DAMAGE, config.getBoolean("player.animal-damage"), world -> true);
registerDefault(POTION_SPLASH, config.getBoolean("region.potion-splash"), world -> true);
registerDefault(FORCE_CHEST_ACCESS, false);
// TODO: 10/25/21 Find a way to fix this without nms
registerDefault(PVP, config.getBoolean("player.pvp"), world -> false);
// this is what the above used to be
//registerDefault(PVP, config.getBoolean("player.pvp"),
// world -> ((CraftServer) Bukkit.getServer()).getServer().getDedicatedServerProperties().f);
registerDefault(BED_ENTER, true);
registerDefault(WATER_FLOW, true);
registerDefault(LAVA_FLOW, true);
registerDefault(SNOW_CHANGE, config.getBoolean("world.snow-change"), world -> true);
registerDefault(ICE_CHANGE, config.getBoolean("world.ice-change"), world -> true);
registerDefault(CORAL_DEATH, config.getBoolean("world.coral-death"),
world -> world.getGameRuleValue(GameRule.RANDOM_TICK_SPEED) > 0);
registerDefault(LEAF_DECAY, config.getBoolean("world.leaf-decay"),
world -> world.getGameRuleValue(GameRule.RANDOM_TICK_SPEED) > 0);
registerDefault(LIGHTNING_MOB_DAMAGE, config.getBoolean("world.lightning-mob-damage"), world -> true);
registerDefault(PORTAL_PAIR_FORMATION, config.getBoolean("world.portal-pair-formation"), world -> true);
registerDefault(ENTER_COMMAND, CommandMeta.EMPTY_META);
registerDefault(EXIT_COMMAND, CommandMeta.EMPTY_META);
registerDefault(DENY_COMMAND, StringFilter.EMPTY_FILTER);
registerDefault(FOLLOW, true);
registerDefault(DENY_AGGRO, EntityFilter.EMPTY_FILTER);
registerDefault(DENY_GROWTH, MaterialFilter.EMPTY_FILTER,
world -> world.getGameRuleValue(GameRule.RANDOM_TICK_SPEED) > 0);
registerDefault(DENY_BLOCK_USE, MaterialFilter.EMPTY_FILTER);
registerDefault(KEEP_INVENTORY, false, world -> world.getGameRuleValue(GameRule.KEEP_INVENTORY));
registerDefault(KEEP_XP, false, world -> world.getGameRuleValue(GameRule.KEEP_INVENTORY));
registerDefault(RESPAWN_LOCATION, null);
registerDefault(DENY_ENTITY_USE, EntityFilter.EMPTY_FILTER);
registerDefault(DENY_ITEM_USE, MaterialFilter.EMPTY_FILTER);
registerDefault(DENY_WEAPON_USE, MaterialFilter.EMPTY_FILTER);
registerDefault(FLIGHT, false, world -> Bukkit.getServer().getAllowFlight());
registerDefault(HOSTILE_GRIEF_BLOCKS, config.getBoolean("entity.hostile-grief-blocks"),
world -> world.getGameRuleValue(GameRule.MOB_GRIEFING));
registerDefault(HOSTILE_GRIEF_ENTITIES, config.getBoolean("entity.hostile-grief-entities"),
world -> world.getGameRuleValue(GameRule.MOB_GRIEFING));
registerDefault(LIGHTNING_STRIKES, true);
registerDefault(DENY_ITEM_CONSUMPTION, MaterialFilter.EMPTY_FILTER);
registerDefault(TNT_IGNITION, true);
registerDefault(TNT_ENTITY_DAMAGE, true);
registerDefault(TNT_BLOCK_DAMAGE, true);
registerDefault(FALL_DAMAGE, true);
registerDefault(ELYTRA_FLIGHT, true);
registerDefault(DENY_ENTITY_TELEPORT, EntityFilter.EMPTY_FILTER);
registerDefault(ANIMAL_CONTAINERS, true);
registerDefault(ITEM_DAMAGE, true);
registerDefault(RIPTIDE, true);
registerDefault(FARMLAND_MOISTURE_CHANGE, true);
registerDefault(ENTRANCE_RESTRICTION, BorderPolicy.NONE);
registerDefault(FIRE_TICK, config.getBoolean("world.fire-tick"),
world -> world.getGameRuleValue(GameRule.DO_FIRE_TICK));
registerDefault(ENTRY_GAMEMODE, null);
registerDefault(EXIT_GAMEMODE, null);
registerDefault(PLAYER_COLLISIONS, true);
registerDefault(MODIFY_ARMOR_STANDS, false);
registerDefault(DENY_ENTITY_PICKUP, MaterialFilter.EMPTY_FILTER);
}
/**
* Registers the given flag with the given region default value, and given function to get the default value for a
* world.
*
* @param flag the flag to register default values for.
* @param regionDefault the region default value.
* @param worldDefault the function to get world default values.
*/
private static void registerDefault(RegionFlag flag, Object regionDefault, Function<World, Object> worldDefault) {
DEFAULT_VALUES.put(flag, new Pair<>(regionDefault, worldDefault));
}
/**
* Registers the given flag with the given default value, which will be both the region default value and world
* default value.
*
* @param flag the flag to register the default value for.
* @param value the default value.
*/
private static void registerDefault(RegionFlag flag, Object value) {
registerDefault(flag, value, null);
}
}