-
Notifications
You must be signed in to change notification settings - Fork 18
/
ILevelUpdate.java
174 lines (150 loc) · 3.78 KB
/
ILevelUpdate.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
package knightminer.inspirations.library.recipe.cauldron.util;
import com.google.gson.JsonObject;
import io.netty.handler.codec.DecoderException;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.math.MathHelper;
import javax.annotation.Nullable;
import java.util.Locale;
import java.util.function.IntUnaryOperator;
/**
* Logic to update the level to a new value
*/
public interface ILevelUpdate extends IntUnaryOperator {
String KEY_ADD = "add";
String KEY_SET = "set";
/** Level update that returns the input */
ILevelUpdate IDENTITY = new ILevelUpdate() {
@Override
public void write(PacketBuffer buffer) {
buffer.writeEnumValue(Type.IDENTITY);
}
@Override
public int applyAsInt(int original) {
return original;
}
@Override
public JsonObject toJson() {
return new JsonObject();
}
};
/**
* Writes this to the packet buffer
* @param buffer Buffer instance
*/
void write(PacketBuffer buffer);
/**
* Writes this to the packet buffer
* @param json Json object
*/
default void write(JsonObject json) {}
/**
* Writes this to JSON
*/
default JsonObject toJson() {
JsonObject object = new JsonObject();
write(object);
return object;
}
/**
* Reads a level update from JSON
* @param json JSON object
* @return Level predicate
*/
static ILevelUpdate read(JsonObject json) {
if (json.has(KEY_ADD)) {
return new Add(JSONUtils.getInt(json, KEY_ADD));
}
if (json.has(KEY_SET)) {
return new Set(JSONUtils.getInt(json, KEY_SET));
}
// neither? means identity
return IDENTITY;
}
/**
* Reads a level update from the packet buffer
* @param buffer Buffer instance
* @return Level predicate
*/
static ILevelUpdate read(PacketBuffer buffer) {
Type type = buffer.readEnumValue(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");
}
/**
* Updater that sets the amount
*/
class Set implements ILevelUpdate {
private final int amount;
public Set(int amount) {
this.amount = amount;
}
@Override
public int applyAsInt(int original) {
return amount;
}
@Override
public void write(PacketBuffer buffer) {
buffer.writeEnumValue(Type.SET);
buffer.writeVarInt(amount);
}
@Override
public void write(JsonObject json) {
json.addProperty(KEY_SET, amount);
}
}
/**
* Updater that adds to the amount
*/
class Add implements ILevelUpdate {
private final int amount;
public Add(int amount) {
this.amount = amount;
}
@Override
public int applyAsInt(int original) {
return MathHelper.clamp(original + amount, 0, 3);
}
@Override
public void write(PacketBuffer buffer) {
buffer.writeEnumValue(Type.ADD);
buffer.writeVarInt(amount);
}
@Override
public void write(JsonObject json) {
json.addProperty(KEY_ADD, amount);
}
}
/** All valid level update types */
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;
}
}
}