From a789e2ff7bc43155240ee4dae447eadb044bd433 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Tue, 25 May 2021 12:25:39 +0200 Subject: [PATCH] Update EntityTracker/meta rewriter --- .../viabackwards/api/BackwardsProtocol.java | 23 +- .../api/entities/meta/MetaHandler.java | 27 -- .../api/entities/meta/MetaHandlerEvent.java | 93 ----- .../entities/meta/MetaHandlerSettings.java | 125 ------- .../api/entities/storage/EntityData.java | 14 +- .../entities/storage/EntityObjectData.java | 4 +- .../storage/EntityPositionHandler.java | 9 +- .../storage/EntityPositionStorage.java | 2 +- .../api/entities/storage/EntityStorage.java | 22 -- .../api/entities/storage/EntityTracker.java | 158 -------- ...{MetaStorage.java => WrappedMetadata.java} | 34 +- .../api/rewriters/EntityRewriter.java | 61 +--- .../api/rewriters/EntityRewriterBase.java | 336 +++++------------- .../api/rewriters/ItemRewriterBase.java | 3 +- .../api/rewriters/LegacyEntityRewriter.java | 50 +-- .../api/rewriters/LegacySoundRewriter.java | 3 +- .../viabackwards/api/rewriters/Rewriter.java | 52 --- .../Protocol1_10To1_11.java | 4 +- .../packets/BlockItemPackets1_11.java | 28 +- .../packets/EntityPackets1_11.java | 209 +++++------ .../storage/ChestedHorseStorage.java | 4 +- .../Protocol1_11_1To1_12.java | 4 +- .../data/ParrotStorage.java | 5 +- .../packets/BlockItemPackets1_12.java | 11 +- .../packets/ChatPackets1_12.java | 4 +- .../packets/EntityPackets1_12.java | 92 ++--- .../Protocol1_11To1_11_1.java | 4 +- .../packets/EntityPackets1_11_1.java | 14 +- .../packets/ItemPackets1_11_1.java | 11 +- .../Protocol1_12_2To1_13.java | 4 +- .../packets/EntityPackets1_13.java | 92 ++--- .../packets/PlayerPacket1_13.java | 12 +- .../packets/SoundPackets1_13.java | 4 +- .../packets/EntityPackets1_13_2.java | 6 +- .../Protocol1_13_2To1_14.java | 6 +- .../packets/BlockItemPackets1_14.java | 3 +- .../packets/EntityPackets1_14.java | 198 ++++------- .../packets/PlayerPackets1_14.java | 4 +- .../packets/SoundPackets1_14.java | 8 +- .../Protocol1_13To1_13_1.java | 10 +- .../packets/EntityPackets1_13_1.java | 58 ++- .../Protocol1_14_4To1_15.java | 8 +- .../packets/EntityPackets1_15.java | 71 ++-- .../Protocol1_14To1_14_1.java | 4 +- .../packets/EntityPackets1_14_1.java | 30 +- .../Protocol1_15_2To1_16.java | 9 +- .../packets/EntityPackets1_16.java | 71 ++-- .../Protocol1_16_1To1_16_2.java | 8 +- .../packets/EntityPackets1_16_2.java | 47 +-- .../Protocol1_16_4To1_17.java | 9 +- .../packets/BlockItemPackets1_17.java | 16 +- .../packets/EntityPackets1_17.java | 82 ++--- .../Protocol1_9_4To1_10.java | 4 +- .../packets/BlockItemPackets1_10.java | 11 +- .../packets/EntityPackets1_10.java | 88 ++--- 55 files changed, 689 insertions(+), 1580 deletions(-) delete mode 100644 common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandler.java delete mode 100644 common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandlerEvent.java delete mode 100644 common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandlerSettings.java delete mode 100644 common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityStorage.java delete mode 100644 common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityTracker.java rename common/src/main/java/com/viaversion/viabackwards/api/entities/storage/{MetaStorage.java => WrappedMetadata.java} (62%) delete mode 100644 common/src/main/java/com/viaversion/viabackwards/api/rewriters/Rewriter.java diff --git a/common/src/main/java/com/viaversion/viabackwards/api/BackwardsProtocol.java b/common/src/main/java/com/viaversion/viabackwards/api/BackwardsProtocol.java index 006c64af..17cd23e3 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/BackwardsProtocol.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/BackwardsProtocol.java @@ -18,9 +18,9 @@ package com.viaversion.viabackwards.api; import com.viaversion.viabackwards.api.data.BackwardsMappings; -import com.viaversion.viabackwards.api.entities.storage.EntityTracker; +import com.viaversion.viabackwards.api.rewriters.ItemRewriterBase; +import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; @@ -45,23 +45,22 @@ public abstract class BackwardsProtocol. - */ - -package com.viaversion.viabackwards.api.entities.meta; - -import com.viaversion.viabackwards.api.exceptions.RemovedValueException; -import com.viaversion.viaversion.api.minecraft.metadata.Metadata; - -public interface MetaHandler { - - Metadata handle(MetaHandlerEvent e) throws RemovedValueException; -} diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandlerEvent.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandlerEvent.java deleted file mode 100644 index 16115479..00000000 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandlerEvent.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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.viabackwards.api.entities.meta; - -import com.viaversion.viabackwards.api.entities.storage.EntityTracker; -import com.viaversion.viabackwards.api.entities.storage.MetaStorage; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.ArrayList; -import java.util.List; - -public class MetaHandlerEvent { - private final UserConnection user; - private final EntityTracker.StoredEntity entity; - private final int index; - private final Metadata data; - private final MetaStorage storage; - private List extraData; - - public MetaHandlerEvent(UserConnection user, EntityTracker.StoredEntity entity, int index, Metadata data, MetaStorage storage) { - this.user = user; - this.entity = entity; - this.index = index; - this.data = data; - this.storage = storage; - } - - public boolean hasData() { - return data != null; - } - - public Metadata getMetaByIndex(int index) { - for (Metadata meta : storage.getMetaDataList()) { - if (index == meta.getId()) { - return meta; - } - } - return null; - } - - public void clearExtraData() { - extraData = null; - } - - public void createMeta(Metadata metadata) { - (extraData != null ? extraData : (extraData = new ArrayList<>())).add(metadata); - } - - public UserConnection getUser() { - return user; - } - - public EntityTracker.StoredEntity getEntity() { - return entity; - } - - public int getIndex() { - return index; - } - - public Metadata getData() { - return data; - } - - public MetaStorage getStorage() { - return storage; - } - - /** - * May be null, use {@link #createMeta(Metadata)} for adding metadata. - */ - public @Nullable List getExtraData() { - return extraData; - } -} diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandlerSettings.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandlerSettings.java deleted file mode 100644 index d48413e1..00000000 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/meta/MetaHandlerSettings.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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.viabackwards.api.entities.meta; - -import com.viaversion.viabackwards.api.exceptions.RemovedValueException; -import com.viaversion.viaversion.api.minecraft.entities.EntityType; -import com.viaversion.viaversion.api.minecraft.metadata.Metadata; -import org.checkerframework.checker.nullness.qual.Nullable; - -public class MetaHandlerSettings { - private EntityType filterType; - private boolean filterFamily; - private int filterIndex = -1; - private MetaHandler handler; - - public MetaHandlerSettings filter(EntityType type) { - return filter(type, filterFamily, filterIndex); - } - - public MetaHandlerSettings filter(EntityType type, boolean filterFamily) { - return filter(type, filterFamily, filterIndex); - } - - public MetaHandlerSettings filter(int index) { - return filter(filterType, filterFamily, index); - } - - public MetaHandlerSettings filter(EntityType type, int index) { - return filter(type, filterFamily, index); - } - - public MetaHandlerSettings filter(EntityType type, boolean filterFamily, int index) { - this.filterType = type; - this.filterFamily = filterFamily; - this.filterIndex = index; - return this; - } - - public void handle(@Nullable MetaHandler handler) { - this.handler = handler; - } - - public void handleIndexChange(final int newIndex) { - handle(e -> { - Metadata data = e.getData(); - data.setId(newIndex); - return data; - }); - } - - public void removed() { - handle(e -> { - throw RemovedValueException.EX; - }); - } - - public boolean hasHandler() { - return handler != null; - } - - public boolean hasType() { - return filterType != null; - } - - public boolean hasIndex() { - return filterIndex > -1; - } - - public boolean isFilterFamily() { - return filterFamily; - } - - /** - * Returns true if the metadata should be handled by this object. - * - * @param type entity type - * @param metadata metadata - * @return true if gucci - */ - public boolean isGucci(EntityType type, Metadata metadata) { - if (!hasHandler()) return false; - if (hasType() && (filterFamily ? !type.isOrHasParent(filterType) : !filterType.is(type))) { - return false; - } - return !hasIndex() || metadata.getId() == filterIndex; - } - - public EntityType getFilterType() { - return filterType; - } - - public int getFilterIndex() { - return filterIndex; - } - - public @Nullable MetaHandler getHandler() { - return handler; - } - - @Override - public String toString() { - return "MetaHandlerSettings{" + - "filterType=" + filterType + - ", filterFamily=" + filterFamily + - ", filterIndex=" + filterIndex + - ", handler=" + handler + - '}'; - } -} diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityData.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityData.java index 6791629e..9736c530 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityData.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityData.java @@ -51,30 +51,30 @@ public class EntityData { return this.defaultMeta != null; } - public int getId() { + public int typeId() { return id; } /** * @return custom mobname, can be either a String or a JsonElement */ - public @Nullable Object getMobName() { + public @Nullable Object mobName() { return mobName; } - public int getReplacementId() { + public int replacementId() { return replacementId; } - public @Nullable MetaCreator getDefaultMeta() { + public @Nullable MetaCreator defaultMeta() { return defaultMeta; } - public boolean isObject() { + public boolean isObjectType() { return false; } - public int getObjectData() { + public int objectData() { return -1; } @@ -91,6 +91,6 @@ public class EntityData { @FunctionalInterface public interface MetaCreator { - void createMeta(MetaStorage storage); + void createMeta(WrappedMetadata storage); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityObjectData.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityObjectData.java index f45881db..5bd9cb05 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityObjectData.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityObjectData.java @@ -28,12 +28,12 @@ public class EntityObjectData extends EntityData { } @Override - public boolean isObject() { + public boolean isObjectType() { return isObject; } @Override - public int getObjectData() { + public int objectData() { return objectData; } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java index da692af4..c309905e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionHandler.java @@ -21,6 +21,7 @@ import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.api.rewriters.EntityRewriterBase; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.entity.StoredEntityData; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; @@ -29,12 +30,12 @@ import java.util.function.Supplier; public class EntityPositionHandler { public static final double RELATIVE_MOVE_FACTOR = 32 * 128; - private final EntityRewriterBase entityRewriter; + private final EntityRewriterBase entityRewriter; private final Class storageClass; private final Supplier storageSupplier; private boolean warnedForMissingEntity; - public EntityPositionHandler(EntityRewriterBase entityRewriter, + public EntityPositionHandler(EntityRewriterBase entityRewriter, Class storageClass, Supplier storageSupplier) { this.entityRewriter = entityRewriter; this.storageClass = storageClass; @@ -48,7 +49,7 @@ 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); - EntityTracker.StoredEntity storedEntity = entityRewriter.getEntityTracker(wrapper.user()).getEntity(entityId); + StoredEntityData storedEntity = entityRewriter.tracker(wrapper.user()).entityData(entityId); if (storedEntity == null) { if (Via.getManager().isDebug()) { // There is too many plugins violating this out there, and reading seems to be hard! :> ViaBackwards.getPlatform().getLogger().warning("Stored entity with id " + entityId + " missing at position: " + x + " - " + y + " - " + z + " in " + storageClass.getSimpleName()); @@ -73,7 +74,7 @@ public class EntityPositionHandler { } public EntityPositionStorage getStorage(UserConnection user, int entityId) { - EntityTracker.StoredEntity storedEntity = user.get(EntityTracker.class).get(entityRewriter.getProtocol()).getEntity(entityId); + StoredEntityData storedEntity = entityRewriter.tracker(user).entityData(entityId); EntityPositionStorage entityStorage; if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage.class)) == null) { ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId + " in " + storageClass.getSimpleName()); diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionStorage.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionStorage.java index 31dc079a..ce3a80e6 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityPositionStorage.java @@ -17,7 +17,7 @@ */ package com.viaversion.viabackwards.api.entities.storage; -public abstract class EntityPositionStorage implements EntityStorage { +public abstract class EntityPositionStorage { private double x; private double y; private double z; diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityStorage.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityStorage.java deleted file mode 100644 index a00359c5..00000000 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityStorage.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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.viabackwards.api.entities.storage; - -public interface EntityStorage { -} diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityTracker.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityTracker.java deleted file mode 100644 index ef4426a9..00000000 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/EntityTracker.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.viabackwards.api.entities.storage; - -import com.viaversion.viabackwards.api.BackwardsProtocol; -import com.viaversion.viaversion.api.connection.StoredObject; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.entities.EntityType; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class EntityTracker extends StoredObject { - private final Map trackers = new HashMap<>(); - private int currentWorldSectionHeight = 16; - private int currentMinY; - - public EntityTracker(UserConnection user) { - super(user); - } - - public void initProtocol(BackwardsProtocol protocol) { - trackers.put(protocol, new ProtocolEntityTracker()); - } - - public @Nullable ProtocolEntityTracker get(BackwardsProtocol protocol) { - return trackers.get(protocol); - } - - public Map getTrackers() { - return trackers; - } - - /** - * @return amount of chunk sections of the current world (block height / 16) - */ - public int getCurrentWorldSectionHeight() { - return currentWorldSectionHeight; - } - - public void setCurrentWorldSectionHeight(int currentWorldSectionHeight) { - this.currentWorldSectionHeight = currentWorldSectionHeight; - } - - /** - * @return absolute minimum y coordinate of the current world - */ - public int getCurrentMinY() { - return currentMinY; - } - - public void setCurrentMinY(int currentMinY) { - this.currentMinY = currentMinY; - } - - public static class ProtocolEntityTracker { - private final Map entityMap = new ConcurrentHashMap<>(); - - public void trackEntityType(int id, EntityType type) { - entityMap.putIfAbsent(id, new StoredEntity(id, type)); - } - - public void removeEntity(int id) { - entityMap.remove(id); - } - - @Nullable - public EntityType getEntityType(int id) { - StoredEntity storedEntity = entityMap.get(id); - return storedEntity != null ? storedEntity.getType() : null; - } - - @Nullable - public StoredEntity getEntity(int id) { - return entityMap.get(id); - } - } - - public static final class StoredEntity { - private final int entityId; - private final EntityType type; - private Map, EntityStorage> storedObjects; - - private StoredEntity(final int entityId, final EntityType type) { - this.entityId = entityId; - this.type = type; - } - - /** - * Get an object from the storage - * - * @param objectClass The class of the object to get - * @param The type of the class you want to get. - * @return The requested object - */ - @Nullable - public T get(Class objectClass) { - return storedObjects != null ? (T) storedObjects.get(objectClass) : null; - } - - /** - * Check if the storage has an object - * - * @param objectClass The object class to check - * @return True if the object is in the storage - */ - public boolean has(Class objectClass) { - return storedObjects != null && storedObjects.containsKey(objectClass); - } - - /** - * Put an object into the stored objects based on class - * - * @param object The object to store. - */ - public void put(EntityStorage object) { - if (storedObjects == null) { - storedObjects = new ConcurrentHashMap<>(); - } - storedObjects.put(object.getClass(), object); - } - - public int getEntityId() { - return entityId; - } - - public EntityType getType() { - return type; - } - - @Override - public String toString() { - return "StoredEntity{" + - "entityId=" + entityId + - ", type=" + type + - ", storedObjects=" + storedObjects + - '}'; - } - } -} diff --git a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/MetaStorage.java b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/WrappedMetadata.java similarity index 62% rename from common/src/main/java/com/viaversion/viabackwards/api/entities/storage/MetaStorage.java rename to common/src/main/java/com/viaversion/viabackwards/api/entities/storage/WrappedMetadata.java index 02da79d4..6583f733 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/MetaStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/entities/storage/WrappedMetadata.java @@ -23,48 +23,44 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.util.List; -public class MetaStorage { - private List metaDataList; +public final class WrappedMetadata { + private final List metadataList; - public MetaStorage(List metaDataList) { - this.metaDataList = metaDataList; + public WrappedMetadata(List metadataList) { + this.metadataList = metadataList; } public boolean has(Metadata data) { - return this.metaDataList.contains(data); + return this.metadataList.contains(data); } - public void delete(Metadata data) { - this.metaDataList.remove(data); + public void remove(Metadata data) { + this.metadataList.remove(data); } - public void delete(int index) { - metaDataList.removeIf(meta -> meta.getId() == index); + public void remove(int index) { + metadataList.removeIf(meta -> meta.id() == index); } public void add(Metadata data) { - this.metaDataList.add(data); + this.metadataList.add(data); } public @Nullable Metadata get(int index) { - for (Metadata meta : this.metaDataList) { - if (index == meta.getId()) { + for (Metadata meta : this.metadataList) { + if (index == meta.id()) { return meta; } } return null; } - public List getMetaDataList() { - return metaDataList; - } - - public void setMetaDataList(List metaDataList) { - this.metaDataList = metaDataList; + public List metadataList() { + return metadataList; } @Override public String toString() { - return "MetaStorage{" + "metaDataList=" + metaDataList + '}'; + return "MetaStorage{" + "metaDataList=" + metadataList + '}'; } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java index 75be1538..29238ffd 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriter.java @@ -18,11 +18,8 @@ package com.viaversion.viabackwards.api.rewriters; import com.viaversion.viabackwards.api.BackwardsProtocol; -import com.viaversion.viabackwards.api.entities.storage.EntityData; -import com.viaversion.viabackwards.api.entities.storage.MetaStorage; 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.minecraft.metadata.types.MetaType1_14; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; @@ -30,8 +27,6 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; -import java.util.List; - public abstract class EntityRewriter extends EntityRewriterBase { protected EntityRewriter(T protocol) { @@ -42,7 +37,8 @@ public abstract class EntityRewriter extends Entity super(protocol, displayType, 2, displayVisibilityType, 3); } - public void registerSpawnTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType) { + @Override + public void registerTrackerWithData(ClientboundPacketType packetType, EntityType fallingBlockType) { protocol.registerClientbound(packetType, new PacketRemapper() { @Override public void registerMap() { @@ -55,13 +51,14 @@ public abstract class EntityRewriter extends Entity map(Type.BYTE); // 6 - Pitch map(Type.BYTE); // 7 - Yaw map(Type.INT); // 8 - Data - handler(getSpawnTracketWithDataHandler(fallingBlockType)); + handler(getSpawnTrackerWithDataHandler(fallingBlockType)); } }); } - public PacketHandler getSpawnTracketWithDataHandler(EntityType fallingBlockType) { + public PacketHandler getSpawnTrackerWithDataHandler(EntityType fallingBlockType) { return wrapper -> { + // Check against the UNMAPPED entity type EntityType entityType = setOldEntityId(wrapper); if (entityType == fallingBlockType) { int blockState = wrapper.get(Type.INT, 0); @@ -84,52 +81,14 @@ public abstract class EntityRewriter extends Entity private EntityType setOldEntityId(PacketWrapper wrapper) throws Exception { int typeId = wrapper.get(Type.VAR_INT, 1); - EntityType entityType = getTypeFromId(typeId); - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); + EntityType entityType = typeFromId(typeId); + tracker(wrapper.user()).addEntity(wrapper.get(Type.VAR_INT, 0), entityType); - int oldTypeId = getOldEntityId(entityType.getId()); - if (typeId != oldTypeId) { - wrapper.set(Type.VAR_INT, 1, oldTypeId); + int mappedTypeId = newEntityId(entityType.getId()); + if (typeId != mappedTypeId) { + wrapper.set(Type.VAR_INT, 1, mappedTypeId); } return entityType; } - - /** - * Helper method to handle a metadata list packet and its full initial meta rewrite. - */ - protected void registerMetadataRewriter(ClientboundPacketType packetType, Type> oldMetaType, Type> newMetaType) { - getProtocol().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); - EntityType type = getEntityType(wrapper.user(), entityId); - - MetaStorage storage = new MetaStorage(wrapper.get(newMetaType, 0)); - handleMeta(wrapper.user(), entityId, storage); - - EntityData entityData = getEntityData(type); - //TODO only do this once for a first meta packet? - if (entityData != null) { - if (entityData.hasBaseMeta()) { - entityData.getDefaultMeta().createMeta(storage); - } - } - - wrapper.set(newMetaType, 0, storage.getMetaDataList()); - }); - } - }); - } - - protected void registerMetadataRewriter(ClientboundPacketType packetType, Type> metaType) { - registerMetadataRewriter(packetType, null, metaType); - } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java index 34aa7e68..6c89c4c8 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java @@ -20,97 +20,120 @@ package com.viaversion.viabackwards.api.rewriters; import com.google.common.base.Preconditions; import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.api.BackwardsProtocol; -import com.viaversion.viabackwards.api.entities.meta.MetaHandlerEvent; -import com.viaversion.viabackwards.api.entities.meta.MetaHandlerSettings; import com.viaversion.viabackwards.api.entities.storage.EntityData; -import com.viaversion.viabackwards.api.entities.storage.EntityTracker; -import com.viaversion.viabackwards.api.entities.storage.MetaStorage; -import com.viaversion.viabackwards.api.exceptions.RemovedValueException; -import com.viaversion.viaversion.api.Via; +import com.viaversion.viabackwards.api.entities.storage.WrappedMetadata; import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.data.ParticleMappings; +import com.viaversion.viaversion.api.data.entity.StoredEntityData; 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.packet.ClientboundPacketType; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; 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.exception.CancelException; -import com.viaversion.viaversion.libs.fastutil.ints.Int2IntMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2IntOpenHashMap; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag; -import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; +import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap; +import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; +import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import com.viaversion.viaversion.rewriter.EntityRewriter; +import com.viaversion.viaversion.rewriter.meta.MetaHandlerEvent; import org.checkerframework.checker.nullness.qual.Nullable; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.logging.Logger; -import java.util.stream.Collectors; /** * Entity rewriter base class. * - * @see EntityRewriter + * @see com.viaversion.viabackwards.api.rewriters.EntityRewriter * @see LegacyEntityRewriter */ -public abstract class EntityRewriterBase extends Rewriter { - private final Map entityTypes = new HashMap<>(); - private final List metaHandlers = new ArrayList<>(); +public abstract class EntityRewriterBase extends EntityRewriter { + private final Int2ObjectMap entityDataMappings = new Int2ObjectOpenHashMap<>(); private final MetaType displayNameMetaType; private final MetaType displayVisibilityMetaType; private final int displayNameIndex; private final int displayVisibilityIndex; - protected Int2IntMap typeMapping; EntityRewriterBase(T protocol, MetaType displayNameMetaType, int displayNameIndex, MetaType displayVisibilityMetaType, int displayVisibilityIndex) { - super(protocol); + super(protocol, false); this.displayNameMetaType = displayNameMetaType; this.displayNameIndex = displayNameIndex; this.displayVisibilityMetaType = displayVisibilityMetaType; this.displayVisibilityIndex = displayVisibilityIndex; } - protected EntityType getEntityType(UserConnection connection, int id) { - return getEntityTracker(connection).getEntityType(id); + @Override + public void handleMetadata(int entityId, List metadataList, UserConnection connection) { + super.handleMetadata(entityId, metadataList, connection); + + EntityType type = tracker(connection).entityType(entityId); + if (type == null) return; + + EntityData entityData = entityDataForType(type); + + // Set the mapped entity name if there is no custom name set already + Metadata meta = getMeta(displayNameIndex, metadataList); + if (meta != null && entityData != null && entityData.mobName() != null + && (meta.getValue() == null || meta.getValue().toString().isEmpty()) + && meta.metaType().typeId() == displayNameMetaType.typeId()) { + meta.setValue(entityData.mobName()); + if (ViaBackwards.getConfig().alwaysShowOriginalMobName()) { + removeMeta(displayVisibilityIndex, metadataList); + metadataList.add(new Metadata(displayVisibilityIndex, displayVisibilityMetaType, true)); + } + } + + // Add any other extra meta for mapped entities + if (entityData != null) { + //TODO only do this once for a first meta packet? + if (entityData != null && entityData.hasBaseMeta()) { + entityData.defaultMeta().createMeta(new WrappedMetadata(metadataList)); + } + } } - protected void addTrackedEntity(PacketWrapper wrapper, int entityId, EntityType type) throws Exception { - getEntityTracker(wrapper.user()).trackEntityType(entityId, type); + protected @Nullable Metadata getMeta(int metaIndex, List metadataList) { + for (Metadata metadata : metadataList) { + if (metadata.id() == metaIndex) { + return metadata; + } + } + return null; + } + + protected void removeMeta(int metaIndex, List metadataList) { + metadataList.removeIf(meta -> meta.id() == metaIndex); } protected boolean hasData(EntityType type) { - return entityTypes.containsKey(type); + return entityDataMappings.containsKey(type.getId()); } - protected @Nullable EntityData getEntityData(EntityType type) { - return entityTypes.get(type); + protected @Nullable EntityData entityDataForType(EntityType type) { + return entityDataMappings.get(type.getId()); + } + + protected @Nullable StoredEntityData storedEntityData(MetaHandlerEvent event) { + return tracker(event.user()).entityData(event.entityId()); } /** - * Note that both types should be of the SAME version. + * Maps an entity type to another with extra data. + * Note that both types should be of the same version. * - * @param oldType old type of the higher version - * @param replacement new type of the higher version + * @param type entity type + * @param mappedType mapped entity type * @return created entity data - * @see #mapEntityDirect(EntityType, EntityType) for id only rewriting + * @see #mapEntityType(EntityType, EntityType) for id only rewriting */ - protected EntityData mapEntity(EntityType oldType, EntityType replacement) { - Preconditions.checkArgument(oldType.getClass() == replacement.getClass()); + protected EntityData mapEntityTypeWithData(EntityType type, EntityType mappedType) { + Preconditions.checkArgument(type.getClass() == mappedType.getClass()); // Already rewrite the id here - int mappedReplacementId = getOldEntityId(replacement.getId()); - EntityData data = new EntityData(oldType.getId(), mappedReplacementId); - mapEntityDirect(oldType.getId(), mappedReplacementId); - entityTypes.put(oldType, data); + int mappedReplacementId = newEntityId(mappedType.getId()); + EntityData data = new EntityData(type.getId(), mappedReplacementId); + mapEntityType(type.getId(), mappedReplacementId); + entityDataMappings.put(type.getId(), data); return data; } @@ -119,167 +142,40 @@ public abstract class EntityRewriterBase extends Re * * @param oldTypes entity types of the higher version * @param newTypeClass entity types enum class of the lower version - * @param new type class + * @param new enum type */ - public & EntityType> void mapTypes(EntityType[] oldTypes, Class newTypeClass) { - if (typeMapping == null) { - typeMapping = new Int2IntOpenHashMap(oldTypes.length, 1F); - typeMapping.defaultReturnValue(-1); + @Override + 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 { - T newType = Enum.valueOf(newTypeClass, oldType.name()); - typeMapping.put(oldType.getId(), newType.getId()); + E newType = Enum.valueOf(newTypeClass, oldType.name()); + typeMappings.put(oldType.getId(), newType.getId()); } catch (IllegalArgumentException ignored) { + // Don't warn } } } - /** - * Directly maps the entity without any other rewriting. - * - * @param oldType type of the higher version - * @param newType type of the lower version - * @see #mapEntity(EntityType, EntityType) for mapping with data - */ - public void mapEntityDirect(EntityType oldType, EntityType newType) { - Preconditions.checkArgument(oldType.getClass() != newType.getClass()); - mapEntityDirect(oldType.getId(), newType.getId()); - } - - private void mapEntityDirect(int oldType, int newType) { - if (typeMapping == null) { - typeMapping = new Int2IntOpenHashMap(); - typeMapping.defaultReturnValue(-1); - } - typeMapping.put(oldType, newType); - } - - 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(); - for (MetaHandlerSettings settings : metaHandlers) { - List newData = new ArrayList<>(); - for (Metadata meta : storage.getMetaDataList()) { - MetaHandlerEvent event = null; - try { - Metadata modifiedMeta = meta; - if (settings.isGucci(type, meta)) { - event = new MetaHandlerEvent(user, storedEntity, meta.getId(), meta, storage); - modifiedMeta = settings.getHandler().handle(event); - - if (event.getExtraData() != null) { - newData.addAll(event.getExtraData()); - event.clearExtraData(); - } - } - - if (modifiedMeta == null) { - throw RemovedValueException.EX; - } - - newData.add(modifiedMeta); - } catch (RemovedValueException e) { - // Add the additionally created data here in case of an interruption - if (event != null && event.getExtraData() != null) { - newData.addAll(event.getExtraData()); - } - } catch (Exception e) { - Logger log = ViaBackwards.getPlatform().getLogger(); - log.warning("Unable to handle metadata " + meta + " for entity type " + type); - log.warning(storage.getMetaDataList().stream().sorted(Comparator.comparingInt(Metadata::getId)) - .map(Metadata::toString).collect(Collectors.joining("\n", "Full metadata list: ", ""))); - e.printStackTrace(); + public void registerMetaTypeHandler(@Nullable MetaType itemType, @Nullable MetaType blockType, @Nullable MetaType particleType, @Nullable MetaType optChatType) { + filter().handler((event, meta) -> { + MetaType type = meta.metaType(); + if (itemType != null && type == itemType) { + protocol.getBlockItemPackets().handleItemToClient(meta.value()); + } else if (blockType != null && type == blockType) { + int data = meta.value(); + meta.setValue(protocol.getMappingData().getNewBlockStateId(data)); + } else if (particleType != null && type == particleType) { + rewriteParticle(meta.value()); + } else if (optChatType != null && type == optChatType) { + JsonElement text = meta.value(); + if (text != null) { + protocol.getTranslatableRewriter().processText(text); } } - - storage.setMetaDataList(newData); - } - - // Handle Entity Name - Metadata data = storage.get(displayNameIndex); - if (data != null) { - EntityData entityData = getEntityData(type); - // Set the name if there is no custom name set already - if (entityData != null && entityData.getMobName() != null - && (data.getValue() == null || data.getValue().toString().isEmpty()) - && data.getMetaType().getTypeID() == displayNameMetaType.getTypeID()) { - data.setValue(entityData.getMobName()); - if (ViaBackwards.getConfig().alwaysShowOriginalMobName()) { - storage.delete(displayVisibilityIndex); - storage.add(new Metadata(displayVisibilityIndex, displayVisibilityMetaType, true)); - } - } - } - - return storage; - } - - /** - * Helper method to handle player, painting, or xp orb trackers without meta changes. - */ - protected void registerExtraTracker(ClientboundPacketType packetType, EntityType entityType, Type intType) { - getProtocol().registerClientbound(packetType, new PacketRemapper() { - @Override - public void registerMap() { - map(intType); // 0 - Entity id - handler(wrapper -> addTrackedEntity(wrapper, (int) wrapper.get(intType, 0), entityType)); - } - }); - } - - protected void registerExtraTracker(ClientboundPacketType packetType, EntityType entityType) { - registerExtraTracker(packetType, entityType, Type.VAR_INT); - } - - /** - * Sub 1.17 method for entity remove packets. - * - * @param packetType remove entities packet type - */ - protected void registerEntityDestroy(ClientboundPacketType packetType) { - getProtocol().registerClientbound(packetType, 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); - } - }); - } - }); - } - - /** - * 1.17+ method for entity remove packets. - * - * @param packetType remove entities packet type - */ - protected 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); - getEntityTracker(wrapper.user()).removeEntity(entity); - }); - } }); } @@ -287,7 +183,7 @@ public abstract class EntityRewriterBase extends Re 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())); + tracker(wrapper.user()).addEntity(wrapper.get(Type.VAR_INT, 0), typeFromId(id.intValue())); }; } @@ -296,29 +192,7 @@ public abstract class EntityRewriterBase extends Re } protected PacketHandler getTrackerHandler(EntityType entityType, Type intType) { - return wrapper -> addTrackedEntity(wrapper, (int) wrapper.get(intType, 0), entityType); - } - - protected PacketHandler getWorldDataTracker(int nbtIndex) { - return wrapper -> { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - - 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 { - ViaBackwards.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 { - ViaBackwards.getPlatform().getLogger().warning("Min Y missing in dimension data: " + registryData); - } - }; + return wrapper -> tracker(wrapper.user()).addEntity((int) wrapper.get(intType, 0), entityType); } protected PacketHandler getDimensionHandler(int index) { @@ -328,28 +202,4 @@ public abstract class EntityRewriterBase extends Re clientWorld.setEnvironment(dimensionId); }; } - - public EntityTracker.ProtocolEntityTracker getEntityTracker(UserConnection user) { - return user.get(EntityTracker.class).get(getProtocol()); - } - - 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)); - } - - protected abstract EntityType getTypeFromId(int typeId); - - public int getOldEntityId(int newId) { - return typeMapping != null ? typeMapping.getOrDefault(newId, newId) : newId; - } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriterBase.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriterBase.java index 0ebe73eb..4d7bacb3 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriterBase.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/ItemRewriterBase.java @@ -23,9 +23,10 @@ import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag; +import com.viaversion.viaversion.rewriter.RewriterBase; import org.checkerframework.checker.nullness.qual.Nullable; -public abstract class ItemRewriterBase extends Rewriter { +public abstract class ItemRewriterBase extends RewriterBase { protected final String nbtTagName; protected final boolean jsonNameFormat; diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java index 059e8517..2a6993e4 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacyEntityRewriter.java @@ -21,13 +21,14 @@ import com.viaversion.viabackwards.ViaBackwards; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.entities.storage.EntityData; import com.viaversion.viabackwards.api.entities.storage.EntityObjectData; -import com.viaversion.viabackwards.api.entities.storage.MetaStorage; +import com.viaversion.viabackwards.api.entities.storage.WrappedMetadata; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.ObjectType; import com.viaversion.viaversion.api.minecraft.metadata.MetaType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; @@ -83,14 +84,15 @@ public abstract class LegacyEntityRewriter extends 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); + addTrackedEntity(wrapper, wrapper.get(Type.INT, 0), playerType); }); } }); } - protected void registerMetadataRewriter(ClientboundPacketType packetType, Type> oldMetaType, Type> newMetaType) { - getProtocol().registerClientbound(packetType, new PacketRemapper() { + @Override + public void registerMetadataRewriter(ClientboundPacketType packetType, Type> oldMetaType, Type> newMetaType) { + protocol.registerClientbound(packetType, new PacketRemapper() { @Override public void registerMap() { map(Type.VAR_INT); // 0 - Entity ID @@ -101,35 +103,32 @@ public abstract class LegacyEntityRewriter extends } 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()); + handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user()); }); } }); } - protected void registerMetadataRewriter(ClientboundPacketType packetType, Type> metaType) { + @Override + public void registerMetadataRewriter(ClientboundPacketType packetType, Type> metaType) { registerMetadataRewriter(packetType, null, metaType); } protected PacketHandler getMobSpawnRewriter(Type> metaType) { return wrapper -> { int entityId = wrapper.get(Type.VAR_INT, 0); - EntityType type = getEntityType(wrapper.user(), entityId); + EntityType type = tracker(wrapper.user()).entityType(entityId); - MetaStorage storage = new MetaStorage(wrapper.get(metaType, 0)); - handleMeta(wrapper.user(), entityId, storage); + List metadata = wrapper.get(metaType, 0); + handleMetadata(entityId, metadata, wrapper.user()); - EntityData entityData = getEntityData(type); + EntityData entityData = entityDataForType(type); if (entityData != null) { - wrapper.set(Type.VAR_INT, 1, entityData.getReplacementId()); + wrapper.set(Type.VAR_INT, 1, entityData.replacementId()); if (entityData.hasBaseMeta()) { - entityData.getDefaultMeta().createMeta(storage); + entityData.defaultMeta().createMeta(new WrappedMetadata(metadata)); } } - - // Rewrite Metadata - wrapper.set(metaType, 0, storage.getMetaDataList()); }; } @@ -140,10 +139,8 @@ public abstract class LegacyEntityRewriter extends 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); + List metadata = wrapper.get(metaType, 0); + handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user()); }; } @@ -157,15 +154,20 @@ public abstract class LegacyEntityRewriter extends EntityData data = getObjectData(type); if (data != null) { - wrapper.set(Type.BYTE, 0, (byte) data.getReplacementId()); - if (data.getObjectData() != -1) { - wrapper.set(Type.INT, 0, data.getObjectData()); + wrapper.set(Type.BYTE, 0, (byte) data.replacementId()); + if (data.objectData() != -1) { + wrapper.set(Type.INT, 0, data.objectData()); } } }; } protected EntityType getObjectTypeFromId(int typeId) { - return getTypeFromId(typeId); + return typeFromId(typeId); + } + + @Deprecated + protected void addTrackedEntity(PacketWrapper wrapper, int entityId, EntityType type) throws Exception { + tracker(wrapper.user()).addEntity(entityId, type); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacySoundRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacySoundRewriter.java index 3358b3a1..067cdd24 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacySoundRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/LegacySoundRewriter.java @@ -21,9 +21,10 @@ package com.viaversion.viabackwards.api.rewriters; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap; import com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap; +import com.viaversion.viaversion.rewriter.RewriterBase; @Deprecated -public abstract class LegacySoundRewriter extends Rewriter { +public abstract class LegacySoundRewriter extends RewriterBase { protected final Int2ObjectMap soundRewrites = new Int2ObjectOpenHashMap<>(64); protected LegacySoundRewriter(T protocol) { diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/Rewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/Rewriter.java deleted file mode 100644 index 41bfbd58..00000000 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/Rewriter.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.viabackwards.api.rewriters; - -import com.viaversion.viabackwards.api.BackwardsProtocol; - -public abstract class Rewriter { - protected final T protocol; - - protected Rewriter(final T protocol) { - this.protocol = protocol; - } - - /** - * Register everything. - */ - public void register() { - registerPackets(); - registerRewrites(); - } - - /** - * Register packet listeners. - */ - protected abstract void registerPackets(); - - /** - * Register rewrites. - */ - protected void registerRewrites() { - } - - public T getProtocol() { - return protocol; - } -} diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java index fa6570bf..90612f5f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/Protocol1_10To1_11.java @@ -26,6 +26,8 @@ import com.viaversion.viabackwards.protocol.protocol1_10to1_11.packets.EntityPac import com.viaversion.viabackwards.protocol.protocol1_10to1_11.packets.PlayerPackets1_11; import com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.WindowTracker; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; 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; @@ -58,7 +60,7 @@ public class Protocol1_10To1_11 extends BackwardsProtocol { - Metadata data = e.getData(); - - if (data.getMetaType().getType().equals(Type.ITEM)) // Is Item - data.setValue(handleItemToClient((Item) data.getValue())); - - return data; + protocol.getEntityPackets().filter().handler((event, meta) -> { + if (meta.metaType().type().equals(Type.ITEM)) // Is Item + meta.setValue(handleItemToClient((Item) meta.getValue())); }); } @@ -404,9 +400,9 @@ public class BlockItemPackets1_11 extends LegacyBlockItemRewriter getChestedHorse(UserConnection user) { WindowTracker tracker = user.get(WindowTracker.class); if (tracker.getInventory() != null && tracker.getInventory().equals("EntityHorse")) { - EntityTracker.ProtocolEntityTracker entTracker = user.get(EntityTracker.class).get(getProtocol()); - EntityTracker.StoredEntity storedEntity = entTracker.getEntity(tracker.getEntityId()); - if (storedEntity != null) - return Optional.of(storedEntity.get(ChestedHorseStorage.class)); + EntityTracker entTracker = user.getEntityTracker(Protocol1_10To1_11.class); + StoredEntityData entityData = entTracker.entityData(tracker.getEntityId()); + if (entityData != null) + return Optional.of(entityData.get(ChestedHorseStorage.class)); } return Optional.empty(); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java index 89407b58..3249deee 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java @@ -19,13 +19,13 @@ package com.viaversion.viabackwards.protocol.protocol1_10to1_11.packets; import com.viaversion.viabackwards.api.entities.storage.EntityData; -import com.viaversion.viabackwards.api.entities.storage.MetaStorage; -import com.viaversion.viabackwards.api.exceptions.RemovedValueException; +import com.viaversion.viabackwards.api.entities.storage.WrappedMetadata; import com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter; import com.viaversion.viabackwards.protocol.protocol1_10to1_11.PotionSplashHandler; import com.viaversion.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11; import com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage; import com.viaversion.viabackwards.utils.Block; +import com.viaversion.viaversion.api.data.entity.StoredEntityData; import com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types; import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; @@ -38,6 +38,7 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_9; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; +import java.util.List; import java.util.Optional; public class EntityPackets1_11 extends LegacyEntityRewriter { @@ -98,7 +99,7 @@ public class EntityPackets1_11 extends LegacyEntityRewriter int objType = objectData & 4095; int data = objectData >> 12 & 15; - Block block = getProtocol().getBlockItemPackets().handleBlock(objType, data); + Block block = protocol.getBlockItemPackets().handleBlock(objType, data); if (block == null) return; @@ -109,8 +110,8 @@ public class EntityPackets1_11 extends LegacyEntityRewriter } }); - registerExtraTracker(ClientboundPackets1_9_3.SPAWN_EXPERIENCE_ORB, Entity1_11Types.EntityType.EXPERIENCE_ORB); - registerExtraTracker(ClientboundPackets1_9_3.SPAWN_GLOBAL_ENTITY, Entity1_11Types.EntityType.WEATHER); + registerTracker(ClientboundPackets1_9_3.SPAWN_EXPERIENCE_ORB, Entity1_11Types.EntityType.EXPERIENCE_ORB); + registerTracker(ClientboundPackets1_9_3.SPAWN_GLOBAL_ENTITY, Entity1_11Types.EntityType.WEATHER); protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_MOB, new PacketRemapper() { @Override @@ -137,35 +138,24 @@ public class EntityPackets1_11 extends LegacyEntityRewriter @Override public void handle(PacketWrapper wrapper) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - EntityType type = getEntityType(wrapper.user(), entityId); + EntityType type = tracker(wrapper.user()).entityType(entityId); - MetaStorage storage = new MetaStorage(wrapper.get(Types1_9.METADATA_LIST, 0)); - handleMeta( - wrapper.user(), - wrapper.get(Type.VAR_INT, 0), - storage - ); + List list = wrapper.get(Types1_9.METADATA_LIST, 0); + handleMetadata(wrapper.get(Type.VAR_INT, 0), list, wrapper.user()); - EntityData entityData = getEntityData(type); + EntityData entityData = entityDataForType(type); if (entityData != null) { - wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.getReplacementId()); + wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.replacementId()); if (entityData.hasBaseMeta()) { - entityData.getDefaultMeta().createMeta(storage); + entityData.defaultMeta().createMeta(new WrappedMetadata(list)); } } - - // Rewrite Metadata - wrapper.set( - Types1_9.METADATA_LIST, - 0, - storage.getMetaDataList() - ); } }); } }); - registerExtraTracker(ClientboundPackets1_9_3.SPAWN_PAINTING, Entity1_11Types.EntityType.PAINTING); + registerTracker(ClientboundPackets1_9_3.SPAWN_PAINTING, Entity1_11Types.EntityType.PAINTING); registerJoinGame(ClientboundPackets1_9_3.JOIN_GAME, Entity1_11Types.EntityType.PLAYER); registerRespawn(ClientboundPackets1_9_3.RESPAWN); @@ -185,7 +175,7 @@ public class EntityPackets1_11 extends LegacyEntityRewriter } }); - registerEntityDestroy(ClientboundPackets1_9_3.DESTROY_ENTITIES); + registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES); registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST); protocol.registerClientbound(ClientboundPackets1_9_3.ENTITY_STATUS, new PacketRemapper() { @@ -214,99 +204,86 @@ public class EntityPackets1_11 extends LegacyEntityRewriter @Override protected void registerRewrites() { // Guardian - mapEntity(Entity1_11Types.EntityType.ELDER_GUARDIAN, Entity1_11Types.EntityType.GUARDIAN); + mapEntityTypeWithData(Entity1_11Types.EntityType.ELDER_GUARDIAN, Entity1_11Types.EntityType.GUARDIAN); // Skeletons - mapEntity(Entity1_11Types.EntityType.WITHER_SKELETON, Entity1_11Types.EntityType.SKELETON).mobName("Wither Skeleton").spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(1))); - mapEntity(Entity1_11Types.EntityType.STRAY, Entity1_11Types.EntityType.SKELETON).mobName("Stray").spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(2))); + mapEntityTypeWithData(Entity1_11Types.EntityType.WITHER_SKELETON, Entity1_11Types.EntityType.SKELETON).mobName("Wither Skeleton").spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(1))); + mapEntityTypeWithData(Entity1_11Types.EntityType.STRAY, Entity1_11Types.EntityType.SKELETON).mobName("Stray").spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(2))); // Zombies - mapEntity(Entity1_11Types.EntityType.HUSK, Entity1_11Types.EntityType.ZOMBIE).mobName("Husk").spawnMetadata(storage -> handleZombieType(storage, 6)); - mapEntity(Entity1_11Types.EntityType.ZOMBIE_VILLAGER, Entity1_11Types.EntityType.ZOMBIE).spawnMetadata(storage -> handleZombieType(storage, 1)); + mapEntityTypeWithData(Entity1_11Types.EntityType.HUSK, Entity1_11Types.EntityType.ZOMBIE).mobName("Husk").spawnMetadata(storage -> handleZombieType(storage, 6)); + mapEntityTypeWithData(Entity1_11Types.EntityType.ZOMBIE_VILLAGER, Entity1_11Types.EntityType.ZOMBIE).spawnMetadata(storage -> handleZombieType(storage, 1)); // Horses - 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))); + mapEntityTypeWithData(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. + mapEntityTypeWithData(Entity1_11Types.EntityType.DONKEY, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(1))); + mapEntityTypeWithData(Entity1_11Types.EntityType.MULE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(2))); + mapEntityTypeWithData(Entity1_11Types.EntityType.SKELETON_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(4))); + mapEntityTypeWithData(Entity1_11Types.EntityType.ZOMBIE_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(3))); // New mobs - 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); + mapEntityTypeWithData(Entity1_11Types.EntityType.EVOCATION_FANGS, Entity1_11Types.EntityType.SHULKER); + mapEntityTypeWithData(Entity1_11Types.EntityType.EVOCATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).mobName("Evoker"); + mapEntityTypeWithData(Entity1_11Types.EntityType.VEX, Entity1_11Types.EntityType.BAT).mobName("Vex"); + mapEntityTypeWithData(Entity1_11Types.EntityType.VINDICATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).mobName("Vindicator").spawnMetadata(storage -> storage.add(new Metadata(13, MetaType1_9.VarInt, 4))); // Base Profession + mapEntityTypeWithData(Entity1_11Types.EntityType.LIAMA, Entity1_11Types.EntityType.HORSE).mobName("Llama").spawnMetadata(storage -> storage.add(getHorseMetaType(1))); + mapEntityTypeWithData(Entity1_11Types.EntityType.LIAMA_SPIT, Entity1_11Types.EntityType.SNOWBALL); mapObjectType(Entity1_11Types.ObjectType.LIAMA_SPIT, Entity1_11Types.ObjectType.SNOWBALL, -1); // Replace with endertorchthingies 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 -> { - Metadata data = e.getData(); - - boolean b = (boolean) data.getValue(); + filter().filterFamily(Entity1_11Types.EntityType.GUARDIAN).index(12).handler((event, meta) -> { + boolean b = (boolean) meta.getValue(); int bitmask = b ? 0x02 : 0; - if (e.getEntity().getType().is(Entity1_11Types.EntityType.ELDER_GUARDIAN)) + if (event.entityType() == Entity1_11Types.EntityType.ELDER_GUARDIAN) { bitmask |= 0x04; + } - data.setMetaType(MetaType1_9.Byte); - data.setValue((byte) bitmask); - - return data; + meta.setMetaType(MetaType1_9.Byte); + meta.setValue((byte) bitmask); }); // Handle skeleton swing - registerMetaHandler().filter(Entity1_11Types.EntityType.ABSTRACT_SKELETON, true, 12).handleIndexChange(13); + filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_SKELETON).index(12).toIndex(13); /* ZOMBIE CHANGES */ - registerMetaHandler().filter(Entity1_11Types.EntityType.ZOMBIE, true).handle(e -> { - Metadata data = e.getData(); - - switch (data.getId()) { + filter().filterFamily(Entity1_11Types.EntityType.ZOMBIE).handler((event, meta) -> { + switch (meta.id()) { case 13: - throw RemovedValueException.EX; + event.cancel(); + return; case 14: - data.setId(15); + event.setIndex(15); break; case 15: - data.setId(14); + event.setIndex(14); break; // Profession case 16: - data.setId(13); - data.setValue(1 + (int) data.getValue()); + event.setIndex(13); + meta.setValue(1 + (int) meta.getValue()); break; } - - return data; }); // Handle Evocation Illager - registerMetaHandler().filter(Entity1_11Types.EntityType.EVOCATION_ILLAGER, 12).handle(e -> { - Metadata data = e.getData(); - data.setId(13); - data.setMetaType(MetaType1_9.VarInt); - data.setValue(((Byte) data.getValue()).intValue()); // Change the profession for the states - - return data; + filter().type(Entity1_11Types.EntityType.EVOCATION_ILLAGER).index(12).handler((event, meta) -> { + event.setIndex(13); + meta.setMetaType(MetaType1_9.VarInt); + meta.setValue(((Byte) meta.getValue()).intValue()); // Change the profession for the states }); // Handle Vex (Remove this field completely since the position is not updated correctly when idling for bats. Sad ): - registerMetaHandler().filter(Entity1_11Types.EntityType.VEX, 12).handle(e -> { - Metadata data = e.getData(); - data.setValue((byte) 0x00); - return data; + filter().type(Entity1_11Types.EntityType.VEX).index(12).handler((event, meta) -> { + meta.setValue((byte) 0x00); }); // Handle VindicationIllager - registerMetaHandler().filter(Entity1_11Types.EntityType.VINDICATION_ILLAGER, 12).handle(e -> { - Metadata data = e.getData(); - data.setId(13); - data.setMetaType(MetaType1_9.VarInt); - data.setValue(((Number) data.getValue()).intValue() == 1 ? 2 : 4); - return data; + filter().type(Entity1_11Types.EntityType.VINDICATION_ILLAGER).index(12).handler((event, meta) -> { + event.setIndex(13); + meta.setMetaType(MetaType1_9.VarInt); + meta.setValue(((Number) meta.getValue()).intValue() == 1 ? 2 : 4); }); /* @@ -314,72 +291,70 @@ public class EntityPackets1_11 extends LegacyEntityRewriter */ // Handle horse flags - registerMetaHandler().filter(Entity1_11Types.EntityType.ABSTRACT_HORSE, true, 13).handle(e -> { - Metadata data = e.getData(); - byte b = (byte) data.getValue(); - if (e.getEntity().has(ChestedHorseStorage.class) && - e.getEntity().get(ChestedHorseStorage.class).isChested()) { + filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_HORSE).index(13).handler((event, meta) -> { + StoredEntityData data = storedEntityData(event); + byte b = (byte) meta.getValue(); + if (data.has(ChestedHorseStorage.class) && data.get(ChestedHorseStorage.class).isChested()) { b |= 0x08; // Chested - data.setValue(b); + meta.setValue(b); } - return data; }); // Create chested horse storage - registerMetaHandler().filter(Entity1_11Types.EntityType.CHESTED_HORSE, true).handle(e -> { - if (!e.getEntity().has(ChestedHorseStorage.class)) - e.getEntity().put(new ChestedHorseStorage()); - return e.getData(); + filter().filterFamily(Entity1_11Types.EntityType.CHESTED_HORSE).handler((event, meta) -> { + StoredEntityData data = storedEntityData(event); + if (!data.has(ChestedHorseStorage.class)) { + data.put(new ChestedHorseStorage()); + } }); // Handle horse armor - registerMetaHandler().filter(Entity1_11Types.EntityType.HORSE, 16).handleIndexChange(17); + filter().type(Entity1_11Types.EntityType.HORSE).index(16).toIndex(17); // Handle chested horse - registerMetaHandler().filter(Entity1_11Types.EntityType.CHESTED_HORSE, true, 15).handle(e -> { - ChestedHorseStorage storage = e.getEntity().get(ChestedHorseStorage.class); - boolean b = (boolean) e.getData().getValue(); + filter().filterFamily(Entity1_11Types.EntityType.CHESTED_HORSE).index(15).handler((event, meta) -> { + StoredEntityData data = storedEntityData(event); + ChestedHorseStorage storage = data.get(ChestedHorseStorage.class); + boolean b = (boolean) meta.getValue(); storage.setChested(b); - - throw RemovedValueException.EX; + event.cancel(); }); // Get rid of Liama metadata - registerMetaHandler().filter(Entity1_11Types.EntityType.LIAMA).handle(e -> { - Metadata data = e.getData(); - ChestedHorseStorage storage = e.getEntity().get(ChestedHorseStorage.class); + filter().type(Entity1_11Types.EntityType.LIAMA).handler((event, meta) -> { + StoredEntityData data = storedEntityData(event); + ChestedHorseStorage storage = data.get(ChestedHorseStorage.class); - int index = e.getIndex(); + int index = event.index(); // Store them for later (: switch (index) { case 16: - storage.setLiamaStrength((int) data.getValue()); - throw RemovedValueException.EX; + storage.setLiamaStrength((int) meta.getValue()); + event.cancel(); + break; case 17: - storage.setLiamaCarpetColor((int) data.getValue()); - throw RemovedValueException.EX; + storage.setLiamaCarpetColor((int) meta.getValue()); + event.cancel(); + break; case 18: - storage.setLiamaVariant((int) data.getValue()); - throw RemovedValueException.EX; + storage.setLiamaVariant((int) meta.getValue()); + event.cancel(); + break; } - return e.getData(); }); // Handle Horse (Correct owner) - registerMetaHandler().filter(Entity1_11Types.EntityType.ABSTRACT_HORSE, true, 14).handleIndexChange(16); + filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_HORSE).index(14).toIndex(16); // Handle villager - Change non-existing profession - registerMetaHandler().filter(Entity1_11Types.EntityType.VILLAGER, 13).handle(e -> { - Metadata data = e.getData(); - if ((int) data.getValue() == 5) - data.setValue(0); - - return data; + filter().type(Entity1_11Types.EntityType.VILLAGER).index(13).handler((event, meta) -> { + if ((int) meta.getValue() == 5) { + meta.setValue(0); + } }); // handle new Shulker color meta - registerMetaHandler().filter(Entity1_11Types.EntityType.SHULKER, 15).removed(); - + filter().type(Entity1_11Types.EntityType.SHULKER).cancel(15); } /* @@ -401,7 +376,7 @@ public class EntityPackets1_11 extends LegacyEntityRewriter return new Metadata(13, MetaType1_9.VarInt, type); } - private void handleZombieType(MetaStorage storage, int type) { + private void handleZombieType(WrappedMetadata storage, int type) { Metadata meta = storage.get(13); if (meta == null) { storage.add(getZombieTypeMeta(type)); @@ -420,7 +395,7 @@ public class EntityPackets1_11 extends LegacyEntityRewriter } @Override - protected EntityType getTypeFromId(int typeId) { + public EntityType typeFromId(int typeId) { return Entity1_11Types.getTypeFromId(typeId, false); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/storage/ChestedHorseStorage.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/storage/ChestedHorseStorage.java index 88a6545d..dbac5b92 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/storage/ChestedHorseStorage.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_10to1_11/storage/ChestedHorseStorage.java @@ -18,9 +18,7 @@ package com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage; -import com.viaversion.viabackwards.api.entities.storage.EntityStorage; - -public class ChestedHorseStorage implements EntityStorage { +public class ChestedHorseStorage { private boolean chested; private int liamaStrength; private int liamaCarpetColor = -1; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/Protocol1_11_1To1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/Protocol1_11_1To1_12.java index d2d2028f..d83bc0a2 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/Protocol1_11_1To1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/Protocol1_11_1To1_12.java @@ -25,8 +25,10 @@ import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.ChatPac import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.EntityPackets1_12; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets.SoundPackets1_12; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; 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.libs.gson.JsonElement; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ServerboundPackets1_12; @@ -76,7 +78,7 @@ public class Protocol1_11_1To1_12 extends BackwardsProtocol { - Metadata data = e.getData(); - - if (data.getMetaType().getType().equals(Type.ITEM)) // Is Item - data.setValue(handleItemToClient((Item) data.getValue())); - - return data; + protocol.getEntityPackets().filter().handler((event, meta) -> { + if (meta.metaType().type().equals(Type.ITEM)) // Is Item + meta.setValue(handleItemToClient((Item) meta.getValue())); }); protocol.registerServerbound(ServerboundPackets1_9_3.CLIENT_STATUS, new PacketRemapper() { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java index 8789bd8e..5874011f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/ChatPackets1_12.java @@ -18,7 +18,6 @@ package com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets; -import com.viaversion.viabackwards.api.rewriters.Rewriter; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.AdvancementTranslations; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; @@ -27,8 +26,9 @@ import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12; import com.viaversion.viaversion.rewriter.ComponentRewriter; +import com.viaversion.viaversion.rewriter.RewriterBase; -public class ChatPackets1_12 extends Rewriter { +public class ChatPackets1_12 extends RewriterBase { private final ComponentRewriter componentRewriter = new ComponentRewriter() { @Override diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java index b4c10134..809c31f6 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java @@ -18,12 +18,12 @@ package com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.packets; -import com.viaversion.viabackwards.api.exceptions.RemovedValueException; import com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ParrotStorage; import com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker; import com.viaversion.viabackwards.utils.Block; +import com.viaversion.viaversion.api.data.entity.StoredEntityData; import com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.metadata.Metadata; @@ -73,7 +73,7 @@ public class EntityPackets1_12 extends LegacyEntityRewriter> 12 & 15; - Block block = getProtocol().getBlockItemPackets().handleBlock(objType, data); + Block block = protocol.getBlockItemPackets().handleBlock(objType, data); if (block == null) { return; } @@ -85,8 +85,8 @@ public class EntityPackets1_12 extends LegacyEntityRewriter storage.add(new Metadata(12, MetaType1_12.Byte, (byte) 0x00))); - mapEntity(Entity1_12Types.EntityType.ILLUSION_ILLAGER, Entity1_12Types.EntityType.EVOCATION_ILLAGER).mobName("Illusioner"); + mapEntityTypeWithData(Entity1_12Types.EntityType.PARROT, Entity1_12Types.EntityType.BAT).mobName("Parrot").spawnMetadata(storage -> storage.add(new Metadata(12, MetaType1_12.Byte, (byte) 0x00))); + mapEntityTypeWithData(Entity1_12Types.EntityType.ILLUSION_ILLAGER, Entity1_12Types.EntityType.EVOCATION_ILLAGER).mobName("Illusioner"); // Handle Illager - registerMetaHandler().filter(Entity1_12Types.EntityType.EVOCATION_ILLAGER, true, 12).removed(); - registerMetaHandler().filter(Entity1_12Types.EntityType.EVOCATION_ILLAGER, true, 13).handleIndexChange(12); + filter().filterFamily(Entity1_12Types.EntityType.EVOCATION_ILLAGER).cancel(12); + filter().filterFamily(Entity1_12Types.EntityType.EVOCATION_ILLAGER).index(13).toIndex(12); - registerMetaHandler().filter(Entity1_12Types.EntityType.ILLUSION_ILLAGER, 0).handle(e -> { - byte mask = (byte) e.getData().getValue(); + filter().type(Entity1_12Types.EntityType.ILLUSION_ILLAGER).index(0).handler((event, meta) -> { + byte mask = (byte) meta.getValue(); - if ((mask & 0x20) == 0x20) + if ((mask & 0x20) == 0x20) { mask &= ~0x20; + } - e.getData().setValue(mask); - return e.getData(); + meta.setValue(mask); }); // Create Parrot storage - registerMetaHandler().filter(Entity1_12Types.EntityType.PARROT, true).handle(e -> { - if (!e.getEntity().has(ParrotStorage.class)) - e.getEntity().put(new ParrotStorage()); - return e.getData(); + filter().filterFamily(Entity1_12Types.EntityType.PARROT).handler((event, meta) -> { + StoredEntityData data = storedEntityData(event); + if (!data.has(ParrotStorage.class)) { + data.put(new ParrotStorage()); + } }); // Parrot remove animal metadata - registerMetaHandler().filter(Entity1_12Types.EntityType.PARROT, 12).removed(); // Is baby - registerMetaHandler().filter(Entity1_12Types.EntityType.PARROT, 13).handle(e -> { - Metadata data = e.getData(); - ParrotStorage storage = e.getEntity().get(ParrotStorage.class); - boolean isSitting = (((byte) data.getValue()) & 0x01) == 0x01; - boolean isTamed = (((byte) data.getValue()) & 0x04) == 0x04; + filter().type(Entity1_12Types.EntityType.PARROT).cancel(12); // Is baby + filter().type(Entity1_12Types.EntityType.PARROT).index(13).handler((event, meta) -> { + StoredEntityData data = storedEntityData(event); + ParrotStorage storage = data.get(ParrotStorage.class); + boolean isSitting = (((byte) meta.getValue()) & 0x01) == 0x01; + boolean isTamed = (((byte) meta.getValue()) & 0x04) == 0x04; if (!storage.isTamed() && isTamed) { // TODO do something to let the user know it's done @@ -250,30 +251,29 @@ public class EntityPackets1_12 extends LegacyEntityRewriter { - CompoundTag tag = (CompoundTag) e.getData().getValue(); - ShoulderTracker tracker = e.getUser().get(ShoulderTracker.class); + filter().type(Entity1_12Types.EntityType.PLAYER).index(15).handler((event, meta) -> { + CompoundTag tag = (CompoundTag) meta.getValue(); + ShoulderTracker tracker = event.user().get(ShoulderTracker.class); if (tag.isEmpty() && tracker.getLeftShoulder() != null) { tracker.setLeftShoulder(null); tracker.update(); - } else if (tag.contains("id") && e.getEntity().getEntityId() == tracker.getEntityId()) { + } else if (tag.contains("id") && event.entityId() == tracker.getEntityId()) { String id = (String) tag.get("id").getValue(); if (tracker.getLeftShoulder() == null || !tracker.getLeftShoulder().equals(id)) { tracker.setLeftShoulder(id); @@ -281,18 +281,18 @@ public class EntityPackets1_12 extends LegacyEntityRewriter { - CompoundTag tag = (CompoundTag) e.getData().getValue(); - ShoulderTracker tracker = e.getUser().get(ShoulderTracker.class); + filter().type(Entity1_12Types.EntityType.PLAYER).index(16).handler((event, meta) -> { + CompoundTag tag = (CompoundTag) event.meta().getValue(); + ShoulderTracker tracker = event.user().get(ShoulderTracker.class); if (tag.isEmpty() && tracker.getRightShoulder() != null) { tracker.setRightShoulder(null); tracker.update(); - } else if (tag.contains("id") && e.getEntity().getEntityId() == tracker.getEntityId()) { + } else if (tag.contains("id") && event.entityId() == tracker.getEntityId()) { String id = (String) tag.get("id").getValue(); if (tracker.getRightShoulder() == null || !tracker.getRightShoulder().equals(id)) { tracker.setRightShoulder(id); @@ -300,12 +300,12 @@ public class EntityPackets1_12 extends LegacyEntityRewriter { - Metadata data = e.getData(); - - if (data.getMetaType().getType().equals(Type.ITEM)) { // Is Item - data.setValue(handleItemToClient((Item) data.getValue())); + protocol.getEntityPackets().filter().handler((event, meta) -> { + if (meta.metaType().type().equals(Type.ITEM)) { // Is Item + meta.setValue(handleItemToClient((Item) meta.getValue())); } - - return data; }); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java index 1ad29d30..668bee11 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/Protocol1_12_2To1_13.java @@ -33,6 +33,8 @@ import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerP import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.TabCompleteStorage; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types; +import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1; @@ -99,7 +101,7 @@ public class Protocol1_12_2To1_13 extends BackwardsProtocol { + mapEntityTypeWithData(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 - mapEntity(Entity1_13Types.EntityType.DOLPHIN, Entity1_13Types.EntityType.SQUID).mobName("Dolphin"); + mapEntityTypeWithData(Entity1_13Types.EntityType.DOLPHIN, Entity1_13Types.EntityType.SQUID).mobName("Dolphin"); // Turtle - mapEntity(Entity1_13Types.EntityType.TURTLE, Entity1_13Types.EntityType.OCELOT).mobName("Turtle"); + mapEntityTypeWithData(Entity1_13Types.EntityType.TURTLE, Entity1_13Types.EntityType.OCELOT).mobName("Turtle"); // Rewrite Meta types - registerMetaHandler().handle(e -> { - Metadata meta = e.getData(); - int typeId = meta.getMetaType().getTypeID(); + filter().handler((event, meta) -> { + int typeId = meta.metaType().typeId(); // Rewrite optional chat to chat if (typeId == 5) { @@ -334,63 +332,49 @@ public class EntityPackets1_13 extends LegacyEntityRewriter { - Metadata meta = e.getData(); + filter().filterFamily(Entity1_13Types.EntityType.ENTITY).index(2).handler((event, meta) -> { String value = meta.getValue().toString(); - if (value.isEmpty()) return meta; - meta.setValue(ChatRewriter.jsonToLegacyText(value)); - return meta; + if (!value.isEmpty()) { + meta.setValue(ChatRewriter.jsonToLegacyText(value)); + } }); // Handle zombie metadata - registerMetaHandler().filter(Entity1_13Types.EntityType.ZOMBIE, true, 15).removed(); - registerMetaHandler().filter(Entity1_13Types.EntityType.ZOMBIE, true).handle(e -> { - Metadata meta = e.getData(); - - if (meta.getId() > 15) { - meta.setId(meta.getId() - 1); - } - - return meta; - }); + filter().filterFamily(Entity1_13Types.EntityType.ZOMBIE).removeIndex(15); // Handle turtle metadata (Remove them all for now) - registerMetaHandler().filter(Entity1_13Types.EntityType.TURTLE, 13).removed(); // Home pos - registerMetaHandler().filter(Entity1_13Types.EntityType.TURTLE, 14).removed(); // Has egg - registerMetaHandler().filter(Entity1_13Types.EntityType.TURTLE, 15).removed(); // Laying egg - registerMetaHandler().filter(Entity1_13Types.EntityType.TURTLE, 16).removed(); // Travel pos - registerMetaHandler().filter(Entity1_13Types.EntityType.TURTLE, 17).removed(); // Going home - registerMetaHandler().filter(Entity1_13Types.EntityType.TURTLE, 18).removed(); // Traveling + filter().type(Entity1_13Types.EntityType.TURTLE).cancel(13); // Home pos + filter().type(Entity1_13Types.EntityType.TURTLE).cancel(14); // Has egg + filter().type(Entity1_13Types.EntityType.TURTLE).cancel(15); // Laying egg + filter().type(Entity1_13Types.EntityType.TURTLE).cancel(16); // Travel pos + filter().type(Entity1_13Types.EntityType.TURTLE).cancel(17); // Going home + filter().type(Entity1_13Types.EntityType.TURTLE).cancel(18); // Traveling // Remove additional fish meta - registerMetaHandler().filter(Entity1_13Types.EntityType.ABSTRACT_FISHES, true, 12).removed(); - registerMetaHandler().filter(Entity1_13Types.EntityType.ABSTRACT_FISHES, true, 13).removed(); + filter().filterFamily(Entity1_13Types.EntityType.ABSTRACT_FISHES).cancel(12); + filter().filterFamily(Entity1_13Types.EntityType.ABSTRACT_FISHES).cancel(13); // Remove phantom size - registerMetaHandler().filter(Entity1_13Types.EntityType.PHANTOM, 12).removed(); + filter().type(Entity1_13Types.EntityType.PHANTOM).cancel(12); // Remove boat splash timer - registerMetaHandler().filter(Entity1_13Types.EntityType.BOAT, 12).removed(); + filter().type(Entity1_13Types.EntityType.BOAT).cancel(12); // Remove Trident special loyalty level - registerMetaHandler().filter(Entity1_13Types.EntityType.TRIDENT, 7).removed(); + filter().type(Entity1_13Types.EntityType.TRIDENT).cancel(7); // Handle new wolf colors - registerMetaHandler().filter(Entity1_13Types.EntityType.WOLF, 17).handle(e -> { - Metadata meta = e.getData(); + filter().type(Entity1_13Types.EntityType.WOLF).index(17).handler((event, meta) -> { meta.setValue(15 - (int) meta.getValue()); - return meta; }); // Rewrite AreaEffectCloud - registerMetaHandler().filter(Entity1_13Types.EntityType.AREA_EFFECT_CLOUD, 9).handle(e -> { - Metadata meta = e.getData(); + filter().type(Entity1_13Types.EntityType.AREA_EFFECT_CLOUD).index(9).handler((event, meta) -> { Particle particle = (Particle) meta.getValue(); ParticleMapping.ParticleData data = ParticleMapping.getMapping(particle.getId()); @@ -408,16 +392,16 @@ public class EntityPackets1_13 extends LegacyEntityRewriter { +public class PlayerPacket1_13 extends RewriterBase { private final CommandRewriter commandRewriter = new CommandRewriter(protocol) { }; @@ -93,16 +93,16 @@ public class PlayerPacket1_13 extends Rewriter { for (int i = 0; i < size; i++) { //Input Item Item input = wrapper.read(Type.FLAT_ITEM); - wrapper.write(Type.ITEM, getProtocol().getBlockItemPackets().handleItemToClient(input)); + wrapper.write(Type.ITEM, protocol.getBlockItemPackets().handleItemToClient(input)); //Output Item Item output = wrapper.read(Type.FLAT_ITEM); - wrapper.write(Type.ITEM, getProtocol().getBlockItemPackets().handleItemToClient(output)); + wrapper.write(Type.ITEM, protocol.getBlockItemPackets().handleItemToClient(output)); boolean secondItem = wrapper.passthrough(Type.BOOLEAN); //Has second item if (secondItem) { //Second Item Item second = wrapper.read(Type.FLAT_ITEM); - wrapper.write(Type.ITEM, getProtocol().getBlockItemPackets().handleItemToClient(second)); + wrapper.write(Type.ITEM, protocol.getBlockItemPackets().handleItemToClient(second)); } wrapper.passthrough(Type.BOOLEAN); //Trade disabled @@ -448,7 +448,7 @@ public class PlayerPacket1_13 extends Rewriter { case "MC|BEdit": wrapper.setId(0x0B); Item book = wrapper.read(Type.ITEM); - wrapper.write(Type.FLAT_ITEM, getProtocol().getBlockItemPackets().handleItemToServer(book)); + wrapper.write(Type.FLAT_ITEM, protocol.getBlockItemPackets().handleItemToServer(book)); boolean signing = channel.equals("MC|BSign"); wrapper.write(Type.BOOLEAN, signing); break; diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java index 043b4506..70d3082d 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_12_2to1_13/packets/SoundPackets1_13.java @@ -18,7 +18,6 @@ package com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.packets; import com.viaversion.viabackwards.ViaBackwards; -import com.viaversion.viabackwards.api.rewriters.Rewriter; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.NamedSoundMapping; import com.viaversion.viaversion.api.Via; @@ -26,8 +25,9 @@ import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; +import com.viaversion.viaversion.rewriter.RewriterBase; -public class SoundPackets1_13 extends Rewriter { +public class SoundPackets1_13 extends RewriterBase { private static final String[] SOUND_SOURCES = {"master", "music", "record", "weather", "block", "hostile", "neutral", "player", "ambient", "voice"}; public SoundPackets1_13(Protocol1_12_2To1_13 protocol) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/EntityPackets1_13_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/EntityPackets1_13_2.java index a5139136..b452b0b8 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/EntityPackets1_13_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_1to1_13_2/packets/EntityPackets1_13_2.java @@ -54,7 +54,7 @@ public class EntityPackets1_13_2 { @Override public void handle(PacketWrapper wrapper) throws Exception { for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { - if (metadata.getMetaType() == MetaType1_13_2.Slot) { + if (metadata.metaType() == MetaType1_13_2.Slot) { metadata.setMetaType(MetaType1_13.Slot); } } @@ -79,7 +79,7 @@ public class EntityPackets1_13_2 { @Override public void handle(PacketWrapper wrapper) throws Exception { for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { - if (metadata.getMetaType() == MetaType1_13_2.Slot) { + if (metadata.metaType() == MetaType1_13_2.Slot) { metadata.setMetaType(MetaType1_13.Slot); } } @@ -98,7 +98,7 @@ public class EntityPackets1_13_2 { @Override public void handle(PacketWrapper wrapper) throws Exception { for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) { - if (metadata.getMetaType() == MetaType1_13_2.Slot) { + if (metadata.metaType() == MetaType1_13_2.Slot) { metadata.setMetaType(MetaType1_13.Slot); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java index 402b3e57..dc89a844 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/Protocol1_13_2To1_14.java @@ -27,10 +27,12 @@ import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets.PlayerP import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets.SoundPackets1_14; import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage; 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.protocol.remapper.PacketHandler; 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_13to1_12_2.ClientboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; @@ -71,7 +73,7 @@ public class Protocol1_13_2To1_14 extends BackwardsProtocol { @@ -54,6 +54,7 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { - Metadata meta = e.getData(); - int typeId = meta.getMetaType().getTypeID(); + filter().handler((event, meta) -> { + int typeId = meta.metaType().typeId(); if (typeId <= 15) { meta.setMetaType(MetaType1_13_2.byId(typeId)); } - MetaType type = meta.getMetaType(); + MetaType type = meta.metaType(); if (type == MetaType1_13_2.Slot) { Item item = (Item) meta.getValue(); - meta.setValue(getProtocol().getBlockItemPackets().handleItemToClient(item)); + meta.setValue(protocol.getBlockItemPackets().handleItemToClient(item)); } else if (type == MetaType1_13_2.BlockID) { int blockstate = (Integer) meta.getValue(); meta.setValue(protocol.getMappingData().getNewBlockStateId(blockstate)); } - return meta; }); - registerMetaHandler().filter(Entity1_14Types.PILLAGER, 15).removed(); + filter().type(Entity1_14Types.PILLAGER).cancel(15); - registerMetaHandler().filter(Entity1_14Types.FOX, 15).removed(); - registerMetaHandler().filter(Entity1_14Types.FOX, 16).removed(); - registerMetaHandler().filter(Entity1_14Types.FOX, 17).removed(); - registerMetaHandler().filter(Entity1_14Types.FOX, 18).removed(); + filter().type(Entity1_14Types.FOX).cancel(15); + filter().type(Entity1_14Types.FOX).cancel(16); + filter().type(Entity1_14Types.FOX).cancel(17); + filter().type(Entity1_14Types.FOX).cancel(18); - registerMetaHandler().filter(Entity1_14Types.PANDA, 15).removed(); - registerMetaHandler().filter(Entity1_14Types.PANDA, 16).removed(); - registerMetaHandler().filter(Entity1_14Types.PANDA, 17).removed(); - registerMetaHandler().filter(Entity1_14Types.PANDA, 18).removed(); - registerMetaHandler().filter(Entity1_14Types.PANDA, 19).removed(); - registerMetaHandler().filter(Entity1_14Types.PANDA, 20).removed(); + filter().type(Entity1_14Types.PANDA).cancel(15); + filter().type(Entity1_14Types.PANDA).cancel(16); + filter().type(Entity1_14Types.PANDA).cancel(17); + filter().type(Entity1_14Types.PANDA).cancel(18); + filter().type(Entity1_14Types.PANDA).cancel(19); + filter().type(Entity1_14Types.PANDA).cancel(20); - registerMetaHandler().filter(Entity1_14Types.CAT, 18).removed(); - registerMetaHandler().filter(Entity1_14Types.CAT, 19).removed(); - registerMetaHandler().filter(Entity1_14Types.CAT, 20).removed(); + filter().type(Entity1_14Types.CAT).cancel(18); + filter().type(Entity1_14Types.CAT).cancel(19); + filter().type(Entity1_14Types.CAT).cancel(20); - registerMetaHandler().handle(e -> { - EntityType type = e.getEntity().getType(); - Metadata meta = e.getData(); + filter().handler((event, meta) -> { + EntityType type = event.entityType(); if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE) || type == Entity1_14Types.RAVAGER || type == Entity1_14Types.WITCH) { - int index = e.getIndex(); + int index = event.index(); if (index == 14) { - throw RemovedValueException.EX; + event.cancel(); } else if (index > 14) { - meta.setId(index - 1); + event.setIndex(index - 1); } } - return meta; }); - registerMetaHandler().filter(Entity1_14Types.AREA_EFFECT_CLOUD, 10).handle(e -> { - Metadata meta = e.getData(); + filter().type(Entity1_14Types.AREA_EFFECT_CLOUD).index(10).handler((event, meta) -> { rewriteParticle((Particle) meta.getValue()); - return meta; }); - registerMetaHandler().filter(Entity1_14Types.FIREWORK_ROCKET, 8).handle(e -> { - Metadata meta = e.getData(); + filter().type(Entity1_14Types.FIREWORK_ROCKET).index(8).handler((event, meta) -> { meta.setMetaType(MetaType1_13_2.VarInt); Integer value = (Integer) meta.getValue(); if (value == null) meta.setValue(0); - return meta; }); - registerMetaHandler().filter(Entity1_14Types.ABSTRACT_ARROW, true).handle(e -> { - Metadata meta = e.getData(); - int index = e.getIndex(); - if (index == 9) { - throw RemovedValueException.EX; - } else if (index > 9) { - meta.setId(index - 1); - } - return meta; - }); + filter().filterFamily(Entity1_14Types.ABSTRACT_ARROW).removeIndex(9); - registerMetaHandler().filter(Entity1_14Types.VILLAGER, 15).removed(); // Head shake timer + filter().type(Entity1_14Types.VILLAGER).cancel(15); // Head shake timer - MetaHandler villagerDataHandler = e -> { - Metadata meta = e.getData(); + MetaHandler villagerDataHandler = (event, meta) -> { VillagerData villagerData = (VillagerData) meta.getValue(); meta.setValue(villagerDataToProfession(villagerData)); meta.setMetaType(MetaType1_13_2.VarInt); - if (meta.getId() == 16) { - meta.setId(15); // decreased by 2 again in one of the following handlers + if (meta.id() == 16) { + event.setIndex(15); // decreased by 2 again in one of the following handlers } - return meta; }; - registerMetaHandler().filter(Entity1_14Types.ZOMBIE_VILLAGER, 18).handle(villagerDataHandler); - registerMetaHandler().filter(Entity1_14Types.VILLAGER, 16).handle(villagerDataHandler); + filter().type(Entity1_14Types.ZOMBIE_VILLAGER).index(18).handler(villagerDataHandler); + filter().type(Entity1_14Types.VILLAGER).index(16).handler(villagerDataHandler); // Holding arms up - from bitfield into own boolean - registerMetaHandler().filter(Entity1_14Types.ABSTRACT_SKELETON, true, 13).handle(e -> { - byte value = (byte) e.getData().getValue(); + filter().filterFamily(Entity1_14Types.ABSTRACT_SKELETON).index(13).handler((event, meta) -> { + byte value = (byte) meta.getValue(); if ((value & 4) != 0) { - e.createMeta(new Metadata(14, MetaType1_13_2.Boolean, true)); + event.createExtraMeta(new Metadata(14, MetaType1_13_2.Boolean, true)); } - return e.getData(); }); - registerMetaHandler().filter(Entity1_14Types.ZOMBIE, true, 13).handle(e -> { - byte value = (byte) e.getData().getValue(); + filter().filterFamily(Entity1_14Types.ZOMBIE).index(13).handler((event, meta) -> { + byte value = (byte) meta.getValue(); if ((value & 4) != 0) { - e.createMeta(new Metadata(16, MetaType1_13_2.Boolean, true)); + event.createExtraMeta(new Metadata(16, MetaType1_13_2.Boolean, true)); } - return e.getData(); }); - registerMetaHandler().filter(Entity1_14Types.ZOMBIE, true).handle(e -> { - Metadata meta = e.getData(); - int index = e.getIndex(); - if (index >= 16) { - meta.setId(index + 1); - } - return meta; - }); + filter().filterFamily(Entity1_14Types.ZOMBIE).addIndex(16); // Remove bed location - registerMetaHandler().filter(Entity1_14Types.LIVINGENTITY, true).handle(e -> { - Metadata meta = e.getData(); - int index = e.getIndex(); + filter().filterFamily(Entity1_14Types.LIVINGENTITY).handler((event, meta) -> { + int index = event.index(); if (index == 12) { Position position = (Position) meta.getValue(); if (position != null) { // Use bed - PacketWrapper wrapper = PacketWrapper.create(0x33, null, e.getUser()); - wrapper.write(Type.VAR_INT, e.getEntity().getEntityId()); + PacketWrapper wrapper = PacketWrapper.create(0x33, null, event.user()); + wrapper.write(Type.VAR_INT, event.entityId()); wrapper.write(Type.POSITION, position); try { @@ -491,50 +463,34 @@ public class EntityPackets1_14 extends LegacyEntityRewriter 12) { - meta.setId(index - 1); + event.setIndex(index - 1); } - return meta; }); - registerMetaHandler().handle(e -> { - Metadata meta = e.getData(); - int index = e.getIndex(); - if (index == 6) { - throw RemovedValueException.EX; - } else if (index > 6) { - meta.setId(index - 1); - } - return meta; - }); + // Something + filter().removeIndex(6); - registerMetaHandler().handle(e -> { - Metadata meta = e.getData(); - int typeId = meta.getMetaType().getTypeID(); + filter().handler((event, meta) -> { + int typeId = meta.metaType().typeId(); if (typeId > 15) { - ViaBackwards.getPlatform().getLogger().warning("New 1.14 metadata was not handled: " + meta + " entity: " + e.getEntity().getType()); - return null; + ViaBackwards.getPlatform().getLogger().warning("New 1.14 metadata was not handled: " + meta + " entity: " + event.entityType()); } - return meta; }); - registerMetaHandler().filter(Entity1_14Types.OCELOT, 13).handle(e -> { - Metadata meta = e.getData(); - meta.setId(15); + filter().type(Entity1_14Types.OCELOT).index(13).handler((event, meta) -> { + event.setIndex(15); meta.setMetaType(MetaType1_13_2.VarInt); meta.setValue(0); - return meta; }); - registerMetaHandler().filter(Entity1_14Types.CAT).handle(e -> { - Metadata meta = e.getData(); - if (meta.getId() == 15) { + filter().type(Entity1_14Types.CAT).handler((event, meta) -> { + if (event.index() == 15) { meta.setValue(1); - } else if (meta.getId() == 13) { + } else if (event.index() == 13) { meta.setValue((byte) ((byte) meta.getValue() & 0x4)); } - return meta; }); } @@ -566,7 +522,7 @@ public class EntityPackets1_14 extends LegacyEntityRewriter { +public class PlayerPackets1_14 extends RewriterBase { public PlayerPackets1_14(Protocol1_13_2To1_14 protocol) { super(protocol); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java index acbf2ce1..bbeddc37 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13_2to1_14/packets/SoundPackets1_14.java @@ -18,17 +18,17 @@ package com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.packets; import com.viaversion.viabackwards.ViaBackwards; -import com.viaversion.viabackwards.api.entities.storage.EntityTracker; -import com.viaversion.viabackwards.api.rewriters.Rewriter; import com.viaversion.viabackwards.api.rewriters.SoundRewriter; import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14; import com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14; +import com.viaversion.viaversion.api.data.entity.StoredEntityData; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; 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.rewriter.RewriterBase; -public class SoundPackets1_14 extends Rewriter { +public class SoundPackets1_14 extends RewriterBase { public SoundPackets1_14(Protocol1_13_2To1_14 protocol) { super(protocol); @@ -55,7 +55,7 @@ public class SoundPackets1_14 extends Rewriter { int category = wrapper.read(Type.VAR_INT); int entityId = wrapper.read(Type.VAR_INT); - EntityTracker.StoredEntity storedEntity = wrapper.user().get(EntityTracker.class).get(protocol).getEntity(entityId); + StoredEntityData storedEntity = wrapper.user().getEntityTracker(protocol.getClass()).entityData(entityId); EntityPositionStorage1_14 entityStorage; if (storedEntity == null || (entityStorage = storedEntity.get(EntityPositionStorage1_14.class)) == null) { ViaBackwards.getPlatform().getLogger().warning("Untracked entity with id " + entityId); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java index 501498c9..f42d10b1 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_13to1_13_1/Protocol1_13To1_13_1.java @@ -19,19 +19,20 @@ package com.viaversion.viabackwards.protocol.protocol1_13to1_13_1; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.data.BackwardsMappings; -import com.viaversion.viabackwards.api.entities.storage.EntityTracker; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.data.CommandRewriter1_13_1; import com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.packets.EntityPackets1_13_1; import com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.packets.InventoryPackets1_13_1; import com.viaversion.viabackwards.protocol.protocol1_13to1_13_1.packets.WorldPackets1_13_1; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; 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.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; @@ -215,12 +216,7 @@ public class Protocol1_13To1_13_1 extends BackwardsProtocol { public EntityPackets1_13_1(Protocol1_13To1_13_1 protocol) { @@ -73,14 +74,14 @@ public class EntityPackets1_13_1 extends LegacyEntityRewriter metadata = wrapper.get(Types1_13.METADATA_LIST, 0); + handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user()); } }); } @@ -134,58 +130,46 @@ public class EntityPackets1_13_1 extends LegacyEntityRewriter { - Metadata meta = e.getData(); - if (meta.getMetaType() == MetaType1_13.Slot) { + filter().handler((event, meta) -> { + if (meta.metaType() == MetaType1_13.Slot) { InventoryPackets1_13_1.toClient((Item) meta.getValue()); - } else if (meta.getMetaType() == MetaType1_13.BlockID) { + } else if (meta.metaType() == MetaType1_13.BlockID) { // Convert to new block id int data = (int) meta.getValue(); meta.setValue(protocol.getMappingData().getNewBlockStateId(data)); - } else if (meta.getMetaType() == MetaType1_13.PARTICLE) { + } else if (meta.metaType() == MetaType1_13.PARTICLE) { rewriteParticle((Particle) meta.getValue()); } - return meta; }); // Remove shooter UUID - registerMetaHandler(). - filter(Entity1_13Types.EntityType.ABSTRACT_ARROW, true, 7) - .removed(); + filter().filterFamily(Entity1_13Types.EntityType.ABSTRACT_ARROW).cancel(7); // Move colors to old position - registerMetaHandler().filter(Entity1_13Types.EntityType.SPECTRAL_ARROW, 8) - .handleIndexChange(7); + filter().type(Entity1_13Types.EntityType.SPECTRAL_ARROW).index(8).toIndex(7); // Move loyalty level to old position - registerMetaHandler().filter(Entity1_13Types.EntityType.TRIDENT, 8) - .handleIndexChange(7); + filter().type(Entity1_13Types.EntityType.TRIDENT).index(8).toIndex(7); // Rewrite Minecart blocks - registerMetaHandler() - .filter(Entity1_13Types.EntityType.MINECART_ABSTRACT, true, 9) - .handle(e -> { - Metadata meta = e.getData(); - - int data = (int) meta.getValue(); - meta.setValue(protocol.getMappingData().getNewBlockStateId(data)); - - return meta; - }); + filter().filterFamily(Entity1_13Types.EntityType.MINECART_ABSTRACT).index(9).handler((event, meta) -> { + int data = (int) meta.getValue(); + meta.setValue(protocol.getMappingData().getNewBlockStateId(data)); + }); } @Override - protected EntityType getTypeFromId(int typeId) { + public EntityType typeFromId(int typeId) { return Entity1_13Types.getTypeFromId(typeId, false); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java index a9de84d2..28ec6673 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14_4to1_15/Protocol1_14_4To1_15.java @@ -19,7 +19,6 @@ package com.viaversion.viabackwards.protocol.protocol1_14_4to1_15; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.data.BackwardsMappings; -import com.viaversion.viabackwards.api.entities.storage.EntityTracker; import com.viaversion.viabackwards.api.rewriters.SoundRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.EntityTypeMapping; @@ -27,9 +26,11 @@ import com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateR import com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.packets.BlockItemPackets1_15; import com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.packets.EntityPackets1_15; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; 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.ClientboundPackets1_15; @@ -105,10 +106,7 @@ public class Protocol1_14_4To1_15 extends BackwardsProtocol { } }); - registerSpawnTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_15Types.FALLING_BLOCK); + registerTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_15Types.FALLING_BLOCK); protocol.registerClientbound(ClientboundPackets1_15.SPAWN_MOB, new PacketRemapper() { @Override @@ -96,7 +92,7 @@ public class EntityPackets1_15 extends EntityRewriter { handler(wrapper -> { int type = wrapper.get(Type.VAR_INT, 1); EntityType entityType = Entity1_15Types.getTypeFromId(type); - addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType); + tracker(wrapper.user()).addEntity(wrapper.get(Type.VAR_INT, 0), entityType); wrapper.set(Type.VAR_INT, 1, EntityTypeMapping.getOldEntityId(type)); }); } @@ -130,9 +126,9 @@ public class EntityPackets1_15 extends EntityRewriter { } }); - registerExtraTracker(ClientboundPackets1_15.SPAWN_EXPERIENCE_ORB, Entity1_15Types.EXPERIENCE_ORB); - registerExtraTracker(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY, Entity1_15Types.LIGHTNING_BOLT); - registerExtraTracker(ClientboundPackets1_15.SPAWN_PAINTING, Entity1_15Types.PAINTING); + registerTracker(ClientboundPackets1_15.SPAWN_EXPERIENCE_ORB, Entity1_15Types.EXPERIENCE_ORB); + registerTracker(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY, Entity1_15Types.LIGHTNING_BOLT); + registerTracker(ClientboundPackets1_15.SPAWN_PAINTING, Entity1_15Types.PAINTING); protocol.registerClientbound(ClientboundPackets1_15.SPAWN_PLAYER, new PacketRemapper() { @Override @@ -150,7 +146,7 @@ public class EntityPackets1_15 extends EntityRewriter { } }); - registerEntityDestroy(ClientboundPackets1_15.DESTROY_ENTITIES); + registerRemoveEntities(ClientboundPackets1_15.DESTROY_ENTITIES); registerMetadataRewriter(ClientboundPackets1_15.ENTITY_METADATA, Types1_14.METADATA_LIST); // Attributes (get rid of generic.flyingSpeed for the Bee remap) @@ -161,7 +157,7 @@ public class EntityPackets1_15 extends EntityRewriter { map(Type.INT); handler(wrapper -> { int entityId = wrapper.get(Type.VAR_INT, 0); - EntityType entityType = getEntityType(wrapper.user(), entityId); + EntityType entityType = tracker(wrapper.user()).entityType(entityId); if (entityType != Entity1_15Types.BEE) return; int size = wrapper.get(Type.INT, 0); @@ -199,62 +195,35 @@ public class EntityPackets1_15 extends EntityRewriter { @Override protected void registerRewrites() { - registerMetaHandler().handle(e -> { - Metadata meta = e.getData(); - MetaType type = meta.getMetaType(); - if (type == MetaType1_14.Slot) { - Item item = (Item) meta.getValue(); - meta.setValue(protocol.getBlockItemPackets().handleItemToClient(item)); - } else if (type == MetaType1_14.BlockID) { - int blockstate = (int) meta.getValue(); - meta.setValue(protocol.getMappingData().getNewBlockStateId(blockstate)); - } else if (type == MetaType1_14.PARTICLE) { - rewriteParticle((Particle) meta.getValue()); - } - return meta; - }); + registerMetaTypeHandler(MetaType1_14.Slot, MetaType1_14.BlockID, MetaType1_14.PARTICLE, null); - registerMetaHandler().filter(Entity1_15Types.LIVINGENTITY, true).handle(e -> { - int index = e.getIndex(); - if (index == 12) { - throw RemovedValueException.EX; - } else if (index > 12) { - e.getData().setId(index - 1); - } - return e.getData(); - }); + filter().filterFamily(Entity1_15Types.LIVINGENTITY).removeIndex(12); - registerMetaHandler().filter(Entity1_15Types.BEE, 15).removed(); - registerMetaHandler().filter(Entity1_15Types.BEE, 16).removed(); + filter().type(Entity1_15Types.BEE).cancel(15); + filter().type(Entity1_15Types.BEE).cancel(16); - mapEntity(Entity1_15Types.BEE, Entity1_15Types.PUFFERFISH).jsonName("Bee").spawnMetadata(storage -> { + mapEntityTypeWithData(Entity1_15Types.BEE, Entity1_15Types.PUFFERFISH).jsonName("Bee").spawnMetadata(storage -> { storage.add(new Metadata(14, MetaType1_14.Boolean, false)); storage.add(new Metadata(15, MetaType1_14.VarInt, 2)); }); - registerMetaHandler().filter(Entity1_15Types.ENDERMAN, 16).removed(); - registerMetaHandler().filter(Entity1_15Types.TRIDENT, 10).removed(); + filter().type(Entity1_15Types.ENDERMAN).cancel(16); + filter().type(Entity1_15Types.TRIDENT).cancel(10); - registerMetaHandler().filter(Entity1_15Types.WOLF).handle(e -> { - int index = e.getIndex(); - if (index >= 17) { - e.getData().setId(index + 1); // redundant health removed in 1.15 - } - return e.getData(); - }); - registerMetaHandler().filter(Entity1_15Types.WOLF, 8).handle(e -> { - e.createMeta(new Metadata(17/*WOLF_HEALTH*/, MetaType1_14.Float, e.getData().getValue())); - return e.getData(); + // Redundant health removed in 1.15 + filter().type(Entity1_15Types.WOLF).addIndex(17); + filter().type(Entity1_15Types.WOLF).index(8).handler((event, meta) -> { + event.createExtraMeta(new Metadata(17/*WOLF_HEALTH*/, MetaType1_14.Float, event.meta().value())); }); } @Override - protected EntityType getTypeFromId(int typeId) { + public EntityType typeFromId(int typeId) { return Entity1_15Types.getTypeFromId(typeId); } @Override - public int getOldEntityId(final int newId) { + public int newEntityId(final int newId) { return EntityTypeMapping.getOldEntityId(newId); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14to1_14_1/Protocol1_14To1_14_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14to1_14_1/Protocol1_14To1_14_1.java index b9878fa7..bceb138e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14to1_14_1/Protocol1_14To1_14_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_14to1_14_1/Protocol1_14To1_14_1.java @@ -20,6 +20,8 @@ package com.viaversion.viabackwards.protocol.protocol1_14to1_14_1; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.protocol.protocol1_14to1_14_1.packets.EntityPackets1_14_1; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types; +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; @@ -36,6 +38,6 @@ public class Protocol1_14To1_14_1 extends BackwardsProtocol { public EntityPackets1_14_1(Protocol1_14To1_14_1 protocol) { @@ -37,12 +39,12 @@ public class EntityPackets1_14_1 extends LegacyEntityRewriter metadata = wrapper.get(Types1_14.METADATA_LIST, 0); + handleMetadata(entityId, metadata, wrapper.user()); } }); } @@ -94,13 +96,13 @@ public class EntityPackets1_14_1 extends LegacyEntityRewriter { map(Type.BYTE); // 7 - Yaw map(Type.INT); // 8 - Data handler(wrapper -> { - EntityType entityType = getTypeFromId(wrapper.get(Type.VAR_INT, 1)); + EntityType entityType = typeFromId(wrapper.get(Type.VAR_INT, 1)); if (entityType == Entity1_16Types.LIGHTNING_BOLT) { // Map to old weather entity packet wrapper.cancel(); @@ -89,7 +88,7 @@ public class EntityPackets1_16 extends EntityRewriter { spawnLightningPacket.send(Protocol1_15_2To1_16.class, true, true); } }); - handler(getSpawnTracketWithDataHandler(Entity1_16Types.FALLING_BLOCK)); + handler(getSpawnTrackerWithDataHandler(Entity1_16Types.FALLING_BLOCK)); } }); @@ -156,7 +155,7 @@ public class EntityPackets1_16 extends EntityRewriter { 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), Entity1_16Types.PLAYER); + tracker(wrapper.user()).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER); wrapper.write(Type.STRING, "default"); // Level type @@ -172,11 +171,11 @@ public class EntityPackets1_16 extends EntityRewriter { } }); - registerExtraTracker(ClientboundPackets1_16.SPAWN_EXPERIENCE_ORB, Entity1_16Types.EXPERIENCE_ORB); + registerTracker(ClientboundPackets1_16.SPAWN_EXPERIENCE_ORB, Entity1_16Types.EXPERIENCE_ORB); // F Spawn Global Object, it is no longer with us :( - registerExtraTracker(ClientboundPackets1_16.SPAWN_PAINTING, Entity1_16Types.PAINTING); - registerExtraTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16Types.PLAYER); - registerEntityDestroy(ClientboundPackets1_16.DESTROY_ENTITIES); + registerTracker(ClientboundPackets1_16.SPAWN_PAINTING, Entity1_16Types.PAINTING); + registerTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16Types.PLAYER); + registerRemoveEntities(ClientboundPackets1_16.DESTROY_ENTITIES); registerMetadataRewriter(ClientboundPackets1_16.ENTITY_METADATA, Types1_14.METADATA_LIST); protocol.registerClientbound(ClientboundPackets1_16.ENTITY_PROPERTIES, new PacketRemapper() { @@ -244,9 +243,8 @@ public class EntityPackets1_16 extends EntityRewriter { @Override protected void registerRewrites() { - registerMetaHandler().handle(e -> { - Metadata meta = e.getData(); - MetaType type = meta.getMetaType(); + filter().handler((event, meta) -> { + MetaType type = meta.metaType(); if (type == MetaType1_14.Slot) { meta.setValue(protocol.getBlockItemPackets().handleItemToClient((Item) meta.getValue())); } else if (type == MetaType1_14.BlockID) { @@ -254,52 +252,49 @@ public class EntityPackets1_16 extends EntityRewriter { } else if (type == MetaType1_14.PARTICLE) { rewriteParticle((Particle) meta.getValue()); } else if (type == MetaType1_14.OptChat) { - JsonElement text = meta.getCastedValue(); + JsonElement text = meta.value(); if (text != null) { protocol.getTranslatableRewriter().processText(text); } } - return meta; }); - mapEntityDirect(Entity1_16Types.ZOMBIFIED_PIGLIN, Entity1_15Types.ZOMBIE_PIGMAN); + mapEntityType(Entity1_16Types.ZOMBIFIED_PIGLIN, Entity1_15Types.ZOMBIE_PIGMAN); mapTypes(Entity1_16Types.values(), Entity1_15Types.class); - mapEntity(Entity1_16Types.HOGLIN, Entity1_16Types.COW).jsonName("Hoglin"); - mapEntity(Entity1_16Types.ZOGLIN, Entity1_16Types.COW).jsonName("Zoglin"); - mapEntity(Entity1_16Types.PIGLIN, Entity1_16Types.ZOMBIFIED_PIGLIN).jsonName("Piglin"); - mapEntity(Entity1_16Types.STRIDER, Entity1_16Types.MAGMA_CUBE).jsonName("Strider"); + mapEntityTypeWithData(Entity1_16Types.HOGLIN, Entity1_16Types.COW).jsonName("Hoglin"); + mapEntityTypeWithData(Entity1_16Types.ZOGLIN, Entity1_16Types.COW).jsonName("Zoglin"); + mapEntityTypeWithData(Entity1_16Types.PIGLIN, Entity1_16Types.ZOMBIFIED_PIGLIN).jsonName("Piglin"); + mapEntityTypeWithData(Entity1_16Types.STRIDER, Entity1_16Types.MAGMA_CUBE).jsonName("Strider"); - registerMetaHandler().filter(Entity1_16Types.ZOGLIN, 16).removed(); - registerMetaHandler().filter(Entity1_16Types.HOGLIN, 15).removed(); + filter().type(Entity1_16Types.ZOGLIN).cancel(16); + filter().type(Entity1_16Types.HOGLIN).cancel(15); - registerMetaHandler().filter(Entity1_16Types.PIGLIN, 16).removed(); - registerMetaHandler().filter(Entity1_16Types.PIGLIN, 17).removed(); - registerMetaHandler().filter(Entity1_16Types.PIGLIN, 18).removed(); + filter().type(Entity1_16Types.PIGLIN).cancel(16); + filter().type(Entity1_16Types.PIGLIN).cancel(17); + filter().type(Entity1_16Types.PIGLIN).cancel(18); - registerMetaHandler().filter(Entity1_16Types.STRIDER, 15).handle(meta -> { - boolean baby = meta.getData().getCastedValue(); - meta.getData().setValue(baby ? 1 : 3); - meta.getData().setMetaType(MetaType1_14.VarInt); - return meta.getData(); + filter().type(Entity1_16Types.STRIDER).index(15).handler((event, meta) -> { + boolean baby = meta.value(); + meta.setValue(baby ? 1 : 3); + meta.setMetaType(MetaType1_14.VarInt); }); - registerMetaHandler().filter(Entity1_16Types.STRIDER, 16).removed(); - registerMetaHandler().filter(Entity1_16Types.STRIDER, 17).removed(); - registerMetaHandler().filter(Entity1_16Types.STRIDER, 18).removed(); + filter().type(Entity1_16Types.STRIDER).cancel(16); + filter().type(Entity1_16Types.STRIDER).cancel(17); + filter().type(Entity1_16Types.STRIDER).cancel(18); - registerMetaHandler().filter(Entity1_16Types.FISHING_BOBBER, 8).removed(); + filter().type(Entity1_16Types.FISHING_BOBBER).cancel(8); - registerMetaHandler().filter(Entity1_16Types.ABSTRACT_ARROW, true, 8).removed(); - registerMetaHandler().filter(Entity1_16Types.ABSTRACT_ARROW, true).handle(meta -> { - if (meta.getIndex() >= 8) { - meta.getData().setId(meta.getIndex() + 1); + filter().filterFamily(Entity1_16Types.ABSTRACT_ARROW).cancel(8); + filter().filterFamily(Entity1_16Types.ABSTRACT_ARROW).handler((event, meta) -> { + if (event.index() >= 8) { + event.setIndex(event.index() + 1); // TODO is this right...? } - return meta.getData(); }); } @Override - protected EntityType getTypeFromId(int typeId) { + public EntityType typeFromId(int typeId) { return Entity1_16Types.getTypeFromId(typeId); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java index f1e7af3a..8ee516cf 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_1to1_16_2/Protocol1_16_1To1_16_2.java @@ -25,10 +25,12 @@ import com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.data.CommandR import com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.packets.BlockItemPackets1_16_2; import com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.packets.EntityPackets1_16_2; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; 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.data.entity.EntityTrackerBase; import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1; @@ -127,14 +129,14 @@ public class Protocol1_16_1To1_16_2 extends BackwardsProtocol @Override protected void registerPackets() { - registerSpawnTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK); + registerTrackerWithData(ClientboundPackets1_16_2.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK); registerSpawnTracker(ClientboundPackets1_16_2.SPAWN_MOB); - registerExtraTracker(ClientboundPackets1_16_2.SPAWN_EXPERIENCE_ORB, Entity1_16_2Types.EXPERIENCE_ORB); - registerExtraTracker(ClientboundPackets1_16_2.SPAWN_PAINTING, Entity1_16_2Types.PAINTING); - registerExtraTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, Entity1_16_2Types.PLAYER); - registerEntityDestroy(ClientboundPackets1_16_2.DESTROY_ENTITIES); + registerTracker(ClientboundPackets1_16_2.SPAWN_EXPERIENCE_ORB, Entity1_16_2Types.EXPERIENCE_ORB); + registerTracker(ClientboundPackets1_16_2.SPAWN_PAINTING, Entity1_16_2Types.PAINTING); + registerTracker(ClientboundPackets1_16_2.SPAWN_PLAYER, Entity1_16_2Types.PLAYER); + registerRemoveEntities(ClientboundPackets1_16_2.DESTROY_ENTITIES); registerMetadataRewriter(ClientboundPackets1_16_2.ENTITY_METADATA, Types1_14.METADATA_LIST); protocol.registerClientbound(ClientboundPackets1_16_2.JOIN_GAME, new PacketRemapper() { @@ -109,39 +104,17 @@ public class EntityPackets1_16_2 extends EntityRewriter @Override protected void registerRewrites() { - registerMetaHandler().handle(e -> { - Metadata meta = e.getData(); - MetaType type = meta.getMetaType(); - if (type == MetaType1_14.Slot) { - meta.setValue(protocol.getBlockItemPackets().handleItemToClient((Item) meta.getValue())); - } else if (type == MetaType1_14.BlockID) { - meta.setValue(protocol.getMappingData().getNewBlockStateId((int) meta.getValue())); - } else if (type == MetaType1_14.OptChat) { - JsonElement text = meta.getCastedValue(); - if (text != null) { - protocol.getTranslatableRewriter().processText(text); - } - } else if (type == MetaType1_14.PARTICLE) { - rewriteParticle((Particle) meta.getValue()); - } - return meta; - }); + registerMetaTypeHandler(MetaType1_14.Slot, MetaType1_14.BlockID, MetaType1_14.PARTICLE, MetaType1_14.OptChat); mapTypes(Entity1_16_2Types.values(), Entity1_16Types.class); - mapEntity(Entity1_16_2Types.PIGLIN_BRUTE, Entity1_16_2Types.PIGLIN).jsonName("Piglin Brute"); + mapEntityTypeWithData(Entity1_16_2Types.PIGLIN_BRUTE, Entity1_16_2Types.PIGLIN).jsonName("Piglin Brute"); - registerMetaHandler().filter(Entity1_16_2Types.ABSTRACT_PIGLIN, true).handle(meta -> { - if (meta.getIndex() == 15) { - meta.getData().setId(16); - } else if (meta.getIndex() == 16) { - meta.getData().setId(15); - } - return meta.getData(); - }); + filter().filterFamily(Entity1_16_2Types.ABSTRACT_PIGLIN).index(15).toIndex(16); + filter().filterFamily(Entity1_16_2Types.ABSTRACT_PIGLIN).index(16).toIndex(15); } @Override - protected EntityType getTypeFromId(int typeId) { + public EntityType typeFromId(int typeId) { return Entity1_16_2Types.getTypeFromId(typeId); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java index a79b9022..db697f0f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/Protocol1_16_4To1_17.java @@ -24,9 +24,11 @@ import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.BlockItemPackets1_17; import com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.EntityPackets1_17; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; 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.libs.fastutil.ints.IntArrayList; import com.viaversion.viaversion.libs.fastutil.ints.IntList; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; @@ -79,7 +81,7 @@ public class Protocol1_16_4To1_17 extends BackwardsProtocol { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - int startFromSection = Math.max(0, -(tracker.getCurrentMinY() >> 4)); + EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_16_4To1_17.class); + int startFromSection = Math.max(0, -(tracker.currentMinY() >> 4)); long[] skyLightMask = wrapper.read(Type.LONG_ARRAY_PRIMITIVE); long[] blockLightMask = wrapper.read(Type.LONG_ARRAY_PRIMITIVE); @@ -158,8 +158,8 @@ public class BlockItemPackets1_17 extends com.viaversion.viabackwards.api.rewrit wrapper.write(Type.VAR_INT, cutLightMask(emptySkyLightMask, startFromSection)); wrapper.write(Type.VAR_INT, cutLightMask(emptyBlockLightMask, startFromSection)); - writeLightArrays(wrapper, BitSet.valueOf(skyLightMask), cutSkyLightMask, startFromSection, tracker.getCurrentWorldSectionHeight()); - writeLightArrays(wrapper, BitSet.valueOf(blockLightMask), cutBlockLightMask, startFromSection, tracker.getCurrentWorldSectionHeight()); + writeLightArrays(wrapper, BitSet.valueOf(skyLightMask), cutSkyLightMask, startFromSection, tracker.currentWorldSectionHeight()); + writeLightArrays(wrapper, BitSet.valueOf(blockLightMask), cutBlockLightMask, startFromSection, tracker.currentWorldSectionHeight()); }); } @@ -242,14 +242,14 @@ public class BlockItemPackets1_17 extends com.viaversion.viabackwards.api.rewrit @Override public void registerMap() { handler(wrapper -> { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - int currentWorldSectionHeight = tracker.getCurrentWorldSectionHeight(); + EntityTracker tracker = wrapper.user().getEntityTracker(Protocol1_16_4To1_17.class); + int currentWorldSectionHeight = tracker.currentWorldSectionHeight(); Chunk chunk = wrapper.read(new Chunk1_17Type(currentWorldSectionHeight)); wrapper.write(new Chunk1_16_2Type(), chunk); // Cut sections - int startFromSection = Math.max(0, -(tracker.getCurrentMinY() >> 4)); + int startFromSection = Math.max(0, -(tracker.currentMinY() >> 4)); chunk.setBiomeData(Arrays.copyOfRange(chunk.getBiomeData(), startFromSection * 64, (startFromSection * 64) + 1024)); chunk.setBitmask(cutMask(chunk.getChunkMask(), startFromSection, false)); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java index 5a2028ab..830b07fb 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_16_4to1_17/packets/EntityPackets1_17.java @@ -23,16 +23,13 @@ import com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.Protocol1_16_4T import com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types; import com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types; import com.viaversion.viaversion.api.minecraft.entities.EntityType; -import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.metadata.MetaType; -import com.viaversion.viaversion.api.minecraft.metadata.Metadata; import com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_14; 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.api.type.types.version.Types1_14; import com.viaversion.viaversion.api.type.types.version.Types1_17; -import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag; import com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag; @@ -49,11 +46,11 @@ public class EntityPackets1_17 extends EntityRewriter { @Override protected void registerPackets() { - registerSpawnTrackerWithData(ClientboundPackets1_17.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK); + registerTrackerWithData(ClientboundPackets1_17.SPAWN_ENTITY, Entity1_16_2Types.FALLING_BLOCK); registerSpawnTracker(ClientboundPackets1_17.SPAWN_MOB); - registerExtraTracker(ClientboundPackets1_17.SPAWN_EXPERIENCE_ORB, Entity1_16_2Types.EXPERIENCE_ORB); - registerExtraTracker(ClientboundPackets1_17.SPAWN_PAINTING, Entity1_16_2Types.PAINTING); - registerExtraTracker(ClientboundPackets1_17.SPAWN_PLAYER, Entity1_16_2Types.PLAYER); + registerTracker(ClientboundPackets1_17.SPAWN_EXPERIENCE_ORB, Entity1_16_2Types.EXPERIENCE_ORB); + registerTracker(ClientboundPackets1_17.SPAWN_PAINTING, Entity1_16_2Types.PAINTING); + registerTracker(ClientboundPackets1_17.SPAWN_PLAYER, Entity1_16_2Types.PLAYER); registerMetadataRewriter(ClientboundPackets1_17.ENTITY_METADATA, Types1_17.METADATA_LIST, Types1_14.METADATA_LIST); protocol.registerClientbound(ClientboundPackets1_17.REMOVE_ENTITY, ClientboundPackets1_16_2.DESTROY_ENTITIES, new PacketRemapper() { @@ -61,7 +58,7 @@ public class EntityPackets1_17 extends EntityRewriter { public void registerMap() { handler(wrapper -> { int entityId = wrapper.read(Type.VAR_INT); - getEntityTracker(wrapper.user()).removeEntity(entityId); + tracker(wrapper.user()).removeEntity(entityId); // Write into single value array int[] array = {entityId}; @@ -87,7 +84,7 @@ public class EntityPackets1_17 extends EntityRewriter { } }); handler(getTrackerHandler(Entity1_16_2Types.PLAYER, Type.INT)); - handler(getWorldDataTracker(1)); + handler(worldDataTrackerHandler(1)); handler(wrapper -> { CompoundTag registry = wrapper.get(Type.NBT, 0); CompoundTag biomeRegsitry = registry.get("minecraft:worldgen/biome"); @@ -116,10 +113,8 @@ public class EntityPackets1_17 extends EntityRewriter { @Override public void registerMap() { map(Type.NBT); // Dimension data - handler(getWorldDataTracker(0)); - handler(wrapper -> { - reduceExtendedHeight(wrapper.get(Type.NBT, 0), true); - }); + handler(worldDataTrackerHandler(0)); + handler(wrapper -> reduceExtendedHeight(wrapper.get(Type.NBT, 0), true)); } }); @@ -157,21 +152,11 @@ public class EntityPackets1_17 extends EntityRewriter { @Override protected void registerRewrites() { - registerMetaHandler().handle(e -> { - Metadata meta = e.getData(); - meta.setMetaType(MetaType1_14.byId(meta.getMetaType().getTypeID())); + filter().handler((event, meta) -> { + meta.setMetaType(MetaType1_14.byId(meta.metaType().typeId())); - MetaType type = meta.getMetaType(); - if (type == MetaType1_14.Slot) { - meta.setValue(protocol.getBlockItemPackets().handleItemToClient((Item) meta.getValue())); - } else if (type == MetaType1_14.BlockID) { - meta.setValue(protocol.getMappingData().getNewBlockStateId((int) meta.getValue())); - } else if (type == MetaType1_14.OptChat) { - JsonElement text = meta.getCastedValue(); - if (text != null) { - protocol.getTranslatableRewriter().processText(text); - } - } else if (type == MetaType1_14.PARTICLE) { + MetaType type = meta.metaType(); + if (type == MetaType1_14.PARTICLE) { Particle particle = (Particle) meta.getValue(); if (particle.getId() == 15) { // Dust / Dust Transition // Remove transition target color values 4-6 @@ -180,55 +165,46 @@ public class EntityPackets1_17 extends EntityRewriter { // No nice mapping possible without tracking entity positions and doing particle tasks particle.setId(0); particle.getArguments().clear(); - return meta; + return; } rewriteParticle(particle); } else if (type == MetaType1_14.Pose) { // Goat LONG_JUMP added at 6 - int pose = meta.getCastedValue(); + int pose = meta.value(); if (pose == 6) { meta.setValue(1); // FALL_FLYING } else if (pose > 6) { meta.setValue(pose - 1); } } - return meta; }); + // Particles have already been handled + registerMetaTypeHandler(MetaType1_14.Slot, MetaType1_14.BlockID, null, MetaType1_14.OptChat); + mapTypes(Entity1_17Types.values(), Entity1_16_2Types.class); - registerMetaHandler().filter(Entity1_17Types.AXOLOTL, 17).removed(); - registerMetaHandler().filter(Entity1_17Types.AXOLOTL, 18).removed(); - registerMetaHandler().filter(Entity1_17Types.AXOLOTL, 19).removed(); + filter().type(Entity1_17Types.AXOLOTL).cancel(17); + filter().type(Entity1_17Types.AXOLOTL).cancel(18); + filter().type(Entity1_17Types.AXOLOTL).cancel(19); - registerMetaHandler().filter(Entity1_17Types.GLOW_SQUID, 16).removed(); + filter().type(Entity1_17Types.GLOW_SQUID).cancel(16); - registerMetaHandler().filter(Entity1_17Types.GOAT, 17).removed(); + filter().type(Entity1_17Types.GOAT).cancel(17); - mapEntity(Entity1_17Types.AXOLOTL, Entity1_17Types.TROPICAL_FISH).jsonName("Axolotl"); - mapEntity(Entity1_17Types.GOAT, Entity1_17Types.SHEEP).jsonName("Goat"); + mapEntityTypeWithData(Entity1_17Types.AXOLOTL, Entity1_17Types.TROPICAL_FISH).jsonName("Axolotl"); + mapEntityTypeWithData(Entity1_17Types.GOAT, Entity1_17Types.SHEEP).jsonName("Goat"); - mapEntity(Entity1_17Types.GLOW_SQUID, Entity1_17Types.SQUID).jsonName("Glow Squid"); - mapEntity(Entity1_17Types.GLOW_ITEM_FRAME, Entity1_17Types.ITEM_FRAME); + mapEntityTypeWithData(Entity1_17Types.GLOW_SQUID, Entity1_17Types.SQUID).jsonName("Glow Squid"); + mapEntityTypeWithData(Entity1_17Types.GLOW_ITEM_FRAME, Entity1_17Types.ITEM_FRAME); - registerMetaHandler().filter(Entity1_17Types.SHULKER).handle(meta -> { - if (meta.getIndex() >= 17) { - meta.getData().setId(meta.getIndex() + 1); // TODO Handle attachment pos? - } - return meta.getData(); - }); + filter().type(Entity1_17Types.SHULKER).addIndex(17); // TODO Handle attachment pos? - registerMetaHandler().filter(7).removed(); // Ticks frozen - registerMetaHandler().handle(meta -> { - if (meta.getIndex() > 7) { - meta.getData().setId(meta.getIndex() - 1); - } - return meta.getData(); - }); + filter().removeIndex(7); // Ticks frozen } @Override - protected EntityType getTypeFromId(int typeId) { + public EntityType typeFromId(int typeId) { return Entity1_17Types.getTypeFromId(typeId); } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/Protocol1_9_4To1_10.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/Protocol1_9_4To1_10.java index aedd32b7..99dbf5be 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/Protocol1_9_4To1_10.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/Protocol1_9_4To1_10.java @@ -24,10 +24,12 @@ import com.viaversion.viabackwards.api.rewriters.SoundRewriter; import com.viaversion.viabackwards.protocol.protocol1_9_4to1_10.packets.BlockItemPackets1_10; import com.viaversion.viabackwards.protocol.protocol1_9_4to1_10.packets.EntityPackets1_10; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; 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_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; @@ -94,7 +96,7 @@ public class Protocol1_9_4To1_10 extends BackwardsProtocol { - Metadata data = e.getData(); - - if (data.getMetaType().getType().equals(Type.ITEM)) // Is Item - data.setValue(handleItemToClient((Item) data.getValue())); - - return data; + protocol.getEntityPackets().filter().handler((event, meta) -> { + if (meta.metaType().type().equals(Type.ITEM)) // Is Item + meta.setValue(handleItemToClient((Item) meta.getValue())); }); // Particle diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java index dec490cd..25f7481b 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java @@ -19,8 +19,7 @@ package com.viaversion.viabackwards.protocol.protocol1_9_4to1_10.packets; import com.viaversion.viabackwards.api.entities.storage.EntityData; -import com.viaversion.viabackwards.api.entities.storage.MetaStorage; -import com.viaversion.viabackwards.api.exceptions.RemovedValueException; +import com.viaversion.viabackwards.api.entities.storage.WrappedMetadata; import com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter; import com.viaversion.viabackwards.protocol.protocol1_9_4to1_10.Protocol1_9_4To1_10; import com.viaversion.viabackwards.utils.Block; @@ -37,6 +36,7 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_9; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3; +import java.util.List; import java.util.Optional; public class EntityPackets1_10 extends LegacyEntityRewriter { @@ -74,7 +74,7 @@ public class EntityPackets1_10 extends LegacyEntityRewriter int objType = objectData & 4095; int data = objectData >> 12 & 15; - Block block = getProtocol().getBlockItemPackets().handleBlock(objType, data); + Block block = protocol.getBlockItemPackets().handleBlock(objType, data); if (block == null) return; @@ -85,8 +85,8 @@ public class EntityPackets1_10 extends LegacyEntityRewriter } }); - registerExtraTracker(ClientboundPackets1_9_3.SPAWN_EXPERIENCE_ORB, Entity1_10Types.EntityType.EXPERIENCE_ORB); - registerExtraTracker(ClientboundPackets1_9_3.SPAWN_GLOBAL_ENTITY, Entity1_10Types.EntityType.WEATHER); + registerTracker(ClientboundPackets1_9_3.SPAWN_EXPERIENCE_ORB, Entity1_10Types.EntityType.EXPERIENCE_ORB); + registerTracker(ClientboundPackets1_9_3.SPAWN_GLOBAL_ENTITY, Entity1_10Types.EntityType.WEATHER); protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_MOB, new PacketRemapper() { @Override @@ -113,35 +113,25 @@ public class EntityPackets1_10 extends LegacyEntityRewriter @Override public void handle(PacketWrapper wrapper) throws Exception { int entityId = wrapper.get(Type.VAR_INT, 0); - EntityType type = getEntityType(wrapper.user(), entityId); + EntityType type = tracker(wrapper.user()).entityType(entityId); - MetaStorage storage = new MetaStorage(wrapper.get(Types1_9.METADATA_LIST, 0)); - handleMeta( - wrapper.user(), - wrapper.get(Type.VAR_INT, 0), - storage - ); + List metadata = wrapper.get(Types1_9.METADATA_LIST, 0); + handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user()); - EntityData entityData = getEntityData(type); + EntityData entityData = entityDataForType(type); if (entityData != null) { - wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.getReplacementId()); + WrappedMetadata storage = new WrappedMetadata(metadata); + wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.replacementId()); if (entityData.hasBaseMeta()) - entityData.getDefaultMeta().createMeta(storage); + entityData.defaultMeta().createMeta(storage); } - - // Rewrite Metadata - wrapper.set( - Types1_9.METADATA_LIST, - 0, - storage.getMetaDataList() - ); } }); } }); - registerExtraTracker(ClientboundPackets1_9_3.SPAWN_PAINTING, Entity1_10Types.EntityType.PAINTING); + registerTracker(ClientboundPackets1_9_3.SPAWN_PAINTING, Entity1_10Types.EntityType.PAINTING); registerJoinGame(ClientboundPackets1_9_3.JOIN_GAME, Entity1_10Types.EntityType.PLAYER); registerRespawn(ClientboundPackets1_9_3.RESPAWN); @@ -161,61 +151,43 @@ public class EntityPackets1_10 extends LegacyEntityRewriter } }); - registerEntityDestroy(ClientboundPackets1_9_3.DESTROY_ENTITIES); + registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES); registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST); } @Override protected void registerRewrites() { - mapEntity(Entity1_10Types.EntityType.POLAR_BEAR, Entity1_10Types.EntityType.SHEEP).mobName("Polar Bear"); + mapEntityTypeWithData(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 -> { - Metadata data = e.getData(); - boolean b = (boolean) data.getValue(); + filter().type(Entity1_10Types.EntityType.POLAR_BEAR).index(13).handler((event, meta) -> { + boolean b = (boolean) meta.getValue(); - data.setMetaType(MetaType1_9.Byte); - data.setValue(b ? (byte) (14 & 0x0F) : (byte) (0)); - - return data; - })); + meta.setMetaType(MetaType1_9.Byte); + meta.setValue(b ? (byte) (14 & 0x0F) : (byte) (0)); + }); // Handle husk (index 13 -> Zombie Type) - registerMetaHandler().filter(Entity1_10Types.EntityType.ZOMBIE, 13).handle(e -> { - Metadata data = e.getData(); - - if ((int) data.getValue() == 6) // Is type Husk - data.setValue(0); - - return data; + filter().type(Entity1_10Types.EntityType.ZOMBIE).index(13).handler((event, meta) -> { + if ((int) meta.getValue() == 6) { // Is type Husk + meta.setValue(0); + } }); // Handle Stray (index 12 -> Skeleton Type) - registerMetaHandler().filter(Entity1_10Types.EntityType.SKELETON, 12).handle(e -> { - Metadata data = e.getData(); - - if ((int) data.getValue() == 2) - data.setValue(0); // Change to default skeleton - - return data; + filter().type(Entity1_10Types.EntityType.SKELETON).index(12).handler((event, meta) -> { + if ((int) meta.getValue() == 2) { + meta.setValue(0); // Change to default skeleton + } }); // Handle the missing NoGravity tag for every metadata - registerMetaHandler().handle(e -> { - Metadata data = e.getData(); - - if (data.getId() == 5) - throw RemovedValueException.EX; - else if (data.getId() >= 5) - data.setId(data.getId() - 1); - - return data; - }); + filter().removeIndex(5); } @Override - protected EntityType getTypeFromId(int typeId) { + public EntityType typeFromId(int typeId) { return Entity1_10Types.getTypeFromId(typeId, false); }