diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/BlockItemRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/BlockItemRewriter.java index 810d8971..dc23f7a5 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/BlockItemRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/BlockItemRewriter.java @@ -10,6 +10,7 @@ package nl.matsv.viabackwards.api.rewriters; +import lombok.*; import nl.matsv.viabackwards.api.BackwardsProtocol; import nl.matsv.viabackwards.utils.Block; import nl.matsv.viabackwards.utils.ItemUtil; @@ -19,29 +20,36 @@ import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.viaversion.libs.opennbt.conversion.builtin.CompoundTagConverter; import us.myles.viaversion.libs.opennbt.tag.builtin.*; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public abstract class BlockItemRewriter extends Rewriter { private static final CompoundTagConverter converter = new CompoundTagConverter(); - private final Map itemRewriter = new ConcurrentHashMap<>(); - private final Map blockRewriter = new ConcurrentHashMap<>(); + private final Map replacementData = new ConcurrentHashMap<>(); protected void rewriteItem(int oldItem, Item newItem) { - itemRewriter.put((short) oldItem, newItem); + replacementData.put(oldItem, new BlockItemData(oldItem, newItem, null, null)); } protected void rewriteBlockItem(int oldId, Item newItem, Block newBlock) { - itemRewriter.put((short) oldId, newItem); - blockRewriter.put(oldId, newBlock); + rewriteBlockItem(oldId, newItem, newBlock, null); + } + + protected void rewriteBlockItem(int oldId, Item newItem, Block newBlock, BlockEntityHandler handler) { + replacementData.put(oldId, new BlockItemData(oldId, newItem, newBlock, handler)); } protected Item handleItemToClient(Item item) { if (item == null) return null; - if (!itemRewriter.containsKey(item.getId())) + if (!replacementData.containsKey((int) item.getId())) return item; - Item i = ItemUtil.copyItem(itemRewriter.get(item.getId())); + BlockItemData data = replacementData.get((int) item.getId()); + if (!data.hasRepItem()) + return item; + + Item i = ItemUtil.copyItem(data.getRepItem()); if (i.getTag() == null) i.setTag(new CompoundTag("")); @@ -71,7 +79,7 @@ public abstract class BlockItemRewriter extends Rew item.setData(data); item.setAmount(amount); item.setTag(converter.convert("", converter.convert(extras))); - // Remove data tag + // Remove data blockEntityHandler tag.remove("ViaBackwards|" + getProtocolName()); } return item; @@ -92,14 +100,26 @@ public abstract class BlockItemRewriter extends Rew if (!containsBlock(block)) return null; - Block b = blockRewriter.get(block); + Block b = replacementData.get(block).getRepBlock(); // For some blocks, the data can still be useful (: - if (b.getData() == -1) + if (b.getData() != -1) b.setData(data); return b; } protected void handleChunk(Chunk chunk) { + // Map Block Entities + Map tags = new HashMap<>(); + for (CompoundTag tag : chunk.getBlockEntities()) { + if (!(tag.contains("x") && tag.contains("y") && tag.contains("z"))) + continue; + Pos pos = new Pos( + (int) tag.get("x").getValue() % 16, + (int) tag.get("y").getValue(), + (int) tag.get("z").getValue() % 16); + tags.put(pos, tag); + } + for (int i = 0; i < chunk.getSections().length; i++) { ChunkSection section = chunk.getSections()[i]; if (section == null) @@ -114,6 +134,21 @@ public abstract class BlockItemRewriter extends Rew Block b = handleBlock(btype, block & 15); // Type / data section.setBlock(x, y, z, b.getId(), b.getData()); } + // Entity Tags + if (hasBlockEntityHandler(btype)) { + Pos pos = new Pos(x, (y + (i << 4)), z); + CompoundTag tag = null; + if (tags.containsKey(pos)) { + tag = tags.get(pos); + } else { + tag = new CompoundTag(""); + tag.put(new IntTag("x", x + (chunk.getX() << 4))); + tag.put(new IntTag("y", y + (i << 4))); + tag.put(new IntTag("z", z + (chunk.getZ() << 4))); + chunk.getBlockEntities().add(tag); + } + replacementData.get(btype).getBlockEntityHandler().handleOrNewCompoundTag(block, tag); + } } } } @@ -121,7 +156,11 @@ public abstract class BlockItemRewriter extends Rew } protected boolean containsBlock(int block) { - return blockRewriter.containsKey(block); + return replacementData.containsKey(block) && replacementData.get(block).hasRepBlock(); + } + + protected boolean hasBlockEntityHandler(int block) { + return replacementData.containsKey(block) && replacementData.get(block).hasHandler(); } private CompoundTag createViaNBT(Item i) { @@ -147,4 +186,37 @@ public abstract class BlockItemRewriter extends Rew return getProtocol().getClass().getSimpleName(); } + public interface BlockEntityHandler { + CompoundTag handleOrNewCompoundTag(int block, CompoundTag tag); + } + + @Data + @RequiredArgsConstructor + public class BlockItemData { + private final int id; + private final Item repItem; + private final Block repBlock; + private final BlockEntityHandler blockEntityHandler; + + public boolean hasRepItem() { + return repItem != null; + } + + public boolean hasRepBlock() { + return repBlock != null; + } + + public boolean hasHandler() { + return blockEntityHandler != null; + } + + } + + @Data + @AllArgsConstructor + @ToString + @EqualsAndHashCode + private class Pos { + private int x, y, z; + } } diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java index 75749748..45d0ab32 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/SoundRewriter.java @@ -14,11 +14,11 @@ import lombok.AllArgsConstructor; import lombok.Data; import nl.matsv.viabackwards.api.BackwardsProtocol; -import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public abstract class SoundRewriter extends Rewriter { - private Map soundRewrites = new HashMap<>(); + private Map soundRewrites = new ConcurrentHashMap<>(); public SoundData added(int id, int replacement) { return added(id, replacement, -1); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/EntityTypeNames.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/EntityTypeNames.java new file mode 100644 index 00000000..b59d4f9e --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/EntityTypeNames.java @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2016 Matsv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package nl.matsv.viabackwards.protocol.protocol1_10to1_11; + +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; +import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag; + +/* + Copied from ViaVersion //TODO implement in EntityTypes? + */ +public class EntityTypeNames { + private static BiMap newToOldNames = HashBiMap.create(); + + static { + add("AreaEffectCloud", "minecraft:area_effect_cloud"); + add("ArmorStand", "minecraft:armor_stand"); + add("Arrow", "minecraft:arrow"); + add("Bat", "minecraft:bat"); + add("Blaze", "minecraft:blaze"); + add("Boat", "minecraft:boat"); + add("CaveSpider", "minecraft:cave_spider"); + add("Chicken", "minecraft:chicken"); + add("Cow", "minecraft:cow"); + add("Creeper", "minecraft:creeper"); + add("Donkey", "minecraft:donkey"); + add("DragonFireball", "minecraft:dragon_fireball"); + add("ElderGuardian", "minecraft:elder_guardian"); + add("EnderCrystal", "minecraft:ender_crystal"); + add("EnderDragon", "minecraft:ender_dragon"); + add("Enderman", "minecraft:enderman"); + add("Endermite", "minecraft:endermite"); + add("EntityHorse", "minecraft:horse"); + add("EyeOfEnderSignal", "minecraft:eye_of_ender_signal"); + add("FallingSand", "minecraft:falling_block"); + add("Fireball", "minecraft:fireball"); + add("FireworksRocketEntity", "minecraft:fireworks_rocket"); + add("Ghast", "minecraft:ghast"); + add("Giant", "minecraft:giant"); + add("Guardian", "minecraft:guardian"); + add("Husk", "minecraft:husk"); + add("Item", "minecraft:item"); + add("ItemFrame", "minecraft:item_frame"); + add("LavaSlime", "minecraft:magma_cube"); + add("LeashKnot", "minecraft:leash_knot"); + add("MinecartChest", "minecraft:chest_minecart"); + add("MinecartCommandBlock", "minecraft:commandblock_minecart"); + add("MinecartFurnace", "minecraft:furnace_minecart"); + add("MinecartHopper", "minecraft:hopper_minecart"); + add("MinecartRideable", "minecraft:minecart"); + add("MinecartSpawner", "minecraft:spawner_minecart"); + add("MinecartTNT", "minecraft:tnt_minecart"); + add("Mule", "minecraft:mule"); + add("MushroomCow", "minecraft:mooshroom"); + add("Ozelot", "minecraft:ocelot"); + add("Painting", "minecraft:painting"); + add("Pig", "minecraft:pig"); + add("PigZombie", "minecraft:zombie_pigman"); + add("PolarBear", "minecraft:polar_bear"); + add("PrimedTnt", "minecraft:tnt"); + add("Rabbit", "minecraft:rabbit"); + add("Sheep", "minecraft:sheep"); + add("Shulker", "minecraft:shulker"); + add("ShulkerBullet", "minecraft:shulker_bullet"); + add("Silverfish", "minecraft:silverfish"); + add("Skeleton", "minecraft:skeleton"); + add("SkeletonHorse", "minecraft:skeleton_horse"); + add("Slime", "minecraft:slime"); + add("SmallFireball", "minecraft:small_fireball"); + add("Snowball", "minecraft:snowball"); + add("SnowMan", "minecraft:snowman"); + add("SpectralArrow", "minecraft:spectral_arrow"); + add("Spider", "minecraft:spider"); + add("Squid", "minecraft:squid"); + add("Stray", "minecraft:stray"); + add("ThrownEgg", "minecraft:egg"); + add("ThrownEnderpearl", "minecraft:ender_pearl"); + add("ThrownExpBottle", "minecraft:xp_bottle"); + add("ThrownPotion", "minecraft:potion"); + add("Villager", "minecraft:villager"); + add("VillagerGolem", "minecraft:villager_golem"); + add("Witch", "minecraft:witch"); + add("WitherBoss", "minecraft:wither"); + add("WitherSkeleton", "minecraft:wither_skeleton"); + add("WitherSkull", "minecraft:wither_skull"); + add("Wolf", "minecraft:wolf"); + add("XPOrb", "minecraft:xp_orb"); + add("Zombie", "minecraft:zombie"); + add("ZombieHorse", "minecraft:zombie_horse"); + add("ZombieVillager", "minecraft:zombie_villager"); + } + + // Other way around (-: + private static void add(String oldName, String newName) { + newToOldNames.put(newName, oldName); + } + + public static void toClient(CompoundTag tag) { + if (tag.get("id") instanceof StringTag) { + StringTag id = tag.get("id"); + if (newToOldNames.containsKey(id.getValue())) { + id.setValue(newToOldNames.get(id.getValue())); + } + } + } + + public static void toClientSpawner(CompoundTag tag) { + if (tag != null && tag.contains("SpawnData")) { + CompoundTag spawnData = tag.get("SpawnData"); + if (spawnData != null && spawnData.contains("id")) + toClient(spawnData); + } + } + + /*public static void toClientItem(Item item) { + if (hasEntityTag(item)) { + CompoundTag entityTag = item.getBlockEntityHandler().get("EntityTag"); + toClient(entityTag); + } + if (item != null && item.getAmount() <= 0) item.setAmount((byte) 1); + } + + public static void toServerItem(Item item) { + if (hasEntityTag(item)) { + CompoundTag entityTag = item.getBlockEntityHandler().get("EntityTag"); + if (entityTag.get("id") instanceof StringTag) { + StringTag id = entityTag.get("id"); + if (newToOldNames.inverse().containsKey(id.getValue())) { + id.setValue(newToOldNames.inverse().get(id.getValue())); + } + } + } + }*/ + + private static boolean hasEntityTag(Item item) { + if (item != null && item.getId() == 383) { // Monster Egg + CompoundTag tag = item.getTag(); + if (tag != null && tag.contains("EntityTag") && tag.get("EntityTag") instanceof CompoundTag) { + if (((CompoundTag) tag.get("EntityTag")).get("id") instanceof StringTag) { + return true; + } + } + } + return false; + } +} + diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets.java index ce8ddf8a..5f3e793a 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/BlockItemPackets.java @@ -11,6 +11,7 @@ package nl.matsv.viabackwards.protocol.protocol1_10to1_11.packets; import nl.matsv.viabackwards.api.rewriters.BlockItemRewriter; +import nl.matsv.viabackwards.protocol.protocol1_10to1_11.EntityTypeNames; import nl.matsv.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11; import nl.matsv.viabackwards.utils.Block; import us.myles.ViaVersion.api.PacketWrapper; @@ -24,6 +25,7 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.chunks.Chunk1_9_3_4; import us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag; public class BlockItemPackets extends BlockItemRewriter { @Override @@ -234,6 +236,11 @@ public class BlockItemPackets extends BlockItemRewriter { if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 10) { wrapper.cancel(); } + // Handler Spawners + if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1) { + CompoundTag tag = wrapper.get(Type.NBT, 0); + EntityTypeNames.toClientSpawner(tag); + } } }); } @@ -260,6 +267,12 @@ public class BlockItemPackets extends BlockItemRewriter { // Observer to Dispenser TODO facing position? rewriteBlockItem(218, new Item((short) 23, (byte) 1, (short) 0, getNamedTag("1.11 Observer")), new Block(23, 0)); + // Handle spawner block entity + rewriteBlockItem(52, null, null, (b, tag) -> { + EntityTypeNames.toClientSpawner(tag); + return tag; + }); + // Totem of Undying to Dead Bush rewriteItem(449, new Item((short) 32, (byte) 1, (short) 0, getNamedTag("1.11 Totem of Undying"))); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets.java index 870f386d..21a11a36 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_10to1_11/packets/EntityPackets.java @@ -35,7 +35,6 @@ import static nl.matsv.viabackwards.api.entities.types.EntityType1_11.*; public class EntityPackets extends EntityRewriter { - @Override protected void registerPackets(Protocol1_10To1_11 protocol) { // Spawn Object