diff --git a/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerSettings.java b/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerSettings.java index 0f12ef11..58370083 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerSettings.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerSettings.java @@ -77,19 +77,17 @@ public class MetaHandlerSettings { 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()) { - if (filterFamily) { - if (!type.isOrHasParent(filterType)) { - return false; - } - } else { - if (!filterType.is(type)) { - return false; - } - } + if (hasType() && (filterFamily ? !type.isOrHasParent(filterType) : !filterType.is(type))) { + return false; } return !hasIndex() || metadata.getId() == filterIndex; } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/exceptions/RemovedValueException.java b/core/src/main/java/nl/matsv/viabackwards/api/exceptions/RemovedValueException.java index d7a978a5..fb8334ec 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/exceptions/RemovedValueException.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/exceptions/RemovedValueException.java @@ -18,7 +18,8 @@ public class RemovedValueException extends IOException { * May be cached since it is never actually printed, only checked. */ public static final RemovedValueException EX = new RemovedValueException() { - public synchronized Throwable fillInStackTrace() { + @Override + public Throwable fillInStackTrace() { return this; } }; diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriterBase.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriterBase.java index ab334419..6f5221de 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriterBase.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriterBase.java @@ -24,10 +24,12 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; 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. @@ -138,60 +140,52 @@ public abstract class EntityRewriterBase extends Re } EntityType type = storedEntity.getType(); - List newList = new ArrayList<>(); for (MetaHandlerSettings settings : metaHandlers) { - List extraData = null; - for (Metadata md : storage.getMetaDataList()) { - Metadata nmd = md; + List newData = new ArrayList<>(); + for (Metadata meta : storage.getMetaDataList()) { MetaHandlerEvent event = null; try { - if (settings.isGucci(type, nmd)) { - event = new MetaHandlerEvent(user, storedEntity, nmd.getId(), nmd, storage); - nmd = settings.getHandler().handle(event); + 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) { - (extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData()); + newData.addAll(event.getExtraData()); event.clearExtraData(); } } - if (nmd == null) { + if (modifiedMeta == null) { throw RemovedValueException.EX; } - newList.add(nmd); + newData.add(modifiedMeta); } catch (RemovedValueException e) { - // add the additionally created data here in case of an interruption + // Add the additionally created data here in case of an interruption if (event != null && event.getExtraData() != null) { - (extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData()); + newData.addAll(event.getExtraData()); } } catch (Exception e) { Logger log = ViaBackwards.getPlatform().getLogger(); - log.warning("Unable to handle metadata " + nmd); - log.warning("Full metadata list " + storage); + 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(); } } - List newData = new ArrayList<>(newList); - if (extraData != null) { - newData.addAll(extraData); - } - storage.setMetaDataList(newData); - newList.clear(); } // Handle Entity Name Metadata data = storage.get(displayNameIndex); if (data != null) { EntityData entityData = getEntityData(type); - if (entityData != null) { - if (entityData.getMobName() != null && - (data.getValue() == null || ((String) data.getValue()).isEmpty()) && - data.getMetaType().getTypeID() == displayNameMetaType.getTypeID()) { - data.setValue(entityData.getMobName()); - } + if (entityData != null && entityData.getMobName() != null + && (data.getValue() == null || ((String) data.getValue()).isEmpty()) + && data.getMetaType().getTypeID() == displayNameMetaType.getTypeID()) { + data.setValue(entityData.getMobName()); } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java index 676e9cff..54b3dd90 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets1_11.java @@ -166,26 +166,10 @@ public class EntityPackets1_11 extends LegacyEntityRewriter registerExtraTracker(0x04, Entity1_11Types.EntityType.PAINTING); // Join game - protocol.registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Entity ID - map(Type.UNSIGNED_BYTE); // 1 - Gamemode - map(Type.INT); // 2 - Dimension + registerJoinGame(0x23, 0x23, Entity1_11Types.EntityType.PLAYER); - handler(getTrackerHandler(Entity1_11Types.EntityType.PLAYER, Type.INT)); - handler(getDimensionHandler(1)); - } - }); - - // Respawn Packet (save dimension id) - protocol.registerOutgoing(State.PLAY, 0x33, 0x33, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Dimension ID - handler(getDimensionHandler(0)); - } - }); + // Respawn Packet + registerRespawn(0x33, 0x33); // Spawn Player protocol.registerOutgoing(State.PLAY, 0x05, 0x05, new PacketRemapper() { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java index 9744a8f7..9da368f7 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_11_1to1_12/packets/EntityPackets1_12.java @@ -165,14 +165,8 @@ public class EntityPackets1_12 extends LegacyEntityRewriter { public void registerMap() { map(Type.INT); map(Type.LONG, Type.NOTHING); // Seed - handler(wrapper -> wrapper.user().get(ClientWorld.class).setEnvironment(wrapper.get(Type.INT, 0))); + handler(getDimensionHandler(0)); } }); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java index 028214f4..3dca6687 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/EntityPackets1_10.java @@ -142,26 +142,10 @@ public class EntityPackets1_10 extends LegacyEntityRewriter registerExtraTracker(0x04, Entity1_10Types.EntityType.PAINTING); // Join game - protocol.registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Entity ID - map(Type.UNSIGNED_BYTE); // 1 - Gamemode - map(Type.INT); // 2 - Dimension + registerJoinGame(0x23, 0x23, Entity1_10Types.EntityType.PLAYER); - handler(getTrackerHandler(Entity1_10Types.EntityType.PLAYER, Type.INT)); - handler(getDimensionHandler(1)); - } - }); - - // Respawn Packet (save dimension id) - protocol.registerOutgoing(State.PLAY, 0x33, 0x33, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Dimension ID - handler(getDimensionHandler(0)); - } - }); + // Respawn Packet + registerRespawn(0x33, 0x33); // Spawn Player protocol.registerOutgoing(State.PLAY, 0x05, 0x05, new PacketRemapper() {