/
GameRules.java
257 lines (216 loc) · 8.99 KB
/
GameRules.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
package cn.nukkit.level;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.utils.BinaryStream;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.EnumMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import static cn.nukkit.level.GameRule.*;
@SuppressWarnings({"unchecked"})
public class GameRules {
private final EnumMap<GameRule, Value> gameRules = new EnumMap<>(GameRule.class);
private boolean stale;
private GameRules() {
}
public static GameRules getDefault() {
GameRules gameRules = new GameRules();
gameRules.gameRules.put(COMMAND_BLOCKS_ENABLED, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(COMMAND_BLOCK_OUTPUT, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(DO_DAYLIGHT_CYCLE, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(DO_ENTITY_DROPS, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(DO_FIRE_TICK, new Value(Type.BOOLEAN, true));
gameRules.gameRules.put(DO_INSOMNIA, new Value(Type.BOOLEAN, true));
gameRules.gameRules.put(DO_IMMEDIATE_RESPAWN, new Value(Type.BOOLEAN, false));
gameRules.gameRules.put(DO_MOB_LOOT, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(DO_MOB_SPAWNING, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(DO_TILE_DROPS, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(DO_WEATHER_CYCLE, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(DROWNING_DAMAGE, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(FALL_DAMAGE, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(FIRE_DAMAGE, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(FREEZE_DAMAGE, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(FUNCTION_COMMAND_LIMIT, new Value<>(Type.INTEGER, 10000));
gameRules.gameRules.put(KEEP_INVENTORY, new Value<>(Type.BOOLEAN, false));
gameRules.gameRules.put(MAX_COMMAND_CHAIN_LENGTH, new Value<>(Type.INTEGER, 65536));
gameRules.gameRules.put(MOB_GRIEFING, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(NATURAL_REGENERATION, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(PVP, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(RANDOM_TICK_SPEED, new Value<>(Type.INTEGER, 3));
gameRules.gameRules.put(SEND_COMMAND_FEEDBACK, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(SHOW_COORDINATES, new Value<>(Type.BOOLEAN, false));
gameRules.gameRules.put(SHOW_DEATH_MESSAGES, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(SPAWN_RADIUS, new Value<>(Type.INTEGER, 5));
gameRules.gameRules.put(TNT_EXPLODES, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(SHOW_TAGS, new Value<>(Type.BOOLEAN, true));
gameRules.gameRules.put(PLAYERS_SLEEPING_PERCENTAGE, new Value<>(Type.INTEGER, 100));
return gameRules;
}
public Map<GameRule, Value> getGameRules() {
return ImmutableMap.copyOf(gameRules);
}
public boolean isStale() {
return stale;
}
public void refresh() {
stale = false;
}
public void setGameRule(GameRule gameRule, boolean value) {
if (!gameRules.containsKey(gameRule)) {
throw new IllegalArgumentException("Gamerule does not exist");
}
gameRules.get(gameRule).setValue(value, Type.BOOLEAN);
stale = true;
}
public void setGameRule(GameRule gameRule, int value) {
if (!gameRules.containsKey(gameRule)) {
throw new IllegalArgumentException("Gamerule does not exist");
}
gameRules.get(gameRule).setValue(value, Type.INTEGER);
stale = true;
}
public void setGameRule(GameRule gameRule, float value) {
if (!gameRules.containsKey(gameRule)) {
throw new IllegalArgumentException("Gamerule does not exist");
}
gameRules.get(gameRule).setValue(value, Type.FLOAT);
stale = true;
}
public void setGameRules(GameRule gameRule, String value) throws IllegalArgumentException {
Preconditions.checkNotNull(gameRule, "gameRule");
Preconditions.checkNotNull(value, "value");
switch (getGameRuleType(gameRule)) {
case BOOLEAN:
if (value.equalsIgnoreCase("true")) {
setGameRule(gameRule, true);
} else if (value.equalsIgnoreCase("false")) {
setGameRule(gameRule, false);
} else {
throw new IllegalArgumentException("Was not a boolean");
}
break;
case INTEGER:
setGameRule(gameRule, Integer.parseInt(value));
break;
case FLOAT:
setGameRule(gameRule, Float.parseFloat(value));
}
}
public boolean getBoolean(GameRule gameRule) {
return gameRules.get(gameRule).getValueAsBoolean();
}
public int getInteger(GameRule gameRule) {
Preconditions.checkNotNull(gameRule, "gameRule");
return gameRules.get(gameRule).getValueAsInteger();
}
public float getFloat(GameRule gameRule) {
Preconditions.checkNotNull(gameRule, "gameRule");
return gameRules.get(gameRule).getValueAsFloat();
}
public String getString(GameRule gameRule) {
Preconditions.checkNotNull(gameRule, "gameRule");
return gameRules.get(gameRule).value.toString();
}
public Type getGameRuleType(GameRule gameRule) {
Preconditions.checkNotNull(gameRule, "gameRule");
return gameRules.get(gameRule).getType();
}
public boolean hasRule(GameRule gameRule) {
return gameRules.containsKey(gameRule);
}
public GameRule[] getRules() {
return gameRules.keySet().toArray(new GameRule[0]);
}
// TODO: This needs to be moved out since there is not a separate compound tag in the LevelDB format for Game Rules.
public CompoundTag writeNBT() {
CompoundTag nbt = new CompoundTag();
for (Entry<GameRule, Value> entry : gameRules.entrySet()) {
nbt.putString(entry.getKey().getName(), entry.getValue().value.toString());
}
return nbt;
}
public void readNBT(CompoundTag nbt) {
Preconditions.checkNotNull(nbt);
for (String key : nbt.getTags().keySet()) {
Optional<GameRule> gameRule = GameRule.parseString(key);
if (!gameRule.isPresent()) {
continue;
}
setGameRules(gameRule.get(), nbt.getString(key));
}
}
public enum Type {
UNKNOWN {
@Override
void write(BinaryStream pk, Value value) {
}
},
BOOLEAN {
@Override
void write(BinaryStream pk, Value value) {
pk.putBoolean(value.getValueAsBoolean());
}
},
INTEGER {
@Override
void write(BinaryStream pk, Value value) {
pk.putUnsignedVarInt(value.getValueAsInteger());
}
},
FLOAT {
@Override
void write(BinaryStream pk, Value value) {
pk.putLFloat(value.getValueAsFloat());
}
};
abstract void write(BinaryStream pk, Value value);
}
public static class Value<T> {
private final Type type;
private T value;
private boolean canBeChanged;
public Value(Type type, T value) {
this.type = type;
this.value = value;
}
private void setValue(T value, Type type) {
if (this.type != type) {
throw new UnsupportedOperationException("Rule not of type " + type.name().toLowerCase());
}
this.value = value;
}
public boolean isCanBeChanged() {
return this.canBeChanged;
}
public void setCanBeChanged(boolean canBeChanged) {
this.canBeChanged = canBeChanged;
}
public Type getType() {
return this.type;
}
private boolean getValueAsBoolean() {
if (type != Type.BOOLEAN) {
throw new UnsupportedOperationException("Rule not of type boolean");
}
return (Boolean) value;
}
private int getValueAsInteger() {
if (type != Type.INTEGER) {
throw new UnsupportedOperationException("Rule not of type integer");
}
return (Integer) value;
}
private float getValueAsFloat() {
if (type != Type.FLOAT) {
throw new UnsupportedOperationException("Rule not of type float");
}
return (Float) value;
}
public void write(BinaryStream stream) {
stream.putBoolean(this.canBeChanged);
stream.putUnsignedVarInt(type.ordinal());
type.write(stream, this);
}
}
}