From 7a331bdb09aa7639e08005a4df41cd981c803c92 Mon Sep 17 00:00:00 2001 From: Matsv Date: Fri, 20 Jul 2018 11:15:37 +0200 Subject: [PATCH] Rewrite entity names for spawners, fixes #884 --- .../data/EntityNameRewriter.java | 56 +++++++++++++++++++ .../providers/BlockEntityProvider.java | 18 +++--- .../blockentities/SpawnerHandler.java | 24 ++++++++ 3 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/EntityNameRewriter.java create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/SpawnerHandler.java diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/EntityNameRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/EntityNameRewriter.java new file mode 100644 index 000000000..30db785d7 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/data/EntityNameRewriter.java @@ -0,0 +1,56 @@ +package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/* + CHANGED ENTITY NAMES IN 1.13 + + commandblock_minecart => command_block_minecart + ender_crystal => end_crystal + evocation_fangs => evoker_fangs + evocation_illager => evoker + eye_of_ender_signal => eye_of_ender + fireworks_rocket => firework_rocket + illusion_illager => illusioner + snowman => snow_golem + villager_golem => iron_golem + vindication_illager => vindicator + xp_bottle => experience_bottle + xp_orb => experience_orb + */ +public class EntityNameRewriter { + private static Map entityNames = new ConcurrentHashMap<>(); + + static { + /* + CHANGED NAMES IN 1.13 + */ + reg("commandblock_minecart", "command_block_minecart"); + reg("ender_crystal", "end_crystal"); + reg("evocation_fangs", "evoker_fangs"); + reg("evocation_illager", "evoker"); + reg("eye_of_ender_signal", "eye_of_ender"); + reg("fireworks_rocket", "firework_rocket"); + reg("illusion_illager", "illusioner"); + reg("snowman", "snow_golem"); + reg("villager_golem", "iron_golem"); + reg("vindication_illager", "vindicator"); + reg("xp_bottle", "experience_bottle"); + reg("xp_orb", "experience_orb"); + } + + + private static void reg(String past, String future) { + entityNames.put("minecraft:" + past, "minecraft:" + future); + } + + public static String rewrite(String entName) { + if (entityNames.containsKey(entName)) + return entityNames.get(entName); + if (entityNames.containsKey("minecraft:" + entName)) + return entityNames.get("minecraft:" + entName); + else + return entName; + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/BlockEntityProvider.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/BlockEntityProvider.java index 1d7352a9c..a65261642 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/BlockEntityProvider.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/BlockEntityProvider.java @@ -2,15 +2,13 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.platform.providers.Provider; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.blockentities.BannerHandler; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.blockentities.BedHandler; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.blockentities.FlowerPotHandler; -import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.blockentities.SkullHandler; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.blockentities.*; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -23,6 +21,7 @@ public class BlockEntityProvider implements Provider { handlers.put("minecraft:bed", new BedHandler()); handlers.put("minecraft:banner", new BannerHandler()); handlers.put("minecraft:skull", new SkullHandler()); + handlers.put("minecraft:mob_spawner", new SpawnerHandler()); } /** @@ -42,7 +41,8 @@ public class BlockEntityProvider implements Provider { String id = (String) tag.get("id").getValue(); if (!handlers.containsKey(id)) { - //System.out.println("Unhandled BlockEntity " + id + " full tag: " + tag); + if (Via.getManager().isDebug()) + System.out.println("Unhandled BlockEntity " + id + " full tag: " + tag); return -1; } @@ -54,10 +54,6 @@ public class BlockEntityProvider implements Provider { return newBlock; } - public interface BlockEntityHandler { - int transform(UserConnection user, CompoundTag tag); - } - private void sendBlockChange(UserConnection user, Position position, int blockId) throws Exception { PacketWrapper wrapper = new PacketWrapper(0x0B, null, user); wrapper.write(Type.POSITION, position); @@ -66,5 +62,9 @@ public class BlockEntityProvider implements Provider { wrapper.send(Protocol1_13To1_12_2.class, true, true); } + public interface BlockEntityHandler { + int transform(UserConnection user, CompoundTag tag); + } + } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/SpawnerHandler.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/SpawnerHandler.java new file mode 100644 index 000000000..ad0b50519 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/providers/blockentities/SpawnerHandler.java @@ -0,0 +1,24 @@ +package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.blockentities; + +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.StringTag; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.EntityNameRewriter; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider; + +public class SpawnerHandler implements BlockEntityProvider.BlockEntityHandler { + @Override + public int transform(UserConnection user, CompoundTag tag) { + if (tag.contains("SpawnData") && tag.get("SpawnData") instanceof CompoundTag) { + CompoundTag data = tag.get("SpawnData"); + if (data.contains("id") && data.get("id") instanceof StringTag) { + StringTag s = data.get("id"); + s.setValue(EntityNameRewriter.rewrite(s.getValue())); + } + + } + + // Always return -1 because the block is still the same id + return -1; + } +}