3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-30 18:10:12 +01:00

Merge pull request #1647 from KennyTV/abstraction

Improvements to 1.11 EntityIdRewriter
Dieser Commit ist enthalten in:
Myles 2020-02-05 16:07:33 +00:00 committet von GitHub
Commit 712aa0dbf0
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23

Datei anzeigen

@ -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);
}
public static void toServerItem(Item item, boolean backwards) {
if (!hasEntityTag(item)) return;
CompoundTag entityTag = item.getTag().get("EntityTag"); CompoundTag entityTag = item.getTag().get("EntityTag");
if (entityTag.get("id") instanceof StringTag) { Tag idTag = entityTag.get("id");
StringTag id = entityTag.get("id"); if (idTag instanceof StringTag) {
String newName = oldToNewNames.inverse().get(id.getValue()); StringTag id = (StringTag) idTag;
String newName = backwards ? oldToNewNames.get(id.getValue()) : oldToNewNames.inverse().get(id.getValue());
if (newName != null) { if (newName != null) {
id.setValue(newName); 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(); CompoundTag tag = item.getTag();
if (tag != null && tag.contains("EntityTag") && tag.get("EntityTag") instanceof CompoundTag) { if (tag == null) return false;
if (((CompoundTag) tag.get("EntityTag")).get("id") instanceof StringTag) {
return true; Tag entityTag = tag.get("EntityTag");
} return entityTag instanceof CompoundTag && ((CompoundTag) entityTag).get("id") instanceof StringTag;
}
}
return false;
} }
} }