-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMetadata.java
More file actions
116 lines (96 loc) · 4.1 KB
/
Copy pathMetadata.java
File metadata and controls
116 lines (96 loc) · 4.1 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
package me.tofaa.entitylib.meta;
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.wrapper.play.server.WrapperPlayServerEntityMetadata;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.EntityLibAPI;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.Wrapper;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("unchecked")
public class Metadata {
private final int entityId;
private volatile boolean notifyAboutChanges = true;
private final HashMap<Byte, EntityData<?>> notNotifiedChanges = new HashMap<>();
private final ConcurrentHashMap<Byte, EntityData<?>> metadataMap = new ConcurrentHashMap<>();
public Metadata(int entityId) {
this.entityId = entityId;
}
public void copyTo(Metadata other) {
other.clear();
synchronized (other.notNotifiedChanges) {
other.notNotifiedChanges.putAll(notNotifiedChanges);
}
other.metadataMap.putAll(metadataMap);
}
public void copyFrom(Metadata other) {
other.copyTo(this); // Scuffed pepelaugh
}
/**
* Clears the internal metadata map, is not responsible for informing the clients entity view with the newly reset information
*/
public void clear() {
this.metadataMap.clear();
this.notNotifiedChanges.clear();
}
public <T> T getIndex(byte index, @Nullable T defaultValue) {
EntityData<?> value = this.metadataMap.get(index);
return value != null ? (T) value.getValue() : defaultValue;
}
public <T> void setIndex(byte index, @NotNull EntityDataType<T> dataType, T value) {
final EntityData<?> entry = new EntityData<>(index, dataType, value);
this.metadataMap.put(index, entry);
final Optional<EntityLibAPI<?>> optionalApi = EntityLib.getOptionalApi();
if (!optionalApi.isPresent()) return;
final WrapperEntity entity = optionalApi.get().getEntity(entityId);
if (entity == null || !entity.isSpawned()) return; // Not EntityLib entity then, the user must send the packet manually. OR not spawned.
if (!this.notifyAboutChanges) {
synchronized (this.notNotifiedChanges) {
this.notNotifiedChanges.put(index, entry);
}
}
else {
entity.sendPacketToViewers(createPacket());
}
}
public void setNotifyAboutChanges(boolean notifyAboutChanges) {
if (this.notifyAboutChanges == notifyAboutChanges) {
return;
}
List<EntityData<?>> entries = null;
synchronized (this.notNotifiedChanges) {
this.notifyAboutChanges = notifyAboutChanges;
if (notifyAboutChanges) {
entries = new ArrayList<>(this.notNotifiedChanges.values());
if (entries.isEmpty()) {
return;
}
this.notNotifiedChanges.clear();
}
}
final WrapperEntity entity = EntityLib.getApi().getEntity(entityId);
if (entries == null || entity == null || !entity.isSpawned()) {
return;
}
WrapperPlayServerEntityMetadata packet = new WrapperPlayServerEntityMetadata(entityId, entries);
entity.sendPacketsToViewers(packet);
}
public void setMetaFromPacket(WrapperPlayServerEntityMetadata wrapper) {
for (EntityData<?> data : wrapper.getEntityMetadata()) {
metadataMap.put((byte) data.getIndex(), data);
}
}
public boolean isNotifyingChanges() {
return notifyAboutChanges;
}
@NotNull List<EntityData<?>> getEntries() {
return Collections.unmodifiableList(new ArrayList<>(metadataMap.values()));
}
public WrapperPlayServerEntityMetadata createPacket() {
return new WrapperPlayServerEntityMetadata(entityId, getEntries());
}
}