2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2023-01-12 12:45:53 +01:00
|
|
|
* Copyright (C) 2016-2023 ViaVersion and contributors
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.protocols.protocol1_11to1_10;
|
2016-08-10 20:12:27 +02:00
|
|
|
|
2017-05-14 14:34:51 +02:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
|
|
|
import com.github.steveice10.opennbt.tag.builtin.StringTag;
|
2020-02-04 15:33:36 +01:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.Tag;
|
2017-01-30 19:02:12 +01:00
|
|
|
import com.google.common.collect.BiMap;
|
|
|
|
import com.google.common.collect.HashBiMap;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.minecraft.item.Item;
|
2023-10-24 01:58:43 +02:00
|
|
|
import com.viaversion.viaversion.util.Key;
|
2016-08-10 20:12:27 +02:00
|
|
|
|
2017-05-14 16:02:04 +02:00
|
|
|
public class EntityIdRewriter {
|
2020-02-04 15:33:36 +01:00
|
|
|
private static final BiMap<String, String> oldToNewNames = HashBiMap.create();
|
2017-01-30 19:02:12 +01:00
|
|
|
|
|
|
|
static {
|
2023-10-24 01:58:43 +02:00
|
|
|
rewrite("AreaEffectCloud", "area_effect_cloud");
|
|
|
|
rewrite("ArmorStand", "armor_stand");
|
|
|
|
rewrite("Arrow", "arrow");
|
|
|
|
rewrite("Bat", "bat");
|
|
|
|
rewrite("Blaze", "blaze");
|
|
|
|
rewrite("Boat", "boat");
|
|
|
|
rewrite("CaveSpider", "cave_spider");
|
|
|
|
rewrite("Chicken", "chicken");
|
|
|
|
rewrite("Cow", "cow");
|
|
|
|
rewrite("Creeper", "creeper");
|
|
|
|
rewrite("Donkey", "donkey");
|
|
|
|
rewrite("DragonFireball", "dragon_fireball");
|
|
|
|
rewrite("ElderGuardian", "elder_guardian");
|
|
|
|
rewrite("EnderCrystal", "ender_crystal");
|
|
|
|
rewrite("EnderDragon", "ender_dragon");
|
|
|
|
rewrite("Enderman", "enderman");
|
|
|
|
rewrite("Endermite", "endermite");
|
|
|
|
rewrite("EntityHorse", "horse");
|
|
|
|
rewrite("EyeOfEnderSignal", "eye_of_ender_signal");
|
|
|
|
rewrite("FallingSand", "falling_block");
|
|
|
|
rewrite("Fireball", "fireball");
|
|
|
|
rewrite("FireworksRocketEntity", "fireworks_rocket");
|
|
|
|
rewrite("Ghast", "ghast");
|
|
|
|
rewrite("Giant", "giant");
|
|
|
|
rewrite("Guardian", "guardian");
|
|
|
|
rewrite("Husk", "husk");
|
|
|
|
rewrite("Item", "item");
|
|
|
|
rewrite("ItemFrame", "item_frame");
|
|
|
|
rewrite("LavaSlime", "magma_cube");
|
|
|
|
rewrite("LeashKnot", "leash_knot");
|
|
|
|
rewrite("MinecartChest", "chest_minecart");
|
|
|
|
rewrite("MinecartCommandBlock", "commandblock_minecart");
|
|
|
|
rewrite("MinecartFurnace", "furnace_minecart");
|
|
|
|
rewrite("MinecartHopper", "hopper_minecart");
|
|
|
|
rewrite("MinecartRideable", "minecart");
|
|
|
|
rewrite("MinecartSpawner", "spawner_minecart");
|
|
|
|
rewrite("MinecartTNT", "tnt_minecart");
|
|
|
|
rewrite("Mule", "mule");
|
|
|
|
rewrite("MushroomCow", "mooshroom");
|
|
|
|
rewrite("Ozelot", "ocelot");
|
|
|
|
rewrite("Painting", "painting");
|
|
|
|
rewrite("Pig", "pig");
|
|
|
|
rewrite("PigZombie", "zombie_pigman");
|
|
|
|
rewrite("PolarBear", "polar_bear");
|
|
|
|
rewrite("PrimedTnt", "tnt");
|
|
|
|
rewrite("Rabbit", "rabbit");
|
|
|
|
rewrite("Sheep", "sheep");
|
|
|
|
rewrite("Shulker", "shulker");
|
|
|
|
rewrite("ShulkerBullet", "shulker_bullet");
|
|
|
|
rewrite("Silverfish", "silverfish");
|
|
|
|
rewrite("Skeleton", "skeleton");
|
|
|
|
rewrite("SkeletonHorse", "skeleton_horse");
|
|
|
|
rewrite("Slime", "slime");
|
|
|
|
rewrite("SmallFireball", "small_fireball");
|
|
|
|
rewrite("Snowball", "snowball");
|
|
|
|
rewrite("SnowMan", "snowman");
|
|
|
|
rewrite("SpectralArrow", "spectral_arrow");
|
|
|
|
rewrite("Spider", "spider");
|
|
|
|
rewrite("Squid", "squid");
|
|
|
|
rewrite("Stray", "stray");
|
|
|
|
rewrite("ThrownEgg", "egg");
|
|
|
|
rewrite("ThrownEnderpearl", "ender_pearl");
|
|
|
|
rewrite("ThrownExpBottle", "xp_bottle");
|
|
|
|
rewrite("ThrownPotion", "potion");
|
|
|
|
rewrite("Villager", "villager");
|
|
|
|
rewrite("VillagerGolem", "villager_golem");
|
|
|
|
rewrite("Witch", "witch");
|
|
|
|
rewrite("WitherBoss", "wither");
|
|
|
|
rewrite("WitherSkeleton", "wither_skeleton");
|
|
|
|
rewrite("WitherSkull", "wither_skull");
|
|
|
|
rewrite("Wolf", "wolf");
|
|
|
|
rewrite("XPOrb", "xp_orb");
|
|
|
|
rewrite("Zombie", "zombie");
|
|
|
|
rewrite("ZombieHorse", "zombie_horse");
|
|
|
|
rewrite("ZombieVillager", "zombie_villager");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void rewrite(String oldName, String newName) {
|
|
|
|
oldToNewNames.put(oldName, Key.namespaced(newName));
|
2017-01-30 19:02:12 +01:00
|
|
|
}
|
|
|
|
|
2017-05-14 16:02:04 +02:00
|
|
|
public static void toClient(CompoundTag tag) {
|
2020-02-04 15:33:36 +01:00
|
|
|
toClient(tag, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void toClient(CompoundTag tag, boolean backwards) {
|
|
|
|
Tag idTag = tag.get("id");
|
|
|
|
if (idTag instanceof StringTag) {
|
|
|
|
StringTag id = (StringTag) idTag;
|
|
|
|
String newName = backwards ? oldToNewNames.inverse().get(id.getValue()) : oldToNewNames.get(id.getValue());
|
2018-09-30 20:48:23 +02:00
|
|
|
if (newName != null) {
|
|
|
|
id.setValue(newName);
|
2017-05-14 16:02:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void toClientSpawner(CompoundTag tag) {
|
2020-02-04 15:33:36 +01:00
|
|
|
toClientSpawner(tag, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void toClientSpawner(CompoundTag tag, boolean backwards) {
|
|
|
|
if (tag == null) return;
|
|
|
|
|
|
|
|
Tag spawnDataTag = tag.get("SpawnData");
|
|
|
|
if (spawnDataTag != null) {
|
|
|
|
toClient((CompoundTag) spawnDataTag, backwards);
|
2017-05-14 16:02:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void toClientItem(Item item) {
|
2020-02-04 15:33:36 +01:00
|
|
|
toClientItem(item, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void toClientItem(Item item, boolean backwards) {
|
2016-08-10 20:12:27 +02:00
|
|
|
if (hasEntityTag(item)) {
|
2021-06-04 12:15:14 +02:00
|
|
|
toClient(item.tag().get("EntityTag"), backwards);
|
2016-08-10 20:12:27 +02:00
|
|
|
}
|
2021-06-04 12:15:14 +02:00
|
|
|
if (item != null && item.amount() <= 0) item.setAmount(1);
|
2016-08-10 20:12:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-14 16:02:04 +02:00
|
|
|
public static void toServerItem(Item item) {
|
2020-02-04 15:33:36 +01:00
|
|
|
toServerItem(item, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void toServerItem(Item item, boolean backwards) {
|
|
|
|
if (!hasEntityTag(item)) return;
|
|
|
|
|
2021-06-04 12:15:14 +02:00
|
|
|
CompoundTag entityTag = item.tag().get("EntityTag");
|
2020-02-04 15:33:36 +01:00
|
|
|
Tag idTag = entityTag.get("id");
|
|
|
|
if (idTag instanceof StringTag) {
|
|
|
|
StringTag id = (StringTag) idTag;
|
|
|
|
String newName = backwards ? oldToNewNames.get(id.getValue()) : oldToNewNames.inverse().get(id.getValue());
|
|
|
|
if (newName != null) {
|
|
|
|
id.setValue(newName);
|
2016-08-10 20:12:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean hasEntityTag(Item item) {
|
2021-06-04 12:15:14 +02:00
|
|
|
if (item == null || item.identifier() != 383) return false; // Monster Egg
|
2020-02-04 15:33:36 +01:00
|
|
|
|
2021-06-04 12:15:14 +02:00
|
|
|
CompoundTag tag = item.tag();
|
2020-02-04 15:33:36 +01:00
|
|
|
if (tag == null) return false;
|
|
|
|
|
|
|
|
Tag entityTag = tag.get("EntityTag");
|
|
|
|
return entityTag instanceof CompoundTag && ((CompoundTag) entityTag).get("id") instanceof StringTag;
|
2016-08-10 20:12:27 +02:00
|
|
|
}
|
|
|
|
}
|