-
Notifications
You must be signed in to change notification settings - Fork 18
/
LevelUpdate.java
236 lines (205 loc) · 5.73 KB
/
LevelUpdate.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
package knightminer.inspirations.library.recipe.cauldron.util;
import com.google.gson.JsonObject;
import io.netty.handler.codec.DecoderException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.util.GsonHelper;
import net.minecraft.util.Mth;
import javax.annotation.Nullable;
import java.util.Locale;
import java.util.function.IntUnaryOperator;
import static knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe.MAX;
/**
* Logic to update the level to a new value
* @deprecated Will need a lot of rethinking to work with variable levels
*/
@Deprecated
public abstract class LevelUpdate implements IntUnaryOperator {
private static final String KEY_ADD = "add";
private static final String KEY_SET = "set";
/** Cache of add operations, ranges from -MAX to MAX */
private static final LevelUpdate[] ADD_CACHE = new LevelUpdate[MAX + MAX + 1];
/** Cache of set operations */
private static final LevelUpdate[] SET_CACHE = new LevelUpdate[MAX + 1];
/** No constructor to prevent extension, it will not work with the read methods */
private LevelUpdate() {}
/** Level update that returns the input */
public static final LevelUpdate IDENTITY = new LevelUpdate() {
@Override
public void write(FriendlyByteBuf buffer) {
buffer.writeEnum(Type.IDENTITY);
}
@Override
public int applyAsInt(int original) {
return original;
}
@Override
public JsonObject toJson() {
return new JsonObject();
}
@Override
public String toString() {
return "LevelUpdate.Identity";
}
};
static {
// -MAX (offset) + MAX = 0
ADD_CACHE[MAX] = IDENTITY;
}
/**
* Creates a new level update to add the specified amount
* @param amount Amount to add
* @return Level update
*/
public static LevelUpdate add(int amount) {
if (amount < -MAX || amount > MAX) {
throw new IllegalArgumentException("Invalid amount " + amount + ", must be between -12 and 12");
}
// negatives are not array indexes
int key = amount + MAX;
if (ADD_CACHE[key] == null) {
ADD_CACHE[key] = new Add(amount);
}
return ADD_CACHE[key];
}
/**
* Creates a new level update to set the specified amount
* @param amount Amount to set
* @return Level update
*/
public static LevelUpdate set(int amount) {
if (amount < 0 || amount > MAX) {
throw new IllegalArgumentException("Invalid amount " + amount + ", must be between 0 and 12");
}
if (SET_CACHE[amount] == null) {
SET_CACHE[amount] = new Set(amount);
}
return SET_CACHE[amount];
}
/**
* Reads a level update from JSON
* @param json JSON object
* @return Level predicate
*/
public static LevelUpdate read(JsonObject json) {
if (json.has(KEY_ADD)) {
return new Add(GsonHelper.getAsInt(json, KEY_ADD));
}
if (json.has(KEY_SET)) {
return new Set(GsonHelper.getAsInt(json, KEY_SET));
}
// neither? means identity
return IDENTITY;
}
/**
* Reads a level update from the packet buffer
* @param buffer Buffer instance
* @return Level predicate
*/
public static LevelUpdate read(FriendlyByteBuf buffer) {
Type type = buffer.readEnum(Type.class);
switch (type) {
case IDENTITY: return IDENTITY;
case SET: return new Set(buffer.readVarInt());
case ADD: return new Add(buffer.readVarInt());
}
throw new DecoderException("Got null type, this should not be possible");
}
/**
* Writes this to the packet buffer
* @param buffer Buffer instance
*/
public abstract void write(FriendlyByteBuf buffer);
/**
* Writes this to the packet buffer
* @param json Json object
*/
protected void write(JsonObject json) {}
/**
* Writes this to JSON
*/
public JsonObject toJson() {
JsonObject object = new JsonObject();
write(object);
return object;
}
/**
* Updater that sets the amount
*/
private static class Set extends LevelUpdate {
private final int amount;
private Set(int amount) {
this.amount = amount;
}
@Override
public int applyAsInt(int original) {
return amount;
}
@Override
public void write(FriendlyByteBuf buffer) {
buffer.writeEnum(Type.SET);
buffer.writeVarInt(amount);
}
@Override
protected void write(JsonObject json) {
json.addProperty(KEY_SET, amount);
}
@Override
public String toString() {
return String.format("LevelUpdate.Set(=%d)", amount);
}
}
/**
* Updater that adds to the amount
*/
private static class Add extends LevelUpdate {
private final int amount;
private Add(int amount) {
this.amount = amount;
}
@Override
public int applyAsInt(int original) {
return Mth.clamp(original + amount, 0, MAX);
}
@Override
public void write(FriendlyByteBuf buffer) {
buffer.writeEnum(Type.ADD);
buffer.writeVarInt(amount);
}
@Override
protected void write(JsonObject json) {
json.addProperty(KEY_ADD, amount);
}
@Override
public String toString() {
return String.format("LevelUpdate.Add(%+d)", amount);
}
}
/** All valid level update types */
private enum Type {
IDENTITY,
SET,
ADD;
private final String name = name().toLowerCase(Locale.US);
/**
* Gets the name of this type
* @return Type name
*/
public String getName() {
return name;
}
/**
* Gets a predicate type for the given name
* @param name Name to check
* @return Value, or null if missing
*/
@Nullable
public static Type byName(String name) {
for (Type type : values()) {
if (type.getName().equals(name)) {
return type;
}
}
return null;
}
}
}