-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathEntityMeta.java
More file actions
287 lines (227 loc) · 9.44 KB
/
Copy pathEntityMeta.java
File metadata and controls
287 lines (227 loc) · 9.44 KB
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
package me.tofaa.entitylib.meta;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.manager.server.VersionComparison;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.entity.data.EntityMetadataProvider;
import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.extras.InvalidVersionException;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
public class EntityMeta implements EntityMetadataProvider {
private static final MetaConverterRegistry registry = new MetaConverterRegistry();
public static @NotNull BiFunction<Integer, Metadata, EntityMeta> getConverter(EntityType entityType) {
return registry.get(entityType);
}
public static @NotNull Class<? extends EntityMeta> getMetaClass(EntityType entityType) {
return registry.getMetaClass(entityType);
}
public static @NotNull EntityMeta createMeta(int entityId, EntityType entityType) {
Metadata metadata = new Metadata(entityId);
BiFunction<Integer, Metadata, EntityMeta> converter = getConverter(entityType);
return converter.apply(entityId, metadata);
}
public static final byte OFFSET = 0;
public static final byte MAX_OFFSET = OFFSET + 8;
private final static byte ON_FIRE_BIT = 0x01;
private final static byte CROUCHING_BIT = 0x02;
private final static byte SPRINTING_BIT = 0x08;
private final static byte SWIMMING_BIT = 0x10;
private final static byte INVISIBLE_BIT = 0x20;
private final static byte HAS_GLOWING_EFFECT_BIT = 0x40;
private final static byte FLYING_WITH_ELYTRA_BIT = (byte) 0x80;
protected final int entityId;
protected final Metadata metadata;
public EntityMeta(int entityId, Metadata metadata) {
this.entityId = entityId;
this.metadata = metadata;
}
public EntityMeta(EntityMeta other) {
this(other.entityId, new Metadata(other.entityId));
metadata.setMetaFromPacket(other.createPacket());
}
public EntityMeta(int entityId) {
this(entityId, new Metadata(entityId));
}
public void setNotifyAboutChanges(boolean notifyAboutChanges) {
metadata.setNotifyAboutChanges(notifyAboutChanges);
}
public boolean isNotifyingChanges() {
return metadata.isNotifyingChanges();
}
public boolean isOnFire() {
return getMaskBit(OFFSET, ON_FIRE_BIT);
}
public void setOnFire(boolean value) {
setMaskBit(OFFSET, ON_FIRE_BIT, value);
}
public boolean isSneaking() {
return getMaskBit(OFFSET, CROUCHING_BIT);
}
public void setSneaking(boolean value) {
setMaskBit(OFFSET, CROUCHING_BIT, value);
}
public boolean isSprinting() {
return getMaskBit(OFFSET, SPRINTING_BIT);
}
public void setSprinting(boolean value) {
setMaskBit(OFFSET, SPRINTING_BIT, value);
}
public boolean isInvisible() {
return getMaskBit(OFFSET, INVISIBLE_BIT);
}
public void setInvisible(boolean value) {
setMaskBit(OFFSET, INVISIBLE_BIT, value);
}
public boolean hasGlowingEffect() {
return getMaskBit(OFFSET, HAS_GLOWING_EFFECT_BIT);
}
public boolean isGlowing() {
return hasGlowingEffect();
}
public void setHasGlowingEffect(boolean value) {
setMaskBit(OFFSET, HAS_GLOWING_EFFECT_BIT, value);
}
public void setGlowing(boolean value) {
setHasGlowingEffect(value);
}
public boolean isSwimming() {
return getMaskBit(OFFSET, SWIMMING_BIT);
}
public void setSwimming(boolean value) {
setMaskBit(OFFSET, SWIMMING_BIT, value);
}
public boolean isFlyingWithElytra() {
return getMaskBit(OFFSET, FLYING_WITH_ELYTRA_BIT);
}
public void setFlyingWithElytra(boolean value) {
setMaskBit(OFFSET, FLYING_WITH_ELYTRA_BIT, value);
}
public short getAirTicks() {
return this.metadata.getIndex((byte)1, (short) 300);
}
public void setAirTicks(short value) {
this.metadata.setIndex((byte)1, EntityDataTypes.SHORT, value);
}
public Component getCustomName() {
Optional<Component> component = this.metadata.getIndex((byte)2, Optional.empty());
return component.orElse(null);
}
public void setCustomName(Component value) {
this.metadata.setIndex((byte)2, EntityDataTypes.OPTIONAL_ADV_COMPONENT, Optional.ofNullable(value));
}
public boolean isCustomNameVisible() {
return this.metadata.getIndex((byte)3, false);
}
public void setCustomNameVisible(boolean value) {
this.metadata.setIndex((byte)3, EntityDataTypes.BOOLEAN, value);
}
public boolean isSilent() {
return this.metadata.getIndex((byte)4, false);
}
public void setSilent(boolean value) {
this.metadata.setIndex((byte)4, EntityDataTypes.BOOLEAN, value);
}
public boolean hasNoGravity() {
return this.metadata.getIndex((byte)5, true);
}
public void setHasNoGravity(boolean value) {
this.metadata.setIndex((byte)5, EntityDataTypes.BOOLEAN, value);
}
public EntityPose getPose() {
return this.metadata.getIndex((byte)6, EntityPose.STANDING);
}
public void setPose(EntityPose value) {
this.metadata.setIndex((byte)6, EntityDataTypes.ENTITY_POSE, value);
}
public int getTicksFrozenInPowderedSnow() {
return this.metadata.getIndex((byte)7, 0);
}
public void setTicksFrozenInPowderedSnow(int value) {
this.metadata.setIndex((byte)7, EntityDataTypes.INT, value);
}
public WrapperPlayServerEntityMetadata createPacket() {
return metadata.createPacket();
}
protected static void isVersionNewer(ServerVersion version) {
if (EntityLib.getOptionalApi().isPresent()) {
if (!EntityLib.getApi().getPacketEvents().getServerManager().getVersion().is(VersionComparison.NEWER_THAN, version)) {
throw new InvalidVersionException("This method is only available for versions newer than " + version.name() + ".");
}
}
if (!PacketEvents.getAPI().getServerManager().getVersion().is(VersionComparison.NEWER_THAN, version)) {
throw new InvalidVersionException("This method is only available for versions newer than " + version.name() + ".");
}
}
protected static boolean isVersion(ServerVersion version, VersionComparison comparison) {
if (EntityLib.getOptionalApi().isPresent()) {
return EntityLib.getApi().getPacketEvents().getServerManager().getVersion().is(comparison, version);
}
return PacketEvents.getAPI().getServerManager().getVersion().is(comparison, version);
}
protected static boolean isVersion(ServerVersion version) {
if (EntityLib.getOptionalApi().isPresent()) {
return EntityLib.getApi().getPacketEvents().getServerManager().getVersion().is(VersionComparison.EQUALS, version);
}
return PacketEvents.getAPI().getServerManager().getVersion().is(VersionComparison.EQUALS, version);
}
/**
* Annoying java 8 not letting me do OFFSET + amount in the method call so this is a workaround
*
* @param value the value to offset
* @param amount the amount to offset by
* @return the offset value
*/
protected static byte offset(byte value, int amount) {
return (byte) (value + amount);
}
public <T> void setIndex(byte index, @NotNull EntityDataType<T> dataType, T value) {
this.metadata.setIndex(index, dataType, value);
}
public <T> T getIndex(byte index, @Nullable T defaultValue) {
return this.metadata.getIndex(index, defaultValue);
}
public byte getMask(byte index) {
return this.metadata.getIndex(index, (byte) 0);
}
public void setMask(byte index, byte mask) {
this.metadata.setIndex(index, EntityDataTypes.BYTE, mask);
}
public boolean getMaskBit(byte index, byte bit) {
return (getMask(index) & bit) == bit;
}
public void setMaskBit(int index, byte bit, boolean value) {
byte mask = getMask((byte)index);
boolean currentValue = (mask & bit) == bit;
if (currentValue == value) {
return;
}
if (value) {
mask |= bit;
} else {
mask &= (byte) ~bit;
}
setMask((byte)index, mask);
}
@Override
public @NotNull List<EntityData<?>> entityData(@NotNull ClientVersion clientVersion) {
return metadata.getEntries(); // TODO: Atm this is useless cause of the way the api works. Might change in the future
}
@Override
public @NotNull List<EntityData<?>> entityData() {
return metadata.getEntries();
}
public Metadata getMetadata() {
return metadata;
}
}