Minor cleanup

This commit is contained in:
KennyTV 2021-03-13 18:39:15 +01:00
parent f498d59303
commit 35ced30047
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
13 changed files with 30 additions and 87 deletions

View File

@ -1,7 +1,9 @@
package nl.matsv.viabackwards.api;
import nl.matsv.viabackwards.api.data.BackwardsMappings;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import org.jetbrains.annotations.Nullable;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ClientboundPacketType;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
@ -25,6 +27,15 @@ public abstract class BackwardsProtocol<C1 extends ClientboundPacketType, C2 ext
ProtocolRegistry.addMappingLoaderFuture(getClass(), protocolClass, runnable);
}
protected void initEntityTracker(UserConnection user) {
EntityTracker entityTracker = user.get(EntityTracker.class);
if (entityTracker == null) {
user.put(entityTracker = new EntityTracker(user));
}
entityTracker.initProtocol(this);
}
@Override
public boolean hasMappingDataToLoad() {
// Manually load them later, since they depend on VV's mappings

View File

@ -16,11 +16,12 @@ import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.entities.EntityType;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class EntityTracker extends StoredObject {
private final Map<BackwardsProtocol, ProtocolEntityTracker> trackers = new ConcurrentHashMap<>();
private final Map<BackwardsProtocol, ProtocolEntityTracker> trackers = new HashMap<>();
private int currentWorldSectionHeight = 16;
private int currentMinY;
@ -89,7 +90,7 @@ public class EntityTracker extends StoredObject {
public static final class StoredEntity {
private final int entityId;
private final EntityType type;
private final Map<Class<? extends EntityStorage>, EntityStorage> storedObjects = new ConcurrentHashMap<>();
private Map<Class<? extends EntityStorage>, EntityStorage> storedObjects;
private StoredEntity(final int entityId, final EntityType type) {
this.entityId = entityId;
@ -105,7 +106,7 @@ public class EntityTracker extends StoredObject {
*/
@Nullable
public <T extends EntityStorage> T get(Class<T> objectClass) {
return (T) storedObjects.get(objectClass);
return storedObjects != null ? (T) storedObjects.get(objectClass) : null;
}
/**
@ -115,7 +116,7 @@ public class EntityTracker extends StoredObject {
* @return True if the object is in the storage
*/
public boolean has(Class<? extends EntityStorage> objectClass) {
return storedObjects.containsKey(objectClass);
return storedObjects != null && storedObjects.containsKey(objectClass);
}
/**
@ -124,6 +125,9 @@ public class EntityTracker extends StoredObject {
* @param object The object to store.
*/
public void put(EntityStorage object) {
if (storedObjects == null) {
storedObjects = new ConcurrentHashMap<>();
}
storedObjects.put(object.getClass(), object);
}

View File

@ -48,18 +48,6 @@ public class MetaStorage {
return null;
}
public Metadata getOrDefault(int index, Metadata data) {
return getOrDefault(index, false, data);
}
public Metadata getOrDefault(int index, boolean removeIfExists, Metadata data) {
Metadata existingData = get(index);
if (removeIfExists && existingData != null) {
delete(existingData);
}
return existingData != null ? existingData : data;
}
public List<Metadata> getMetaDataList() {
return metaDataList;
}

View File

@ -12,7 +12,6 @@ package nl.matsv.viabackwards.protocol.protocol1_10to1_11;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.data.BackwardsMappings;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.api.rewriters.SoundRewriter;
import nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets.BlockItemPackets1_11;
import nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets.EntityPackets1_11;
@ -51,17 +50,11 @@ public class Protocol1_10To1_11 extends BackwardsProtocol<ClientboundPackets1_9_
user.put(new ClientWorld(user));
}
// Register EntityTracker if it doesn't exist yet.
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
initEntityTracker(user);
if (!user.has(WindowTracker.class)) {
user.put(new WindowTracker(user));
}
// Init protocol in EntityTracker
user.get(EntityTracker.class).initProtocol(this);
}
public EntityPackets1_11 getEntityPackets() {

View File

@ -11,7 +11,6 @@
package nl.matsv.viabackwards.protocol.protocol1_11_1to1_12;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker;
import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.packets.BlockItemPackets1_12;
import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.packets.ChatPackets1_12;
@ -69,15 +68,9 @@ public class Protocol1_11_1To1_12 extends BackwardsProtocol<ClientboundPackets1_
user.put(new ClientWorld(user));
}
// Register EntityTracker if it doesn't exist yet.
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
initEntityTracker(user);
user.put(new ShoulderTracker(user));
// Init protocol in EntityTracker
user.get(EntityTracker.class).initProtocol(this);
}
public EntityPackets1_12 getEntityPackets() {

View File

@ -11,7 +11,6 @@
package nl.matsv.viabackwards.protocol.protocol1_11to1_11_1;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.protocol.protocol1_11to1_11_1.packets.EntityPackets1_11_1;
import nl.matsv.viabackwards.protocol.protocol1_11to1_11_1.packets.ItemPackets1_11_1;
import us.myles.ViaVersion.api.data.UserConnection;
@ -40,13 +39,7 @@ public class Protocol1_11To1_11_1 extends BackwardsProtocol<ClientboundPackets1_
user.put(new ClientWorld(user));
}
// Register EntityTracker if it doesn't exist yet.
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
// Init protocol in EntityTracker
user.get(EntityTracker.class).initProtocol(this);
initEntityTracker(user);
}
public EntityPackets1_11_1 getEntityPackets() {

View File

@ -12,7 +12,6 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13;
import nl.matsv.viabackwards.ViaBackwards;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings;
import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.PaintingMapping;
@ -93,13 +92,7 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol<ClientboundPackets1_
user.put(new ClientWorld(user));
}
// Register EntityTracker if it doesn't exist yet.
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
// Init protocol in EntityTracker
user.get(EntityTracker.class).initProtocol(this);
initEntityTracker(user);
// Register Block Storage
if (!user.has(BackwardsBlockStorage.class)) {

View File

@ -2,7 +2,6 @@ package nl.matsv.viabackwards.protocol.protocol1_13_2to1_14;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.data.BackwardsMappings;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.CommandRewriter1_14;
import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.packets.BlockItemPackets1_14;
@ -171,13 +170,7 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol<ClientboundPackets1_
user.put(new ClientWorld(user));
}
// Register EntityTracker if it doesn't exist yet.
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
// Init protocol in EntityTracker
user.get(EntityTracker.class).initProtocol(this);
initEntityTracker(user);
if (!user.has(ChunkLightStorage.class)) {
user.put(new ChunkLightStorage(user));

View File

@ -1,12 +1,10 @@
package nl.matsv.viabackwards.protocol.protocol1_14to1_14_1;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.protocol.protocol1_14to1_14_1.packets.EntityPackets1_14_1;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15;
public class Protocol1_14To1_14_1 extends BackwardsProtocol<ClientboundPackets1_14, ClientboundPackets1_14, ServerboundPackets1_14, ServerboundPackets1_14> {
@ -21,12 +19,6 @@ public class Protocol1_14To1_14_1 extends BackwardsProtocol<ClientboundPackets1_
@Override
public void init(UserConnection user) {
// Register EntityTracker if it doesn't exist yet.
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
// Init protocol in EntityTracker
user.get(EntityTracker.class).initProtocol(this);
initEntityTracker(user);
}
}

View File

@ -1,7 +1,6 @@
package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.api.rewriters.SoundRewriter;
import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter;
import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.chat.TranslatableRewriter1_16;
@ -181,12 +180,10 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol<ClientboundPackets1_
if (!user.has(ClientWorld.class)) {
user.put(new ClientWorld(user));
}
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
user.put(new PlayerSneakStorage(user));
user.put(new WorldNameTracker(user));
user.get(EntityTracker.class).initProtocol(this);
initEntityTracker(user);
}
public BlockItemPackets1_16 getBlockItemPackets() {

View File

@ -2,7 +2,6 @@ package nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.data.BackwardsMappings;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.api.rewriters.SoundRewriter;
import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter;
import nl.matsv.viabackwards.protocol.protocol1_16_1to1_16_2.data.CommandRewriter1_16_2;
@ -118,10 +117,7 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol<ClientboundPackets
@Override
public void init(UserConnection user) {
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
user.get(EntityTracker.class).initProtocol(this);
initEntityTracker(user);
}
public BlockItemPackets1_16_2 getBlockItemPackets() {

View File

@ -200,10 +200,7 @@ public class Protocol1_16_4To1_17 extends BackwardsProtocol<ClientboundPackets1_
@Override
public void init(UserConnection user) {
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
user.get(EntityTracker.class).initProtocol(this);
initEntityTracker(user);
}
public BlockItemPackets1_17 getBlockItemPackets() {

View File

@ -12,7 +12,6 @@ package nl.matsv.viabackwards.protocol.protocol1_9_4to1_10;
import nl.matsv.viabackwards.api.BackwardsProtocol;
import nl.matsv.viabackwards.api.data.BackwardsMappings;
import nl.matsv.viabackwards.api.entities.storage.EntityTracker;
import nl.matsv.viabackwards.api.rewriters.SoundRewriter;
import nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.packets.BlockItemPackets1_10;
import nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.packets.EntityPackets1_10;
@ -87,13 +86,7 @@ public class Protocol1_9_4To1_10 extends BackwardsProtocol<ClientboundPackets1_9
user.put(new ClientWorld(user));
}
// Register EntityTracker if it doesn't exist yet.
if (!user.has(EntityTracker.class)) {
user.put(new EntityTracker(user));
}
// Init protocol in EntityTracker
user.get(EntityTracker.class).initProtocol(this);
initEntityTracker(user);
}
public EntityPackets1_10 getEntityPackets() {