Update Metadata.java
This commit is contained in:
parent
803b9d7f16
commit
6012e8ba80
1 changed files with 31 additions and 36 deletions
|
@ -7,49 +7,38 @@ import me.tofaa.entitylib.EntityLib;
|
||||||
import me.tofaa.entitylib.wrapper.WrapperEntity;
|
import me.tofaa.entitylib.wrapper.WrapperEntity;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.sql.Wrapper;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public class Metadata {
|
public class Metadata {
|
||||||
|
|
||||||
private final int entityId;
|
private final int entityId;
|
||||||
private EntityData[] entries = new EntityData[0];
|
private volatile boolean notifyAboutChanges = true;
|
||||||
private volatile Map<Integer, EntityData> entryMap = null;
|
|
||||||
private volatile boolean notifyChanges = true;
|
|
||||||
private final Map<Byte, EntityData> notNotifiedChanges = new HashMap<>();
|
private final Map<Byte, EntityData> notNotifiedChanges = new HashMap<>();
|
||||||
|
private final Map<Byte, EntityData> metadataMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public Metadata(int entityId) {
|
public Metadata(int entityId) {
|
||||||
this.entityId = entityId;
|
this.entityId = entityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getIndex(byte index, @Nullable T defaultValue) {
|
public <T> T getIndex(byte index, @Nullable T defaultValue) {
|
||||||
final EntityData[] entries = this.entries;
|
EntityData value = this.metadataMap.get(index);
|
||||||
if (index < 0 || index >= entries.length) {
|
return value != null ? (T) value.getValue() : defaultValue;
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
final EntityData entry = entries[index];
|
|
||||||
if (entry == null) return defaultValue;
|
|
||||||
if (entry.getValue() == null) return defaultValue;
|
|
||||||
return (T) entry.getValue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> void setIndex(byte index, @NotNull EntityDataType<T> dataType, T value) {
|
public <T> void setIndex(byte index, @NotNull EntityDataType<T> dataType, T value) {
|
||||||
|
|
||||||
EntityData[] entries = this.entries;
|
final EntityData entry = new EntityData(index, dataType, value);
|
||||||
if (index >= entries.length) {
|
this.metadataMap.put(index, entry);
|
||||||
final int newLength = Math.max(entries.length * 2, index + 1);
|
|
||||||
this.entries = entries = Arrays.copyOf(entries, newLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityData data = new EntityData(index, dataType, value);
|
|
||||||
entries[index] = data;
|
|
||||||
this.entryMap = null;
|
|
||||||
|
|
||||||
final WrapperEntity entity = EntityLib.getApi().getEntity(entityId);
|
final WrapperEntity entity = EntityLib.getApi().getEntity(entityId);
|
||||||
if (entity == null || entity.isSpawned()) return; // Not EntityLib entity then, the user must send the packet manually. OR not spawned.
|
if (entity == null || entity.isSpawned()) return; // Not EntityLib entity then, the user must send the packet manually. OR not spawned.
|
||||||
if (!this.notifyChanges) {
|
if (!this.notifyAboutChanges) {
|
||||||
synchronized (this.notNotifiedChanges) {
|
synchronized (this.notNotifiedChanges) {
|
||||||
this.notNotifiedChanges.put(index, data);
|
this.notNotifiedChanges.put(index, entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -57,32 +46,38 @@ public class Metadata {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNotifyAboutChanges(boolean value) {
|
public void setNotifyAboutChanges(boolean notifyAboutChanges) {
|
||||||
if (this.notifyChanges == value) {
|
if (this.notifyAboutChanges == notifyAboutChanges) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!notifyChanges) {
|
|
||||||
return; // cache;
|
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);
|
final WrapperEntity entity = EntityLib.getApi().getEntity(entityId);
|
||||||
if (entity == null || entity.isSpawned()) return;
|
if (entries == null || entity == null || !entity.isSpawned()) {
|
||||||
Map<Byte, EntityData> entries;
|
return;
|
||||||
synchronized (this.notNotifiedChanges) {
|
|
||||||
Map<Byte, EntityData> awaitingChanges = this.notNotifiedChanges;
|
|
||||||
if (awaitingChanges.isEmpty()) return;
|
|
||||||
entries = Collections.unmodifiableMap(awaitingChanges);
|
|
||||||
awaitingChanges.clear();
|
|
||||||
}
|
}
|
||||||
entity.sendPacketsToViewers(new WrapperPlayServerEntityMetadata(entityId, new ArrayList<>(entries.values())));
|
|
||||||
|
WrapperPlayServerEntityMetadata packet = new WrapperPlayServerEntityMetadata(entityId, entries);
|
||||||
|
entity.sendPacketsToViewers(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isNotifyingChanges() {
|
public boolean isNotifyingChanges() {
|
||||||
return notifyChanges;
|
return notifyAboutChanges;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull List<EntityData> getEntries() {
|
@NotNull List<EntityData> getEntries() {
|
||||||
return Collections.unmodifiableList(Arrays.asList(entries));
|
return Collections.unmodifiableList(new ArrayList<>(metadataMap.values()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public WrapperPlayServerEntityMetadata createPacket() {
|
public WrapperPlayServerEntityMetadata createPacket() {
|
||||||
|
|
Loading…
Reference in a new issue