Merge pull request #1647 from KennyTV/abstraction

Improvements to 1.11 EntityIdRewriter
This commit is contained in:
Myles 2020-02-05 16:07:33 +00:00 committed by GitHub
commit 712aa0dbf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,12 +2,13 @@ package us.myles.ViaVersion.protocols.protocol1_11to1_10;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.common.collect.BiMap; import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.item.Item;
public class EntityIdRewriter { public class EntityIdRewriter {
private static BiMap<String, String> oldToNewNames = HashBiMap.create(); private static final BiMap<String, String> oldToNewNames = HashBiMap.create();
static { static {
oldToNewNames.put("AreaEffectCloud", "minecraft:area_effect_cloud"); oldToNewNames.put("AreaEffectCloud", "minecraft:area_effect_cloud");
@ -88,9 +89,14 @@ public class EntityIdRewriter {
} }
public static void toClient(CompoundTag tag) { public static void toClient(CompoundTag tag) {
if (tag.get("id") instanceof StringTag) { toClient(tag, false);
StringTag id = tag.get("id"); }
String newName = oldToNewNames.get(id.getValue());
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());
if (newName != null) { if (newName != null) {
id.setValue(newName); id.setValue(newName);
} }
@ -98,43 +104,54 @@ public class EntityIdRewriter {
} }
public static void toClientSpawner(CompoundTag tag) { public static void toClientSpawner(CompoundTag tag) {
if (tag != null && tag.contains("SpawnData")) { toClientSpawner(tag, false);
CompoundTag spawnData = tag.get("SpawnData"); }
if (spawnData != null && spawnData.contains("id"))
toClient(spawnData); public static void toClientSpawner(CompoundTag tag, boolean backwards) {
if (tag == null) return;
Tag spawnDataTag = tag.get("SpawnData");
if (spawnDataTag != null) {
toClient((CompoundTag) spawnDataTag, backwards);
} }
} }
public static void toClientItem(Item item) { public static void toClientItem(Item item) {
toClientItem(item, false);
}
public static void toClientItem(Item item, boolean backwards) {
if (hasEntityTag(item)) { if (hasEntityTag(item)) {
CompoundTag entityTag = item.getTag().get("EntityTag"); toClient(item.getTag().get("EntityTag"), backwards);
toClient(entityTag);
} }
if (item != null && item.getAmount() <= 0) item.setAmount((byte) 1); if (item != null && item.getAmount() <= 0) item.setAmount((byte) 1);
} }
public static void toServerItem(Item item) { public static void toServerItem(Item item) {
if (hasEntityTag(item)) { toServerItem(item, false);
CompoundTag entityTag = item.getTag().get("EntityTag"); }
if (entityTag.get("id") instanceof StringTag) {
StringTag id = entityTag.get("id"); public static void toServerItem(Item item, boolean backwards) {
String newName = oldToNewNames.inverse().get(id.getValue()); if (!hasEntityTag(item)) return;
if (newName != null) {
id.setValue(newName); CompoundTag entityTag = item.getTag().get("EntityTag");
} 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);
} }
} }
} }
private static boolean hasEntityTag(Item item) { private static boolean hasEntityTag(Item item) {
if (item != null && item.getIdentifier() == 383) { // Monster Egg if (item == null || item.getIdentifier() != 383) return false; // Monster Egg
CompoundTag tag = item.getTag();
if (tag != null && tag.contains("EntityTag") && tag.get("EntityTag") instanceof CompoundTag) { CompoundTag tag = item.getTag();
if (((CompoundTag) tag.get("EntityTag")).get("id") instanceof StringTag) { if (tag == null) return false;
return true;
} Tag entityTag = tag.get("EntityTag");
} return entityTag instanceof CompoundTag && ((CompoundTag) entityTag).get("id") instanceof StringTag;
}
return false;
} }
} }