From a4ed18f59bc79c801d67799190f0aafc6a4eb599 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Fri, 7 Feb 2020 22:32:24 +0100 Subject: [PATCH] Entity rewriter cleanup --- .../storage/EntityPositionHandler.java | 20 +- .../api/entities/storage/EntityTracker.java | 9 +- .../api/rewriters/EntityRewriter.java | 366 +++--------------- .../api/rewriters/EntityRewriterBase.java | 250 ++++++++++++ .../api/rewriters/ItemRewriterBase.java | 22 +- .../api/rewriters/LegacyEntityRewriter.java | 102 +++++ .../api/rewriters/RecipeRewriter.java | 19 + .../packets/BlockItemPackets1_11.java | 10 +- .../packets/EntityPackets1_11.java | 42 +- .../packets/EntityPackets1_12.java | 10 +- .../packets/EntityPackets1_11_1.java | 6 +- .../packets/EntityPackets1_13.java | 22 +- .../packets/BlockItemPackets1_14.java | 8 + .../packets/EntityPackets1_14.java | 46 +-- .../packets/PlayerPackets1_14.java | 14 - .../packets/SoundPackets1_14.java | 14 +- .../packets/EntityPackets1_13_1.java | 6 +- .../Protocol1_14_4To1_15.java | 4 +- .../packets/BlockItemPackets1_15.java | 33 +- .../packets/EntityPackets1_15.java | 165 +++----- .../packets/EntityPackets1_14_1.java | 6 +- .../packets/BlockItemPackets1_16.java | 34 +- .../packets/EntityPackets1_16.java | 101 +---- .../packets/EntityPackets1_10.java | 8 +- 24 files changed, 602 insertions(+), 715 deletions(-) create mode 100644 core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriterBase.java create mode 100644 core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacyEntityRewriter.java diff --git a/core/src/main/java/nl/matsv/viabackwards/api/entities/storage/EntityPositionHandler.java b/core/src/main/java/nl/matsv/viabackwards/api/entities/storage/EntityPositionHandler.java index 6d545882..c5b66c3c 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/entities/storage/EntityPositionHandler.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/entities/storage/EntityPositionHandler.java @@ -1,24 +1,23 @@ package nl.matsv.viabackwards.api.entities.storage; import nl.matsv.viabackwards.ViaBackwards; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.EntityRewriterBase; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.type.Type; -import java.util.Optional; import java.util.function.Supplier; public class EntityPositionHandler { public static final double RELATIVE_MOVE_FACTOR = 32 * 128; - private final EntityRewriter entityRewriter; + private final EntityRewriterBase entityRewriter; private final Class storageClass; private final Supplier storageSupplier; private boolean warnedForMissingEntity; - public EntityPositionHandler(EntityRewriter entityRewriter, + public EntityPositionHandler(EntityRewriterBase entityRewriter, Class storageClass, Supplier storageSupplier) { this.entityRewriter = entityRewriter; this.storageClass = storageClass; @@ -32,8 +31,8 @@ public class EntityPositionHandler { public void cacheEntityPosition(PacketWrapper wrapper, double x, double y, double z, boolean create, boolean relative) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - Optional optStoredEntity = entityRewriter.getEntityTracker(wrapper.user()).getEntity(entityId); - if (!optStoredEntity.isPresent()) { + EntityTracker.StoredEntity storedEntity = entityRewriter.getEntityTracker(wrapper.user()).getEntity(entityId); + if (storedEntity == null) { if (!Via.getConfig().isSuppressMetadataErrors()) { ViaBackwards.getPlatform().getLogger().warning("Stored entity with id " + entityId + " missing at position: " + x + " - " + y + " - " + z + " in " + storageClass.getSimpleName()); // Reports were getting too much :> @@ -49,7 +48,6 @@ public class EntityPositionHandler { return; } - EntityTracker.StoredEntity storedEntity = optStoredEntity.get(); EntityPositionStorage positionStorage = create ? storageSupplier.get() : storedEntity.get(storageClass); if (positionStorage == null) { ViaBackwards.getPlatform().getLogger().warning("Stored entity with id " + entityId + " missing " + storageClass.getSimpleName()); @@ -61,13 +59,13 @@ public class EntityPositionHandler { } public EntityPositionStorage getStorage(UserConnection user, int entityId) { - Optional optEntity = user.get(EntityTracker.class).get(entityRewriter.getProtocol()).getEntity(entityId); - EntityPositionStorage storedEntity; - if (!optEntity.isPresent() || (storedEntity = optEntity.get().get(EntityPositionStorage.class)) == null) { + EntityTracker.StoredEntity storedEntity = user.get(EntityTracker.class).get(entityRewriter.getProtocol()).getEntity(entityId); + EntityPositionStorage entityStorage; + if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage.class)) == null) { ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId + " in " + storageClass.getSimpleName()); return null; } - return storedEntity; + return entityStorage; } public static void writeFacingAngles(PacketWrapper wrapper, double x, double y, double z, double targetX, double targetY, double targetZ) { diff --git a/core/src/main/java/nl/matsv/viabackwards/api/entities/storage/EntityTracker.java b/core/src/main/java/nl/matsv/viabackwards/api/entities/storage/EntityTracker.java index f09ba3ce..92e0086b 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/entities/storage/EntityTracker.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/entities/storage/EntityTracker.java @@ -16,7 +16,6 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.EntityType; import java.util.Map; -import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; public class EntityTracker extends StoredObject { @@ -50,12 +49,8 @@ public class EntityTracker extends StoredObject { return storedEntity != null ? storedEntity.getType() : null; } - public Optional getEntity(int id) { - return Optional.ofNullable(entityMap.get(id)); - } - - public boolean containsEntity(int id) { - return entityMap.containsKey(id); + public StoredEntity getEntity(int id) { + return entityMap.get(id); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java index 653b0e07..0ef7f1c7 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java @@ -1,180 +1,79 @@ -/* - * Copyright (c) 2016 Matsv - * - * 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 nl.matsv.viabackwards.api.rewriters; -import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.BackwardsProtocol; -import nl.matsv.viabackwards.api.entities.meta.MetaHandlerEvent; -import nl.matsv.viabackwards.api.entities.meta.MetaHandlerSettings; import nl.matsv.viabackwards.api.entities.storage.EntityData; -import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.entities.storage.MetaStorage; -import nl.matsv.viabackwards.api.exceptions.RemovedValueException; -import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.entities.EntityType; -import us.myles.ViaVersion.api.entities.ObjectType; import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; -import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; -import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; -import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.packets.State; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; -import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; -import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Logger; -public abstract class EntityRewriter extends Rewriter { - private final Map entityTypes = new ConcurrentHashMap<>(); - private final Map objectTypes = new ConcurrentHashMap<>(); - private final List metaHandlers = new ArrayList<>(); - - private MetaType displayNameMetaType = MetaType1_9.String; - private int displayNameIndex = 2; - private boolean isDisplayNameJson; +public abstract class EntityRewriter extends EntityRewriterBase { protected EntityRewriter(T protocol) { - super(protocol); + super(protocol, MetaType1_14.OptChat, 2, true); } - protected EntityType getEntityType(UserConnection connection, int id) { - return getEntityTracker(connection).getEntityType(id); + protected EntityRewriter(T protocol, MetaType displayType) { + super(protocol, displayType, 2, true); } - protected void addTrackedEntity(PacketWrapper wrapper, int entityId, EntityType type) throws Exception { - getEntityTracker(wrapper.user()).trackEntityType(entityId, type); - } + public void registerSpawnTrackerWithData(int oldPacketId, int newPacketId, EntityType fallingBlockType, ItemRewriter itemRewriter) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, 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(wrapper -> { + int typeId = wrapper.get(Type.VAR_INT, 1); + EntityType entityType = getTypeFromId(typeId); + addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); - protected boolean hasData(EntityType type) { - return entityTypes.containsKey(type); - } - - protected EntityData getEntityData(EntityType type) { - return entityTypes.get(type); - } - - protected EntityData getObjectData(ObjectType type) { - return objectTypes.get(type); - } - - protected EntityData regEntType(EntityType oldEnt, EntityType replacement) { - return regEntType(oldEnt, (short) replacement.getId()); - } - - private EntityData regEntType(EntityType oldEnt, short replacementId) { - EntityData data = new EntityData(oldEnt.getId(), false, replacementId, -1); - entityTypes.put(oldEnt, data); - return data; - } - - protected EntityData regObjType(ObjectType oldObj, ObjectType replacement, int data) { - return regObjType(oldObj, (short) replacement.getId(), data); - } - - private EntityData regObjType(ObjectType oldObj, short replacementId, int data) { - EntityData entData = new EntityData(oldObj.getId(), true, replacementId, data); - objectTypes.put(oldObj, entData); - return entData; - } - - public MetaHandlerSettings registerMetaHandler() { - MetaHandlerSettings settings = new MetaHandlerSettings(); - metaHandlers.add(settings); - return settings; - } - - protected MetaStorage handleMeta(UserConnection user, int entityId, MetaStorage storage) throws Exception { - Optional optEntity = getEntityTracker(user).getEntity(entityId); - if (!optEntity.isPresent()) { - if (!Via.getConfig().isSuppressMetadataErrors()) { - ViaBackwards.getPlatform().getLogger().warning("Metadata for entity id: " + entityId + " not sent because the entity doesn't exist. " + storage); - } - throw CancelException.CACHED; - } - - EntityTracker.StoredEntity entity = optEntity.get(); - EntityType type = entity.getType(); - - List newList = new ArrayList<>(); - - for (MetaHandlerSettings settings : metaHandlers) { - List extraData = null; - for (Metadata md : storage.getMetaDataList()) { - Metadata nmd = md; - MetaHandlerEvent event = null; - try { - if (settings.isGucci(type, nmd)) { - event = new MetaHandlerEvent(user, entity, nmd.getId(), nmd, storage); - nmd = settings.getHandler().handle(event); - - if (event.getExtraData() != null) { - (extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData()); - event.clearExtraData(); - } + int oldTypeId = getOldEntityId(entityType.getId()); + if (typeId != oldTypeId) { + wrapper.set(Type.VAR_INT, 1, oldTypeId); } - if (nmd == null) { - throw RemovedValueException.EX; + if (entityType == fallingBlockType) { + int blockState = wrapper.get(Type.INT, 0); + wrapper.set(Type.INT, 0, itemRewriter.toClientRewriter.rewrite(blockState)); } + }); + } + }); + } - newList.add(nmd); - } catch (RemovedValueException e) { - // add the additionally created data here in case of an interruption - if (event != null && event.getExtraData() != null) { - (extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData()); + public void registerSpawnTracker(int oldPacketId, int newPacketId) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, 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(wrapper -> { + int typeId = wrapper.get(Type.VAR_INT, 1); + EntityType entityType = getTypeFromId(typeId); + addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); + + int oldTypeId = getOldEntityId(entityType.getId()); + if (typeId != oldTypeId) { + wrapper.set(Type.VAR_INT, 1, oldTypeId); } - } catch (Exception e) { - Logger log = ViaBackwards.getPlatform().getLogger(); - log.warning("Unable to handle metadata " + nmd); - log.warning("Full metadata list " + storage); - e.printStackTrace(); - } + }); } - - List newData = new ArrayList<>(newList); - if (extraData != null) { - newData.addAll(extraData); - } - - storage.setMetaDataList(newData); - newList.clear(); - } - - // Handle Entity Name - Metadata data = storage.get(displayNameIndex); - if (data != null) { - EntityData entityData = getEntityData(type); - if (entityData != null) { - if (entityData.getMobName() != null && - (data.getValue() == null || ((String) data.getValue()).isEmpty()) && - data.getMetaType().getTypeID() == displayNameMetaType.getTypeID()) { - String mobName = entityData.getMobName(); - if (isDisplayNameJson) { - mobName = ChatRewriter.legacyTextToJson(mobName); - } - data.setValue(mobName); - } - } - } - - return storage; + }); } /** @@ -213,171 +112,4 @@ public abstract class EntityRewriter extends Rewrit protected void registerMetadataRewriter(int oldPacketId, int newPacketId, Type> metaType) { registerMetadataRewriter(oldPacketId, newPacketId, null, metaType); } - - /** - * Helper method to handle a metadata list packet. - */ - protected void registerLegacyMetadataRewriter(int oldPacketId, int newPacketId, Type> oldMetaType, Type> newMetaType) { - getProtocol().registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - if (oldMetaType != null) { - map(oldMetaType, newMetaType); - } else { - map(newMetaType); - } - handler(wrapper -> { - List metadata = wrapper.get(newMetaType, 0); - wrapper.set(newMetaType, 0, - handleMeta(wrapper.user(), wrapper.get(Type.VAR_INT, 0), new MetaStorage(metadata)).getMetaDataList()); - }); - } - }); - } - - protected void registerLegacyMetadataRewriter(int oldPacketId, int newPacketId, Type> metaType) { - registerLegacyMetadataRewriter(oldPacketId, newPacketId, null, metaType); - } - - /** - * Helper method to handle player, painting, or xp orb trackers without meta changes. - */ - protected void registerExtraTracker(int packetId, EntityType entityType, Type intType) { - getProtocol().registerOutgoing(State.PLAY, packetId, packetId, new PacketRemapper() { - @Override - public void registerMap() { - map(intType); // 0 - Entity id - handler(wrapper -> addTrackedEntity(wrapper, (int) wrapper.get(intType, 0), entityType)); - } - }); - } - - protected void registerExtraTracker(int packetId, EntityType entityType) { - registerExtraTracker(packetId, entityType, Type.VAR_INT); - } - - /** - * Helper method to handle the destroy entities packet. - */ - protected void registerEntityDestroy(int oldPacketId, int newPacketId) { - getProtocol().registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT_ARRAY_PRIMITIVE); // 0 - Entity ids - handler(wrapper -> { - EntityTracker.ProtocolEntityTracker tracker = getEntityTracker(wrapper.user()); - for (int entity : wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0)) { - tracker.removeEntity(entity); - } - }); - } - }); - } - - protected void registerEntityDestroy(int packetId) { - registerEntityDestroy(packetId, packetId); - } - - protected PacketHandler getObjectTrackerHandler() { - return wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), getObjectTypeFromId(wrapper.get(Type.BYTE, 0))); - } - - protected PacketHandler getTrackerHandler(Type intType, int typeIndex) { - return wrapper -> { - Number id = (Number) wrapper.get(intType, typeIndex); - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), getTypeFromId(id.intValue())); - }; - } - - protected PacketHandler getTrackerHandler() { - return getTrackerHandler(Type.VAR_INT, 1); - } - - protected PacketHandler getTrackerHandler(EntityType entityType, Type intType) { - return wrapper -> addTrackedEntity(wrapper, (int) wrapper.get(intType, 0), entityType); - } - - protected PacketHandler getMobSpawnRewriter(Type> metaType) { - return wrapper -> { - int entityId = wrapper.get(Type.VAR_INT, 0); - EntityType type = getEntityType(wrapper.user(), entityId); - - MetaStorage storage = new MetaStorage(wrapper.get(metaType, 0)); - handleMeta(wrapper.user(), entityId, storage); - - EntityData entityData = getEntityData(type); - if (entityData != null) { - int replacementId = getOldEntityId(entityData.getReplacementId()); - wrapper.set(Type.VAR_INT, 1, replacementId); - if (entityData.hasBaseMeta()) { - entityData.getDefaultMeta().handle(storage); - } - } - - // Rewrite Metadata - wrapper.set(metaType, 0, storage.getMetaDataList()); - }; - } - - protected PacketHandler getTrackerAndMetaHandler(Type> metaType, EntityType entityType) { - return wrapper -> { - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); - - List metaDataList = handleMeta(wrapper.user(), wrapper.get(Type.VAR_INT, 0), - new MetaStorage(wrapper.get(metaType, 0))).getMetaDataList(); - wrapper.set(metaType, 0, metaDataList); - }; - } - - protected PacketHandler getDimensionHandler(int index) { - return wrapper -> { - ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - int dimensionId = wrapper.get(Type.INT, index); - clientWorld.setEnvironment(dimensionId); - }; - } - - public EntityTracker.ProtocolEntityTracker getEntityTracker(UserConnection user) { - return user.get(EntityTracker.class).get(getProtocol()); - } - - protected MetaType getDisplayNameMetaType() { - return displayNameMetaType; - } - - protected void setDisplayNameMetaType(MetaType displayNameMetaType) { - this.displayNameMetaType = displayNameMetaType; - } - - protected int getDisplayNameIndex() { - return displayNameIndex; - } - - protected void setDisplayNameIndex(int displayNameIndex) { - this.displayNameIndex = displayNameIndex; - } - - protected boolean isDisplayNameJson() { - return isDisplayNameJson; - } - - protected void setDisplayNameJson(boolean displayNameJson) { - isDisplayNameJson = displayNameJson; - } - - protected abstract EntityType getTypeFromId(int typeId); - - protected EntityType getObjectTypeFromId(int typeId) { - return getTypeFromId(typeId); - } - - // Only needs to be overriden when getMobSpawnTracker is used - protected Optional getOptOldEntityId(int newId) { - return Optional.of(newId); - } - - protected int getOldEntityId(int newId) { - return newId; - } } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriterBase.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriterBase.java new file mode 100644 index 00000000..f9eb520b --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriterBase.java @@ -0,0 +1,250 @@ +package nl.matsv.viabackwards.api.rewriters; + +import nl.matsv.viabackwards.ViaBackwards; +import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.entities.meta.MetaHandlerEvent; +import nl.matsv.viabackwards.api.entities.meta.MetaHandlerSettings; +import nl.matsv.viabackwards.api.entities.storage.EntityData; +import nl.matsv.viabackwards.api.entities.storage.EntityTracker; +import nl.matsv.viabackwards.api.entities.storage.MetaStorage; +import nl.matsv.viabackwards.api.exceptions.RemovedValueException; +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.entities.EntityType; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.exception.CancelException; +import us.myles.ViaVersion.packets.State; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; +import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + +public abstract class EntityRewriterBase extends Rewriter { + private final Map entityTypes = new ConcurrentHashMap<>(); + private final List metaHandlers = new ArrayList<>(); + private final MetaType displayNameMetaType; + private final int displayNameIndex; + private final boolean isDisplayNameJson; + + protected EntityRewriterBase(T protocol) { + this(protocol, MetaType1_9.String, 2, false); + } + + protected EntityRewriterBase(T protocol, MetaType displayNameMetaType, int displayNameIndex, boolean isDisplayNameJson) { + super(protocol); + this.displayNameMetaType = displayNameMetaType; + this.displayNameIndex = displayNameIndex; + this.isDisplayNameJson = isDisplayNameJson; + } + + protected EntityType getEntityType(UserConnection connection, int id) { + return getEntityTracker(connection).getEntityType(id); + } + + protected void addTrackedEntity(PacketWrapper wrapper, int entityId, EntityType type) throws Exception { + getEntityTracker(wrapper.user()).trackEntityType(entityId, type); + } + + protected boolean hasData(EntityType type) { + return entityTypes.containsKey(type); + } + + protected EntityData getEntityData(EntityType type) { + return entityTypes.get(type); + } + + protected EntityData mapEntity(EntityType oldType, EntityType replacement) { + EntityData data = new EntityData(oldType.getId(), false, replacement.getId(), -1); + entityTypes.put(oldType, data); + return data; + } + + public MetaHandlerSettings registerMetaHandler() { + MetaHandlerSettings settings = new MetaHandlerSettings(); + metaHandlers.add(settings); + return settings; + } + + protected MetaStorage handleMeta(UserConnection user, int entityId, MetaStorage storage) throws Exception { + EntityTracker.StoredEntity storedEntity = getEntityTracker(user).getEntity(entityId); + if (storedEntity == null) { + if (!Via.getConfig().isSuppressMetadataErrors()) { + ViaBackwards.getPlatform().getLogger().warning("Metadata for entity id: " + entityId + " not sent because the entity doesn't exist. " + storage); + } + throw CancelException.CACHED; + } + + EntityType type = storedEntity.getType(); + List newList = new ArrayList<>(); + for (MetaHandlerSettings settings : metaHandlers) { + List extraData = null; + for (Metadata md : storage.getMetaDataList()) { + Metadata nmd = md; + MetaHandlerEvent event = null; + try { + if (settings.isGucci(type, nmd)) { + event = new MetaHandlerEvent(user, storedEntity, nmd.getId(), nmd, storage); + nmd = settings.getHandler().handle(event); + + if (event.getExtraData() != null) { + (extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData()); + event.clearExtraData(); + } + } + + if (nmd == null) { + throw RemovedValueException.EX; + } + + newList.add(nmd); + } catch (RemovedValueException e) { + // add the additionally created data here in case of an interruption + if (event != null && event.getExtraData() != null) { + (extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData()); + } + } catch (Exception e) { + Logger log = ViaBackwards.getPlatform().getLogger(); + log.warning("Unable to handle metadata " + nmd); + log.warning("Full metadata list " + storage); + e.printStackTrace(); + } + } + + List newData = new ArrayList<>(newList); + if (extraData != null) { + newData.addAll(extraData); + } + + storage.setMetaDataList(newData); + newList.clear(); + } + + // Handle Entity Name + Metadata data = storage.get(displayNameIndex); + if (data != null) { + EntityData entityData = getEntityData(type); + if (entityData != null) { + if (entityData.getMobName() != null && + (data.getValue() == null || ((String) data.getValue()).isEmpty()) && + data.getMetaType().getTypeID() == displayNameMetaType.getTypeID()) { + String mobName = entityData.getMobName(); + if (isDisplayNameJson) { + mobName = ChatRewriter.legacyTextToJson(mobName); + } + data.setValue(mobName); + } + } + } + + return storage; + } + + public void registerRespawn(int oldPacketId, int newPacketId) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); + handler(wrapper -> { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + clientWorld.setEnvironment(wrapper.get(Type.INT, 0)); + }); + } + }); + } + + public void registerJoinGame(int oldPacketId, int newPacketId, EntityType playerType) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Entity ID + map(Type.UNSIGNED_BYTE); // 1 - Gamemode + map(Type.INT); // 2 - Dimension + handler(wrapper -> { + ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); + clientChunks.setEnvironment(wrapper.get(Type.INT, 1)); + getEntityTracker(wrapper.user()).trackEntityType(wrapper.get(Type.INT, 0), playerType); + }); + } + }); + } + + /** + * Helper method to handle player, painting, or xp orb trackers without meta changes. + */ + protected void registerExtraTracker(int packetId, EntityType entityType, Type intType) { + getProtocol().registerOutgoing(State.PLAY, packetId, packetId, new PacketRemapper() { + @Override + public void registerMap() { + map(intType); // 0 - Entity id + handler(wrapper -> addTrackedEntity(wrapper, (int) wrapper.get(intType, 0), entityType)); + } + }); + } + + protected void registerExtraTracker(int packetId, EntityType entityType) { + registerExtraTracker(packetId, entityType, Type.VAR_INT); + } + + protected void registerEntityDestroy(int oldPacketId, int newPacketId) { + getProtocol().registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT_ARRAY_PRIMITIVE); // 0 - Entity ids + handler(wrapper -> { + EntityTracker.ProtocolEntityTracker tracker = getEntityTracker(wrapper.user()); + for (int entity : wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0)) { + tracker.removeEntity(entity); + } + }); + } + }); + } + + protected void registerEntityDestroy(int packetId) { + registerEntityDestroy(packetId, packetId); + } + + // ONLY TRACKS, DOESN'T REWRITE IDS + protected PacketHandler getTrackerHandler(Type intType, int typeIndex) { + return wrapper -> { + Number id = (Number) wrapper.get(intType, typeIndex); + addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), getTypeFromId(id.intValue())); + }; + } + + protected PacketHandler getTrackerHandler() { + return getTrackerHandler(Type.VAR_INT, 1); + } + + protected PacketHandler getTrackerHandler(EntityType entityType, Type intType) { + return wrapper -> addTrackedEntity(wrapper, (int) wrapper.get(intType, 0), entityType); + } + + protected PacketHandler getDimensionHandler(int index) { + return wrapper -> { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + int dimensionId = wrapper.get(Type.INT, index); + clientWorld.setEnvironment(dimensionId); + }; + } + + public EntityTracker.ProtocolEntityTracker getEntityTracker(UserConnection user) { + return user.get(EntityTracker.class).get(getProtocol()); + } + + protected abstract EntityType getTypeFromId(int typeId); + + protected int getOldEntityId(int newId) { + return newId; + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriterBase.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriterBase.java index 23271336..c7335209 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriterBase.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/ItemRewriterBase.java @@ -11,15 +11,15 @@ import us.myles.viaversion.libs.opennbt.tag.builtin.ShortTag; public abstract class ItemRewriterBase extends Rewriter { protected static final CompoundTagConverter CONVERTER = new CompoundTagConverter(); - protected final IdRewriteFunction oldRewriter; - protected final IdRewriteFunction newRewriter; + protected final IdRewriteFunction toClientRewriter; + protected final IdRewriteFunction toServerRewriter; protected final String nbtTagName; protected final boolean jsonNameFormat; - protected ItemRewriterBase(T protocol, IdRewriteFunction oldRewriter, IdRewriteFunction newRewriter, boolean jsonNameFormat) { + protected ItemRewriterBase(T protocol, IdRewriteFunction toClientRewriter, IdRewriteFunction toServerRewriter, boolean jsonNameFormat) { super(protocol); - this.oldRewriter = oldRewriter; - this.newRewriter = newRewriter; + this.toClientRewriter = toClientRewriter; + this.toServerRewriter = toServerRewriter; this.jsonNameFormat = jsonNameFormat; nbtTagName = "ViaBackwards|" + protocol.getClass().getSimpleName(); } @@ -30,8 +30,8 @@ public abstract class ItemRewriterBase extends Rewr public Item handleItemToClient(Item item) { if (item == null) return null; - if (oldRewriter != null) { - item.setIdentifier(oldRewriter.rewrite(item.getIdentifier())); + if (toClientRewriter != null) { + item.setIdentifier(toClientRewriter.rewrite(item.getIdentifier())); } return item; } @@ -41,8 +41,8 @@ public abstract class ItemRewriterBase extends Rewr CompoundTag tag = item.getTag(); if (tag == null) { - if (newRewriter != null) { - item.setIdentifier(newRewriter.rewrite(item.getIdentifier())); + if (toServerRewriter != null) { + item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier())); } return item; } @@ -64,8 +64,8 @@ public abstract class ItemRewriterBase extends Rewr tag.remove(nbtTagName); } else { // Rewrite id normally - if (newRewriter != null) { - item.setIdentifier(newRewriter.rewrite(item.getIdentifier())); + if (toServerRewriter != null) { + item.setIdentifier(toServerRewriter.rewrite(item.getIdentifier())); } } return item; diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacyEntityRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacyEntityRewriter.java new file mode 100644 index 00000000..78e2f4d0 --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/LegacyEntityRewriter.java @@ -0,0 +1,102 @@ +package nl.matsv.viabackwards.api.rewriters; + +import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.entities.storage.EntityData; +import nl.matsv.viabackwards.api.entities.storage.MetaStorage; +import us.myles.ViaVersion.api.entities.EntityType; +import us.myles.ViaVersion.api.entities.ObjectType; +import us.myles.ViaVersion.api.minecraft.metadata.MetaType; +import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public abstract class LegacyEntityRewriter extends EntityRewriterBase { + private final Map objectTypes = new ConcurrentHashMap<>(); + + protected LegacyEntityRewriter(T protocol) { + super(protocol); + } + + protected LegacyEntityRewriter(T protocol, MetaType displayType, boolean isDisplayJson) { + super(protocol, displayType, 2, isDisplayJson); + } + + protected EntityData mapObjectType(ObjectType oldObjectType, ObjectType replacement, int data) { + EntityData entData = new EntityData(oldObjectType.getId(), true, replacement.getId(), data); + objectTypes.put(oldObjectType, entData); + return entData; + } + + protected EntityData getObjectData(ObjectType type) { + return objectTypes.get(type); + } + + protected void registerMetadataRewriter(int oldPacketId, int newPacketId, Type> oldMetaType, Type> newMetaType) { + getProtocol().registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.VAR_INT); // 0 - Entity ID + if (oldMetaType != null) { + map(oldMetaType, newMetaType); + } else { + map(newMetaType); + } + handler(wrapper -> { + List metadata = wrapper.get(newMetaType, 0); + wrapper.set(newMetaType, 0, + handleMeta(wrapper.user(), wrapper.get(Type.VAR_INT, 0), new MetaStorage(metadata)).getMetaDataList()); + }); + } + }); + } + + protected void registerMetadataRewriter(int oldPacketId, int newPacketId, Type> metaType) { + registerMetadataRewriter(oldPacketId, newPacketId, null, metaType); + } + + protected PacketHandler getMobSpawnRewriter(Type> metaType) { + return wrapper -> { + int entityId = wrapper.get(Type.VAR_INT, 0); + EntityType type = getEntityType(wrapper.user(), entityId); + + MetaStorage storage = new MetaStorage(wrapper.get(metaType, 0)); + handleMeta(wrapper.user(), entityId, storage); + + EntityData entityData = getEntityData(type); + if (entityData != null) { + int replacementId = getOldEntityId(entityData.getReplacementId()); + wrapper.set(Type.VAR_INT, 1, replacementId); + if (entityData.hasBaseMeta()) { + entityData.getDefaultMeta().handle(storage); + } + } + + // Rewrite Metadata + wrapper.set(metaType, 0, storage.getMetaDataList()); + }; + } + + protected PacketHandler getObjectTrackerHandler() { + return wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), getObjectTypeFromId(wrapper.get(Type.BYTE, 0))); + } + + protected PacketHandler getTrackerAndMetaHandler(Type> metaType, EntityType entityType) { + return wrapper -> { + addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); + + List metaDataList = handleMeta(wrapper.user(), wrapper.get(Type.VAR_INT, 0), + new MetaStorage(wrapper.get(metaType, 0))).getMetaDataList(); + wrapper.set(metaType, 0, metaDataList); + }; + } + + protected EntityType getObjectTypeFromId(int typeId) { + return getTypeFromId(typeId); + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/RecipeRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/RecipeRewriter.java index c7408b67..585ad88a 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/RecipeRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/RecipeRewriter.java @@ -1,6 +1,9 @@ package nl.matsv.viabackwards.api.rewriters; import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; public abstract class RecipeRewriter { @@ -11,4 +14,20 @@ public abstract class RecipeRewriter { } public abstract void handle(PacketWrapper wrapper, String type) throws Exception; + + public void registerDefaultHandler(int oldId, int newId) { + rewriter.getProtocol().registerOutgoing(State.PLAY, oldId, newId, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + int size = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < size; i++) { + String type = wrapper.passthrough(Type.STRING).replace("minecraft:", ""); + String id = wrapper.passthrough(Type.STRING); // Recipe Identifier + handle(wrapper, type); + } + }); + } + }); + } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java index 6c13fc08..0f8e22e8 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets1_11.java @@ -409,8 +409,8 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter optEntity = entTracker.getEntity(tracker.getEntityId()); - return optEntity.isPresent() && optEntity.get().getType().is(Entity1_11Types.EntityType.LIAMA); + EntityTracker.StoredEntity storedEntity = entTracker.getEntity(tracker.getEntityId()); + return storedEntity != null && storedEntity.getType().is(Entity1_11Types.EntityType.LIAMA); } return false; } @@ -419,9 +419,9 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter optEntity = entTracker.getEntity(tracker.getEntityId()); - if (optEntity.isPresent()) - return Optional.of(optEntity.get().get(ChestedHorseStorage.class)); + EntityTracker.StoredEntity storedEntity = entTracker.getEntity(tracker.getEntityId()); + if (storedEntity != null) + return Optional.of(storedEntity.get(ChestedHorseStorage.class)); } return Optional.empty(); } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java index 641cc5a4..ebf492a8 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java @@ -14,7 +14,7 @@ import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.entities.storage.EntityData; import nl.matsv.viabackwards.api.entities.storage.MetaStorage; import nl.matsv.viabackwards.api.exceptions.RemovedValueException; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacyEntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_10to1_11.PotionSplashHandler; import nl.matsv.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11; import nl.matsv.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage; @@ -34,7 +34,7 @@ import us.myles.ViaVersion.packets.State; import java.util.Optional; -public class EntityPackets1_11 extends EntityRewriter { +public class EntityPackets1_11 extends LegacyEntityRewriter { public EntityPackets1_11(Protocol1_10To1_11 protocol) { super(protocol); @@ -229,7 +229,7 @@ public class EntityPackets1_11 extends EntityRewriter { registerEntityDestroy(0x30); // Metadata packet - registerLegacyMetadataRewriter(0x39, 0x39, Types1_9.METADATA_LIST); + registerMetadataRewriter(0x39, 0x39, Types1_9.METADATA_LIST); // Entity Status protocol.registerOutgoing(State.PLAY, 0x1B, 0x1B, new PacketRemapper() { @@ -258,30 +258,30 @@ public class EntityPackets1_11 extends EntityRewriter { @Override protected void registerRewrites() { // Guardian - regEntType(Entity1_11Types.EntityType.ELDER_GUARDIAN, Entity1_11Types.EntityType.GUARDIAN); + mapEntity(Entity1_11Types.EntityType.ELDER_GUARDIAN, Entity1_11Types.EntityType.GUARDIAN); // Skeletons - regEntType(Entity1_11Types.EntityType.WITHER_SKELETON, Entity1_11Types.EntityType.SKELETON).spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(1))); - regEntType(Entity1_11Types.EntityType.STRAY, Entity1_11Types.EntityType.SKELETON).spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(2))); + mapEntity(Entity1_11Types.EntityType.WITHER_SKELETON, Entity1_11Types.EntityType.SKELETON).spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(1))); + mapEntity(Entity1_11Types.EntityType.STRAY, Entity1_11Types.EntityType.SKELETON).spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(2))); // Zombies - regEntType(Entity1_11Types.EntityType.HUSK, Entity1_11Types.EntityType.ZOMBIE).spawnMetadata(storage -> handleZombieType(storage, 6)); - regEntType(Entity1_11Types.EntityType.ZOMBIE_VILLAGER, Entity1_11Types.EntityType.ZOMBIE).spawnMetadata(storage -> handleZombieType(storage, 1)); + mapEntity(Entity1_11Types.EntityType.HUSK, Entity1_11Types.EntityType.ZOMBIE).spawnMetadata(storage -> handleZombieType(storage, 6)); + mapEntity(Entity1_11Types.EntityType.ZOMBIE_VILLAGER, Entity1_11Types.EntityType.ZOMBIE).spawnMetadata(storage -> handleZombieType(storage, 1)); // Horses - regEntType(Entity1_11Types.EntityType.HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(0))); // Nob able to ride the horse without having the MetaType sent. - regEntType(Entity1_11Types.EntityType.DONKEY, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(1))); - regEntType(Entity1_11Types.EntityType.MULE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(2))); - regEntType(Entity1_11Types.EntityType.SKELETON_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(4))); - regEntType(Entity1_11Types.EntityType.ZOMBIE_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(3))); + mapEntity(Entity1_11Types.EntityType.HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(0))); // Nob able to ride the horse without having the MetaType sent. + mapEntity(Entity1_11Types.EntityType.DONKEY, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(1))); + mapEntity(Entity1_11Types.EntityType.MULE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(2))); + mapEntity(Entity1_11Types.EntityType.SKELETON_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(4))); + mapEntity(Entity1_11Types.EntityType.ZOMBIE_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(3))); // New mobs - regEntType(Entity1_11Types.EntityType.EVOCATION_FANGS, Entity1_11Types.EntityType.SHULKER); - regEntType(Entity1_11Types.EntityType.EVOCATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).mobName("Evoker"); - regEntType(Entity1_11Types.EntityType.VEX, Entity1_11Types.EntityType.BAT).mobName("Vex"); - regEntType(Entity1_11Types.EntityType.VINDICATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).mobName("Vindicator").spawnMetadata(storage -> storage.add(new Metadata(13, MetaType1_9.VarInt, 4))); // Base Profession - regEntType(Entity1_11Types.EntityType.LIAMA, Entity1_11Types.EntityType.HORSE).mobName("Llama").spawnMetadata(storage -> storage.add(getHorseMetaType(1))); - regEntType(Entity1_11Types.EntityType.LIAMA_SPIT, Entity1_11Types.EntityType.SNOWBALL); + mapEntity(Entity1_11Types.EntityType.EVOCATION_FANGS, Entity1_11Types.EntityType.SHULKER); + mapEntity(Entity1_11Types.EntityType.EVOCATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).mobName("Evoker"); + mapEntity(Entity1_11Types.EntityType.VEX, Entity1_11Types.EntityType.BAT).mobName("Vex"); + mapEntity(Entity1_11Types.EntityType.VINDICATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).mobName("Vindicator").spawnMetadata(storage -> storage.add(new Metadata(13, MetaType1_9.VarInt, 4))); // Base Profession + mapEntity(Entity1_11Types.EntityType.LIAMA, Entity1_11Types.EntityType.HORSE).mobName("Llama").spawnMetadata(storage -> storage.add(getHorseMetaType(1))); + mapEntity(Entity1_11Types.EntityType.LIAMA_SPIT, Entity1_11Types.EntityType.SNOWBALL); - regObjType(Entity1_11Types.ObjectType.LIAMA_SPIT, Entity1_11Types.ObjectType.SNOWBALL, -1); + mapObjectType(Entity1_11Types.ObjectType.LIAMA_SPIT, Entity1_11Types.ObjectType.SNOWBALL, -1); // Replace with endertorchthingies - regObjType(Entity1_11Types.ObjectType.EVOCATION_FANGS, Entity1_11Types.ObjectType.FALLING_BLOCK, 198 | 1 << 12); + mapObjectType(Entity1_11Types.ObjectType.EVOCATION_FANGS, Entity1_11Types.ObjectType.FALLING_BLOCK, 198 | 1 << 12); // Handle ElderGuardian & target metadata registerMetaHandler().filter(Entity1_11Types.EntityType.GUARDIAN, true, 12).handle(e -> { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java index 9a6ca644..600b44dc 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java @@ -13,7 +13,7 @@ package nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.packets; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.entities.storage.EntityData; import nl.matsv.viabackwards.api.exceptions.RemovedValueException; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacyEntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12; import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.data.ParrotStorage; import nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker; @@ -33,7 +33,7 @@ import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; import java.util.Optional; -public class EntityPackets1_12 extends EntityRewriter { +public class EntityPackets1_12 extends LegacyEntityRewriter { public EntityPackets1_12(Protocol1_11_1To1_12 protocol) { super(protocol); @@ -200,7 +200,7 @@ public class EntityPackets1_12 extends EntityRewriter { registerEntityDestroy(0x31, 0x30); // Metadata packet - registerLegacyMetadataRewriter(0x3B, 0x39, Types1_12.METADATA_LIST); + registerMetadataRewriter(0x3B, 0x39, Types1_12.METADATA_LIST); // Entity Properties protocol.registerOutgoing(State.PLAY, 0x4D, 0x4A, new PacketRemapper() { @@ -245,8 +245,8 @@ public class EntityPackets1_12 extends EntityRewriter { @Override protected void registerRewrites() { - regEntType(Entity1_12Types.EntityType.PARROT, Entity1_12Types.EntityType.BAT).mobName("Parrot").spawnMetadata(storage -> storage.add(new Metadata(12, MetaType1_12.Byte, (byte) 0x00))); - regEntType(Entity1_12Types.EntityType.ILLUSION_ILLAGER, Entity1_12Types.EntityType.EVOCATION_ILLAGER).mobName("Illusioner"); + mapEntity(Entity1_12Types.EntityType.PARROT, Entity1_12Types.EntityType.BAT).mobName("Parrot").spawnMetadata(storage -> storage.add(new Metadata(12, MetaType1_12.Byte, (byte) 0x00))); + mapEntity(Entity1_12Types.EntityType.ILLUSION_ILLAGER, Entity1_12Types.EntityType.EVOCATION_ILLAGER).mobName("Illusioner"); // Handle Illager registerMetaHandler().filter(Entity1_12Types.EntityType.EVOCATION_ILLAGER, true, 12).removed(); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11to1_11_1/packets/EntityPackets1_11_1.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11to1_11_1/packets/EntityPackets1_11_1.java index b90097d7..3b93325d 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11to1_11_1/packets/EntityPackets1_11_1.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11to1_11_1/packets/EntityPackets1_11_1.java @@ -12,7 +12,7 @@ package nl.matsv.viabackwards.protocol.protocol1_11to1_11_1.packets; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.entities.storage.EntityData; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacyEntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_11to1_11_1.Protocol1_11To1_11_1; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; @@ -26,7 +26,7 @@ import us.myles.ViaVersion.packets.State; import java.util.Optional; -public class EntityPackets1_11_1 extends EntityRewriter { +public class EntityPackets1_11_1 extends LegacyEntityRewriter { public EntityPackets1_11_1(Protocol1_11To1_11_1 protocol) { super(protocol); @@ -151,7 +151,7 @@ public class EntityPackets1_11_1 extends EntityRewriter { registerEntityDestroy(0x30); // Metadata packet - registerLegacyMetadataRewriter(0x39, 0x39, Types1_9.METADATA_LIST); + registerMetadataRewriter(0x39, 0x39, Types1_9.METADATA_LIST); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java index ece106d0..457dceac 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java @@ -3,7 +3,7 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.entities.storage.EntityPositionHandler; import nl.matsv.viabackwards.api.exceptions.RemovedValueException; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacyEntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.EntityTypeMapping; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.PaintingMapping; @@ -26,7 +26,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import java.util.Optional; -public class EntityPackets1_13 extends EntityRewriter { +public class EntityPackets1_13 extends LegacyEntityRewriter { public EntityPackets1_13(Protocol1_12_2To1_13 protocol) { super(protocol); @@ -223,7 +223,7 @@ public class EntityPackets1_13 extends EntityRewriter { registerEntityDestroy(0x35, 0x32); // Entity Metadata packet - registerLegacyMetadataRewriter(0x3F, 0x3C, Types1_13.METADATA_LIST, Types1_12.METADATA_LIST); + registerMetadataRewriter(0x3F, 0x3C, Types1_13.METADATA_LIST, Types1_12.METADATA_LIST); // Face Player (new packet) protocol.out(State.PLAY, 0x31, -1, new PacketRemapper() { @@ -287,25 +287,25 @@ public class EntityPackets1_13 extends EntityRewriter { @Override protected void registerRewrites() { // Rewrite new Entity 'drowned' - regEntType(Entity1_13Types.EntityType.DROWNED, Entity1_13Types.EntityType.ZOMBIE_VILLAGER).mobName("Drowned"); + mapEntity(Entity1_13Types.EntityType.DROWNED, Entity1_13Types.EntityType.ZOMBIE_VILLAGER).mobName("Drowned"); // Fishy - regEntType(Entity1_13Types.EntityType.COD_MOB, Entity1_13Types.EntityType.SQUID).mobName("Cod"); - regEntType(Entity1_13Types.EntityType.SALMON_MOB, Entity1_13Types.EntityType.SQUID).mobName("Salmon"); - regEntType(Entity1_13Types.EntityType.PUFFER_FISH, Entity1_13Types.EntityType.SQUID).mobName("Puffer Fish"); - regEntType(Entity1_13Types.EntityType.TROPICAL_FISH, Entity1_13Types.EntityType.SQUID).mobName("Tropical Fish"); + mapEntity(Entity1_13Types.EntityType.COD_MOB, Entity1_13Types.EntityType.SQUID).mobName("Cod"); + mapEntity(Entity1_13Types.EntityType.SALMON_MOB, Entity1_13Types.EntityType.SQUID).mobName("Salmon"); + mapEntity(Entity1_13Types.EntityType.PUFFER_FISH, Entity1_13Types.EntityType.SQUID).mobName("Puffer Fish"); + mapEntity(Entity1_13Types.EntityType.TROPICAL_FISH, Entity1_13Types.EntityType.SQUID).mobName("Tropical Fish"); // Phantom - regEntType(Entity1_13Types.EntityType.PHANTOM, Entity1_13Types.EntityType.PARROT).mobName("Phantom").spawnMetadata(storage -> { + mapEntity(Entity1_13Types.EntityType.PHANTOM, Entity1_13Types.EntityType.PARROT).mobName("Phantom").spawnMetadata(storage -> { // The phantom is grey/blue so let's do yellow/blue storage.add(new Metadata(15, MetaType1_12.VarInt, 3)); }); // Dolphin - regEntType(Entity1_13Types.EntityType.DOLPHIN, Entity1_13Types.EntityType.SQUID).mobName("Dolphin"); + mapEntity(Entity1_13Types.EntityType.DOLPHIN, Entity1_13Types.EntityType.SQUID).mobName("Dolphin"); // Turtle - regEntType(Entity1_13Types.EntityType.TURTLE, Entity1_13Types.EntityType.OCELOT).mobName("Turtle"); + mapEntity(Entity1_13Types.EntityType.TURTLE, Entity1_13Types.EntityType.OCELOT).mobName("Turtle"); // Rewrite Meta types registerMetaHandler().handle(e -> { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java index 7a33d50e..ded1308c 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/BlockItemPackets1_14.java @@ -53,6 +53,14 @@ public class BlockItemPackets1_14 extends nl.matsv.viabackwards.api.rewriters.It @Override protected void registerPackets() { + // Edit Book + protocol.registerIncoming(State.PLAY, 0x0C, 0x0B, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); + } + }); + // Open window protocol.registerOutgoing(State.PLAY, 0x2E, 0x14, new PacketRemapper() { @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java index 52b1b559..f4bb92eb 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/EntityPackets1_14.java @@ -5,7 +5,7 @@ import nl.matsv.viabackwards.api.entities.meta.MetaHandler; import nl.matsv.viabackwards.api.entities.storage.EntityData; import nl.matsv.viabackwards.api.entities.storage.EntityPositionHandler; import nl.matsv.viabackwards.api.exceptions.RemovedValueException; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacyEntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.data.EntityTypeMapping; import nl.matsv.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage; @@ -29,12 +29,12 @@ import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; -public class EntityPackets1_14 extends EntityRewriter { +public class EntityPackets1_14 extends LegacyEntityRewriter { private EntityPositionHandler positionHandler; public EntityPackets1_14(Protocol1_13_2To1_14 protocol) { - super(protocol); + super(protocol, MetaType1_13_2.OptChat, true); } @Override @@ -213,12 +213,7 @@ public class EntityPackets1_14 extends EntityRewriter { map(Type.DOUBLE); // Needs to be mapped for the position cache map(Type.DOUBLE); map(Type.DOUBLE); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EntityType.XP_ORB); - } - }); + handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EntityType.XP_ORB)); } }); @@ -231,12 +226,7 @@ public class EntityPackets1_14 extends EntityRewriter { map(Type.DOUBLE); // Needs to be mapped for the position cache map(Type.DOUBLE); map(Type.DOUBLE); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EntityType.LIGHTNING_BOLT); - } - }); + handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EntityType.LIGHTNING_BOLT)); } }); @@ -251,12 +241,7 @@ public class EntityPackets1_14 extends EntityRewriter { map(Type.BYTE); // Track entity - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EntityType.PAINTING); - } - }); + handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EntityType.PAINTING)); } }); @@ -282,7 +267,7 @@ public class EntityPackets1_14 extends EntityRewriter { registerEntityDestroy(0x37, 0x35); // Entity Metadata packet - registerLegacyMetadataRewriter(0x43, 0x3F, Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST); + registerMetadataRewriter(0x43, 0x3F, Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST); // Join game protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() { @@ -330,16 +315,13 @@ public class EntityPackets1_14 extends EntityRewriter { @Override protected void registerRewrites() { - setDisplayNameJson(true); - setDisplayNameMetaType(MetaType1_13_2.OptChat); - - regEntType(Entity1_14Types.EntityType.CAT, Entity1_14Types.EntityType.OCELOT).mobName("Cat"); - regEntType(Entity1_14Types.EntityType.TRADER_LLAMA, Entity1_14Types.EntityType.LLAMA).mobName("Trader Llama"); - regEntType(Entity1_14Types.EntityType.FOX, Entity1_14Types.EntityType.WOLF).mobName("Fox"); - regEntType(Entity1_14Types.EntityType.PANDA, Entity1_14Types.EntityType.POLAR_BEAR).mobName("Panda"); - regEntType(Entity1_14Types.EntityType.PILLAGER, Entity1_14Types.EntityType.VILLAGER).mobName("Pillager"); - regEntType(Entity1_14Types.EntityType.WANDERING_TRADER, Entity1_14Types.EntityType.VILLAGER).mobName("Wandering Trader"); - regEntType(Entity1_14Types.EntityType.RAVAGER, Entity1_14Types.EntityType.COW).mobName("Ravager"); + mapEntity(Entity1_14Types.EntityType.CAT, Entity1_14Types.EntityType.OCELOT).mobName("Cat"); + mapEntity(Entity1_14Types.EntityType.TRADER_LLAMA, Entity1_14Types.EntityType.LLAMA).mobName("Trader Llama"); + mapEntity(Entity1_14Types.EntityType.FOX, Entity1_14Types.EntityType.WOLF).mobName("Fox"); + mapEntity(Entity1_14Types.EntityType.PANDA, Entity1_14Types.EntityType.POLAR_BEAR).mobName("Panda"); + mapEntity(Entity1_14Types.EntityType.PILLAGER, Entity1_14Types.EntityType.VILLAGER).mobName("Pillager"); + mapEntity(Entity1_14Types.EntityType.WANDERING_TRADER, Entity1_14Types.EntityType.VILLAGER).mobName("Wandering Trader"); + mapEntity(Entity1_14Types.EntityType.RAVAGER, Entity1_14Types.EntityType.COW).mobName("Ravager"); registerMetaHandler().handle(e -> { Metadata meta = e.getData(); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java index 7e8bf127..9de30a3f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/PlayerPackets1_14.java @@ -17,7 +17,6 @@ public class PlayerPackets1_14 extends Rewriter { @Override protected void registerPackets() { - // Server Difficulty protocol.registerOutgoing(State.PLAY, 0x0D, 0x0D, new PacketRemapper() { @Override @@ -44,19 +43,6 @@ public class PlayerPackets1_14 extends Rewriter { } }); - // Edit Book - protocol.registerIncoming(State.PLAY, 0x0C, 0x0B, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - getProtocol().getBlockItemPackets().handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); - } - }); - } - }); - // Player Digging protocol.registerIncoming(State.PLAY, 0x1A, 0x18, new PacketRemapper() { @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java index 398186b0..7a833739 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java @@ -12,8 +12,6 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; -import java.util.Optional; - public class SoundPackets1_14 extends Rewriter { public SoundPackets1_14(Protocol1_13_2To1_14 protocol) { @@ -57,18 +55,18 @@ public class SoundPackets1_14 extends Rewriter { int category = wrapper.read(Type.VAR_INT); int entityId = wrapper.read(Type.VAR_INT); - Optional optEntity = wrapper.user().get(EntityTracker.class).get(protocol).getEntity(entityId); - EntityPositionStorage1_14 storedEntity; - if (!optEntity.isPresent() || (storedEntity = optEntity.get().get(EntityPositionStorage1_14.class)) == null) { + EntityTracker.StoredEntity storedEntity = wrapper.user().get(EntityTracker.class).get(protocol).getEntity(entityId); + EntityPositionStorage1_14 entityStorage; + if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage1_14.class)) == null) { ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId); return; } float volume = wrapper.read(Type.FLOAT); float pitch = wrapper.read(Type.FLOAT); - int x = (int) (storedEntity.getX() * 8D); - int y = (int) (storedEntity.getY() * 8D); - int z = (int) (storedEntity.getZ() * 8D); + int x = (int) (entityStorage.getX() * 8D); + int y = (int) (entityStorage.getY() * 8D); + int z = (int) (entityStorage.getZ() * 8D); PacketWrapper soundPacket = wrapper.create(0x4D); soundPacket.write(Type.VAR_INT, newId); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/packets/EntityPackets1_13_1.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/packets/EntityPackets1_13_1.java index 42fcf572..cdc4bfb4 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/packets/EntityPackets1_13_1.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_13to1_13_1/packets/EntityPackets1_13_1.java @@ -2,7 +2,7 @@ package nl.matsv.viabackwards.protocol.protocol1_13to1_13_1.packets; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.entities.storage.MetaStorage; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacyEntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_13to1_13_1.Protocol1_13To1_13_1; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.entities.Entity1_13Types; @@ -16,7 +16,7 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_13; import us.myles.ViaVersion.packets.State; -public class EntityPackets1_13_1 extends EntityRewriter { +public class EntityPackets1_13_1 extends LegacyEntityRewriter { public EntityPackets1_13_1(Protocol1_13To1_13_1 protocol) { super(protocol); @@ -153,7 +153,7 @@ public class EntityPackets1_13_1 extends EntityRewriter { registerEntityDestroy(0x35); // Entity Metadata packet - registerLegacyMetadataRewriter(0x3F, 0x3F, Types1_13.METADATA_LIST); + registerMetadataRewriter(0x3F, 0x3F, Types1_13.METADATA_LIST); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java index 17ec96f6..897e7950 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java @@ -70,7 +70,7 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol { map(Type.VAR_INT); // Sound Id handler(wrapper -> { int id = wrapper.get(Type.VAR_INT, 0); - int newId = nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings.soundMappings.getNewId(id); + int newId = BackwardsMappings.soundMappings.getNewId(id); if (newId != -1 && id != newId) { wrapper.set(Type.VAR_INT, 0, newId); } @@ -85,7 +85,7 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol { map(Type.VAR_INT); // Sound Id handler(wrapper -> { int id = wrapper.get(Type.VAR_INT, 0); - int newId = nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings.soundMappings.getNewId(id); + int newId = BackwardsMappings.soundMappings.getNewId(id); if (newId != -1 && id != newId) { wrapper.set(Type.VAR_INT, 0, newId); } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java index 9b217d44..df6494a9 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/BlockItemPackets1_15.java @@ -1,7 +1,6 @@ package nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.packets; import nl.matsv.viabackwards.ViaBackwards; -import nl.matsv.viabackwards.api.rewriters.RecipeRewriter; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.ParticleMapping; @@ -32,6 +31,17 @@ public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.It ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer); BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_14_4To1_15::getNewBlockStateId, Protocol1_14_4To1_15::getNewBlockId); + // Declare Recipes + new RecipeRewriter1_15(this).registerDefaultHandler(0x5B, 0x5A); + + // Edit Book + protocol.registerIncoming(State.PLAY, 0x0C, 0x0C, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); + } + }); + // Set cooldown itemRewriter.registerSetCooldown(0x18, 0x17, BlockItemPackets1_15::getOldItemId); @@ -84,27 +94,6 @@ public class BlockItemPackets1_15 extends nl.matsv.viabackwards.api.rewriters.It // Entity Equipment Packet itemRewriter.registerEntityEquipment(Type.FLAT_VAR_INT_ITEM, 0x47, 0x46); - // Declare Recipes - protocol.registerOutgoing(State.PLAY, 0x5B, 0x5A, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - private final RecipeRewriter recipeHandler = new RecipeRewriter1_15(BlockItemPackets1_15.this); - - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int size = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < size; i++) { - String type = wrapper.passthrough(Type.STRING).replace("minecraft:", ""); - String id = wrapper.passthrough(Type.STRING); // Recipe Identifier - recipeHandler.handle(wrapper, type); - } - } - }); - } - }); - - // Incoming packets // Click window packet itemRewriter.registerClickWindow(Type.FLAT_VAR_INT_ITEM, 0x09, 0x09); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java index f69d08a0..3dd1bd78 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14_4to1_15/packets/EntityPackets1_15.java @@ -7,14 +7,12 @@ import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.EntityTypeMappin import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateRespawn; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.ParticleMapping; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.entities.Entity1_14Types; import us.myles.ViaVersion.api.entities.Entity1_15Types; import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; -import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.Particle; @@ -36,18 +34,15 @@ public class EntityPackets1_15 extends EntityRewriter { protocol.registerOutgoing(State.PLAY, 0x49, 0x48, new PacketRemapper() { @Override public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - float health = wrapper.passthrough(Type.FLOAT); - if (health > 0) return; - if (!wrapper.user().get(ImmediateRespawn.class).isImmediateRespawn()) return; + handler(wrapper -> { + float health = wrapper.passthrough(Type.FLOAT); + if (health > 0) return; + if (!wrapper.user().get(ImmediateRespawn.class).isImmediateRespawn()) return; - // Instantly request respawn when 1.15 gamerule is set - PacketWrapper statusPacket = wrapper.create(0x04); - statusPacket.write(Type.VAR_INT, 0); - statusPacket.sendToServer(Protocol1_14_4To1_15.class); - } + // Instantly request respawn when 1.15 gamerule is set + PacketWrapper statusPacket = wrapper.create(0x04); + statusPacket.write(Type.VAR_INT, 0); + statusPacket.sendToServer(Protocol1_14_4To1_15.class); }); } }); @@ -58,48 +53,16 @@ public class EntityPackets1_15 extends EntityRewriter { public void registerMap() { map(Type.UNSIGNED_BYTE); map(Type.FLOAT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 11) { - wrapper.user().get(ImmediateRespawn.class).setImmediateRespawn(wrapper.get(Type.FLOAT, 0) == 1); - } + handler(wrapper -> { + if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 11) { + wrapper.user().get(ImmediateRespawn.class).setImmediateRespawn(wrapper.get(Type.FLOAT, 0) == 1); } }); } }); // Spawn Object - protocol.registerOutgoing(State.PLAY, 0x00, 0x00, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity id - map(Type.UUID); // 1 - UUID - map(Type.VAR_INT); // 2 - 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(getTrackerHandler()); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int typeId = wrapper.get(Type.VAR_INT, 1); - Entity1_14Types.EntityType entityType = Entity1_14Types.getTypeFromId(getOldEntityId(typeId)); - wrapper.set(Type.VAR_INT, 1, entityType.getId()); - - if (entityType == Entity1_14Types.EntityType.FALLING_BLOCK) { - int blockState = wrapper.get(Type.INT, 0); - int combined = Protocol1_14_4To1_15.getNewBlockStateId(blockState); - wrapper.set(Type.INT, 0, combined); - } - } - }); - } - }); + registerSpawnTrackerWithData(0x00, 0x00, Entity1_15Types.EntityType.FALLING_BLOCK, protocol.getBlockItemPackets()); // Spawn mob packet protocol.registerOutgoing(State.PLAY, 0x03, 0x03, new PacketRemapper() { @@ -119,14 +82,11 @@ public class EntityPackets1_15 extends EntityRewriter { map(Type.SHORT); // 11 - Velocity Z create(wrapper -> wrapper.write(Types1_14.METADATA_LIST, new ArrayList<>())); // Metadata is no longer sent in 1.15, so we have to send an empty one - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int type = wrapper.get(Type.VAR_INT, 1); - Entity1_15Types.EntityType entityType = Entity1_15Types.getTypeFromId(type); - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); - wrapper.set(Type.VAR_INT, 1, EntityTypeMapping.getOldEntityId(type)); - } + handler(wrapper -> { + int type = wrapper.get(Type.VAR_INT, 1); + Entity1_15Types.EntityType entityType = Entity1_15Types.getTypeFromId(type); + addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); + wrapper.set(Type.VAR_INT, 1, EntityTypeMapping.getOldEntityId(type)); }); } }); @@ -137,13 +97,7 @@ public class EntityPackets1_15 extends EntityRewriter { public void registerMap() { map(Type.INT); map(Type.LONG, Type.NOTHING); // Seed - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - clientWorld.setEnvironment(wrapper.get(Type.INT, 0)); - } - }); + handler(wrapper -> wrapper.user().get(ClientWorld.class).setEnvironment(wrapper.get(Type.INT, 0))); } }); @@ -165,26 +119,7 @@ public class EntityPackets1_15 extends EntityRewriter { handler(getTrackerHandler(Entity1_15Types.EntityType.PLAYER, Type.INT)); handler(getDimensionHandler(1)); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - boolean immediateRespawn = wrapper.read(Type.BOOLEAN); - wrapper.user().get(ImmediateRespawn.class).setImmediateRespawn(immediateRespawn); - } - }); - } - }); - - // Edit Book - protocol.registerIncoming(State.PLAY, 0x0C, 0x0C, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - getProtocol().getBlockItemPackets().handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); - } - }); + handler(wrapper -> wrapper.user().get(ImmediateRespawn.class).setImmediateRespawn(wrapper.read(Type.BOOLEAN))); } }); @@ -226,41 +161,38 @@ public class EntityPackets1_15 extends EntityRewriter { public void registerMap() { map(Type.VAR_INT); map(Type.INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int entityId = wrapper.get(Type.VAR_INT, 0); - EntityType entityType = getEntityType(wrapper.user(), entityId); - if (entityType != Entity1_15Types.EntityType.BEE) return; + handler(wrapper -> { + int entityId = wrapper.get(Type.VAR_INT, 0); + EntityType entityType = getEntityType(wrapper.user(), entityId); + if (entityType != Entity1_15Types.EntityType.BEE) return; - int size = wrapper.get(Type.INT, 0); - int newSize = size; - for (int i = 0; i < size; i++) { - String key = wrapper.read(Type.STRING); - if (key.equals("generic.flyingSpeed")) { - newSize--; + int size = wrapper.get(Type.INT, 0); + int newSize = size; + for (int i = 0; i < size; i++) { + String key = wrapper.read(Type.STRING); + if (key.equals("generic.flyingSpeed")) { + newSize--; + wrapper.read(Type.DOUBLE); + int modSize = wrapper.read(Type.VAR_INT); + for (int j = 0; j < modSize; j++) { + wrapper.read(Type.UUID); wrapper.read(Type.DOUBLE); - int modSize = wrapper.read(Type.VAR_INT); - for (int j = 0; j < modSize; j++) { - wrapper.read(Type.UUID); - wrapper.read(Type.DOUBLE); - wrapper.read(Type.BYTE); - } - } else { - wrapper.write(Type.STRING, key); + wrapper.read(Type.BYTE); + } + } else { + wrapper.write(Type.STRING, key); + wrapper.passthrough(Type.DOUBLE); + int modSize = wrapper.passthrough(Type.VAR_INT); + for (int j = 0; j < modSize; j++) { + wrapper.passthrough(Type.UUID); wrapper.passthrough(Type.DOUBLE); - int modSize = wrapper.passthrough(Type.VAR_INT); - for (int j = 0; j < modSize; j++) { - wrapper.passthrough(Type.UUID); - wrapper.passthrough(Type.DOUBLE); - wrapper.passthrough(Type.BYTE); - } + wrapper.passthrough(Type.BYTE); } } + } - if (newSize != size) { - wrapper.set(Type.INT, 0, newSize); - } + if (newSize != size) { + wrapper.set(Type.INT, 0, newSize); } }); } @@ -269,15 +201,12 @@ public class EntityPackets1_15 extends EntityRewriter { @Override protected void registerRewrites() { - setDisplayNameJson(true); - setDisplayNameMetaType(MetaType1_14.OptChat); - registerMetaHandler().handle(e -> { Metadata meta = e.getData(); MetaType type = meta.getMetaType(); if (type == MetaType1_14.Slot) { Item item = (Item) meta.getValue(); - meta.setValue(getProtocol().getBlockItemPackets().handleItemToClient(item)); + meta.setValue(protocol.getBlockItemPackets().handleItemToClient(item)); } else if (type == MetaType1_14.BlockID) { int blockstate = (int) meta.getValue(); meta.setValue(Protocol1_14_4To1_15.getNewBlockStateId(blockstate)); @@ -301,7 +230,7 @@ public class EntityPackets1_15 extends EntityRewriter { registerMetaHandler().filter(Entity1_15Types.EntityType.BEE, 15).removed(); registerMetaHandler().filter(Entity1_15Types.EntityType.BEE, 16).removed(); - regEntType(Entity1_15Types.EntityType.BEE, Entity1_15Types.EntityType.PUFFER_FISH).mobName("Bee").spawnMetadata(storage -> { + mapEntity(Entity1_15Types.EntityType.BEE, Entity1_15Types.EntityType.PUFFER_FISH).mobName("Bee").spawnMetadata(storage -> { storage.add(new Metadata(14, MetaType1_14.Boolean, false)); storage.add(new Metadata(15, MetaType1_14.VarInt, 2)); }); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14to1_14_1/packets/EntityPackets1_14_1.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14to1_14_1/packets/EntityPackets1_14_1.java index 76843090..f8131a4f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14to1_14_1/packets/EntityPackets1_14_1.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_14to1_14_1/packets/EntityPackets1_14_1.java @@ -1,7 +1,7 @@ package nl.matsv.viabackwards.protocol.protocol1_14to1_14_1.packets; import nl.matsv.viabackwards.api.entities.storage.MetaStorage; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacyEntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_14to1_14_1.Protocol1_14To1_14_1; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.entities.Entity1_14Types; @@ -12,7 +12,7 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.version.Types1_14; import us.myles.ViaVersion.packets.State; -public class EntityPackets1_14_1 extends EntityRewriter { +public class EntityPackets1_14_1 extends LegacyEntityRewriter { public EntityPackets1_14_1(Protocol1_14To1_14_1 protocol) { super(protocol); @@ -74,7 +74,7 @@ public class EntityPackets1_14_1 extends EntityRewriter { }); // Entity Metadata - registerLegacyMetadataRewriter(0x43, 0x43, Types1_14.METADATA_LIST); + registerMetadataRewriter(0x43, 0x43, Types1_14.METADATA_LIST); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java index 5979fb36..48a041a4 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/BlockItemPackets1_16.java @@ -1,15 +1,12 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets; import nl.matsv.viabackwards.ViaBackwards; -import nl.matsv.viabackwards.api.rewriters.RecipeRewriter; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.RecipeRewriter1_15; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings; -import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.minecraft.item.Item; -import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.BlockRewriter; import us.myles.ViaVersion.api.rewriters.ItemRewriter; @@ -30,6 +27,17 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It ItemRewriter itemRewriter = new ItemRewriter(protocol, this::handleItemToClient, this::handleItemToServer); BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_15_2To1_16::getNewBlockStateId, Protocol1_15_2To1_16::getNewBlockId); + // Declare Recipes + new RecipeRewriter1_15(this).registerDefaultHandler(0x5B, 0x5B); + + // Edit Book + protocol.registerIncoming(State.PLAY, 0x0C, 0x0C, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); + } + }); + // Set cooldown itemRewriter.registerSetCooldown(0x18, 0x18, BlockItemPackets1_16::getOldItemId); @@ -79,26 +87,6 @@ public class BlockItemPackets1_16 extends nl.matsv.viabackwards.api.rewriters.It // Entity Equipment Packet itemRewriter.registerEntityEquipment(Type.FLAT_VAR_INT_ITEM, 0x47, 0x47); - // Declare Recipes - protocol.registerOutgoing(State.PLAY, 0x5B, 0x5B, new PacketRemapper() { - @Override - public void registerMap() { - handler(new PacketHandler() { - private final RecipeRewriter recipeHandler = new RecipeRewriter1_15(BlockItemPackets1_16.this); - - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int size = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < size; i++) { - String type = wrapper.passthrough(Type.STRING).replace("minecraft:", ""); - String id = wrapper.passthrough(Type.STRING); // Recipe Identifier - recipeHandler.handle(wrapper, type); - } - } - }); - } - }); - // Click window packet itemRewriter.registerClickWindow(Type.FLAT_VAR_INT_ITEM, 0x09, 0x09); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java index 7d7f010d..99332cc3 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/packets/EntityPackets1_16.java @@ -3,19 +3,14 @@ package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets; import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_14_4to1_15.data.ParticleMapping; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; -import us.myles.ViaVersion.api.entities.Entity1_15Types; import us.myles.ViaVersion.api.entities.Entity1_16Types; import us.myles.ViaVersion.api.entities.EntityType; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_14; -import us.myles.ViaVersion.api.remapper.PacketRemapper; -import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.Particle; import us.myles.ViaVersion.api.type.types.version.Types1_14; -import us.myles.ViaVersion.packets.State; -import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class EntityPackets1_16 extends EntityRewriter { @@ -26,84 +21,16 @@ public class EntityPackets1_16 extends EntityRewriter { @Override protected void registerPackets() { // Spawn Object - protocol.registerOutgoing(State.PLAY, 0x00, 0x00, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity id - map(Type.UUID); // 1 - UUID - map(Type.VAR_INT); // 2 - 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(getTrackerHandler()); - handler(wrapper -> { - int typeId = wrapper.get(Type.VAR_INT, 1); - Entity1_15Types.EntityType entityType = Entity1_15Types.getTypeFromId(getOldEntityId(typeId)); - wrapper.set(Type.VAR_INT, 1, entityType.getId()); - - if (entityType == Entity1_15Types.EntityType.FALLING_BLOCK) { - int blockState = wrapper.get(Type.INT, 0); - int newId = Protocol1_15_2To1_16.getNewBlockStateId(blockState); - wrapper.set(Type.INT, 0, newId); - } - }); - } - }); + registerSpawnTrackerWithData(0x00, 0x00, Entity1_16Types.EntityType.FALLING_BLOCK, protocol.getBlockItemPackets()); // Spawn mob packet - protocol.registerOutgoing(State.PLAY, 0x03, 0x03, 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(wrapper -> { - int type = wrapper.get(Type.VAR_INT, 1); - Entity1_16Types.EntityType entityType = Entity1_16Types.getTypeFromId(type); - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); - wrapper.set(Type.VAR_INT, 1, getOldEntityId(type)); - }); - } - }); + registerSpawnTracker(0x03, 0x03); // Respawn - protocol.registerOutgoing(State.PLAY, 0x3B, 0x3B, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); - map(Type.LONG, Type.NOTHING); // Seed - handler(wrapper -> { - ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - clientWorld.setEnvironment(wrapper.get(Type.INT, 0)); - }); - } - }); + registerRespawn(0x3B, 0x3B); // Join Game - protocol.registerOutgoing(State.PLAY, 0x26, 0x26, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Entity ID - map(Type.UNSIGNED_BYTE); // 1 - Gamemode - map(Type.INT); // 2 - Dimension - - handler(getTrackerHandler(Entity1_16Types.EntityType.PLAYER, Type.INT)); - handler(getDimensionHandler(1)); - } - }); - - // Edit Book - protocol.registerIncoming(State.PLAY, 0x0C, 0x0C, new PacketRemapper() { - @Override - public void registerMap() { - handler(wrapper -> getProtocol().getBlockItemPackets().handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM))); - } - }); + registerJoinGame(0x26, 0x26, Entity1_16Types.EntityType.PLAYER); // Spawn Experience Orb registerExtraTracker(0x01, Entity1_16Types.EntityType.XP_ORB); @@ -115,20 +42,7 @@ public class EntityPackets1_16 extends EntityRewriter { registerExtraTracker(0x04, Entity1_16Types.EntityType.PAINTING); // Spawn player packet - protocol.registerOutgoing(State.PLAY, 0x05, 0x05, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.VAR_INT); // 0 - Entity ID - map(Type.UUID); // 1 - Player UUID - map(Type.DOUBLE); // 2 - X - map(Type.DOUBLE); // 3 - Y - map(Type.DOUBLE); // 4 - Z - map(Type.BYTE); // 5 - Yaw - map(Type.BYTE); // 6 - Pitch - - handler(getTrackerHandler(Entity1_16Types.EntityType.PLAYER, Type.VAR_INT)); - } - }); + registerExtraTracker(0x05, Entity1_16Types.EntityType.PLAYER); // Destroy entities registerEntityDestroy(0x38, 0x38); @@ -139,9 +53,6 @@ public class EntityPackets1_16 extends EntityRewriter { @Override protected void registerRewrites() { - setDisplayNameJson(true); - setDisplayNameMetaType(MetaType1_14.OptChat); - registerMetaHandler().handle(e -> { Metadata meta = e.getData(); MetaType type = meta.getMetaType(); @@ -156,7 +67,7 @@ public class EntityPackets1_16 extends EntityRewriter { return meta; }); - regEntType(Entity1_16Types.EntityType.HOGLIN, Entity1_16Types.EntityType.COW).mobName("Hoglin"); + mapEntity(Entity1_16Types.EntityType.HOGLIN, Entity1_16Types.EntityType.COW).mobName("Hoglin"); } @Override diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java index 99b7057d..d65e649a 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java @@ -14,7 +14,7 @@ import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.entities.storage.EntityData; import nl.matsv.viabackwards.api.entities.storage.MetaStorage; import nl.matsv.viabackwards.api.exceptions.RemovedValueException; -import nl.matsv.viabackwards.api.rewriters.EntityRewriter; +import nl.matsv.viabackwards.api.rewriters.LegacyEntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.Protocol1_9_4To1_10; import nl.matsv.viabackwards.utils.Block; import us.myles.ViaVersion.api.PacketWrapper; @@ -33,7 +33,7 @@ import us.myles.ViaVersion.packets.State; import java.util.Optional; -public class EntityPackets1_10 extends EntityRewriter { +public class EntityPackets1_10 extends LegacyEntityRewriter { public EntityPackets1_10(Protocol1_9_4To1_10 protocol) { super(protocol); @@ -205,12 +205,12 @@ public class EntityPackets1_10 extends EntityRewriter { registerEntityDestroy(0x30); // Metadata packet - registerLegacyMetadataRewriter(0x39, 0x39, Types1_9.METADATA_LIST); + registerMetadataRewriter(0x39, 0x39, Types1_9.METADATA_LIST); } @Override protected void registerRewrites() { - regEntType(Entity1_10Types.EntityType.POLAR_BEAR, Entity1_10Types.EntityType.SHEEP).mobName("Polar Bear"); + mapEntity(Entity1_10Types.EntityType.POLAR_BEAR, Entity1_10Types.EntityType.SHEEP).mobName("Polar Bear"); // Change the sheep color when the polar bear is standing up (index 13 -> Standing up) registerMetaHandler().filter(Entity1_10Types.EntityType.POLAR_BEAR, 13).handle((e -> {