From e6059845aa242598496f82affa73544117d4f02c Mon Sep 17 00:00:00 2001 From: Nassim <28825609+KennyTV@users.noreply.github.com> Date: Sat, 7 Sep 2019 14:49:05 +0200 Subject: [PATCH] Remap tridents, fix falling dust and lingering potions (#140) * Fix falling dust particle for 1.9 clients * Fix concurrent meta addition / lingering potions Fixes #125 * Remap trident and trident sounds --- .../api/entities/meta/MetaHandler.java | 1 + .../api/entities/meta/MetaHandlerEvent.java | 28 +++++++++----- .../api/rewriters/EntityRewriter.java | 35 +++++++++++++---- .../data/SoundMapping.java | 10 +++++ .../packets/EntityPackets1_13.java | 38 ++++++++++--------- .../packets/BlockItemPackets1_10.java | 27 +++++++++++++ 6 files changed, 105 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandler.java b/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandler.java index 184c9b79..2945c439 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandler.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandler.java @@ -14,5 +14,6 @@ import nl.matsv.viabackwards.api.exceptions.RemovedValueException; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; public interface MetaHandler { + Metadata handle(MetaHandlerEvent e) throws RemovedValueException; } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerEvent.java b/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerEvent.java index c3adc562..eca20d4e 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerEvent.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/entities/meta/MetaHandlerEvent.java @@ -10,33 +10,43 @@ package nl.matsv.viabackwards.api.entities.meta; -import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.RequiredArgsConstructor; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.entities.storage.MetaStorage; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; @Getter -@AllArgsConstructor +@RequiredArgsConstructor public class MetaHandlerEvent { - private UserConnection user; - private EntityTracker.StoredEntity entity; - private int index = -1; - private Metadata data; - @Getter - private MetaStorage storage; + 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 boolean hasData() { return data != null; } public Optional getMetaByIndex(int index) { - for (Metadata meta : getStorage().getMetaDataList()) + for (Metadata meta : storage.getMetaDataList()) if (index == meta.getId()) return Optional.of(meta); return Optional.empty(); } + + public void clearExtraData() { + extraData = null; + } + + public void createMeta(Metadata metadata) { + (extraData != null ? extraData : (extraData = new ArrayList<>())).add(metadata); + } } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java index ae5b46c9..9834ac0e 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/EntityRewriter.java @@ -120,16 +120,31 @@ public abstract class EntityRewriter extends Rewrit List newList = new CopyOnWriteArrayList<>(); for (MetaHandlerSettings settings : metaHandlers) { + List extraData = null; for (Metadata md : storage.getMetaDataList()) { Metadata nmd = md; + MetaHandlerEvent event = null; try { - if (settings.isGucci(type, nmd)) - nmd = settings.getHandler().handle(new MetaHandlerEvent(user, entity, nmd.getId(), nmd, storage)); + if (settings.isGucci(type, nmd)) { + event = new MetaHandlerEvent(user, entity, nmd.getId(), nmd, storage); + nmd = settings.getHandler().handle(event); - if (nmd == null) + if (event.getExtraData() != null) { + (extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData()); + event.clearExtraData(); + } + } + + if (nmd == null) { throw new RemovedValueException(); + } + newList.add(nmd); - } catch (RemovedValueException ignored) { + } catch (RemovedValueException e) { + // add the additionally created data here in case of an interruption + if (event != null && event.getExtraData() != null) { + (extraData != null ? extraData : (extraData = new ArrayList<>())).addAll(event.getExtraData()); + } } catch (Exception e) { if (Via.getManager().isDebug()) { Logger log = ViaBackwards.getPlatform().getLogger(); @@ -139,12 +154,18 @@ public abstract class EntityRewriter extends Rewrit } } } - storage.setMetaDataList(new ArrayList<>(newList)); + + List newData = new ArrayList<>(newList); + if (extraData != null) { + newData.addAll(extraData); + } + + storage.setMetaDataList(newData); newList.clear(); } // Handle Entity Name - Optional opMd = storage.get(getDisplayNameIndex()); + Optional opMd = storage.get(displayNameIndex); if (opMd.isPresent()) { Optional opEd = getEntityData(type); if (opEd.isPresent()) { @@ -152,7 +173,7 @@ public abstract class EntityRewriter extends Rewrit EntityData entData = opEd.get(); if (entData.getMobName() != null && (data.getValue() == null || ((String) data.getValue()).isEmpty()) && - data.getMetaType().getTypeID() == getDisplayNameMetaType().getTypeID()) { + data.getMetaType().getTypeID() == displayNameMetaType.getTypeID()) { String mobName = entData.getMobName(); if (isDisplayNameJson) { mobName = ChatRewriter.legacyTextToJson(mobName); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/SoundMapping.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/SoundMapping.java index 80dcd2c4..69fb372c 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/SoundMapping.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/data/SoundMapping.java @@ -17,6 +17,16 @@ public class SoundMapping { Arrays.fill(sounds, (short) -1); mapIdentifiers(sounds, mapping1_13.getAsJsonArray("sounds"), mapping1_12.getAsJsonArray("sounds")); + + // Simulate some trident sounds + sounds[628] = 138; // throw -> shoot + sounds[629] = 137; // hit -> hit_player + sounds[630] = 137; // hit_ground -> hit + sounds[631] = 139; // riptide_1 -> shoot + sounds[632] = 139; // riptide_2 + sounds[633] = 139; // riptide_3 + sounds[634] = 139; // throw -> shoot + // no fitting thunder remap } private static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers) { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java index ec4fa3db..0f4ed087 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/EntityPackets1_13.java @@ -12,6 +12,7 @@ import nl.matsv.viabackwards.api.rewriters.EntityRewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.EntityTypeMapping; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.PaintingMapping; +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.ParticleMapping; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; @@ -26,6 +27,7 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.Particle; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import java.util.List; import java.util.Optional; public class EntityPackets1_13 extends EntityRewriter { @@ -65,13 +67,16 @@ public class EntityPackets1_13 extends EntityRewriter { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - Optional type = EntityType1_13.ObjectType.findById(wrapper.get(Type.BYTE, 0)); - if (type.isPresent() && type.get() == EntityType1_13.ObjectType.FALLING_BLOCK) { + Optional optionalType = EntityType1_13.ObjectType.findById(wrapper.get(Type.BYTE, 0)); + if (!optionalType.isPresent()) return; + + final EntityType1_13.ObjectType type = optionalType.get(); + if (type == EntityType1_13.ObjectType.FALLING_BLOCK) { int blockState = wrapper.get(Type.INT, 0); int combined = BlockItemPackets1_13.toOldId(blockState); combined = ((combined >> 4) & 0xFFF) | ((combined & 0xF) << 12); wrapper.set(Type.INT, 0, combined); - } else if (type.isPresent() && type.get() == EntityType1_13.ObjectType.ITEM_FRAME) { + } else if (type == EntityType1_13.ObjectType.ITEM_FRAME) { int data = wrapper.get(Type.INT, 0); switch (data) { case 3: @@ -85,6 +90,8 @@ public class EntityPackets1_13 extends EntityRewriter { break; } wrapper.set(Type.INT, 0, data); + } else if (type == EntityType1_13.ObjectType.TRIDENT) { + wrapper.set(Type.BYTE, 0, (byte) EntityType1_13.ObjectType.TIPPED_ARROW.getId()); } } }); @@ -347,15 +354,12 @@ public class EntityPackets1_13 extends EntityRewriter { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - wrapper.set( - Types1_12.METADATA_LIST, - 0, - handleMeta( - wrapper.user(), - wrapper.get(Type.VAR_INT, 0), - new MetaStorage(wrapper.get(Types1_12.METADATA_LIST, 0)) - ).getMetaDataList() - ); + final List metaDataList = handleMeta( + wrapper.user(), + wrapper.get(Type.VAR_INT, 0), + new MetaStorage(wrapper.get(Types1_12.METADATA_LIST, 0)) + ).getMetaDataList(); + wrapper.set(Types1_12.METADATA_LIST, 0, metaDataList); } }); } @@ -385,7 +389,6 @@ public class EntityPackets1_13 extends EntityRewriter { // Turtle regEntType(EntityType.TURTLE, EntityType.OCELOT).mobName("Turtle"); - // Rewrite Meta types registerMetaHandler().handle(e -> { Metadata meta = e.getData(); @@ -482,15 +485,14 @@ public class EntityPackets1_13 extends EntityRewriter { Metadata meta = e.getData(); Particle particle = (Particle) meta.getValue(); - // TODO Rewrite particle ids - e.getStorage().add(new Metadata(9, MetaType1_12.VarInt, 0)); - e.getStorage().add(new Metadata(10, MetaType1_12.VarInt, 0)); - e.getStorage().add(new Metadata(11, MetaType1_12.VarInt, 0)); + ParticleMapping.ParticleData data = ParticleMapping.getMapping(particle.getId()); + e.createMeta(new Metadata(9, MetaType1_12.VarInt, data.getHistoryId())); + e.createMeta(new Metadata(10, MetaType1_12.VarInt, 0)); //TODO particle data + e.createMeta(new Metadata(11, MetaType1_12.VarInt, 0)); //TODO particle data throw new RemovedValueException(); }); // TODO REWRITE BLOCKS IN MINECART - } } diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/BlockItemPackets1_10.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/BlockItemPackets1_10.java index e0b23f4e..8d64df2f 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/BlockItemPackets1_10.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_9_4to1_10/packets/BlockItemPackets1_10.java @@ -223,6 +223,33 @@ public class BlockItemPackets1_10 extends BlockItemRewriter return data; }); + + // Particle + protocol.registerOutgoing(State.PLAY, 0x22, 0x22, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); + map(Type.BOOLEAN); + map(Type.FLOAT); + map(Type.FLOAT); + map(Type.FLOAT); + map(Type.FLOAT); + map(Type.FLOAT); + map(Type.FLOAT); + map(Type.FLOAT); + map(Type.INT); + + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int id = wrapper.get(Type.INT, 0); + if (id == 46) { // new falling_dust + wrapper.set(Type.INT, 0, 38); // -> block_dust + } + } + }); + } + }); } @Override