From 3a1e364d4ac61c19e942fb75552fa8a0dc98b4a4 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Mon, 24 May 2021 23:24:50 +0200 Subject: [PATCH] Refactor entity tracking and meta handling This essentially merges the two approaches to the metadata handling from ViaVersion and ViaBackwards and improves on both designs. ViaVersion did not track every single entity, but only those needed (at least in theory) and can work with untracked entities' metadata. It had a very simple method overridden by metadata rewriter implementations, directly operating on the full metadata list and manually handling meta index changes as well as item/block/particle id changes. ViaBackwards on the other hand had to track *every single* entity and threw warnings otherwise - while less prone to errors due to giving obvious warnings in the console, it unnecessarily tracks a lot of entities, and those warnings also annoys users when encountering virtual entity plugins (operating asynchronously and sending update packets while already untracked or not yet tracked). Dedicated MetaHandlers made id changes and filtering a lot easier to read and write. However, the actual metadata list handling and its distribution to handlers was not very well implemented and required a lot of list copying and creation as well as exception throws to cancel individual metadata entries. This version has MetaFilters built with a Builder containing multiple helper functions, and the entity tracking is properly given its own map, hashed by a Protocol's class, to be easily and generically accessible from anywhere with only a Protocol class from the UserConnection, along with more optimized metadata list iteration. The entity tracking is largely unchanged, keeping ViaVersion's approach to not having to track *all* entities (and being able to handle null types in meta handlers). All of this is by no means absolutely perfect, but is much less prone to errors than both previous systems and takes a lot less effort to actually write. A last possible change would be to use a primitive int to object map that is built to be concurrency save for the EntityTracker, tho that would have to be chosen carefully. --- .../api/connection/UserConnection.java | 27 + .../entity/ClientEntityIdChangeListener.java} | 6 +- .../api/data/entity/EntityTracker.java | 129 +++++ .../api/data/entity/StoredEntityData.java | 60 ++ .../api/minecraft/metadata/MetaType.java | 4 +- .../api/minecraft/metadata/Metadata.java | 16 +- .../metadata/types/MetaType1_12.java | 4 +- .../metadata/types/MetaType1_13.java | 4 +- .../metadata/types/MetaType1_13_2.java | 4 +- .../metadata/types/MetaType1_14.java | 4 +- .../metadata/types/MetaType1_17.java | 4 +- .../minecraft/metadata/types/MetaType1_8.java | 4 +- .../minecraft/metadata/types/MetaType1_9.java | 4 +- .../api/protocol/AbstractProtocol.java | 6 + .../type/types/minecraft/ModernMetaType.java | 10 +- .../type/types/version/Metadata1_8Type.java | 6 +- .../protocol1_9to1_8/BlockListener.java | 13 +- .../bungee/handlers/BungeeServerHandler.java | 21 +- .../bungee/listeners/ElytraPatch.java | 3 +- .../connection/UserConnectionImpl.java | 20 + .../viaversion/data/EntityTracker.java | 68 --- .../data/entity/EntityTrackerBase.java | 133 +++++ .../data/entity/StoredEntityImpl.java | 54 ++ .../Protocol1_10To1_9_3_4.java | 4 +- .../Protocol1_11To1_10.java | 15 +- .../metadata/MetadataRewriter1_11To1_10.java | 38 +- .../storage/EntityTracker1_11.java | 4 +- .../Protocol1_12To1_11_1.java | 16 +- .../MetadataRewriter1_12To1_11_1.java | 13 +- .../storage/EntityTracker1_12.java | 29 - .../Protocol1_13_1To1_13.java | 8 +- .../MetadataRewriter1_13_1To1_13.java | 23 +- .../packets/EntityPackets.java | 9 +- .../packets/EntityPackets.java | 2 +- .../Protocol1_13To1_12_2.java | 11 +- .../MetadataRewriter1_13To1_12_2.java | 45 +- .../packets/EntityPackets.java | 9 +- .../storage/EntityTracker1_13.java | 29 - .../Protocol1_14_1To1_14.java | 10 +- .../MetadataRewriter1_14_1To1_14.java | 13 +- .../packets/EntityPackets.java | 6 +- .../Protocol1_14To1_13_2.java | 9 +- .../MetadataRewriter1_14To1_13_2.java | 68 +-- .../packets/EntityPackets.java | 18 +- .../packets/InventoryPackets.java | 6 +- .../packets/WorldPackets.java | 6 +- .../storage/EntityTracker1_14.java | 10 +- .../Protocol1_15To1_14_4.java | 14 +- .../MetadataRewriter1_15To1_14_4.java | 29 +- .../packets/EntityPackets.java | 13 +- .../packets/PlayerPackets.java | 5 +- .../Protocol1_16_2To1_16_1.java | 14 +- .../MetadataRewriter1_16_2To1_16_1.java | 21 +- .../packets/EntityPackets.java | 7 +- .../storage/EntityTracker1_16_2.java | 29 - .../Protocol1_16To1_15_2.java | 14 +- .../MetadataRewriter1_16To1_15_2.java | 25 +- .../packets/EntityPackets.java | 9 +- .../Protocol1_17To1_16_4.java | 12 +- .../MetadataRewriter1_17To1_16_4.java | 70 +-- .../packets/EntityPackets.java | 7 +- .../packets/WorldPackets.java | 3 +- .../storage/EntityTracker1_17.java | 29 - .../protocol1_9to1_8/Protocol1_9To1_8.java | 7 +- .../protocol1_9to1_8/chat/ChatRewriter.java | 4 +- .../metadata/MetadataRewriter1_9To1_8.java | 13 +- .../packets/EntityPackets.java | 16 +- .../packets/InventoryPackets.java | 8 +- .../packets/PlayerPackets.java | 19 +- .../packets/SpawnPackets.java | 28 +- .../packets/WorldPackets.java | 8 +- .../providers/CommandBlockProvider.java | 3 +- .../providers/EntityIdProvider.java | 4 +- .../storage/EntityTracker1_9.java | 56 +- .../storage/InventoryTracker.java | 3 +- .../viaversion/rewriter/EntityRewriter.java | 521 ++++++++++++++++++ .../viaversion/rewriter/MetadataRewriter.java | 330 ----------- .../Rewriter.java} | 22 +- .../RewriterBase.java} | 36 +- .../viaversion/rewriter/meta/MetaFilter.java | 244 ++++++++ .../meta/MetaHandler.java} | 22 +- .../rewriter/meta/MetaHandlerEvent.java | 121 ++++ .../rewriter/meta/MetaHandlerEventImpl.java | 119 ++++ .../protocol1_9to1_8/BlockListener.java | 6 +- 84 files changed, 1924 insertions(+), 972 deletions(-) rename api/src/main/java/com/viaversion/viaversion/api/{platform/ExternalJoinGameListener.java => data/entity/ClientEntityIdChangeListener.java} (89%) create mode 100644 api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java create mode 100644 api/src/main/java/com/viaversion/viaversion/api/data/entity/StoredEntityData.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/data/EntityTracker.java create mode 100644 common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java create mode 100644 common/src/main/java/com/viaversion/viaversion/data/entity/StoredEntityImpl.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/storage/EntityTracker1_16_2.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/storage/EntityTracker1_17.java create mode 100644 common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java delete mode 100644 common/src/main/java/com/viaversion/viaversion/rewriter/MetadataRewriter.java rename common/src/main/java/com/viaversion/viaversion/{protocols/protocol1_15to1_14_4/storage/EntityTracker1_15.java => rewriter/Rewriter.java} (65%) rename common/src/main/java/com/viaversion/viaversion/{protocols/protocol1_14_1to1_14/storage/EntityTracker1_14_1.java => rewriter/RewriterBase.java} (52%) create mode 100644 common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaFilter.java rename common/src/main/java/com/viaversion/viaversion/{protocols/protocol1_16to1_15_2/storage/EntityTracker1_16.java => rewriter/meta/MetaHandler.java} (58%) create mode 100644 common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandlerEvent.java create mode 100644 common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandlerEventImpl.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/connection/UserConnection.java b/api/src/main/java/com/viaversion/viaversion/api/connection/UserConnection.java index 58410f255..aa918e600 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/connection/UserConnection.java +++ b/api/src/main/java/com/viaversion/viaversion/api/connection/UserConnection.java @@ -23,6 +23,8 @@ package com.viaversion.viaversion.api.connection; import com.viaversion.viaversion.api.configuration.ViaVersionConfig; +import com.viaversion.viaversion.api.data.entity.EntityTracker; +import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.PacketTracker; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.exception.CancelException; @@ -32,6 +34,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import org.checkerframework.checker.nullness.qual.Nullable; +import java.util.Collection; import java.util.Map; import java.util.UUID; import java.util.function.Function; @@ -62,6 +65,30 @@ public interface UserConnection { */ void put(StoredObject object); + /** + * Returns a collection of entity trackers currently registered. + * + * @return collection of entity trackers currently registered + */ + Collection getEntityTrackers(); + + /** + * Returns the entity tracker by the given protocol class if present. + * + * @param protocolClass protocol class + * @param entity tracker type + * @return entity tracker if present + */ + @Nullable T getEntityTracker(Class protocolClass); + + /** + * Adds an entity tracker to the user connection. + * + * @param protocolClass protocol class + * @param tracker entity tracker + */ + void addEntityTracker(Class protocolClass, EntityTracker tracker); + /** * Clear all the stored objects. * Used for bungee when switching servers. diff --git a/api/src/main/java/com/viaversion/viaversion/api/platform/ExternalJoinGameListener.java b/api/src/main/java/com/viaversion/viaversion/api/data/entity/ClientEntityIdChangeListener.java similarity index 89% rename from api/src/main/java/com/viaversion/viaversion/api/platform/ExternalJoinGameListener.java rename to api/src/main/java/com/viaversion/viaversion/api/data/entity/ClientEntityIdChangeListener.java index 155845a38..4180d1349 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/platform/ExternalJoinGameListener.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/entity/ClientEntityIdChangeListener.java @@ -20,9 +20,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package com.viaversion.viaversion.api.platform; +package com.viaversion.viaversion.api.data.entity; -public interface ExternalJoinGameListener { +public interface ClientEntityIdChangeListener { - void onExternalJoinGame(int playerEntityId); + void setClientEntityId(int entityId); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java b/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java new file mode 100644 index 000000000..344a6a84e --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/data/entity/EntityTracker.java @@ -0,0 +1,129 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.data.entity; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import org.checkerframework.checker.nullness.qual.Nullable; + +public interface EntityTracker extends ClientEntityIdChangeListener { + + /** + * User connection the tracker belongs to. + * + * @return user connection + */ + UserConnection user(); + + /** + * Tracks an entity. + * + * @param id entity id + * @param type entity type + */ + void addEntity(int id, EntityType type); + + /** + * Returns whether the entity is currently tracked. + * + * @param id entity id + * @return whether the entity is tracked + */ + boolean hasEntity(int id); + + /** + * Entity type of the entity if tracked. + * This returning null does not necessarily mean no entity by the id exists. + * + * @param id entity id + * @return entity type of the entity if tracked + */ + @Nullable EntityType entityType(int id); + + /** + * Untracks an entity. + * + * @param id entity id + */ + void removeEntity(int id); + + /** + * Returns the stored entity data if an entity with the id is tracked, else null. + * If no data has been initialized yet, it will be done and returned by this method. + * + * @param id entity id + * @return stored entity data if an entity with the id is tracked, else null + */ + @Nullable StoredEntityData entityData(int id); + + /** + * Returns stored entity data if it has previously been initialized by {@link #entityData(int)}, else null. + * + * @param id entity id + * @return stored entity data if it has previously been initialized by {@link #entityData(int)} + */ + @Nullable StoredEntityData entityDataIfPresent(int id); + + /** + * Returns the client entity id or -1 if unset. + * + * @return client entity id or -1 if unset + */ + int clientEntityId(); + + /** + * Sets the client entity id. + * + * @param clientEntityId client entity id + */ + void setClientEntityId(int clientEntityId); + + /** + * Returns the current world section height. + * Always 16 for sub 1.17 worlds. + * + * @return current world section height + */ + int currentWorldSectionHeight(); + + /** + * Sets the current world section height. + * + * @param currentWorldSectionHeight world section height + */ + void setCurrentWorldSectionHeight(int currentWorldSectionHeight); + + /** + * Returns the minimum y of the current player world. + * + * @return minimum y of the current world + */ + int currentMinY(); + + /** + * Sets the minimum y of the current player world. + * + * @param currentMinY minimum y of the current world + */ + void setCurrentMinY(int currentMinY); +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/entity/StoredEntityData.java b/api/src/main/java/com/viaversion/viaversion/api/data/entity/StoredEntityData.java new file mode 100644 index 000000000..a239b75dc --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/data/entity/StoredEntityData.java @@ -0,0 +1,60 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.data.entity; + +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import org.checkerframework.checker.nullness.qual.Nullable; + +public interface StoredEntityData { + + /** + * Returns the entity type of the stored entity. + * + * @return entity type + */ + EntityType type(); + + /** + * Checks if the storage contains an object of the given type. + * + * @param objectClass object class to check + * @return whether an object of the given type is in the storage + */ + boolean has(Class objectClass); + + /** + * Returns an object from the storage if present. + * + * @param objectClass class of the object to get + * @param object type + * @return object if present + */ + @Nullable T get(Class objectClass); + + /** + * Stores an object based on its class. + * + * @param object object to store + */ + void put(Object object); +} \ No newline at end of file diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/MetaType.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/MetaType.java index c87c84e33..c2a90ecbd 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/MetaType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/MetaType.java @@ -31,12 +31,12 @@ public interface MetaType { * * @return Type instance */ - Type getType(); + Type type(); /** * Get type id from the specific MetaDataType * * @return Type id as an integer */ - int getTypeID(); + int typeId(); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/Metadata.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/Metadata.java index 943843a2f..e6cd6c8ec 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/Metadata.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/Metadata.java @@ -24,7 +24,7 @@ package com.viaversion.viaversion.api.minecraft.metadata; import java.util.Objects; -public class Metadata { +public final class Metadata { private int id; private MetaType metaType; private Object value; @@ -35,7 +35,7 @@ public class Metadata { this.value = value; } - public int getId() { + public int id() { return id; } @@ -43,7 +43,7 @@ public class Metadata { this.id = id; } - public MetaType getMetaType() { + public MetaType metaType() { return metaType; } @@ -51,12 +51,12 @@ public class Metadata { this.metaType = metaType; } - public Object getValue() { - return value; + public T value() { + return (T) value; } - public T getCastedValue() { - return (T) value; + public Object getValue() { + return value; } public void setValue(Object value) { @@ -70,7 +70,7 @@ public class Metadata { Metadata metadata = (Metadata) o; if (id != metadata.id) return false; - if (!Objects.equals(metaType, metadata.metaType)) return false; + if (metaType != metaType) return false; return Objects.equals(value, metadata.value); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_12.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_12.java index 0dfd593ff..d704000d4 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_12.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_12.java @@ -55,12 +55,12 @@ public enum MetaType1_12 implements MetaType { } @Override - public int getTypeID() { + public int typeId() { return typeID; } @Override - public Type getType() { + public Type type() { return type; } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_13.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_13.java index 3f5a6db62..e5c1bd772 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_13.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_13.java @@ -58,12 +58,12 @@ public enum MetaType1_13 implements MetaType { } @Override - public int getTypeID() { + public int typeId() { return typeID; } @Override - public Type getType() { + public Type type() { return type; } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_13_2.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_13_2.java index 86e25f5b5..9ea629b16 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_13_2.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_13_2.java @@ -58,12 +58,12 @@ public enum MetaType1_13_2 implements MetaType { } @Override - public int getTypeID() { + public int typeId() { return typeID; } @Override - public Type getType() { + public Type type() { return type; } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_14.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_14.java index c5234322e..387d86a55 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_14.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_14.java @@ -61,12 +61,12 @@ public enum MetaType1_14 implements MetaType { } @Override - public int getTypeID() { + public int typeId() { return typeID; } @Override - public Type getType() { + public Type type() { return type; } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_17.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_17.java index 95f001a23..0be7e9588 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_17.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_17.java @@ -61,12 +61,12 @@ public enum MetaType1_17 implements MetaType { } @Override - public int getTypeID() { + public int typeId() { return typeID; } @Override - public Type getType() { + public Type type() { return type; } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_8.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_8.java index 9360eb750..3693b8eef 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_8.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_8.java @@ -49,12 +49,12 @@ public enum MetaType1_8 implements MetaType { } @Override - public int getTypeID() { + public int typeId() { return typeID; } @Override - public Type getType() { + public Type type() { return type; } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_9.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_9.java index 4daf048af..de626b340 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_9.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/metadata/types/MetaType1_9.java @@ -54,12 +54,12 @@ public enum MetaType1_9 implements MetaType { } @Override - public int getTypeID() { + public int typeId() { return typeID; } @Override - public Type getType() { + public Type type() { return type; } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java index 1dc29cb40..06d68a933 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/AbstractProtocol.java @@ -24,6 +24,8 @@ package com.viaversion.viaversion.api.protocol; import com.google.common.base.Preconditions; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.Direction; import com.viaversion.viaversion.api.protocol.packet.PacketType; @@ -144,6 +146,10 @@ public abstract class AbstractProtocol> 5; MetaType1_8 type = MetaType1_8.byId(typeID); int id = item & 0x1F; - return new Metadata(id, type, type.getType().read(buffer)); + return new Metadata(id, type, type.type().read(buffer)); } @Override public void write(ByteBuf buffer, Metadata meta) throws Exception { - byte item = (byte) (meta.getMetaType().getTypeID() << 5 | meta.getId() & 0x1F); + byte item = (byte) (meta.metaType().typeId() << 5 | meta.id() & 0x1F); buffer.writeByte(item); - meta.getMetaType().getType().write(buffer, meta.getValue()); + meta.metaType().type().write(buffer, meta.getValue()); } } diff --git a/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_9to1_8/BlockListener.java b/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_9to1_8/BlockListener.java index 21f8395ff..9f9ed4f2f 100644 --- a/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_9to1_8/BlockListener.java +++ b/bukkit-legacy/src/main/java/com/viaversion/viaversion/bukkit/listeners/protocol1_9to1_8/BlockListener.java @@ -17,15 +17,15 @@ */ package com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8; +import com.viaversion.viaversion.api.minecraft.Position; +import com.viaversion.viaversion.bukkit.listeners.ViaBukkitListener; +import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; +import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.plugin.Plugin; -import com.viaversion.viaversion.api.minecraft.Position; -import com.viaversion.viaversion.bukkit.listeners.ViaBukkitListener; -import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; public class BlockListener extends ViaBukkitListener { @@ -37,9 +37,8 @@ public class BlockListener extends ViaBukkitListener { public void placeBlock(BlockPlaceEvent e) { if (isOnPipe(e.getPlayer())) { Block b = e.getBlockPlaced(); - getUserConnection(e.getPlayer()) - .get(EntityTracker1_9.class) - .addBlockInteraction(new Position(b.getX(), (short) b.getY(), b.getZ())); + EntityTracker1_9 tracker = getUserConnection(e.getPlayer()).getEntityTracker(Protocol1_9To1_8.class); + tracker.addBlockInteraction(new Position(b.getX(), (short) b.getY(), b.getZ())); } } } diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java index 7b56469e3..013a05bc5 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java @@ -21,10 +21,11 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.ProtocolInfo; import com.viaversion.viaversion.api.connection.StoredObject; import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.platform.ExternalJoinGameListener; +import com.viaversion.viaversion.api.data.entity.ClientEntityIdChangeListener; +import com.viaversion.viaversion.api.data.entity.EntityTracker; +import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.ProtocolPathEntry; import com.viaversion.viaversion.api.protocol.ProtocolPipeline; -import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.type.Type; @@ -125,9 +126,15 @@ public class BungeeServerHandler implements Listener { } catch (Exception ex) { return; // Ignored } - for (StoredObject storedObject : userConnection.getStoredObjects().values()) { - if (storedObject instanceof ExternalJoinGameListener) { - ((ExternalJoinGameListener) storedObject).onExternalJoinGame(playerId); + + for (EntityTracker tracker : userConnection.getEntityTrackers()) { + tracker.setClientEntityId(playerId); + } + + // For ViaRewind + for (StoredObject object : userConnection.getStoredObjects().values()) { + if (object instanceof ClientEntityIdChangeListener) { + ((ClientEntityIdChangeListener) object).setClientEntityId(playerId); } } } @@ -143,7 +150,7 @@ public class BungeeServerHandler implements Listener { if (e.getServer() != null) { if (!e.getServer().getInfo().getName().equals(storage.getCurrentServer())) { // Clear auto-team - EntityTracker1_9 oldEntityTracker = user.get(EntityTracker1_9.class); + EntityTracker1_9 oldEntityTracker = user.getEntityTracker(Protocol1_9To1_8.class); if (oldEntityTracker != null) { if (oldEntityTracker.isAutoTeam() && oldEntityTracker.isTeamExists()) { oldEntityTracker.sendTeamPacket(false, true); @@ -239,7 +246,7 @@ public class BungeeServerHandler implements Listener { protocol.init(user); } - EntityTracker1_9 newTracker = user.get(EntityTracker1_9.class); + EntityTracker1_9 newTracker = user.getEntityTracker(Protocol1_9To1_8.class); if (newTracker != null) { if (Via.getConfig().isAutoTeam()) { String currentTeam = null; diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/listeners/ElytraPatch.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/listeners/ElytraPatch.java index ce1abf649..fee202dd5 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/listeners/ElytraPatch.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/listeners/ElytraPatch.java @@ -45,7 +45,8 @@ public class ElytraPatch implements Listener { try { if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) { - int entityId = user.get(EntityTracker1_9.class).getProvidedEntityId(); + EntityTracker1_9 tracker = user.getEntityTracker(Protocol1_9To1_8.class); + int entityId = tracker.getProvidedEntityId(); PacketWrapper wrapper = PacketWrapper.create(0x39, null, user); diff --git a/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java b/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java index 9da325c37..67235f96f 100644 --- a/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java @@ -22,6 +22,8 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.ProtocolInfo; import com.viaversion.viaversion.api.connection.StoredObject; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.entity.EntityTracker; +import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.Direction; import com.viaversion.viaversion.api.protocol.packet.PacketTracker; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -35,7 +37,9 @@ import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import org.checkerframework.checker.nullness.qual.Nullable; +import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -48,6 +52,7 @@ public class UserConnectionImpl implements UserConnection { private static final AtomicLong IDS = new AtomicLong(); private final long id = IDS.incrementAndGet(); private final Map, StoredObject> storedObjects = new ConcurrentHashMap<>(); + private final Map, EntityTracker> entityTrackers = new HashMap<>(); private final PacketTracker packetTracker = new PacketTracker(this); private final Set passthroughTokens = Collections.newSetFromMap(CacheBuilder.newBuilder() .expireAfterWrite(10, TimeUnit.SECONDS) @@ -91,6 +96,21 @@ public class UserConnectionImpl implements UserConnection { storedObjects.put(object.getClass(), object); } + @Override + public Collection getEntityTrackers() { + return entityTrackers.values(); + } + + @Override + public @Nullable T getEntityTracker(Class protocolClass) { + return (T) entityTrackers.get(protocolClass); + } + + @Override + public void addEntityTracker(Class protocolClass, EntityTracker tracker) { + entityTrackers.put(protocolClass, tracker); + } + @Override public void clearStoredObjects() { storedObjects.clear(); diff --git a/common/src/main/java/com/viaversion/viaversion/data/EntityTracker.java b/common/src/main/java/com/viaversion/viaversion/data/EntityTracker.java deleted file mode 100644 index 82f38b679..000000000 --- a/common/src/main/java/com/viaversion/viaversion/data/EntityTracker.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.data; - -import com.viaversion.viaversion.api.connection.StoredObject; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.EntityType; -import com.viaversion.viaversion.api.platform.ExternalJoinGameListener; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public abstract class EntityTracker extends StoredObject implements ExternalJoinGameListener { - private final Map clientEntityTypes = new ConcurrentHashMap<>(); - private final EntityType playerType; - private int clientEntityId; - - protected EntityTracker(UserConnection user, EntityType playerType) { - super(user); - this.playerType = playerType; - } - - public void removeEntity(int entityId) { - clientEntityTypes.remove(entityId); - } - - public void addEntity(int entityId, EntityType type) { - clientEntityTypes.put(entityId, type); - } - - public boolean hasEntity(int entityId) { - return clientEntityTypes.containsKey(entityId); - } - - public @Nullable EntityType getEntity(int entityId) { - return clientEntityTypes.get(entityId); - } - - @Override - public void onExternalJoinGame(int playerEntityId) { - clientEntityId = playerEntityId; - clientEntityTypes.put(playerEntityId, playerType); - } - - public int getClientEntityId() { - return clientEntityId; - } - - public void setClientEntityId(int clientEntityId) { - this.clientEntityId = clientEntityId; - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java b/common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java new file mode 100644 index 000000000..77ede46f9 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/data/entity/EntityTrackerBase.java @@ -0,0 +1,133 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.viaversion.viaversion.data.entity; + +import com.google.common.base.Preconditions; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.entity.EntityTracker; +import com.viaversion.viaversion.api.data.entity.StoredEntityData; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class EntityTrackerBase implements EntityTracker { + private final Map entityTypes = new ConcurrentHashMap<>(); + private final Map entityData; + private final UserConnection connection; + private final EntityType playerType; + private int clientEntityId = -1; + private int currentWorldSectionHeight = 16; + private int currentMinY; + + public EntityTrackerBase(UserConnection connection, @Nullable EntityType playerType) { + this(connection, playerType, false); + } + + public EntityTrackerBase(UserConnection connection, @Nullable EntityType playerType, boolean storesEntityData) { + this.connection = connection; + this.playerType = playerType; + this.entityData = storesEntityData ? new ConcurrentHashMap<>() : null; + } + + @Override + public UserConnection user() { + return connection; + } + + @Override + public void addEntity(int id, EntityType type) { + entityTypes.put(id, type); + } + + @Override + public boolean hasEntity(int id) { + return entityTypes.containsKey(id); + } + + @Override + public @Nullable EntityType entityType(int id) { + return entityTypes.get(id); + } + + @Override + public StoredEntityData entityData(int id) { + EntityType type = entityType(id); + return entityData.computeIfAbsent(id, s -> new StoredEntityImpl(type)); + } + + @Override + public @Nullable StoredEntityData entityDataIfPresent(int id) { + return entityData.get(id); + } + + @Override + public void removeEntity(int id) { + entityTypes.remove(id); + if (entityData != null) { + entityData.remove(id); + } + } + + @Override + public int clientEntityId() { + return clientEntityId; + } + + @Override + public void setClientEntityId(int clientEntityId) { + Preconditions.checkNotNull(playerType); + entityTypes.put(clientEntityId, playerType); + if (entityData != null) { + StoredEntityData data = entityData.remove(this.clientEntityId); + if (data != null) { + entityData.put(clientEntityId, data); + } + } + + this.clientEntityId = clientEntityId; + } + + /** + * @return amount of chunk sections of the current world (block height / 16) + */ + @Override + public int currentWorldSectionHeight() { + return currentWorldSectionHeight; + } + + @Override + public void setCurrentWorldSectionHeight(int currentWorldSectionHeight) { + this.currentWorldSectionHeight = currentWorldSectionHeight; + } + + /** + * @return absolute minimum y coordinate of the current world + */ + @Override + public int currentMinY() { + return currentMinY; + } + + @Override + public void setCurrentMinY(int currentMinY) { + this.currentMinY = currentMinY; + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/data/entity/StoredEntityImpl.java b/common/src/main/java/com/viaversion/viaversion/data/entity/StoredEntityImpl.java new file mode 100644 index 000000000..4f85ea80f --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/data/entity/StoredEntityImpl.java @@ -0,0 +1,54 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.data.entity; + +import com.viaversion.viaversion.api.data.entity.StoredEntityData; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public final class StoredEntityImpl implements StoredEntityData { + private final Map, Object> storedObjects = new ConcurrentHashMap<>(); + private final EntityType type; + + public StoredEntityImpl(EntityType type) { + this.type = type; + } + + @Override + public EntityType type() { + return type; + } + + @Override + public @Nullable T get(Class objectClass) { + return (T) storedObjects.get(objectClass); + } + + @Override + public boolean has(Class objectClass) { + return storedObjects.containsKey(objectClass); + } + + @Override + public void put(Object object) { + storedObjects.put(object.getClass(), object); + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_10to1_9_3/Protocol1_10To1_9_3_4.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_10to1_9_3/Protocol1_10To1_9_3_4.java index 791220555..e9e6888f1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_10to1_9_3/Protocol1_10To1_9_3_4.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_10to1_9_3/Protocol1_10To1_9_3_4.java @@ -48,8 +48,8 @@ public class Protocol1_10To1_9_3_4 extends AbstractProtocol transform(PacketWrapper wrapper, List inputValue) throws Exception { List metaList = new CopyOnWriteArrayList<>(inputValue); for (Metadata m : metaList) { - if (m.getId() >= 5) - m.setId(m.getId() + 1); + if (m.id() >= 5) + m.setId(m.id() + 1); } return metaList; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java index d4be500af..2f19df9d4 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java @@ -39,7 +39,7 @@ import com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1 import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.util.Pair; @@ -57,7 +57,8 @@ public class Protocol1_11To1_10 extends AbstractProtocol { public MetadataRewriter1_11To1_10(Protocol1_11To1_10 protocol) { - super(protocol, EntityTracker1_11.class); + super(protocol); } @Override @@ -49,7 +49,7 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter { if (type == null) return; if (type.is(EntityType.ELDER_GUARDIAN) || type.is(EntityType.GUARDIAN)) { // Guardians - int oldid = metadata.getId(); + int oldid = metadata.id(); if (oldid == 12) { metadata.setMetaType(MetaType1_9.Boolean); boolean val = (((byte) metadata.getValue()) & 0x02) == 0x02; @@ -58,7 +58,7 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter { } if (type.isOrHasParent(EntityType.ABSTRACT_SKELETON)) { // Skeletons - int oldid = metadata.getId(); + int oldid = metadata.id(); if (oldid == 12) { metadatas.remove(metadata); } @@ -68,13 +68,13 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter { } if (type.isOrHasParent(EntityType.ZOMBIE)) { // Zombie | Zombie Villager | Husk - if (type.is(EntityType.ZOMBIE, EntityType.HUSK) && metadata.getId() == 14) { + if (type.is(EntityType.ZOMBIE, EntityType.HUSK) && metadata.id() == 14) { metadatas.remove(metadata); } else { - if (metadata.getId() == 15) { + if (metadata.id() == 15) { metadata.setId(14); } else { - if (metadata.getId() == 14) { + if (metadata.id() == 14) { metadata.setId(15); } } @@ -83,7 +83,7 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter { if (type.isOrHasParent(EntityType.ABSTRACT_HORSE)) { // Horses // Remap metadata id - int oldid = metadata.getId(); + int oldid = metadata.id(); if (oldid == 14) { // Type metadatas.remove(metadata); } @@ -99,13 +99,13 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter { // Normal Horse } else { // Remove 15, 16 - if (metadata.getId() == 15 || metadata.getId() == 16) { + if (metadata.id() == 15 || metadata.id() == 16) { metadatas.remove(metadata); } } if (type.is(EntityType.DONKEY, EntityType.MULE)) { // Chested Horse - if (metadata.getId() == 13) { + if (metadata.id() == 13) { if ((((byte) metadata.getValue()) & 0x08) == 0x08) { metadatas.add(new Metadata(15, MetaType1_9.Boolean, true)); } else { @@ -116,15 +116,15 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter { } if (type.is(EntityType.ARMOR_STAND) && Via.getConfig().isHologramPatch()) { - Metadata flags = getMetaByIndex(11, metadatas); - Metadata customName = getMetaByIndex(2, metadatas); - Metadata customNameVisible = getMetaByIndex(3, metadatas); - if (metadata.getId() == 0 && flags != null && customName != null && customNameVisible != null) { + Metadata flags = metaByIndex(11, metadatas); + Metadata customName = metaByIndex(2, metadatas); + Metadata customNameVisible = metaByIndex(3, metadatas); + if (metadata.id() == 0 && flags != null && customName != null && customNameVisible != null) { byte data = (byte) metadata.getValue(); // Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true if ((data & 0x20) == 0x20 && ((byte) flags.getValue() & 0x01) == 0x01 && !((String) customName.getValue()).isEmpty() && (boolean) customNameVisible.getValue()) { - EntityTracker1_11 tracker = connection.get(EntityTracker1_11.class); + EntityTracker1_11 tracker = tracker(connection); if (!tracker.isHologram(entityId)) { tracker.addHologram(entityId); try { @@ -147,12 +147,12 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter { } @Override - protected com.viaversion.viaversion.api.minecraft.entities.EntityType getTypeFromId(int type) { + protected com.viaversion.viaversion.api.minecraft.entities.EntityType typeFromId(int type) { return Entity1_11Types.getTypeFromId(type, false); } @Override - protected com.viaversion.viaversion.api.minecraft.entities.EntityType getObjectTypeFromId(int type) { + protected com.viaversion.viaversion.api.minecraft.entities.EntityType objectTypeFromId(int type) { return Entity1_11Types.getTypeFromId(type, true); } @@ -240,7 +240,7 @@ public class MetadataRewriter1_11To1_10 extends MetadataRewriter { public static Optional getById(List metadatas, int id) { for (Metadata metadata : metadatas) { - if (metadata.getId() == id) return Optional.of(metadata); + if (metadata.id() == id) return Optional.of(metadata); } return Optional.empty(); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java index 68662c750..76fe00a6b 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/storage/EntityTracker1_11.java @@ -20,11 +20,11 @@ package com.viaversion.viaversion.protocols.protocol1_11to1_10.storage; import com.google.common.collect.Sets; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types.EntityType; -import com.viaversion.viaversion.data.EntityTracker; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import java.util.Set; -public class EntityTracker1_11 extends EntityTracker { +public class EntityTracker1_11 extends EntityTrackerBase { private final Set holograms = Sets.newConcurrentHashSet(); public EntityTracker1_11(UserConnection user) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java index e268e0851..c5ced625f 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/Protocol1_12To1_11_1.java @@ -25,6 +25,7 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.chunks.Chunk; import com.viaversion.viaversion.api.minecraft.chunks.ChunkSection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; import com.viaversion.viaversion.api.platform.providers.ViaProviders; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -32,16 +33,16 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_12; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.metadata.MetadataRewriter1_12To1_11_1; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.packets.InventoryPackets; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider; -import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.storage.EntityTracker1_12; import com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter; public class Protocol1_12To1_11_1 extends AbstractProtocol { @@ -52,7 +53,8 @@ public class Protocol1_12To1_11_1 extends AbstractProtocol { public MetadataRewriter1_12To1_11_1(Protocol1_12To1_11_1 protocol) { - super(protocol, EntityTracker1_12.class); + super(protocol); } @Override @@ -45,19 +44,19 @@ public class MetadataRewriter1_12To1_11_1 extends MetadataRewriter { if (type == null) return; // Evocation Illager aggressive property became 13 if (type == Entity1_12Types.EntityType.EVOCATION_ILLAGER) { - if (metadata.getId() == 12) { + if (metadata.id() == 12) { metadata.setId(13); } } } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_12Types.getTypeFromId(type, false); } @Override - protected EntityType getObjectTypeFromId(int type) { + protected EntityType objectTypeFromId(int type) { return Entity1_12Types.getTypeFromId(type, true); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java deleted file mode 100644 index ee11b7610..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/storage/EntityTracker1_12.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_12to1_11_1.storage; - -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types.EntityType; -import com.viaversion.viaversion.data.EntityTracker; - -public class EntityTracker1_12 extends EntityTracker { - - public EntityTracker1_12(UserConnection user) { - super(user, EntityType.PLAYER); - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java index 720690036..88c13aa95 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java @@ -20,6 +20,7 @@ package com.viaversion.viaversion.protocols.protocol1_13_1to1_13; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.data.MappingDataBase; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -27,13 +28,13 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.packets.EntityPackets; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.packets.InventoryPackets; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.packets.WorldPackets; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import com.viaversion.viaversion.rewriter.RegistryType; import com.viaversion.viaversion.rewriter.StatisticsRewriter; @@ -49,7 +50,8 @@ public class Protocol1_13_1To1_13 extends AbstractProtocol { public MetadataRewriter1_13_1To1_13(Protocol1_13_1To1_13 protocol) { - super(protocol, EntityTracker1_13.class); + super(protocol); } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) { // 1.13 changed item to flat item (no data) - if (metadata.getMetaType() == MetaType1_13.Slot) { + if (metadata.metaType() == MetaType1_13.Slot) { InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_13.BlockID) { + } else if (metadata.metaType() == MetaType1_13.BlockID) { // Convert to new block id int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (metadata.getMetaType() == MetaType1_13.PARTICLE) { + } else if (metadata.metaType() == MetaType1_13.PARTICLE) { rewriteParticle((Particle) metadata.getValue()); } if (type == null) return; - if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { + if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.id() == 9) { // New block format int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (type.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW) && metadata.getId() >= 7) { - metadata.setId(metadata.getId() + 1); // New shooter UUID + } else if (type.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW) && metadata.id() >= 7) { + metadata.setId(metadata.id() + 1); // New shooter UUID } } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_13Types.getTypeFromId(type, false); } @Override - protected EntityType getObjectTypeFromId(int type) { + protected EntityType objectTypeFromId(int type) { return Entity1_13Types.getTypeFromId(type, true); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java index 5b8740bb8..215964309 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/EntityPackets.java @@ -26,7 +26,6 @@ import com.viaversion.viaversion.api.type.types.version.Types1_13; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; public class EntityPackets { @@ -60,7 +59,7 @@ public class EntityPackets { wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data)); } // Register Type ID - wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); + wrapper.user().getEntityTracker(Protocol1_13_1To1_13.class).addEntity(entityId, entType); } } }); @@ -84,7 +83,7 @@ public class EntityPackets { map(Type.SHORT); // 11 - Velocity Z map(Types1_13.METADATA_LIST); // 12 - Metadata - handler(metadataRewriter.getTrackerAndRewriter(Types1_13.METADATA_LIST)); + handler(metadataRewriter.trackerAndRewriterHandler(Types1_13.METADATA_LIST)); } }); @@ -100,11 +99,11 @@ public class EntityPackets { map(Type.BYTE); // 6 - Pitch map(Types1_13.METADATA_LIST); // 7 - Metadata - handler(metadataRewriter.getTrackerAndRewriter(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER)); + handler(metadataRewriter.trackerAndRewriterHandler(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER)); } }); - metadataRewriter.registerEntityDestroy(ClientboundPackets1_13.DESTROY_ENTITIES); + metadataRewriter.registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13.METADATA_LIST); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/EntityPackets.java index 02c59689b..555460472 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/EntityPackets.java @@ -32,7 +32,7 @@ public class EntityPackets { public static void register(Protocol protocol) { final PacketHandler metaTypeHandler = wrapper -> { for (Metadata metadata : wrapper.get(Types1_13_2.METADATA_LIST, 0)) { - metadata.setMetaType(MetaType1_13_2.byId(metadata.getMetaType().getTypeID())); + metadata.setMetaType(MetaType1_13_2.byId(metadata.metaType().typeId())); } }; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index 8e9cdd2f1..fe6dc88e0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -35,6 +35,7 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.ValueCreator; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData; @@ -53,10 +54,9 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockE import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.PaintingProvider; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockConnectionStorage; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.TabCompleteTracker; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.util.ChatColorUtil; import com.viaversion.viaversion.util.GsonUtil; @@ -162,7 +162,8 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol { public MetadataRewriter1_13To1_12_2(Protocol1_13To1_12_2 protocol) { - super(protocol, EntityTracker1_13.class); + super(protocol); } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { // Handle new MetaTypes - if (metadata.getMetaType().getTypeID() > 4) { - metadata.setMetaType(MetaType1_13.byId(metadata.getMetaType().getTypeID() + 1)); + if (metadata.metaType().typeId() > 4) { + metadata.setMetaType(MetaType1_13.byId(metadata.metaType().typeId() + 1)); } else { - metadata.setMetaType(MetaType1_13.byId(metadata.getMetaType().getTypeID())); + metadata.setMetaType(MetaType1_13.byId(metadata.metaType().typeId())); } // Handle String -> Chat DisplayName - if (metadata.getId() == 2) { + if (metadata.id() == 2) { metadata.setMetaType(MetaType1_13.OptChat); if (metadata.getValue() != null && !((String) metadata.getValue()).isEmpty()) { metadata.setValue(ChatRewriter.legacyTextToJson((String) metadata.getValue())); @@ -61,7 +60,7 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { } // Remap held block to match new format for remapping to flat block - if (type == Entity1_13Types.EntityType.ENDERMAN && metadata.getId() == 12) { + if (type == Entity1_13Types.EntityType.ENDERMAN && metadata.id() == 12) { int stateId = (int) metadata.getValue(); int id = stateId & 4095; int data = stateId >> 12 & 15; @@ -69,10 +68,10 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { } // 1.13 changed item to flat item (no data) - if (metadata.getMetaType() == MetaType1_13.Slot) { + if (metadata.metaType() == MetaType1_13.Slot) { metadata.setMetaType(MetaType1_13.Slot); InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_13.BlockID) { + } else if (metadata.metaType() == MetaType1_13.BlockID) { // Convert to new block id metadata.setValue(WorldPackets.toNewId((int) metadata.getValue())); } @@ -81,18 +80,18 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { if (type == null) return; // Handle new colors - if (type == Entity1_13Types.EntityType.WOLF && metadata.getId() == 17) { + if (type == Entity1_13Types.EntityType.WOLF && metadata.id() == 17) { metadata.setValue(15 - (int) metadata.getValue()); } // Handle new zombie meta (INDEX 15 - Boolean - Zombie is shaking while enabled) if (type.isOrHasParent(Entity1_13Types.EntityType.ZOMBIE)) { - if (metadata.getId() > 14) - metadata.setId(metadata.getId() + 1); + if (metadata.id() > 14) + metadata.setId(metadata.id() + 1); } // Handle Minecart inner block - if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.getId() == 9) { + if (type.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT) && metadata.id() == 9) { // New block format int oldId = (int) metadata.getValue(); int combined = (((oldId & 4095) << 4) | (oldId >> 12 & 15)); @@ -102,10 +101,10 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { // Handle other changes if (type == Entity1_13Types.EntityType.AREA_EFFECT_CLOUD) { - if (metadata.getId() == 9) { + if (metadata.id() == 9) { int particleId = (int) metadata.getValue(); - Metadata parameter1Meta = getMetaByIndex(10, metadatas); - Metadata parameter2Meta = getMetaByIndex(11, metadatas); + Metadata parameter1Meta = metaByIndex(10, metadatas); + Metadata parameter2Meta = metaByIndex(11, metadatas); int parameter1 = parameter1Meta != null ? (int) parameter1Meta.getValue() : 0; int parameter2 = parameter2Meta != null ? (int) parameter2Meta.getValue() : 0; @@ -115,11 +114,11 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { } } - if (metadata.getId() >= 9) + if (metadata.id() >= 9) metadatas.remove(metadata); // Remove } - if (metadata.getId() == 0) { + if (metadata.id() == 0) { metadata.setValue((byte) ((byte) metadata.getValue() & ~0x10)); // Previously unused, now swimming } @@ -127,17 +126,17 @@ public class MetadataRewriter1_13To1_12_2 extends MetadataRewriter { } @Override - public int getNewEntityId(final int oldId) { + public int newEntityId(final int oldId) { return EntityTypeRewriter.getNewId(oldId); } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_13Types.getTypeFromId(type, false); } @Override - protected EntityType getObjectTypeFromId(int type) { + protected EntityType objectTypeFromId(int type) { return Entity1_13Types.getTypeFromId(type, true); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java index 9f98b55b0..965b4e439 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/EntityPackets.java @@ -27,7 +27,6 @@ import com.viaversion.viaversion.api.type.types.version.Types1_13; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.metadata.MetadataRewriter1_13To1_12_2; -import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; public class EntityPackets { @@ -85,7 +84,7 @@ public class EntityPackets { wrapper.set(Type.INT, 0, data); // Register Type ID - wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType); + wrapper.user().getEntityTracker(Protocol1_13To1_12_2.class).addEntity(entityId, entType); } } } @@ -110,7 +109,7 @@ public class EntityPackets { map(Type.SHORT); // 11 - Velocity Z map(Types1_12.METADATA_LIST, Types1_13.METADATA_LIST); // 12 - Metadata - handler(metadataRewriter.getTrackerAndRewriter(Types1_13.METADATA_LIST)); + handler(metadataRewriter.trackerAndRewriterHandler(Types1_13.METADATA_LIST)); } }); @@ -126,11 +125,11 @@ public class EntityPackets { map(Type.BYTE); // 6 - Pitch map(Types1_12.METADATA_LIST, Types1_13.METADATA_LIST); // 7 - Metadata - handler(metadataRewriter.getTrackerAndRewriter(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER)); + handler(metadataRewriter.trackerAndRewriterHandler(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER)); } }); - metadataRewriter.registerEntityDestroy(ClientboundPackets1_12_1.DESTROY_ENTITIES); + metadataRewriter.registerRemoveEntities(ClientboundPackets1_12_1.DESTROY_ENTITIES); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_12_1.ENTITY_METADATA, Types1_12.METADATA_LIST, Types1_13.METADATA_LIST); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java deleted file mode 100644 index ea947a4ec..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/storage/EntityTracker1_13.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage; - -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types.EntityType; -import com.viaversion.viaversion.data.EntityTracker; - -public class EntityTracker1_13 extends EntityTracker { - - public EntityTracker1_13(UserConnection user) { - super(user, EntityType.PLAYER); - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java index 49b086d18..7f134ef2f 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/Protocol1_14_1To1_14.java @@ -18,13 +18,14 @@ package com.viaversion.viaversion.protocols.protocol1_14_1to1_14; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; import com.viaversion.viaversion.api.protocol.AbstractProtocol; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_14_1to1_14.metadata.MetadataRewriter1_14_1To1_14; import com.viaversion.viaversion.protocols.protocol1_14_1to1_14.packets.EntityPackets; -import com.viaversion.viaversion.protocols.protocol1_14_1to1_14.storage.EntityTracker1_14_1; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; public class Protocol1_14_1To1_14 extends AbstractProtocol { @@ -34,13 +35,14 @@ public class Protocol1_14_1To1_14 extends AbstractProtocol { public MetadataRewriter1_14_1To1_14(Protocol1_14_1To1_14 protocol) { - super(protocol, EntityTracker1_14_1.class); + super(protocol); } @Override @@ -38,14 +37,14 @@ public class MetadataRewriter1_14_1To1_14 extends MetadataRewriter { if (type == null) return; if (type == Entity1_14Types.VILLAGER || type == Entity1_14Types.WANDERING_TRADER) { - if (metadata.getId() >= 15) { - metadata.setId(metadata.getId() + 1); + if (metadata.id() >= 15) { + metadata.setId(metadata.id() + 1); } } } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_14Types.getTypeFromId(type); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/packets/EntityPackets.java index 75a821db1..474dc16aa 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/packets/EntityPackets.java @@ -47,11 +47,11 @@ public class EntityPackets { map(Type.SHORT); // 11 - Velocity Z map(Types1_14.METADATA_LIST); // 12 - Metadata - handler(metadataRewriter.getTrackerAndRewriter(Types1_14.METADATA_LIST)); + handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST)); } }); - metadataRewriter.registerEntityDestroy(ClientboundPackets1_14.DESTROY_ENTITIES); + metadataRewriter.registerRemoveEntities(ClientboundPackets1_14.DESTROY_ENTITIES); protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PLAYER, new PacketRemapper() { @Override @@ -65,7 +65,7 @@ public class EntityPackets { map(Type.BYTE); // 6 - Pitch map(Types1_14.METADATA_LIST); // 7 - Metadata - handler(metadataRewriter.getTrackerAndRewriter(Types1_14.METADATA_LIST, Entity1_14Types.PLAYER)); + handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST, Entity1_14Types.PLAYER)); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java index 9c42d5123..aa2063ff3 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java @@ -36,7 +36,7 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.WorldPac import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; import com.viaversion.viaversion.rewriter.ComponentRewriter; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; @@ -50,7 +50,8 @@ public class Protocol1_14To1_13_2 extends AbstractProtocol { public MetadataRewriter1_14To1_13_2(Protocol1_14To1_13_2 protocol) { - super(protocol, EntityTracker1_14.class); + super(protocol); mapTypes(Entity1_13Types.EntityType.values(), Entity1_14Types.class); - mapType(Entity1_13Types.EntityType.OCELOT, Entity1_14Types.CAT); //TODO remap untamed ocelots to ocelots? + mapEntityType(Entity1_13Types.EntityType.OCELOT, Entity1_14Types.CAT); //TODO remap untamed ocelots to ocelots? } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { - metadata.setMetaType(MetaType1_14.byId(metadata.getMetaType().getTypeID())); + metadata.setMetaType(MetaType1_14.byId(metadata.metaType().typeId())); - EntityTracker1_14 tracker = connection.get(EntityTracker1_14.class); + EntityTracker1_14 tracker = tracker(connection); - if (metadata.getMetaType() == MetaType1_14.Slot) { + if (metadata.metaType() == MetaType1_14.Slot) { InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_14.BlockID) { + } else if (metadata.metaType() == MetaType1_14.BlockID) { // Convert to new block id int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (metadata.getMetaType() == MetaType1_14.PARTICLE) { + } else if (metadata.metaType() == MetaType1_14.PARTICLE) { rewriteParticle((Particle) metadata.getValue()); } if (type == null) return; //Metadata 6 added to abstract_entity - if (metadata.getId() > 5) { - metadata.setId(metadata.getId() + 1); + if (metadata.id() > 5) { + metadata.setId(metadata.id() + 1); } - if (metadata.getId() == 8 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) { + if (metadata.id() == 8 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) { final float v = ((Number) metadata.getValue()).floatValue(); if (Float.isNaN(v) && Via.getConfig().is1_14HealthNaNFix()) { metadata.setValue(1F); @@ -74,12 +74,12 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { } //Metadata 12 added to living_entity - if (metadata.getId() > 11 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) { - metadata.setId(metadata.getId() + 1); + if (metadata.id() > 11 && type.isOrHasParent(Entity1_14Types.LIVINGENTITY)) { + metadata.setId(metadata.id() + 1); } if (type.isOrHasParent(Entity1_14Types.ABSTRACT_INSENTIENT)) { - if (metadata.getId() == 13) { + if (metadata.id() == 13) { tracker.setInsentientData(entityId, (byte) ((((Number) metadata.getValue()).byteValue() & ~0x4) | (tracker.getInsentientData(entityId) & 0x4))); // New attacking metadata metadata.setValue(tracker.getInsentientData(entityId)); @@ -87,37 +87,37 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { } if (type.isOrHasParent(Entity1_14Types.PLAYER)) { - if (entityId != tracker.getClientEntityId()) { - if (metadata.getId() == 0) { + if (entityId != tracker.clientEntityId()) { + if (metadata.id() == 0) { byte flags = ((Number) metadata.getValue()).byteValue(); // Mojang overrides the client-side pose updater, see OtherPlayerEntity#updateSize tracker.setEntityFlags(entityId, flags); - } else if (metadata.getId() == 7) { + } else if (metadata.id() == 7) { tracker.setRiptide(entityId, (((Number) metadata.getValue()).byteValue() & 0x4) != 0); } - if (metadata.getId() == 0 || metadata.getId() == 7) { + if (metadata.id() == 0 || metadata.id() == 7) { metadatas.add(new Metadata(6, MetaType1_14.Pose, recalculatePlayerPose(entityId, tracker))); } } } else if (type.isOrHasParent(Entity1_14Types.ZOMBIE)) { - if (metadata.getId() == 16) { + if (metadata.id() == 16) { tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking metadatas.remove(metadata); // "Are hands held up" metadatas.add(new Metadata(13, MetaType1_14.Byte, tracker.getInsentientData(entityId))); - } else if (metadata.getId() > 16) { - metadata.setId(metadata.getId() - 1); + } else if (metadata.id() > 16) { + metadata.setId(metadata.id() - 1); } } if (type.isOrHasParent(Entity1_14Types.MINECART_ABSTRACT)) { - if (metadata.getId() == 10) { + if (metadata.id() == 10) { // New block format int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } } else if (type.is(Entity1_14Types.HORSE)) { - if (metadata.getId() == 18) { + if (metadata.id() == 18) { metadatas.remove(metadata); int armorType = (int) metadata.getValue(); @@ -137,29 +137,29 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { equipmentPacket.send(Protocol1_14To1_13_2.class); } } else if (type.is(Entity1_14Types.VILLAGER)) { - if (metadata.getId() == 15) { + if (metadata.id() == 15) { // plains metadata.setValue(new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0)); metadata.setMetaType(MetaType1_14.VillagerData); } } else if (type.is(Entity1_14Types.ZOMBIE_VILLAGER)) { - if (metadata.getId() == 18) { + if (metadata.id() == 18) { // plains metadata.setValue(new VillagerData(2, getNewProfessionId((int) metadata.getValue()), 0)); metadata.setMetaType(MetaType1_14.VillagerData); } } else if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) { - if (metadata.getId() >= 9) { // New piercing - metadata.setId(metadata.getId() + 1); + if (metadata.id() >= 9) { // New piercing + metadata.setId(metadata.id() + 1); } } else if (type.is(Entity1_14Types.FIREWORK_ROCKET)) { - if (metadata.getId() == 8) { + if (metadata.id() == 8) { if (metadata.getValue().equals(0)) metadata.setValue(null); // https://bugs.mojang.com/browse/MC-111480 metadata.setMetaType(MetaType1_14.OptVarInt); } } else if (type.isOrHasParent(Entity1_14Types.ABSTRACT_SKELETON)) { - if (metadata.getId() == 14) { + if (metadata.id() == 14) { tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | ((boolean) metadata.getValue() ? 0x4 : 0))); // New attacking metadatas.remove(metadata); // "Is swinging arms" @@ -168,7 +168,7 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { } if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE)) { - if (metadata.getId() == 14) { + if (metadata.id() == 14) { tracker.setInsentientData(entityId, (byte) ((tracker.getInsentientData(entityId) & ~0x4) | (((Number) metadata.getValue()).byteValue() != 0 ? 0x4 : 0))); // New attacking metadatas.remove(metadata); // "Has target (aggressive state)" @@ -178,14 +178,14 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter { // TODO Are witch and ravager also abstract illagers? They all inherit the new metadata 14 added in 19w13a if (type.is(Entity1_14Types.WITCH) || type.is(Entity1_14Types.RAVAGER) || type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE)) { - if (metadata.getId() >= 14) { // TODO 19w13 added a new boolean (raid participant - is celebrating) with id 14 - metadata.setId(metadata.getId() + 1); + if (metadata.id() >= 14) { // TODO 19w13 added a new boolean (raid participant - is celebrating) with id 14 + metadata.setId(metadata.id() + 1); } } } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_14Types.getTypeFromId(type); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java index c430f26ab..ccc665051 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/EntityPackets.java @@ -67,7 +67,7 @@ public class EntityPackets { int typeId = wrapper.get(Type.VAR_INT, 1); Entity1_13Types.EntityType type1_13 = Entity1_13Types.getTypeFromId(typeId, true); - typeId = metadataRewriter.getNewEntityId(type1_13.getId()); + typeId = metadataRewriter.newEntityId(type1_13.getId()); EntityType type1_14 = Entity1_14Types.getTypeFromId(typeId); if (type1_14 != null) { @@ -111,7 +111,7 @@ public class EntityPackets { } // Register Type ID - wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, type1_14); + wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class).addEntity(entityId, type1_14); } wrapper.set(Type.VAR_INT, 1, typeId); @@ -137,7 +137,7 @@ public class EntityPackets { map(Type.SHORT); // 11 - Velocity Z map(Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST); // 12 - Metadata - handler(metadataRewriter.getTrackerAndRewriter(Types1_14.METADATA_LIST)); + handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST)); } }); @@ -164,7 +164,7 @@ public class EntityPackets { map(Type.BYTE); // 6 - Pitch map(Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST); // 7 - Metadata - handler(metadataRewriter.getTrackerAndRewriter(Types1_14.METADATA_LIST, Entity1_14Types.PLAYER)); + handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST, Entity1_14Types.PLAYER)); } }); @@ -177,14 +177,14 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { short animation = wrapper.passthrough(Type.UNSIGNED_BYTE); if (animation == 2) { //Leave bed - EntityTracker1_14 tracker = wrapper.user().get(EntityTracker1_14.class); + EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class); int entityId = wrapper.get(Type.VAR_INT, 0); tracker.setSleeping(entityId, false); PacketWrapper metadataPacket = wrapper.create(0x43); metadataPacket.write(Type.VAR_INT, entityId); List metadataList = new LinkedList<>(); - if (tracker.getClientEntityId() != entityId) { + if (tracker.clientEntityId() != entityId) { metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker))); } metadataList.add(new Metadata(12, MetaType1_14.OptPosition, null)); @@ -203,14 +203,14 @@ public class EntityPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker1_14 tracker = wrapper.user().get(EntityTracker1_14.class); + EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class); int entityId = wrapper.get(Type.VAR_INT, 0); tracker.setSleeping(entityId, true); Position position = wrapper.read(Type.POSITION); List metadataList = new LinkedList<>(); metadataList.add(new Metadata(12, MetaType1_14.OptPosition, position)); - if (tracker.getClientEntityId() != entityId) { + if (tracker.clientEntityId() != entityId) { metadataList.add(new Metadata(6, MetaType1_14.Pose, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker))); } wrapper.write(Types1_14.METADATA_LIST, metadataList); @@ -219,7 +219,7 @@ public class EntityPackets { } }); - metadataRewriter.registerEntityDestroy(ClientboundPackets1_13.DESTROY_ENTITIES); + metadataRewriter.registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java index 5411fcf66..c9f4e5a0a 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java @@ -158,7 +158,8 @@ public class InventoryPackets { wrapper.read(Type.STRING); // Remove channel int windowId = wrapper.read(Type.INT); - wrapper.user().get(EntityTracker1_14.class).setLatestTradeWindowId(windowId); + EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class); + tracker.setLatestTradeWindowId(windowId); wrapper.write(Type.VAR_INT, windowId); int size = wrapper.passthrough(Type.UNSIGNED_BYTE); @@ -233,7 +234,8 @@ public class InventoryPackets { public void handle(PacketWrapper wrapper) throws Exception { // Selecting trade now moves the items, we need to resync the inventory PacketWrapper resyncPacket = wrapper.create(0x08); - resyncPacket.write(Type.UNSIGNED_BYTE, ((short) wrapper.user().get(EntityTracker1_14.class).getLatestTradeWindowId())); // 0 - Window ID + EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class); + resyncPacket.write(Type.UNSIGNED_BYTE, ((short) tracker.getLatestTradeWindowId())); // 0 - Window ID resyncPacket.write(Type.SHORT, ((short) -999)); // 1 - Slot resyncPacket.write(Type.BYTE, (byte) 2); // 2 - Button - End left click resyncPacket.write(Type.SHORT, ((short) ThreadLocalRandom.current().nextInt())); // 3 - Action number diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index b614d137f..9cae4de55 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -240,7 +240,7 @@ public class WorldPackets { lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, section.getLight().getBlockLight()); } - EntityTracker1_14 entityTracker = wrapper.user().get(EntityTracker1_14.class); + EntityTracker1_14 entityTracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class); int diffX = Math.abs(entityTracker.getChunkCenterX() - chunk.getX()); int diffZ = Math.abs(entityTracker.getChunkCenterZ() - chunk.getZ()); if (entityTracker.isForceSendCenterChunk() @@ -315,7 +315,7 @@ public class WorldPackets { Entity1_14Types entType = Entity1_14Types.PLAYER; // Register Type ID - EntityTracker1_14 tracker = wrapper.user().get(EntityTracker1_14.class); + EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class); tracker.addEntity(entityId, entType); tracker.setClientEntityId(entityId); } @@ -364,7 +364,7 @@ public class WorldPackets { ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); int dimensionId = wrapper.get(Type.INT, 0); clientWorld.setEnvironment(dimensionId); - EntityTracker1_14 entityTracker = wrapper.user().get(EntityTracker1_14.class); + EntityTracker1_14 entityTracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class); // The client may reset the center chunk if dimension is changed entityTracker.setForceSendCenterChunk(true); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java index 41cc55592..fb6238283 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/storage/EntityTracker1_14.java @@ -21,14 +21,14 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.data.EntityTracker; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.packets.WorldPackets; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class EntityTracker1_14 extends EntityTracker { +public class EntityTracker1_14 extends EntityTrackerBase { private final Map insentientData = new ConcurrentHashMap<>(); // 0x1 = sleeping, 0x2 = riptide private final Map sleepingAndRiptideData = new ConcurrentHashMap<>(); @@ -91,10 +91,10 @@ public class EntityTracker1_14 extends EntityTracker { } @Override - public void onExternalJoinGame(int playerEntityId) { - super.onExternalJoinGame(playerEntityId); + public void setClientEntityId(int playerEntityId) { + super.setClientEntityId(playerEntityId); - PacketWrapper setViewDistance = PacketWrapper.create(0x41, null, getUser()); + PacketWrapper setViewDistance = PacketWrapper.create(0x41, null, user()); setViewDistance.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE); try { setViewDistance.send(Protocol1_14To1_13_2.class, true, true); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java index 9636ee689..c6894793a 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java @@ -18,9 +18,11 @@ package com.viaversion.viaversion.protocols.protocol1_15to1_14_4; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.data.MappingData; @@ -29,8 +31,7 @@ import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.EntityPa import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.InventoryPackets; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.PlayerPackets; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets.WorldPackets; -import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.storage.EntityTracker1_15; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.RegistryType; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; @@ -47,7 +48,8 @@ public class Protocol1_15To1_14_4 extends AbstractProtocol { public MetadataRewriter1_15To1_14_4(Protocol1_15To1_14_4 protocol) { - super(protocol, EntityTracker1_15.class); + super(protocol); } @Override public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { - if (metadata.getMetaType() == MetaType1_14.Slot) { + if (metadata.metaType() == MetaType1_14.Slot) { InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_14.BlockID) { + } else if (metadata.metaType() == MetaType1_14.BlockID) { // Convert to new block id int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (metadata.getMetaType() == MetaType1_13.PARTICLE) { + } else if (metadata.metaType() == MetaType1_13.PARTICLE) { rewriteParticle((Particle) metadata.getValue()); } if (type == null) return; if (type.isOrHasParent(Entity1_15Types.MINECART_ABSTRACT) - && metadata.getId() == 10) { + && metadata.id() == 10) { // Convert to new block id int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } // Metadata 12 added to abstract_living - if (metadata.getId() > 11 && type.isOrHasParent(Entity1_15Types.LIVINGENTITY)) { - metadata.setId(metadata.getId() + 1); //TODO is it 11 or 12? what is it for? + if (metadata.id() > 11 && type.isOrHasParent(Entity1_15Types.LIVINGENTITY)) { + metadata.setId(metadata.id() + 1); //TODO is it 11 or 12? what is it for? } //NOTES: @@ -70,21 +69,21 @@ public class MetadataRewriter1_15To1_14_4 extends MetadataRewriter { //new boolean with id 17 for enderman if (type.isOrHasParent(Entity1_15Types.WOLF)) { - if (metadata.getId() == 18) { + if (metadata.id() == 18) { metadatas.remove(metadata); - } else if (metadata.getId() > 18) { - metadata.setId(metadata.getId() - 1); + } else if (metadata.id() > 18) { + metadata.setId(metadata.id() - 1); } } } @Override - public int getNewEntityId(final int oldId) { + public int newEntityId(final int oldId) { return EntityPackets.getNewEntityId(oldId); } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_15Types.getTypeFromId(type); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java index c0b53b401..26ef1d8f0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/EntityPackets.java @@ -27,8 +27,7 @@ import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.metadata.MetadataRewriter1_15To1_14_4; -import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.storage.EntityTracker1_15; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import java.util.List; @@ -37,7 +36,7 @@ public class EntityPackets { public static void register(Protocol1_15To1_14_4 protocol) { MetadataRewriter1_15To1_14_4 metadataRewriter = protocol.get(MetadataRewriter1_15To1_14_4.class); - metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_14.SPAWN_ENTITY, Entity1_15Types.FALLING_BLOCK); + metadataRewriter.registerTrackerWithData(ClientboundPackets1_14.SPAWN_ENTITY, Entity1_15Types.FALLING_BLOCK); protocol.registerClientbound(ClientboundPackets1_14.SPAWN_MOB, new PacketRemapper() { @Override @@ -55,7 +54,7 @@ public class EntityPackets { map(Type.SHORT); // 10 - Velocity Y map(Type.SHORT); // 11 - Velocity Z - handler(metadataRewriter.getTracker()); + handler(metadataRewriter.trackerHandler()); handler(wrapper -> sendMetadataPacket(wrapper, wrapper.get(Type.VAR_INT, 0), metadataRewriter)); } }); @@ -73,7 +72,7 @@ public class EntityPackets { handler(wrapper -> { int entityId = wrapper.get(Type.VAR_INT, 0); - wrapper.user().get(EntityTracker1_15.class).addEntity(entityId, Entity1_15Types.PLAYER); + wrapper.user().getEntityTracker(Protocol1_15To1_14_4.class).addEntity(entityId, Entity1_15Types.PLAYER); sendMetadataPacket(wrapper, entityId, metadataRewriter); }); @@ -81,10 +80,10 @@ public class EntityPackets { }); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_14.ENTITY_METADATA, Types1_14.METADATA_LIST); - metadataRewriter.registerEntityDestroy(ClientboundPackets1_14.DESTROY_ENTITIES); + metadataRewriter.registerRemoveEntities(ClientboundPackets1_14.DESTROY_ENTITIES); } - private static void sendMetadataPacket(PacketWrapper wrapper, int entityId, MetadataRewriter rewriter) throws Exception { + private static void sendMetadataPacket(PacketWrapper wrapper, int entityId, EntityRewriter rewriter) throws Exception { // Meta is no longer included in the spawn packets, but sent separately List metadata = wrapper.read(Types1_14.METADATA_LIST); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java index 7b86e6865..263bd6fa8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java @@ -18,12 +18,13 @@ package com.viaversion.viaversion.protocols.protocol1_15to1_14_4.packets; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; -import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.storage.EntityTracker1_15; +import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.Protocol1_15To1_14_4; public class PlayerPackets { @@ -45,7 +46,7 @@ public class PlayerPackets { handler(wrapper -> { // Register Type ID - EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class); + EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_15To1_14_4.class); int entityId = wrapper.get(Type.INT, 0); tracker.addEntity(entityId, Entity1_15Types.PLAYER); }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java index 1eaa87351..5324b7bb0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/Protocol1_16_2To1_16_1.java @@ -18,18 +18,19 @@ package com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.data.MappingData; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.metadata.MetadataRewriter1_16_2To1_16_1; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.EntityPackets; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.InventoryPackets; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.WorldPackets; -import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.storage.EntityTracker1_16_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.RegistryType; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; @@ -46,16 +47,17 @@ public class Protocol1_16_2To1_16_1 extends AbstractProtocol { public MetadataRewriter1_16_2To1_16_1(Protocol1_16_2To1_16_1 protocol) { - super(protocol, EntityTracker1_16_2.class); + super(protocol); mapTypes(Entity1_16Types.values(), Entity1_16_2Types.class); } @Override public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { - if (metadata.getMetaType() == MetaType1_14.Slot) { + if (metadata.metaType() == MetaType1_14.Slot) { InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_14.BlockID) { + } else if (metadata.metaType() == MetaType1_14.BlockID) { int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (metadata.getMetaType() == MetaType1_14.PARTICLE) { + } else if (metadata.metaType() == MetaType1_14.PARTICLE) { rewriteParticle((Particle) metadata.getValue()); } if (type == null) return; if (type.isOrHasParent(Entity1_16_2Types.MINECART_ABSTRACT) - && metadata.getId() == 10) { + && metadata.id() == 10) { // Convert to new block id int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } if (type.isOrHasParent(Entity1_16_2Types.ABSTRACT_PIGLIN)) { - if (metadata.getId() == 15) { + if (metadata.id() == 15) { metadata.setId(16); - } else if (metadata.getId() == 16) { + } else if (metadata.id() == 16) { metadata.setId(15); } } } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_16_2Types.getTypeFromId(type); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java index a38b8ecca..a03e09340 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/packets/EntityPackets.java @@ -25,18 +25,17 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_14; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.metadata.MetadataRewriter1_16_2To1_16_1; -import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.storage.EntityTracker1_16_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; public class EntityPackets { public static void register(Protocol1_16_2To1_16_1 protocol) { MetadataRewriter1_16_2To1_16_1 metadataRewriter = protocol.get(MetadataRewriter1_16_2To1_16_1.class); - metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_16.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK); + metadataRewriter.registerTrackerWithData(ClientboundPackets1_16.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK); metadataRewriter.registerTracker(ClientboundPackets1_16.SPAWN_MOB); metadataRewriter.registerTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16_2Types.PLAYER); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_16.ENTITY_METADATA, Types1_14.METADATA_LIST); - metadataRewriter.registerEntityDestroy(ClientboundPackets1_16.DESTROY_ENTITIES); + metadataRewriter.registerRemoveEntities(ClientboundPackets1_16.DESTROY_ENTITIES); protocol.registerClientbound(ClientboundPackets1_16.JOIN_GAME, new PacketRemapper() { @Override @@ -65,7 +64,7 @@ public class EntityPackets { map(Type.UNSIGNED_BYTE, Type.VAR_INT); // Max players // ... handler(wrapper -> { - wrapper.user().get(EntityTracker1_16_2.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16_2Types.PLAYER); + wrapper.user().getEntityTracker(Protocol1_16_2To1_16_1.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16_2Types.PLAYER); }); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/storage/EntityTracker1_16_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/storage/EntityTracker1_16_2.java deleted file mode 100644 index d063f1779..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/storage/EntityTracker1_16_2.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.storage; - -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types; -import com.viaversion.viaversion.data.EntityTracker; - -public class EntityTracker1_16_2 extends EntityTracker { - - public EntityTracker1_16_2(UserConnection user) { - super(user, Entity1_16_2Types.PLAYER); - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index 66c1819fe..4b7f1ec24 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -23,10 +23,12 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; import com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.MappingData; @@ -35,10 +37,9 @@ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.Metadat import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.WorldPackets; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.EntityTracker1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import com.viaversion.viaversion.rewriter.ComponentRewriter; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.RegistryType; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; @@ -62,16 +63,17 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol { public MetadataRewriter1_16To1_15_2(Protocol1_16To1_15_2 protocol) { - super(protocol, EntityTracker1_16.class); - mapType(Entity1_15Types.ZOMBIE_PIGMAN, Entity1_16Types.ZOMBIFIED_PIGLIN); + super(protocol); + mapEntityType(Entity1_15Types.ZOMBIE_PIGMAN, Entity1_16Types.ZOMBIFIED_PIGLIN); mapTypes(Entity1_15Types.values(), Entity1_16Types.class); } @Override public void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { - if (metadata.getMetaType() == MetaType1_14.Slot) { + if (metadata.metaType() == MetaType1_14.Slot) { InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_14.BlockID) { + } else if (metadata.metaType() == MetaType1_14.BlockID) { int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (metadata.getMetaType() == MetaType1_14.PARTICLE) { + } else if (metadata.metaType() == MetaType1_14.PARTICLE) { rewriteParticle((Particle) metadata.getValue()); } if (type == null) return; if (type.isOrHasParent(Entity1_16Types.MINECART_ABSTRACT) - && metadata.getId() == 10) { + && metadata.id() == 10) { // Convert to new block id int data = (int) metadata.getValue(); metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); } if (type.isOrHasParent(Entity1_16Types.ABSTRACT_ARROW)) { - if (metadata.getId() == 8) { + if (metadata.id() == 8) { metadatas.remove(metadata); - } else if (metadata.getId() > 8) { - metadata.setId(metadata.getId() - 1); + } else if (metadata.id() > 8) { + metadata.setId(metadata.id() - 1); } } } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_16Types.getTypeFromId(type); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java index be6f3fb01..c48f398f1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/packets/EntityPackets.java @@ -35,7 +35,6 @@ import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2; -import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.EntityTracker1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16; import java.util.UUID; @@ -152,7 +151,7 @@ public class EntityPackets { public void registerMap() { handler(wrapper -> { int entityId = wrapper.passthrough(Type.VAR_INT); - wrapper.user().get(EntityTracker1_16.class).addEntity(entityId, Entity1_16Types.LIGHTNING_BOLT); + wrapper.user().getEntityTracker(Protocol1_16To1_15_2.class).addEntity(entityId, Entity1_16Types.LIGHTNING_BOLT); wrapper.write(Type.UUID, UUID.randomUUID()); // uuid wrapper.write(Type.VAR_INT, Entity1_16Types.LIGHTNING_BOLT.getId()); // entity type @@ -172,11 +171,11 @@ public class EntityPackets { } }); - metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_16Types.FALLING_BLOCK); + metadataRewriter.registerTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_16Types.FALLING_BLOCK); metadataRewriter.registerTracker(ClientboundPackets1_15.SPAWN_MOB); metadataRewriter.registerTracker(ClientboundPackets1_15.SPAWN_PLAYER, Entity1_16Types.PLAYER); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_15.ENTITY_METADATA, Types1_14.METADATA_LIST); - metadataRewriter.registerEntityDestroy(ClientboundPackets1_15.DESTROY_ENTITIES); + metadataRewriter.registerRemoveEntities(ClientboundPackets1_15.DESTROY_ENTITIES); protocol.registerClientbound(ClientboundPackets1_15.RESPAWN, new PacketRemapper() { @Override @@ -209,7 +208,7 @@ public class EntityPackets { map(Type.LONG); // Seed map(Type.UNSIGNED_BYTE); // Max players handler(wrapper -> { - wrapper.user().get(EntityTracker1_16.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER); + wrapper.user().getEntityTracker(Protocol1_16To1_15_2.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER); final String type = wrapper.read(Type.STRING);// level type wrapper.passthrough(Type.VAR_INT); // View distance diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java index 31875cfc5..8413a0d16 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java @@ -21,18 +21,19 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.data.MappingDataBase; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.metadata.MetadataRewriter1_17To1_16_4; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.EntityPackets; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.InventoryPackets; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.WorldPackets; -import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.EntityTracker1_17; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.rewriter.RegistryType; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; @@ -50,7 +51,8 @@ public class Protocol1_17To1_16_4 extends AbstractProtocol metadatas, UserConnection connection) throws Exception { - metadata.setMetaType(MetaType1_17.byId(metadata.getMetaType().getTypeID())); - if (metadata.getMetaType() == MetaType1_17.Slot) { - InventoryPackets.toClient((Item) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_17.BlockID) { - int data = (int) metadata.getValue(); - metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (metadata.getMetaType() == MetaType1_17.PARTICLE) { - rewriteParticle((Particle) metadata.getValue()); - } else if (metadata.getMetaType() == MetaType1_17.Pose) { - int pose = metadata.getCastedValue(); - if (pose > 5) { - // Added LONG_JUMP at 6 - metadata.setValue(pose + 1); + protected void registerRewrites() { + filter().handler((event, meta) -> { + meta.setMetaType(MetaType1_17.byId(meta.metaType().typeId())); + + if (meta.metaType() == MetaType1_17.Pose) { + int pose = meta.value(); + if (pose > 5) { + // Added LONG_JUMP at 6 + meta.setValue(pose + 1); + } } - } + }); + registerDumMetaTypeHandler(MetaType1_17.Slot, MetaType1_17.BlockID, MetaType1_17.PARTICLE, InventoryPackets::toClient); - if (type == null) return; + // Ticks frozen added with id 7 + filter().filterFamily(Entity1_17Types.ENTITY).addIndex(7); - if (type.isOrHasParent(Entity1_17Types.ENTITY)) { - if (metadata.getId() >= 7) { - metadata.setId(metadata.getId() + 1); // Ticks frozen added with id 7 - } - } - - if (type.isOrHasParent(Entity1_17Types.MINECART_ABSTRACT) - && metadata.getId() == 11) { + filter().filterFamily(Entity1_17Types.MINECART_ABSTRACT).index(11).handler((event, meta) -> { // Convert to new block id - int data = (int) metadata.getValue(); - metadata.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } + int data = (int) meta.getValue(); + meta.setValue(protocol.getMappingData().getNewBlockStateId(data)); + }); - if (type == Entity1_17Types.SHULKER) { - // Attachment position removed - if (metadata.getId() == 16) { - metadatas.remove(metadata); - } else if (metadata.getId() > 16) { - metadata.setId(metadata.getId() - 1); - } - } + // Attachment position removed + filter().type(Entity1_17Types.SHULKER).removeIndex(16); } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_17Types.getTypeFromId(type); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/EntityPackets.java index 4aeb00712..e3f920b73 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/EntityPackets.java @@ -17,6 +17,7 @@ */ package com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets; +import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -24,18 +25,16 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_14; import com.viaversion.viaversion.api.type.types.version.Types1_17; -import com.viaversion.viaversion.data.EntityTracker; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.metadata.MetadataRewriter1_17To1_16_4; -import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.EntityTracker1_17; public class EntityPackets { public static void register(Protocol1_17To1_16_4 protocol) { MetadataRewriter1_17To1_16_4 metadataRewriter = protocol.get(MetadataRewriter1_17To1_16_4.class); - metadataRewriter.registerSpawnTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_17Types.FALLING_BLOCK); + metadataRewriter.registerTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_17Types.FALLING_BLOCK); metadataRewriter.registerTracker(ClientboundPackets1_16_2.SPAWN_MOB); metadataRewriter.registerTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, Entity1_17Types.PLAYER); metadataRewriter.registerMetadataRewriter(ClientboundPackets1_16_2.ENTITY_METADATA, Types1_14.METADATA_LIST, Types1_17.METADATA_LIST); @@ -47,7 +46,7 @@ public class EntityPackets { int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); wrapper.cancel(); - EntityTracker entityTracker = wrapper.user().get(EntityTracker1_17.class); + EntityTracker entityTracker = wrapper.user().getEntityTracker(Protocol1_17To1_16_4.class); for (int entityId : entityIds) { entityTracker.removeEntity(entityId); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/WorldPackets.java index 8f342c2d6..19376b8d8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/packets/WorldPackets.java @@ -35,7 +35,6 @@ import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPac import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4; -import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.EntityTracker1_17; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type; import com.viaversion.viaversion.rewriter.BlockRewriter; @@ -188,7 +187,7 @@ public class WorldPackets { addNewDimensionData(currentDimensionTag); UserConnection user = wrapper.user(); - user.get(EntityTracker1_17.class).addEntity(wrapper.get(Type.INT, 0), Entity1_17Types.PLAYER); + user.getEntityTracker(Protocol1_17To1_16_4.class).addEntity(wrapper.get(Type.INT, 0), Entity1_17Types.PLAYER); }); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/storage/EntityTracker1_17.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/storage/EntityTracker1_17.java deleted file mode 100644 index f80336302..000000000 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/storage/EntityTracker1_17.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage; - -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; -import com.viaversion.viaversion.data.EntityTracker; - -public class EntityTracker1_17 extends EntityTracker { - - public EntityTracker1_17(UserConnection user) { - super(user, Entity1_17Types.PLAYER); - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java index 1c270c5d9..da97d56cd 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/Protocol1_9To1_8.java @@ -48,7 +48,7 @@ import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.CommandBlock import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.InventoryTracker; import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.MovementTracker; -import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.EntityRewriter; import com.viaversion.viaversion.util.GsonUtil; public class Protocol1_9To1_8 extends AbstractProtocol { @@ -108,7 +108,8 @@ public class Protocol1_9To1_8 extends AbstractProtocol { public MetadataRewriter1_9To1_8(Protocol1_9To1_8 protocol) { - super(protocol, EntityTracker1_9.class); + super(protocol); } @Override protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { - MetaIndex metaIndex = MetaIndex.searchIndex(type, metadata.getId()); + MetaIndex metaIndex = MetaIndex.searchIndex(type, metadata.id()); if (metaIndex == null) { throw new Exception("Could not find valid metadata"); } @@ -139,12 +138,12 @@ public class MetadataRewriter1_9To1_8 extends MetadataRewriter { } @Override - protected EntityType getTypeFromId(int type) { + protected EntityType typeFromId(int type) { return Entity1_10Types.getTypeFromId(type, false); } @Override - protected EntityType getObjectTypeFromId(int type) { + protected EntityType objectTypeFromId(int type) { return Entity1_10Types.getTypeFromId(type, true); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java index c42b35cf4..769d31037 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java @@ -64,7 +64,7 @@ public class EntityPackets { map(Type.BOOLEAN, new ValueTransformer(Type.NOTHING) { @Override public Void transform(PacketWrapper wrapper, Boolean inputValue) throws Exception { - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (!inputValue) { int passenger = wrapper.get(Type.INT, 0); int vehicle = wrapper.get(Type.INT, 1); @@ -108,7 +108,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); if (Via.getConfig().isHologramPatch()) { - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (tracker.getKnownHolograms().contains(entityID)) { Double newValue = wrapper.get(Type.DOUBLE, 1); newValue += (Via.getConfig().getHologramYOffset()); @@ -158,7 +158,7 @@ public class EntityPackets { @Override public Integer transform(PacketWrapper wrapper, Short slot) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - int receiverId = wrapper.user().get(EntityTracker1_9.class).getClientEntityId(); + int receiverId = wrapper.user().getEntityTracker(Protocol1_9To1_8.class).clientEntityId(); // Normally, 0 = hand and 1-4 = armor // ... but if the sent id is equal to the receiver's id, 0-3 will instead mark the armor slots // (In 1.9+, every client treats the received the same: 0=hand, 1=offhand, 2-5=armor) @@ -181,7 +181,7 @@ public class EntityPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); int entityID = wrapper.get(Type.VAR_INT, 0); Item stack = wrapper.get(Type.ITEM, 0); @@ -207,7 +207,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityId = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (tracker.hasEntity(entityId)) { protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user()); } else { @@ -224,7 +224,7 @@ public class EntityPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.handleMetadata(entityID, metadataList); } }); @@ -288,7 +288,9 @@ public class EntityPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { if (!Via.getConfig().isMinimizeCooldown()) return; - if (wrapper.get(Type.VAR_INT, 0) != wrapper.user().get(EntityTracker1_9.class).getProvidedEntityId()) { + + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); + if (wrapper.get(Type.VAR_INT, 0) != tracker.getProvidedEntityId()) { return; } int propertiesToRead = wrapper.read(Type.INT); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/InventoryPackets.java index 480e262a7..8db8a4397 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/InventoryPackets.java @@ -121,7 +121,7 @@ public class InventoryPackets { // Check if it is the inventory of the player if (showShieldWhenSwordInHand) { InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); - EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); short slotID = wrapper.get(Type.SHORT, 0); short windowId = wrapper.get(Type.BYTE, 0); @@ -168,7 +168,7 @@ public class InventoryPackets { Short windowId = wrapper.get(Type.UNSIGNED_BYTE, 0); InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); - EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking(); @@ -266,7 +266,7 @@ public class InventoryPackets { if (showShieldWhenSwordInHand) { InventoryTracker inventoryTracker = wrapper.user().get(InventoryTracker.class); - EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); short slotID = wrapper.get(Type.SHORT, 0); // Update item in slot @@ -401,7 +401,7 @@ public class InventoryPackets { boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand() && Via.getConfig().isShieldBlocking(); - EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (entityTracker.isBlocking()) { entityTracker.setBlocking(false); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/PlayerPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/PlayerPackets.java index 3f193081c..a5cc4caaa 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/PlayerPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/PlayerPackets.java @@ -19,6 +19,7 @@ package com.viaversion.viaversion.protocols.protocol1_9to1_8.packets; import com.google.gson.JsonObject; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -139,7 +140,7 @@ public class PlayerPackets { if (mode == 0 || mode == 3 || mode == 4) { String[] players = wrapper.passthrough(Type.STRING_ARRAY); // Players - final EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + final EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); String myName = wrapper.user().getProtocolInfo().getUsername(); String teamName = wrapper.get(Type.STRING, 0); for (String player : players) { @@ -161,7 +162,7 @@ public class PlayerPackets { } if (mode == 1) { // Remove team - final EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + final EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); String teamName = wrapper.get(Type.STRING, 0); if (entityTracker.isAutoTeam() && teamName.equals(entityTracker.getCurrentTeam())) { @@ -187,7 +188,7 @@ public class PlayerPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { int entityId = wrapper.get(Type.INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.addEntity(entityId, Entity1_10Types.EntityType.PLAYER); tracker.setClientEntityId(entityId); } @@ -202,7 +203,7 @@ public class PlayerPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.setGameMode(GameMode.getById(wrapper.get(Type.UNSIGNED_BYTE, 0))); //Set player gamemode } }); @@ -221,7 +222,7 @@ public class PlayerPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (Via.getConfig().isAutoTeam()) { entityTracker.setAutoTeam(true); // Workaround for packet order issue @@ -367,7 +368,8 @@ public class PlayerPackets { cc.getLoadedChunks().clear(); int gamemode = wrapper.get(Type.UNSIGNED_BYTE, 0); - wrapper.user().get(EntityTracker1_9.class).setGameMode(GameMode.getById(gamemode)); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); + tracker.setGameMode(GameMode.getById(gamemode)); } }); @@ -394,7 +396,8 @@ public class PlayerPackets { public void handle(PacketWrapper wrapper) throws Exception { if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 3) { //Change gamemode int gamemode = wrapper.get(Type.FLOAT, 0).intValue(); - wrapper.user().get(EntityTracker1_9.class).setGameMode(GameMode.getById(gamemode)); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); + tracker.setGameMode(GameMode.getById(gamemode)); } } }); @@ -496,7 +499,7 @@ public class PlayerPackets { int action = wrapper.get(Type.VAR_INT, 0); if (action == 2) { // cancel any blocking >.> - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (tracker.isBlocking()) { if (!Via.getConfig().isShowShieldWhenSwordInHand()) { tracker.setSecondHand(null); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/SpawnPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/SpawnPackets.java index 673c803b3..56a89895b 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/SpawnPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/SpawnPackets.java @@ -57,7 +57,7 @@ public class SpawnPackets { @Override public void write(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); @@ -69,7 +69,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); int typeID = wrapper.get(Type.BYTE, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, true)); tracker.sendMetadataBuffer(entityID); } @@ -143,7 +143,7 @@ public class SpawnPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.addEntity(entityID, Entity1_10Types.EntityType.EXPERIENCE_ORB); tracker.sendMetadataBuffer(entityID); } @@ -168,7 +168,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { // Currently only lightning uses this int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.addEntity(entityID, Entity1_10Types.EntityType.LIGHTNING); tracker.sendMetadataBuffer(entityID); } @@ -189,7 +189,7 @@ public class SpawnPackets { @Override public void write(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); @@ -201,7 +201,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); int typeID = wrapper.get(Type.UNSIGNED_BYTE, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.addEntity(entityID, Entity1_10Types.getTypeFromId(typeID, false)); tracker.sendMetadataBuffer(entityID); } @@ -225,7 +225,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityId = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (tracker.hasEntity(entityId)) { protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user()); } else { @@ -240,7 +240,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.handleMetadata(entityID, metadataList); } }); @@ -257,7 +257,7 @@ public class SpawnPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.addEntity(entityID, Entity1_10Types.EntityType.PAINTING); tracker.sendMetadataBuffer(entityID); } @@ -266,7 +266,7 @@ public class SpawnPackets { @Override public void write(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); wrapper.write(Type.UUID, tracker.getEntityUUID(entityID)); // 1 - UUID } }); @@ -288,7 +288,7 @@ public class SpawnPackets { @Override public void handle(PacketWrapper wrapper) throws Exception { int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.addEntity(entityID, Entity1_10Types.EntityType.PLAYER); tracker.sendMetadataBuffer(entityID); } @@ -326,7 +326,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityId = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (tracker.hasEntity(entityId)) { protocol.get(MetadataRewriter1_9To1_8.class).handleMetadata(entityId, metadataList, wrapper.user()); } else { @@ -342,7 +342,7 @@ public class SpawnPackets { public void handle(PacketWrapper wrapper) throws Exception { List metadataList = wrapper.get(Types1_9.METADATA_LIST, 0); int entityID = wrapper.get(Type.VAR_INT, 0); - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.handleMetadata(entityID, metadataList); } }); @@ -361,7 +361,7 @@ public class SpawnPackets { int[] entities = wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0); for (int entity : entities) { // EntityTracker - wrapper.user().get(EntityTracker1_9.class).removeEntity(entity); + wrapper.user().getEntityTracker(Protocol1_9To1_8.class).removeEntity(entity); } } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java index 4e3670dca..fca13b6ca 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -113,7 +113,7 @@ public class WorldPackets { wrapper.set(Type.STRING, 0, newname); wrapper.write(Type.VAR_INT, catid); // Write Category ID if (effect != null && effect.isBreaksound()) { - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); int x = wrapper.passthrough(Type.INT); //Position X int y = wrapper.passthrough(Type.INT); //Position Y int z = wrapper.passthrough(Type.INT); //Position Z @@ -278,7 +278,7 @@ public class WorldPackets { public void handle(PacketWrapper wrapper) throws Exception { int status = wrapper.get(Type.UNSIGNED_BYTE, 0); if (status == 5 || status == 4 || status == 3) { - EntityTracker1_9 entityTracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); if (entityTracker.isBlocking()) { entityTracker.setBlocking(false); if (!Via.getConfig().isShowShieldWhenSwordInHand()) { @@ -308,7 +308,7 @@ public class WorldPackets { Item item = Protocol1_9To1_8.getHandItem(wrapper.user()); // Blocking patch if (Via.getConfig().isShieldBlocking()) { - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); // Check if the shield is already there or if we have to give it here boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand(); @@ -412,7 +412,7 @@ public class WorldPackets { x++; break; } - EntityTracker1_9 tracker = wrapper.user().get(EntityTracker1_9.class); + EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class); tracker.addBlockInteraction(new Position(x, y, z)); } }); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/providers/CommandBlockProvider.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/providers/CommandBlockProvider.java index 07ce7a731..f4209bf19 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/providers/CommandBlockProvider.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/providers/CommandBlockProvider.java @@ -59,7 +59,8 @@ public class CommandBlockProvider implements Provider { return; PacketWrapper wrapper = PacketWrapper.create(0x1B, null, user); // Entity status - wrapper.write(Type.INT, user.get(EntityTracker1_9.class).getProvidedEntityId()); // Entity ID + EntityTracker1_9 tracker = user.getEntityTracker(Protocol1_9To1_8.class); + wrapper.write(Type.INT, tracker.getProvidedEntityId()); // Entity ID wrapper.write(Type.BYTE, (byte) 26); // Hardcoded op permission level wrapper.send(Protocol1_9To1_8.class); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java index 2d48afb62..11a526b65 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/providers/EntityIdProvider.java @@ -19,11 +19,11 @@ package com.viaversion.viaversion.protocols.protocol1_9to1_8.providers; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.platform.providers.Provider; -import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; +import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; public class EntityIdProvider implements Provider { public int getEntityId(UserConnection user) throws Exception { - return user.get(EntityTracker1_9.class).getClientEntityId(); + return user.getEntityTracker(Protocol1_9To1_8.class).clientEntityId(); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java index 7382772db..d8d33347b 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/EntityTracker1_9.java @@ -20,10 +20,10 @@ package com.viaversion.viaversion.protocols.protocol1_9to1_8.storage; import com.google.common.cache.CacheBuilder; import com.google.common.collect.Sets; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.legacy.bossbar.BossBar; import com.viaversion.viaversion.api.legacy.bossbar.BossColor; import com.viaversion.viaversion.api.legacy.bossbar.BossStyle; -import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types.EntityType; import com.viaversion.viaversion.api.minecraft.item.Item; @@ -32,7 +32,7 @@ import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_9; -import com.viaversion.viaversion.data.EntityTracker; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import com.viaversion.viaversion.protocols.protocol1_9to1_8.chat.GameMode; import com.viaversion.viaversion.protocols.protocol1_9to1_8.metadata.MetadataRewriter1_9To1_8; @@ -48,7 +48,7 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; -public class EntityTracker1_9 extends EntityTracker { +public class EntityTracker1_9 extends EntityTrackerBase { private final Map uuidMap = new ConcurrentHashMap<>(); private final Map> metadataBuffer = new ConcurrentHashMap<>(); private final Map vehicleMap = new ConcurrentHashMap<>(); @@ -84,11 +84,11 @@ public class EntityTracker1_9 extends EntityTracker { } public void setSecondHand(Item item) { - setSecondHand(getClientEntityId(), item); + setSecondHand(clientEntityId(), item); } public void setSecondHand(int entityID, Item item) { - PacketWrapper wrapper = PacketWrapper.create(0x3C, null, getUser()); + PacketWrapper wrapper = PacketWrapper.create(0x3C, null, user()); wrapper.write(Type.VAR_INT, entityID); wrapper.write(Type.VAR_INT, 1); // slot wrapper.write(Type.ITEM, this.itemInSecondHand = item); @@ -124,7 +124,7 @@ public class EntityTracker1_9 extends EntityTracker { * @return player has a sword in the main hand */ public boolean hasSwordInHand() { - InventoryTracker inventoryTracker = getUser().get(InventoryTracker.class); + InventoryTracker inventoryTracker = user().get(InventoryTracker.class); // Get item in new selected slot int inventorySlot = this.heldItemSlot + 36; // Hotbar slot index to inventory slot @@ -147,7 +147,7 @@ public class EntityTracker1_9 extends EntityTracker { if (bar != null) { bar.hide(); // Send to provider - Via.getManager().getProviders().get(BossBarProvider.class).handleRemove(getUser(), bar.getId()); + Via.getManager().getProviders().get(BossBarProvider.class).handleRemove(user(), bar.getId()); } } @@ -160,7 +160,7 @@ public class EntityTracker1_9 extends EntityTracker { } public void handleMetadata(int entityId, List metadataList) { - com.viaversion.viaversion.api.minecraft.entities.EntityType type = getEntity(entityId); + com.viaversion.viaversion.api.minecraft.entities.EntityType type = entityType(entityId); if (type == null) { return; } @@ -168,14 +168,14 @@ public class EntityTracker1_9 extends EntityTracker { for (Metadata metadata : new ArrayList<>(metadataList)) { // Fix: wither (crash fix) if (type == EntityType.WITHER) { - if (metadata.getId() == 10) { + if (metadata.id() == 10) { metadataList.remove(metadata); //metadataList.add(new Metadata(10, NewType.Byte.getTypeID(), Type.BYTE, 0)); } } // Fix: enderdragon (crash fix) if (type == EntityType.ENDER_DRAGON) { - if (metadata.getId() == 11) { + if (metadata.id() == 11) { metadataList.remove(metadata); // metadataList.add(new Metadata(11, NewType.Byte.getTypeID(), Type.VAR_INT, 0)); } @@ -190,12 +190,12 @@ public class EntityTracker1_9 extends EntityTracker { //ECHOPET Patch if (type == EntityType.HORSE) { // Wrong metadata value from EchoPet, patch since it's discontinued. (https://github.com/DSH105/EchoPet/blob/06947a8b08ce40be9a518c2982af494b3b99d140/modules/API/src/main/java/com/dsh105/echopet/compat/api/entity/HorseArmour.java#L22) - if (metadata.getId() == 16 && (int) metadata.getValue() == Integer.MIN_VALUE) + if (metadata.id() == 16 && (int) metadata.getValue() == Integer.MIN_VALUE) metadata.setValue(0); } if (type == EntityType.PLAYER) { - if (metadata.getId() == 0) { + if (metadata.id() == 0) { // Byte byte data = (byte) metadata.getValue(); if (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) { @@ -211,7 +211,7 @@ public class EntityTracker1_9 extends EntityTracker { } } } - if (metadata.getId() == 12 && Via.getConfig().isLeftHandedHandling()) { // Player model + if (metadata.id() == 12 && Via.getConfig().isLeftHandedHandling()) { // Player model metadataList.add(new Metadata( 13, // Main hand MetaType1_9.Byte, @@ -220,7 +220,7 @@ public class EntityTracker1_9 extends EntityTracker { } } if (type == EntityType.ARMOR_STAND && Via.getConfig().isHologramPatch()) { - if (metadata.getId() == 0 && getMetaByIndex(metadataList, 10) != null) { + if (metadata.id() == 0 && getMetaByIndex(metadataList, 10) != null) { Metadata meta = getMetaByIndex(metadataList, 10); //Only happens if the armorstand is small byte data = (byte) metadata.getValue(); // Check invisible | Check small | Check if custom name is empty | Check if custom name visible is true @@ -233,7 +233,7 @@ public class EntityTracker1_9 extends EntityTracker { knownHolograms.add(entityId); try { // Send movement - PacketWrapper wrapper = PacketWrapper.create(0x25, null, getUser()); + PacketWrapper wrapper = PacketWrapper.create(0x25, null, user()); wrapper.write(Type.VAR_INT, entityId); wrapper.write(Type.SHORT, (short) 0); wrapper.write(Type.SHORT, (short) (128D * (Via.getConfig().getHologramYOffset() * 32D))); @@ -249,22 +249,22 @@ public class EntityTracker1_9 extends EntityTracker { // Boss bar if (Via.getConfig().isBossbarPatch()) { if (type == EntityType.ENDER_DRAGON || type == EntityType.WITHER) { - if (metadata.getId() == 2) { + if (metadata.id() == 2) { BossBar bar = bossBarMap.get(entityId); String title = (String) metadata.getValue(); title = title.isEmpty() ? (type == EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither") : title; if (bar == null) { bar = Via.getAPI().legacyAPI().createLegacyBossBar(title, BossColor.PINK, BossStyle.SOLID); bossBarMap.put(entityId, bar); - bar.addConnection(getUser()); + bar.addConnection(user()); bar.show(); // Send to provider - Via.getManager().getProviders().get(BossBarProvider.class).handleAdd(getUser(), bar.getId()); + Via.getManager().getProviders().get(BossBarProvider.class).handleAdd(user(), bar.getId()); } else { bar.setTitle(title); } - } else if (metadata.getId() == 6 && !Via.getConfig().isBossbarAntiflicker()) { // If anti flicker is enabled, don't update health + } else if (metadata.id() == 6 && !Via.getConfig().isBossbarAntiflicker()) { // If anti flicker is enabled, don't update health BossBar bar = bossBarMap.get(entityId); // Make health range between 0 and 1 float maxHealth = type == EntityType.ENDER_DRAGON ? 200.0f : 300.0f; @@ -273,10 +273,10 @@ public class EntityTracker1_9 extends EntityTracker { String title = type == EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither"; bar = Via.getAPI().legacyAPI().createLegacyBossBar(title, health, BossColor.PINK, BossStyle.SOLID); bossBarMap.put(entityId, bar); - bar.addConnection(getUser()); + bar.addConnection(user()); bar.show(); // Send to provider - Via.getManager().getProviders().get(BossBarProvider.class).handleAdd(getUser(), bar.getId()); + Via.getManager().getProviders().get(BossBarProvider.class).handleAdd(user(), bar.getId()); } else { bar.setHealth(health); } @@ -288,14 +288,14 @@ public class EntityTracker1_9 extends EntityTracker { public Metadata getMetaByIndex(List list, int index) { for (Metadata meta : list) - if (index == meta.getId()) { + if (index == meta.id()) { return meta; } return null; } public void sendTeamPacket(boolean add, boolean now) { - PacketWrapper wrapper = PacketWrapper.create(0x41, null, getUser()); + PacketWrapper wrapper = PacketWrapper.create(0x41, null, user()); wrapper.write(Type.STRING, "viaversion"); // Use viaversion as name if (add) { // add @@ -311,7 +311,7 @@ public class EntityTracker1_9 extends EntityTracker { } else { wrapper.write(Type.BYTE, (byte) 3); } - wrapper.write(Type.STRING_ARRAY, new String[]{getUser().getProtocolInfo().getUsername()}); + wrapper.write(Type.STRING_ARRAY, new String[]{user().getProtocolInfo().getUsername()}); } else { wrapper.write(Type.BYTE, (byte) 1); // remove team } @@ -335,11 +335,11 @@ public class EntityTracker1_9 extends EntityTracker { public void sendMetadataBuffer(int entityId) { List metadataList = metadataBuffer.get(entityId); if (metadataList != null) { - PacketWrapper wrapper = PacketWrapper.create(0x39, null, getUser()); + PacketWrapper wrapper = PacketWrapper.create(0x39, null, user()); wrapper.write(Type.VAR_INT, entityId); wrapper.write(Types1_9.METADATA_LIST, metadataList); Via.getManager().getProtocolManager().getProtocol(Protocol1_9To1_8.class).get(MetadataRewriter1_9To1_8.class) - .handleMetadata(entityId, metadataList, getUser()); + .handleMetadata(entityId, metadataList, user()); handleMetadata(entityId, metadataList); if (!metadataList.isEmpty()) { try { @@ -354,9 +354,9 @@ public class EntityTracker1_9 extends EntityTracker { public int getProvidedEntityId() { try { - return Via.getManager().getProviders().get(EntityIdProvider.class).getEntityId(getUser()); + return Via.getManager().getProviders().get(EntityIdProvider.class).getEntityId(user()); } catch (Exception e) { - return getClientEntityId(); + return clientEntityId(); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/InventoryTracker.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/InventoryTracker.java index f98fe6db5..68e54c7bf 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/InventoryTracker.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/storage/InventoryTracker.java @@ -19,6 +19,7 @@ package com.viaversion.viaversion.protocols.protocol1_9to1_8.storage; import com.viaversion.viaversion.api.connection.StoredObject; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import java.util.HashMap; import java.util.Map; @@ -83,7 +84,7 @@ public class InventoryTracker extends StoredObject { * @param button The button to use in the click */ public void handleWindowClick(short windowId, byte mode, short hoverSlot, byte button) { - EntityTracker1_9 entityTracker = getUser().get(EntityTracker1_9.class); + EntityTracker1_9 entityTracker = getUser().getEntityTracker(Protocol1_9To1_8.class); // Skip inventory background clicks if (hoverSlot == -1) { diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java new file mode 100644 index 000000000..74ef78e74 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java @@ -0,0 +1,521 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.rewriter; + +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.IntTag; +import com.github.steveice10.opennbt.tag.builtin.Tag; +import com.google.common.base.Preconditions; +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.ParticleMappings; +import com.viaversion.viaversion.api.data.entity.EntityTracker; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import com.viaversion.viaversion.api.minecraft.metadata.MetaType; +import com.viaversion.viaversion.api.minecraft.metadata.Metadata; +import com.viaversion.viaversion.api.protocol.Protocol; +import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; +import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.Particle; +import com.viaversion.viaversion.rewriter.meta.MetaFilter; +import com.viaversion.viaversion.rewriter.meta.MetaHandlerEvent; +import com.viaversion.viaversion.rewriter.meta.MetaHandlerEventImpl; +import it.unimi.dsi.fastutil.ints.Int2IntMap; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +public abstract class EntityRewriter extends RewriterBase { + private static final Metadata[] EMPTY_ARRAY = new Metadata[0]; + protected final List metadataFilters = new ArrayList<>(); + protected final boolean trackMappedType; + protected Int2IntMap typeMappings; + + protected EntityRewriter(T protocol) { + this(protocol, true); + } + + /** + * Creates a new entity rewriter instance. + * + * @param protocol protocol + * @param trackMappedType whether the mapped or unmapped entity type should be tracked + */ + protected EntityRewriter(T protocol, boolean trackMappedType) { + super(protocol); + this.trackMappedType = trackMappedType; + protocol.put(this); + } + + /** + * Returns a meta filter builder. + *

+ * Calling {@link MetaFilter.Builder#register()} will automatically register the filter on this rewriter. + * Don't forget to call {@link MetaFilter.Builder#register()}. + * + * @return meta filter builder + */ + public MetaFilter.Builder filter() { + return new MetaFilter.Builder(this); + } + + /** + * Registers a metadata filter. + * Note that {@link MetaFilter.Builder#register()} already calls this method. + * + * @param filter filter to register + * @throws IllegalArgumentException if the filter is already registered + */ + public void registerFilter(MetaFilter filter) { + Preconditions.checkArgument(!metadataFilters.contains(filter)); + metadataFilters.add(filter); + } + + /** + * Handles and transforms metadata of an entity. + * + * @param entityId entity id + * @param metadataList full, mutable list of metadata + * @param connection user connection + */ + public void handleMetadata(int entityId, List metadataList, UserConnection connection) { + EntityType type = tracker(connection).entityType(entityId); + int i = 0; // Count index for fast removal + for (Metadata metadata : metadataList.toArray(EMPTY_ARRAY)) { // Copy the list to allow mutation + // Call handlers implementing the old handleMetadata + if (!callOldMetaHandler(entityId, type, metadata, metadataList, connection)) { + metadataList.remove(i--); + continue; + } + + for (MetaFilter filter : metadataFilters) { + if (filter.isFiltered(type, metadata)) { + MetaHandlerEvent event = new MetaHandlerEventImpl(connection, type, entityId, metadata.id(), metadata, metadataList); + try { + filter.handler().handle(event, metadata); + } catch (Exception e) { + logException(e, type, metadataList, metadata); + metadataList.remove(i--); + break; + } + + if (event.cancelled()) { + // Remove meta, decrease list index counter, and break current filter loop + metadataList.remove(i--); + break; + } + + Preconditions.checkArgument(event.index() == event.meta().id(), "Meta event id desync: Calls to meta().setId() are illegal"); + if (event.extraMeta() != null) { + // Add newly created meta + metadataList.addAll(event.extraMeta()); + event.clearExtraMeta(); + } + } + } + i++; + } + } + + @Deprecated + private boolean callOldMetaHandler(int entityId, EntityType type, Metadata metadata, List metadataList, UserConnection connection) { + try { + handleMetadata(entityId, type, metadata, metadataList, connection); + return true; + } catch (Exception e) { + logException(e, type, metadataList, metadata); + return false; + } + } + + /** + * To be overridden to handle metadata. + * + * @param entityId entity id + * @param type entity type, or null if not tracked + * @param metadata current metadata + * @param metadatas full, mutable list of metadata + * @param connection user connection + * @deprecated use {@link #filter()} + */ + @Deprecated + protected void handleMetadata(int entityId, @Nullable EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception { + } + + /** + * Returns the entity type from the given (mapped) type id. + * + * @param type mapped type id + * @return entity type + */ + protected abstract EntityType typeFromId(int type); + + /** + * Returns the entity type from the given id. + * From 1.14 and onwards, this is the same exact value as {@link #typeFromId(int)}. + * + * @param type entity type id + * @return EntityType from id + */ + protected EntityType objectTypeFromId(int type) { + return typeFromId(type); + } + + /** + * Returns the mapped entitiy (or the same if it has not changed). + * + * @param oldId old entity id + * @return mapped entity id + */ + public int newEntityId(int oldId) { + return typeMappings != null ? typeMappings.getOrDefault(oldId, oldId) : oldId; + } + + /** + * Maps an entity type. + * + * @param type entity type + * @param mappedType mapped entity type + * @throws IllegalArgumentException if the types share the same implementing class + */ + public void mapEntityType(EntityType type, EntityType mappedType) { + Preconditions.checkArgument(type.getClass() != mappedType.getClass(), "EntityTypes should not be of the same class/enum"); + mapEntityType(type.getId(), mappedType.getId()); + } + + protected void mapEntityType(int id, int mappedId) { + if (typeMappings == null) { + typeMappings = new Int2IntOpenHashMap(); + typeMappings.defaultReturnValue(-1); + } + typeMappings.put(id, mappedId); + } + + /** + * Maps entity ids based on the enum constant's names. + * + * @param oldTypes entity types of the higher version + * @param newTypeClass entity types enum class of the lower version + * @param new enum entity type + */ + public & EntityType> void mapTypes(EntityType[] oldTypes, Class newTypeClass) { + if (typeMappings == null) { + typeMappings = new Int2IntOpenHashMap(oldTypes.length, 1F); + typeMappings.defaultReturnValue(-1); + } + for (EntityType oldType : oldTypes) { + try { + E newType = Enum.valueOf(newTypeClass, oldType.name()); + typeMappings.put(oldType.getId(), newType.getId()); + } catch (IllegalArgumentException notFound) { + if (!typeMappings.containsKey(oldType.getId())) { + Via.getPlatform().getLogger().warning("Could not find new entity type for " + oldType + "! " + + "Old type: " + oldType.getClass().getEnclosingClass().getSimpleName() + ", new type: " + newTypeClass.getEnclosingClass().getSimpleName()); + } + } + } + } + + /** + * Registers a metadata handler to rewrite, item, block, and particle ids stored in metadata. + * + * @param itemType item meta type if needed + * @param blockType block meta type if needed + * @param particleType particle meta type if needed + * @param itemRewriter itemrewriter if needed + */ + public void registerDumMetaTypeHandler(@Nullable MetaType itemType, @Nullable MetaType blockType, @Nullable MetaType particleType, ItemRewriter.@Nullable RewriteFunction itemRewriter) { + filter().handler((event, meta) -> { + if (itemType != null && meta.metaType() == itemType) { + itemRewriter.rewrite(meta.value()); + } else if (blockType != null && meta.metaType() == blockType) { + int data = meta.value(); + meta.setValue(protocol.getMappingData().getNewBlockStateId(data)); + } else if (particleType != null && meta.metaType() == particleType) { + rewriteParticle(meta.value()); + } + }); + } + + public void registerTracker(ClientboundPacketType packetType) { + protocol.registerClientbound(packetType, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity ID + map(Type.UUID); // 1 - Entity UUID + map(Type.VAR_INT); // 2 - Entity Type + handler(trackerHandler()); + } + }); + } + + public void registerTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType) { + protocol.registerClientbound(packetType, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity id + map(Type.UUID); // 1 - Entity UUID + map(Type.VAR_INT); // 2 - Entity Type + map(Type.DOUBLE); // 3 - X + map(Type.DOUBLE); // 4 - Y + map(Type.DOUBLE); // 5 - Z + map(Type.BYTE); // 6 - Pitch + map(Type.BYTE); // 7 - Yaw + map(Type.INT); // 8 - Data + handler(trackerHandler()); + handler(wrapper -> { + int entityId = wrapper.get(Type.VAR_INT, 0); + EntityType entityType = tracker(wrapper.user()).entityType(entityId); + if (entityType == fallingBlockType) { + wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(wrapper.get(Type.INT, 0))); + } + }); + } + }); + } + + /** + * Registers an entity tracker for the extra spawn packets like player, painting, or xp orb spawns. + * + * @param packetType packet type + * @param entityType entity type + * @param intType int type of the entity id + */ + public void registerTracker(ClientboundPacketType packetType, EntityType entityType, Type intType) { + protocol.registerClientbound(packetType, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + int entityId = (int) wrapper.passthrough(intType); + tracker(wrapper.user()).addEntity(entityId, entityType); + }); + } + }); + } + + /** + * Registers an entity tracker for the extra spawn packets. + * + * @param packetType packet type + * @param entityType entity type + */ + public void registerTracker(ClientboundPacketType packetType, EntityType entityType) { + registerTracker(packetType, entityType, Type.VAR_INT); + } + + /** + * Sub 1.17 method for entity remove packets. + * + * @param packetType remove entities packet type + */ + public void registerRemoveEntities(ClientboundPacketType packetType) { + protocol.registerClientbound(packetType, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + int[] entityIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); + EntityTracker entityTracker = tracker(wrapper.user()); + for (int entity : entityIds) { + entityTracker.removeEntity(entity); + } + }); + } + }); + } + + /** + * 1.17+ method for entity remove packets. + * + * @param packetType remove entities packet type + */ + public void registerRemoveEntity(ClientboundPacketType packetType) { + protocol.registerClientbound(packetType, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + int entityId = wrapper.passthrough(Type.VAR_INT); + tracker(wrapper.user()).removeEntity(entityId); + }); + } + }); + } + + public void registerMetadataRewriter(ClientboundPacketType packetType, @Nullable Type> oldMetaType, Type> newMetaType) { + protocol.registerClientbound(packetType, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity ID + if (oldMetaType != null) { + map(oldMetaType, newMetaType); + } else { + map(newMetaType); + } + handler(wrapper -> { + int entityId = wrapper.get(Type.VAR_INT, 0); + List metadata = wrapper.get(newMetaType, 0); + handleMetadata(entityId, metadata, wrapper.user()); + }); + } + }); + } + + public void registerMetadataRewriter(ClientboundPacketType packetType, Type> metaType) { + registerMetadataRewriter(packetType, null, metaType); + } + + public PacketHandler trackerHandler() { + return trackerAndRewriterHandler(null); + } + + protected PacketHandler worldDataTrackerHandler(int nbtIndex) { + return wrapper -> { + EntityTracker tracker = tracker(wrapper.user()); + + CompoundTag registryData = wrapper.get(Type.NBT, nbtIndex); + Tag height = registryData.get("height"); + if (height instanceof IntTag) { + int blockHeight = ((IntTag) height).asInt(); + tracker.setCurrentWorldSectionHeight(blockHeight >> 4); + } else { + Via.getPlatform().getLogger().warning("Height missing in dimension data: " + registryData); + } + + Tag minY = registryData.get("min_y"); + if (minY instanceof IntTag) { + tracker.setCurrentMinY(((IntTag) minY).asInt()); + } else { + Via.getPlatform().getLogger().warning("Min Y missing in dimension data: " + registryData); + } + }; + } + + // --------------------------------------------------------------------------- + // Sub 1.14.1 methods + + /** + * Returns a packethandler to track and rewrite an entity. + * + * @param metaType type of the metadata list + * @return handler for tracking and rewriting entities + */ + public PacketHandler trackerAndRewriterHandler(@Nullable Type> metaType) { + return wrapper -> { + int entityId = wrapper.get(Type.VAR_INT, 0); + int type = wrapper.get(Type.VAR_INT, 1); + + int newType = newEntityId(type); + if (newType != type) { + wrapper.set(Type.VAR_INT, 1, newType); + } + + EntityType entType = typeFromId(trackMappedType ? newType : type); + // Register Type ID + tracker(wrapper.user()).addEntity(entityId, entType); + + if (metaType != null) { + handleMetadata(entityId, wrapper.get(metaType, 0), wrapper.user()); + } + }; + } + + public PacketHandler trackerAndRewriterHandler(@Nullable Type> metaType, EntityType entityType) { + return wrapper -> { + int entityId = wrapper.get(Type.VAR_INT, 0); + // Register Type ID + tracker(wrapper.user()).addEntity(entityId, entityType); + + if (metaType != null) { + handleMetadata(entityId, wrapper.get(metaType, 0), wrapper.user()); + } + }; + } + + /** + * Returns a packethandler to track an object entity. + * + * @return handler for tracking and rewriting entities + */ + public PacketHandler objectTrackerHandler() { + return wrapper -> { + int entityId = wrapper.get(Type.VAR_INT, 0); + byte type = wrapper.get(Type.BYTE, 0); + + EntityType entType = objectTypeFromId(type); + // Register Type ID + tracker(wrapper.user()).addEntity(entityId, entType); + }; + } + + // --------------------------------------------------------------------------- + + @Deprecated + protected @Nullable Metadata metaByIndex(int index, List metadataList) { + for (Metadata metadata : metadataList) { + if (metadata.id() == index) { + return metadata; + } + } + return null; + } + + protected void rewriteParticle(Particle particle) { + ParticleMappings mappings = protocol.getMappingData().getParticleMappings(); + int id = particle.getId(); + if (id == mappings.getBlockId() || id == mappings.getFallingDustId()) { + Particle.ParticleData data = particle.getArguments().get(0); + data.setValue(protocol.getMappingData().getNewBlockStateId(data.get())); + } else if (id == mappings.getItemId()) { + Particle.ParticleData data = particle.getArguments().get(0); + data.setValue(protocol.getMappingData().getNewItemId(data.get())); + } + + particle.setId(protocol.getMappingData().getNewParticleId(id)); + } + + /** + * Returns the entity tracker for the current protocol. + * + * @param connection user connection + * @param entity tracker type + * @return entity tracker + */ + public T tracker(UserConnection connection) { + return connection.getEntityTracker(protocol.getClass()); + } + + private void logException(Exception e, EntityType type, List metadataList, Metadata metadata) { + if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { + Logger logger = Via.getPlatform().getLogger(); + logger.warning("An error occurred with entity metadata handler"); + logger.warning("This is most likely down to one of your plugins sending bad datawatchers. Please test if this occurs without any plugins except ViaVersion before reporting it on GitHub"); + logger.warning("Also make sure that all your plugins are compatible with your server version."); + logger.warning("Entity type: " + type); + logger.warning("Indexed meta: " + metadata); + logger.warning(metadataList.stream().sorted(Comparator.comparingInt(Metadata::id)) + .map(Metadata::toString).collect(Collectors.joining("\n", "Metadata: ", ""))); + e.printStackTrace(); + } + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/MetadataRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/MetadataRewriter.java deleted file mode 100644 index a08884c20..000000000 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/MetadataRewriter.java +++ /dev/null @@ -1,330 +0,0 @@ -/* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion - * Copyright (C) 2016-2021 ViaVersion and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.viaversion.viaversion.rewriter; - -import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.data.ParticleMappings; -import com.viaversion.viaversion.api.minecraft.entities.EntityType; -import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import com.viaversion.viaversion.api.protocol.Protocol; -import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; -import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.api.type.types.Particle; -import com.viaversion.viaversion.data.EntityTracker; -import it.unimi.dsi.fastutil.ints.Int2IntMap; -import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.List; -import java.util.logging.Logger; - -public abstract class MetadataRewriter { - private static final Metadata[] EMPTY_ARRAY = new Metadata[0]; - private final Class entityTrackerClass; - protected final Protocol protocol; - private Int2IntMap typeMapping; - - protected MetadataRewriter(Protocol protocol, Class entityTrackerClass) { - this.protocol = protocol; - this.entityTrackerClass = entityTrackerClass; - protocol.put(this); - } - - public final void handleMetadata(int entityId, List metadatas, UserConnection connection) { - EntityType type = connection.get(entityTrackerClass).getEntity(entityId); - for (Metadata metadata : metadatas.toArray(EMPTY_ARRAY)) { - try { - handleMetadata(entityId, type, metadata, metadatas, connection); - } catch (Exception e) { - metadatas.remove(metadata); - if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { - Logger logger = Via.getPlatform().getLogger(); - - logger.warning("An error occurred with entity metadata handler"); - logger.warning("This is most likely down to one of your plugins sending bad datawatchers. Please test if this occurs without any plugins except ViaVersion before reporting it on GitHub"); - logger.warning("Also make sure that all your plugins are compatible with your server version."); - logger.warning("Entity type: " + type); - logger.warning("Metadata: " + metadata); - e.printStackTrace(); - } - } - } - } - - protected void rewriteParticle(Particle particle) { - ParticleMappings mappings = protocol.getMappingData().getParticleMappings(); - int id = particle.getId(); - if (id == mappings.getBlockId() || id == mappings.getFallingDustId()) { - Particle.ParticleData data = particle.getArguments().get(0); - data.setValue(protocol.getMappingData().getNewBlockStateId(data.get())); - } else if (id == mappings.getItemId()) { - Particle.ParticleData data = particle.getArguments().get(0); - data.setValue(protocol.getMappingData().getNewItemId(data.get())); - } - - particle.setId(protocol.getMappingData().getNewParticleId(id)); - } - - //TODO add respawn/join once they stop changing too much - - public void registerTracker(ClientboundPacketType packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - map(Type.UUID); // 1 - Entity UUID - map(Type.VAR_INT); // 2 - Entity Type - handler(getTracker()); - } - }); - } - - public void registerSpawnTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType) { - protocol.registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity id - map(Type.UUID); // 1 - Entity UUID - map(Type.VAR_INT); // 2 - Entity Type - map(Type.DOUBLE); // 3 - X - map(Type.DOUBLE); // 4 - Y - map(Type.DOUBLE); // 5 - Z - map(Type.BYTE); // 6 - Pitch - map(Type.BYTE); // 7 - Yaw - map(Type.INT); // 8 - Data - handler(getTracker()); - handler(wrapper -> { - int entityId = wrapper.get(Type.VAR_INT, 0); - EntityType entityType = wrapper.user().get(entityTrackerClass).getEntity(entityId); - if (entityType == fallingBlockType) { - wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(wrapper.get(Type.INT, 0))); - } - }); - } - }); - } - - public void registerTracker(ClientboundPacketType packetType, EntityType entityType) { - protocol.registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - handler(wrapper -> { - int entityId = wrapper.get(Type.VAR_INT, 0); - wrapper.user().get(entityTrackerClass).addEntity(entityId, entityType); - }); - } - }); - } - - /** - * Sub 1.17 method for entity remove packets. - * - * @param packetType remove entities packet type - */ - public void registerEntityDestroy(ClientboundPacketType packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT_ARRAY_PRIMITIVE); // 0 - Entity ids - handler(wrapper -> { - EntityTracker entityTracker = wrapper.user().get(entityTrackerClass); - for (int entity : wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0)) { - entityTracker.removeEntity(entity); - } - }); - } - }); - } - - /** - * 1.17+ method for entity remove packets. - * - * @param packetType remove entities packet type - */ - public void registerRemoveEntity(ClientboundPacketType packetType) { - protocol.registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ids - handler(wrapper -> { - int entity = wrapper.get(Type.VAR_INT, 0); - wrapper.user().get(entityTrackerClass).removeEntity(entity); - }); - } - }); - } - - public void registerMetadataRewriter(ClientboundPacketType packetType, @Nullable Type> oldMetaType, Type> newMetaType) { - protocol.registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - if (oldMetaType != null) { - map(oldMetaType, newMetaType); - } else { - map(newMetaType); - } - handler(wrapper -> { - int entityId = wrapper.get(Type.VAR_INT, 0); - List metadata = wrapper.get(newMetaType, 0); - handleMetadata(entityId, metadata, wrapper.user()); - }); - } - }); - } - - public void registerMetadataRewriter(ClientboundPacketType packetType, Type> metaType) { - registerMetadataRewriter(packetType, null, metaType); - } - - public & EntityType> void mapTypes(EntityType[] oldTypes, Class newTypeClass) { - if (typeMapping == null) { - typeMapping = new Int2IntOpenHashMap(oldTypes.length, 1F); - typeMapping.defaultReturnValue(-1); - } - for (EntityType oldType : oldTypes) { - try { - T newType = Enum.valueOf(newTypeClass, oldType.name()); - typeMapping.put(oldType.getId(), newType.getId()); - } catch (IllegalArgumentException notFound) { - if (!typeMapping.containsKey(oldType.getId())) { - Via.getPlatform().getLogger().warning("Could not find new entity type for " + oldType + "! " + - "Old type: " + oldType.getClass().getEnclosingClass().getSimpleName() + ", new type: " + newTypeClass.getEnclosingClass().getSimpleName()); - } - } - } - } - - public void mapType(EntityType oldType, EntityType newType) { - if (typeMapping == null) { - typeMapping = new Int2IntOpenHashMap(); - typeMapping.defaultReturnValue(-1); - } - typeMapping.put(oldType.getId(), newType.getId()); - } - - public PacketHandler getTracker() { - return getTrackerAndRewriter(null); - } - - // --------------------------------------------------------------------------- - // Sub 1.14.1 methods - - /** - * Returns a packethandler to track and rewrite an entity. - * - * @param metaType type of the metadata list - * @return handler for tracking and rewriting entities - */ - public PacketHandler getTrackerAndRewriter(@Nullable Type> metaType) { - return wrapper -> { - int entityId = wrapper.get(Type.VAR_INT, 0); - int type = wrapper.get(Type.VAR_INT, 1); - - int newType = getNewEntityId(type); - if (newType != type) { - wrapper.set(Type.VAR_INT, 1, newType); - } - - EntityType entType = getTypeFromId(newType); - // Register Type ID - wrapper.user().get(entityTrackerClass).addEntity(entityId, entType); - - if (metaType != null) { - handleMetadata(entityId, wrapper.get(metaType, 0), wrapper.user()); - } - }; - } - - public PacketHandler getTrackerAndRewriter(@Nullable Type> metaType, EntityType entityType) { - return wrapper -> { - int entityId = wrapper.get(Type.VAR_INT, 0); - // Register Type ID - wrapper.user().get(entityTrackerClass).addEntity(entityId, entityType); - - if (metaType != null) { - handleMetadata(entityId, wrapper.get(metaType, 0), wrapper.user()); - } - }; - } - - /** - * Returns a packethandler to track an object entity. - * - * @return handler for tracking and rewriting entities - */ - public PacketHandler getObjectTracker() { - return wrapper -> { - int entityId = wrapper.get(Type.VAR_INT, 0); - byte type = wrapper.get(Type.BYTE, 0); - - EntityType entType = getObjectTypeFromId(type); - // Register Type ID - wrapper.user().get(entityTrackerClass).addEntity(entityId, entType); - }; - } - - // --------------------------------------------------------------------------- - - protected abstract EntityType getTypeFromId(int type); - - /** - * Returns the entity type from the given id. - * From 1.14 and onwards, this is the same exact value as {@link #getTypeFromId(int)}. - * - * @param type entity type id - * @return EntityType from id - */ - protected EntityType getObjectTypeFromId(int type) { - return getTypeFromId(type); - } - - /** - * Returns the mapped entitiy (or the same if it has not changed). - * - * @param oldId old entity id - * @return mapped entity id - */ - public int getNewEntityId(int oldId) { - return typeMapping != null ? typeMapping.getOrDefault(oldId, oldId) : oldId; - } - - /** - * To be overridden to handle metadata. - * - * @param entityId entity id - * @param type entity type, or null if not tracked - * @param metadata current metadata - * @param metadatas full, mutable list of metadata - * @param connection user connection - */ - protected abstract void handleMetadata(int entityId, @Nullable EntityType type, Metadata metadata, List metadatas, UserConnection connection) throws Exception; - - protected @Nullable Metadata getMetaByIndex(int index, List metadataList) { - for (Metadata metadata : metadataList) { - if (metadata.getId() == index) { - return metadata; - } - } - return null; - } -} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/storage/EntityTracker1_15.java b/common/src/main/java/com/viaversion/viaversion/rewriter/Rewriter.java similarity index 65% rename from common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/storage/EntityTracker1_15.java rename to common/src/main/java/com/viaversion/viaversion/rewriter/Rewriter.java index 687783f89..de749d266 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/storage/EntityTracker1_15.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/Rewriter.java @@ -15,15 +15,21 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.viaversion.viaversion.protocols.protocol1_15to1_14_4.storage; +package com.viaversion.viaversion.rewriter; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; -import com.viaversion.viaversion.data.EntityTracker; +import com.viaversion.viaversion.api.protocol.Protocol; -public class EntityTracker1_15 extends EntityTracker { +public interface Rewriter { - public EntityTracker1_15(UserConnection user) { - super(user, Entity1_15Types.PLAYER); - } + /** + * Registers any packet handlers or rewrites needed. + */ + void register(); + + /** + * Returns the {@link Protocol} instance of this rewriter. + * + * @return protocol of the rewriter + */ + T protocol(); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/storage/EntityTracker1_14_1.java b/common/src/main/java/com/viaversion/viaversion/rewriter/RewriterBase.java similarity index 52% rename from common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/storage/EntityTracker1_14_1.java rename to common/src/main/java/com/viaversion/viaversion/rewriter/RewriterBase.java index ba1ea133e..0475edef1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14_1to1_14/storage/EntityTracker1_14_1.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/RewriterBase.java @@ -15,15 +15,37 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.viaversion.viaversion.protocols.protocol1_14_1to1_14.storage; +package com.viaversion.viaversion.rewriter; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types; -import com.viaversion.viaversion.data.EntityTracker; +import com.viaversion.viaversion.api.protocol.Protocol; -public class EntityTracker1_14_1 extends EntityTracker { +public abstract class RewriterBase implements Rewriter { + protected final T protocol; - public EntityTracker1_14_1(UserConnection user) { - super(user, Entity1_14Types.PLAYER); + protected RewriterBase(T protocol) { + this.protocol = protocol; + } + + @Override + public void register() { + registerPackets(); + registerRewrites(); + } + + /** + * To be overriden. Called when initializing the EntityRewriter. + */ + protected void registerPackets() { + } + + /** + * To be overriden. Called when initializing the EntityRewriter. + */ + protected void registerRewrites() { + } + + @Override + public T protocol() { + return protocol; } } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaFilter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaFilter.java new file mode 100644 index 000000000..59b21ac6e --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaFilter.java @@ -0,0 +1,244 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.viaversion.viaversion.rewriter.meta; + +import com.google.common.base.Preconditions; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import com.viaversion.viaversion.api.minecraft.metadata.Metadata; +import com.viaversion.viaversion.rewriter.EntityRewriter; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Objects; + +public class MetaFilter { + private final MetaHandler handler; + private final EntityType type; + private final int index; + private final boolean filterFamily; + + public MetaFilter(@Nullable EntityType type, boolean filterFamily, int index, MetaHandler handler) { + Preconditions.checkNotNull(handler, "MetaHandler cannot be null"); + this.type = type; + this.filterFamily = filterFamily; + this.index = index; + this.handler = handler; + } + + /** + * Returns the metadata index to filter, or -1. + * + * @return metadata index, or -1 if unset + */ + public int index() { + return index; + } + + /** + * Returns the filtered entity type if present. + * + * @return filtered entity type if present + */ + public @Nullable EntityType type() { + return type; + } + + /** + * Returns the metadata handler. + * + * @return metadata handler + */ + public MetaHandler handler() { + return handler; + } + + /** + * Returns whether entity parent types should be checked against as well. + * + * @return whether entity parent types should be checked against + */ + public boolean filterFamily() { + return filterFamily; + } + + /** + * Returns whether if the metadata should be handled by this filter. + * + * @param type entity type + * @param metadata metadata + * @return whether the meta should be filtered + */ + public boolean isFiltered(EntityType type, Metadata metadata) { + // First check if the filter has no type or the type is equal or part of the filtered parent types + // Applicable if no specific index is filtered or the indexes are equal + return (this.type == null || (filterFamily ? type.isOrHasParent(this.type) : this.type == type)) && (index == -1 || metadata.id() == index); + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MetaFilter that = (MetaFilter) o; + if (index != that.index) return false; + if (filterFamily != that.filterFamily) return false; + if (!handler.equals(that.handler)) return false; + return Objects.equals(type, that.type); + } + + @Override + public int hashCode() { + int result = handler.hashCode(); + result = 31 * result + (type != null ? type.hashCode() : 0); + result = 31 * result + index; + result = 31 * result + (filterFamily ? 1 : 0); + return result; + } + + @Override + public String toString() { + return "MetaFilter{" + + "type=" + type + + ", filterFamily=" + filterFamily + + ", index=" + index + + ", handler=" + handler + + '}'; + } + + public static final class Builder { + private final EntityRewriter rewriter; + private EntityType type; + private int index = -1; + private boolean filterFamily; + private MetaHandler handler; + + public Builder(EntityRewriter rewriter) { + this.rewriter = rewriter; + } + + public Builder type(EntityType type) { + Preconditions.checkArgument(this.type == null); + this.type = type; + return this; + } + + public Builder index(int index) { + Preconditions.checkArgument(this.index == -1); + this.index = index; + return this; + } + + public Builder filterFamily(EntityType type) { + Preconditions.checkArgument(this.type == null); + this.type = type; + this.filterFamily = true; + return this; + } + + public Builder handlerNoRegister(MetaHandler handler) { + Preconditions.checkArgument(this.handler == null); + this.handler = handler; + return this; + } + + /** + * Sets the metadata handler and registers the metadata filter. + * Should always be called last. + * + * @param handler metadata handler + */ + public void handler(MetaHandler handler) { + Preconditions.checkArgument(this.handler == null); + this.handler = handler; + register(); + } + + /** + * Sets a handler to remove metadata at the given index without affecting any other indexes and registers the filter. + * Should always be called last. + * + * @param index index to cancel + */ + public void cancel(int index) { + this.index = index; + handler((event, meta) -> event.cancel()); + } + + /** + * Sets a handler to change the index. Does not do any other transformation or shifting and registers the filter. + * Should always be called last. + * + * @param newIndex new index + * @throws IllegalArgumentException if no index has been set yet + */ + public void toIndex(int newIndex) { + Preconditions.checkArgument(this.index != -1); + handler((event, meta) -> event.setIndex(newIndex)); + } + + /** + * Sets a handler incrementing every index above the given one and registers the filter. + * Should always be called last. + * + * @param index index to pad + * @throws IllegalArgumentException if the index has already been set + */ + public void addIndex(int index) { + Preconditions.checkArgument(this.index == -1); + handler((event, meta) -> { + if (event.index() >= index) { + event.setIndex(event.index() + 1); + } + }); + } + + /** + * Sets a handler to remove metadata at the given index, decrementing every index above it and registers the filter. + * Should always be called last. + * + * @param index index to remove + * @throws IllegalArgumentException if the index has already been set + */ + public void removeIndex(int index) { + Preconditions.checkArgument(this.index == -1); + handler((event, meta) -> { + int metaIndex = event.index(); + if (metaIndex == index) { + event.cancel(); + } else if (metaIndex > index) { + event.setIndex(metaIndex - 1); + } + }); + } + + /** + * Creates and registers the created MetaFilter in the linked {@link EntityRewriter} instance. + */ + public void register() { + rewriter.registerFilter(build()); + } + + /** + * Returns a new metadata filter without registering it. + * + * @return created meta filter + */ + public MetaFilter build() { + return new MetaFilter(type, filterFamily, index, handler); + } + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/storage/EntityTracker1_16.java b/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandler.java similarity index 58% rename from common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/storage/EntityTracker1_16.java rename to common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandler.java index b15abb230..e5869b864 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/storage/EntityTracker1_16.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandler.java @@ -1,5 +1,5 @@ /* - * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards * Copyright (C) 2016-2021 ViaVersion and contributors * * This program is free software: you can redistribute it and/or modify @@ -15,15 +15,19 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types; -import com.viaversion.viaversion.data.EntityTracker; +package com.viaversion.viaversion.rewriter.meta; -public class EntityTracker1_16 extends EntityTracker { +import com.viaversion.viaversion.api.minecraft.metadata.Metadata; - public EntityTracker1_16(UserConnection user) { - super(user, Entity1_16Types.PLAYER); - } +@FunctionalInterface +public interface MetaHandler { + + /** + * Handles a metadata entry of an entity. + * + * @param event metadata event + * @param meta metadata, convenience field for {@link MetaHandlerEvent#meta()} + */ + void handle(MetaHandlerEvent event, Metadata meta); } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandlerEvent.java b/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandlerEvent.java new file mode 100644 index 000000000..bd9ec03a4 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandlerEvent.java @@ -0,0 +1,121 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.rewriter.meta; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import com.viaversion.viaversion.api.minecraft.metadata.Metadata; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; + +public interface MetaHandlerEvent { + + /** + * Returns the user connection the metadata is sent to. + * + * @return user connection + */ + UserConnection user(); + + /** + * Returns the entity id of the entity. + * + * @return entity id + */ + int entityId(); + + /** + * Returns the entity type of the entity the metadata belongs to. + * + * @return entity type of the entity + */ + EntityType entityType(); + + /** + * Returns the metadata index. + * + * @return return meta index + */ + int index(); + + /** + * Sets the metadata index. + * + * @param index new metadata index + */ + void setIndex(int index); + + /** + * Returns the metadata by the given index if present. + * + * @param index metadata index + * @return metadata by index if present + */ + @Nullable Metadata getMetaByIndex(int index); + + /** + * Returns the metadata. + * Do NOT call {@link Metadata#setId(int)} and instead use {@link MetaHandlerEvent#setIndex(int)}. + * + * @return return metadata + */ + Metadata meta(); + + /** + * Prevents other handlers from being called with this metadata entry and removes it from the list. + */ + void cancel(); + + /** + * Returns whether this metadata entry should be removed. + * + * @return true if cancelled/removed + */ + boolean cancelled(); + + /** + * Returns an immutable metadata view. + * + * @return immutable metadata list + * @see #cancel() + * @see #createExtraMeta(Metadata) + */ + List metadataList(); + + /** + * Returns additionally created metadata. + * May be null; use {@link #createExtraMeta(Metadata)} for adding metadata. + * + * @return additionally created metadata if present + */ + @Nullable List extraMeta(); + + /** + * Adds the given metadata to the metadata list. + * This metadata will not be passed through handlers of the current loop. + * + * @param metadata metadata + */ + void createExtraMeta(Metadata metadata); + + /** + * Clears the additional metadata. + */ + void clearExtraMeta(); +} diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandlerEventImpl.java b/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandlerEventImpl.java new file mode 100644 index 000000000..f8655bff4 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/meta/MetaHandlerEventImpl.java @@ -0,0 +1,119 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.viaversion.viaversion.rewriter.meta; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.EntityType; +import com.viaversion.viaversion.api.minecraft.metadata.Metadata; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class MetaHandlerEventImpl implements MetaHandlerEvent { + private final UserConnection connection; + private final EntityType entityType; + private final int entityId; + private final List metadataList; + private final Metadata meta; + private List extraData; + private int index; + private boolean cancel; + + public MetaHandlerEventImpl(UserConnection connection, EntityType entityType, int entityId, int index, Metadata meta, List metadataList) { + this.connection = connection; + this.entityType = entityType; + this.entityId = entityId; + this.index = index; + this.meta = meta; + this.metadataList = metadataList; + } + + @Override + public @Nullable Metadata getMetaByIndex(int index) { + for (Metadata meta : metadataList) { + if (index == meta.id()) { + return meta; + } + } + return null; + } + + @Override + public UserConnection user() { + return connection; + } + + @Override + public int entityId() { + return entityId; + } + + @Override + public EntityType entityType() { + return entityType; + } + + @Override + public int index() { + return index; + } + + @Override + public void setIndex(int index) { + this.index = index; + meta.setId(index); + } + + @Override + public Metadata meta() { + return meta; + } + + @Override + public void cancel() { + this.cancel = true; + } + + @Override + public boolean cancelled() { + return cancel; + } + + @Override + public List metadataList() { + return Collections.unmodifiableList(metadataList); + } + + @Override + public @Nullable List extraMeta() { + return extraData; + } + + @Override + public void createExtraMeta(Metadata metadata) { + (extraData != null ? extraData : (extraData = new ArrayList<>())).add(metadata); + } + + @Override + public void clearExtraMeta() { + extraData = null; + } +} diff --git a/sponge/src/main/java/com/viaversion/viaversion/sponge/listeners/protocol1_9to1_8/BlockListener.java b/sponge/src/main/java/com/viaversion/viaversion/sponge/listeners/protocol1_9to1_8/BlockListener.java index 175817b03..71146eb6a 100644 --- a/sponge/src/main/java/com/viaversion/viaversion/sponge/listeners/protocol1_9to1_8/BlockListener.java +++ b/sponge/src/main/java/com/viaversion/viaversion/sponge/listeners/protocol1_9to1_8/BlockListener.java @@ -18,6 +18,7 @@ package com.viaversion.viaversion.sponge.listeners.protocol1_9to1_8; import com.viaversion.viaversion.SpongePlugin; +import com.viaversion.viaversion.api.data.entity.EntityTracker; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9; @@ -38,9 +39,8 @@ public class BlockListener extends ViaSpongeListener { public void placeBlock(ChangeBlockEvent.Place e, @Root Player player) { if (isOnPipe(player.getUniqueId())) { Location loc = e.getTransactions().get(0).getFinal().getLocation().get(); - getUserConnection(player.getUniqueId()) - .get(EntityTracker1_9.class) - .addBlockInteraction(new Position(loc.getBlockX(), (short) loc.getBlockY(), loc.getBlockZ())); + EntityTracker1_9 tracker = getUserConnection(player.getUniqueId()).getEntityTracker(Protocol1_9To1_8.class); + tracker.addBlockInteraction(new Position(loc.getBlockX(), (short) loc.getBlockY(), loc.getBlockZ())); } } }