From 01018fe6004e66d35df3e07006147f5d26e55c73 Mon Sep 17 00:00:00 2001 From: KennyTV <28825609+KennyTV@users.noreply.github.com> Date: Fri, 5 Jul 2019 10:22:38 +0200 Subject: [PATCH] Remap 1.13 piston tile entity --- .../block_entity_handlers/PistonHandler.java | 92 +++++++++++++++++++ .../BackwardsBlockEntityProvider.java | 1 + .../storage/BackwardsBlockStorage.java | 18 +++- 3 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java new file mode 100644 index 00000000..f4ea822c --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/block_entity_handlers/PistonHandler.java @@ -0,0 +1,92 @@ +package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.block_entity_handlers; + +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.data.BackwardsMappings; +import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.providers.BackwardsBlockEntityProvider; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData; +import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData; +import us.myles.viaversion.libs.gson.JsonElement; +import us.myles.viaversion.libs.gson.JsonObject; +import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.IntTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.Tag; + +import java.util.HashMap; +import java.util.Map; +import java.util.StringJoiner; + +public class PistonHandler implements BackwardsBlockEntityProvider.BackwardsBlockEntityHandler { + + private final Map pistonIds = new HashMap<>(); + + public PistonHandler() { + if (Via.getConfig().isServersideBlockConnections()) { + Map keyToId; + try { + keyToId = (Map) ConnectionData.class.getDeclaredField("keyToId").get(null); + } catch (IllegalAccessException | NoSuchFieldException e) { + e.printStackTrace(); + return; + } + + for (Map.Entry entry : keyToId.entrySet()) { + if (!entry.getKey().contains("piston")) continue; + + addEnries(entry.getKey(), entry.getValue()); + } + } else { + JsonObject mappings = MappingData.loadData("mapping-1.13.json").getAsJsonObject("blocks"); + for (Map.Entry blockState : mappings.entrySet()) { + String key = blockState.getValue().getAsString(); + if (!key.contains("piston")) continue; + + addEnries(key, Integer.parseInt(blockState.getKey())); + } + } + } + + // There doesn't seem to be a nicer way around it :( + private void addEnries(String data, int id) { + id = BackwardsMappings.blockMappings.getNewBlock(id); + pistonIds.put(data, id); + + String substring = data.substring(10); + if (!substring.startsWith("piston") && !substring.startsWith("sticky_piston")) return; + + // Swap properties and add them to the map + String[] split = data.substring(0, data.length() - 1).split("\\["); + String[] properties = split[1].split(","); + data = split[0] + "[" + properties[1] + "," + properties[0] + "]"; + pistonIds.put(data, id); + } + + @Override + public CompoundTag transform(UserConnection user, int blockId, CompoundTag tag) { + CompoundTag blockState = tag.get("blockState"); + String dataFromTag = getDataFromTag(blockState); + if (dataFromTag == null) return tag; + + int id = pistonIds.get(dataFromTag); + tag.put(new IntTag("blockId", id >> 4)); + tag.put(new IntTag("blockData", id & 15)); + return tag; + } + + // The type hasn't actually been updated in the blockstorage, so we need to construct it + private String getDataFromTag(CompoundTag tag) { + StringTag name = tag.get("Name"); + if (name == null) return null; + + CompoundTag properties = tag.get("Properties"); + if (properties == null) return name.getValue(); + + StringJoiner joiner = new StringJoiner(",", name.getValue() + "[", "]"); + for (Tag property : properties) { + if (!(property instanceof StringTag)) continue; + joiner.add(property.getName() + "=" + ((StringTag) property).getValue()); + } + return joiner.toString(); + } +} diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/providers/BackwardsBlockEntityProvider.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/providers/BackwardsBlockEntityProvider.java index bb02f256..f2024752 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/providers/BackwardsBlockEntityProvider.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/providers/BackwardsBlockEntityProvider.java @@ -33,6 +33,7 @@ public class BackwardsBlockEntityProvider implements Provider { handlers.put("minecraft:banner", new BannerHandler()); handlers.put("minecraft:skull", new SkullHandler()); handlers.put("minecraft:mob_spawner", new SpawnerHandler()); + handlers.put("minecraft:piston", new PistonHandler()); } /** diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java index a1a63f7c..469ee450 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/storage/BackwardsBlockStorage.java @@ -10,18 +10,18 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.storage; -import com.google.common.collect.Sets; import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.Position; +import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; public class BackwardsBlockStorage extends StoredObject { // This BlockStorage is very exclusive (; - private static final Set whitelist = Sets.newConcurrentHashSet(); + private static final Set whitelist = new HashSet<>(); static { // Flower pots @@ -42,6 +42,17 @@ public class BackwardsBlockStorage extends StoredObject { // Skulls for (int i = 5447; i <= 5566; i++) whitelist.add(i); + + // pistons + for (int i = 1028; i <= 1039; i++) { + whitelist.add(i); + } + for (int i = 1047; i <= 1082; i++) { + whitelist.add(i); + } + for (int i = 1099; i <= 1110; i++) { + whitelist.add(i); + } } @@ -54,8 +65,7 @@ public class BackwardsBlockStorage extends StoredObject { public void checkAndStore(Position position, int block) { if (!whitelist.contains(block)) { // Remove if not whitelisted - if (blocks.containsKey(position)) - blocks.remove(position); + blocks.remove(position); return; }