use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_13 method rewriteCanPlaceToClient.
private void rewriteCanPlaceToClient(CompoundTag tag, String tagName) {
// The tag was manually created incorrectly so ignore rewriting it
if (!(tag.get(tagName) instanceof ListTag))
return;
ListTag blockTag = tag.get(tagName);
if (blockTag == null)
return;
ListTag newCanPlaceOn = new ListTag(StringTag.class);
tag.put(extraNbtTag + "|" + tagName, ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(blockTag)));
for (Tag oldTag : blockTag) {
Object value = oldTag.getValue();
String[] newValues = value instanceof String ? BlockIdData.fallbackReverseMapping.get(((String) value).replace("minecraft:", "")) : null;
if (newValues != null) {
for (String newValue : newValues) {
newCanPlaceOn.add(new StringTag(newValue));
}
} else {
newCanPlaceOn.add(oldTag);
}
}
tag.put(tagName, newCanPlaceOn);
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_13 method rewriteEnchantmentsToServer.
private void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnch) {
String key = storedEnch ? "StoredEnchantments" : "Enchantments";
ListTag enchantments = tag.get(storedEnch ? key : "ench");
if (enchantments == null)
return;
ListTag newEnchantments = new ListTag(CompoundTag.class);
boolean dummyEnchant = false;
if (!storedEnch) {
IntTag hideFlags = tag.remove(extraNbtTag + "|OldHideFlags");
if (hideFlags != null) {
tag.put("HideFlags", new IntTag(hideFlags.asByte()));
dummyEnchant = true;
} else if (tag.remove(extraNbtTag + "|DummyEnchant") != null) {
tag.remove("HideFlags");
dummyEnchant = true;
}
}
for (Tag enchEntry : enchantments) {
CompoundTag enchantmentEntry = new CompoundTag();
short oldId = ((NumberTag) ((CompoundTag) enchEntry).get("id")).asShort();
short level = ((NumberTag) ((CompoundTag) enchEntry).get("lvl")).asShort();
if (dummyEnchant && oldId == 0 && level == 0) {
// Skip dummy enchatment
continue;
}
String newId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().get(oldId);
if (newId == null) {
newId = "viaversion:legacy/" + oldId;
}
enchantmentEntry.put("id", new StringTag(newId));
enchantmentEntry.put("lvl", new ShortTag(level));
newEnchantments.add(enchantmentEntry);
}
ListTag noMapped = tag.remove(extraNbtTag + "|Enchantments");
if (noMapped != null) {
for (Tag value : noMapped) {
newEnchantments.add(value);
}
}
CompoundTag display = tag.get("display");
if (display == null) {
tag.put("display", display = new CompoundTag());
}
ListTag oldLore = tag.remove(extraNbtTag + "|OldLore");
if (oldLore != null) {
ListTag lore = display.get("Lore");
if (lore == null) {
tag.put("Lore", lore = new ListTag());
}
lore.setValue(oldLore.getValue());
} else if (tag.remove(extraNbtTag + "|DummyLore") != null) {
display.remove("Lore");
if (display.isEmpty()) {
tag.remove("display");
}
}
if (!storedEnch) {
tag.remove("ench");
}
tag.put(key, newEnchantments);
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_13 method rewriteEnchantmentsToClient.
// TODO un-ugly all of this
private void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnch) {
String key = storedEnch ? "StoredEnchantments" : "Enchantments";
ListTag enchantments = tag.get(key);
if (enchantments == null)
return;
ListTag noMapped = new ListTag(CompoundTag.class);
ListTag newEnchantments = new ListTag(CompoundTag.class);
List<Tag> lore = new ArrayList<>();
boolean hasValidEnchants = false;
for (Tag enchantmentEntryTag : enchantments.clone()) {
CompoundTag enchantmentEntry = (CompoundTag) enchantmentEntryTag;
Tag idTag = enchantmentEntry.get("id");
if (!(idTag instanceof StringTag))
continue;
String newId = (String) idTag.getValue();
int levelValue = ((NumberTag) enchantmentEntry.get("lvl")).asInt();
short level = levelValue < Short.MAX_VALUE ? (short) levelValue : Short.MAX_VALUE;
String mappedEnchantmentId = enchantmentMappings.get(newId);
if (mappedEnchantmentId != null) {
lore.add(new StringTag(mappedEnchantmentId + " " + EnchantmentRewriter.getRomanNumber(level)));
noMapped.add(enchantmentEntry);
} else if (!newId.isEmpty()) {
Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
if (oldId == null) {
if (!newId.startsWith("viaversion:legacy/")) {
// Custom enchant (?)
noMapped.add(enchantmentEntry);
// Some custom-enchant plugins write it into the lore manually, which would double its entry
if (ViaBackwards.getConfig().addCustomEnchantsToLore()) {
String name = newId;
int index = name.indexOf(':') + 1;
if (index != 0 && index != name.length()) {
name = name.substring(index);
}
name = "§7" + Character.toUpperCase(name.charAt(0)) + name.substring(1).toLowerCase(Locale.ENGLISH);
lore.add(new StringTag(name + " " + EnchantmentRewriter.getRomanNumber(level)));
}
if (Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("Found unknown enchant: " + newId);
}
continue;
} else {
oldId = Short.valueOf(newId.substring(18));
}
}
if (level != 0) {
hasValidEnchants = true;
}
CompoundTag newEntry = new CompoundTag();
newEntry.put("id", new ShortTag(oldId));
newEntry.put("lvl", new ShortTag(level));
newEnchantments.add(newEntry);
}
}
// Put here to hide empty enchantment from 1.14 rewrites
if (!storedEnch && !hasValidEnchants) {
IntTag hideFlags = tag.get("HideFlags");
if (hideFlags == null) {
hideFlags = new IntTag();
tag.put(extraNbtTag + "|DummyEnchant", new ByteTag());
} else {
tag.put(extraNbtTag + "|OldHideFlags", new IntTag(hideFlags.asByte()));
}
if (newEnchantments.size() == 0) {
CompoundTag enchEntry = new CompoundTag();
enchEntry.put("id", new ShortTag((short) 0));
enchEntry.put("lvl", new ShortTag((short) 0));
newEnchantments.add(enchEntry);
}
int value = hideFlags.asByte() | 1;
hideFlags.setValue(value);
tag.put("HideFlags", hideFlags);
}
if (noMapped.size() != 0) {
tag.put(extraNbtTag + "|" + key, noMapped);
if (!lore.isEmpty()) {
CompoundTag display = tag.get("display");
if (display == null) {
tag.put("display", display = new CompoundTag());
}
ListTag loreTag = display.get("Lore");
if (loreTag == null) {
display.put("Lore", loreTag = new ListTag(StringTag.class));
tag.put(extraNbtTag + "|DummyLore", new ByteTag());
} else if (loreTag.size() != 0) {
ListTag oldLore = new ListTag(StringTag.class);
for (Tag value : loreTag) {
oldLore.add(value.clone());
}
tag.put(extraNbtTag + "|OldLore", oldLore);
lore.addAll(loreTag.getValue());
}
loreTag.setValue(lore);
}
}
tag.remove("Enchantments");
tag.put(storedEnch ? key : "ench", newEnchantments);
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class BackwardsBlockEntityProvider method transform.
/**
* Transform blocks to block entities!
*
* @param user The user
* @param position The position of the block entity
* @param tag The block entity tag
*/
public CompoundTag transform(UserConnection user, Position position, CompoundTag tag) throws Exception {
final Tag idTag = tag.get("id");
if (!(idTag instanceof StringTag)) {
return tag;
}
String id = (String) idTag.getValue();
BackwardsBlockEntityHandler handler = handlers.get(id);
if (handler == null) {
if (Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("Unhandled BlockEntity " + id + " full tag: " + tag);
}
return tag;
}
BackwardsBlockStorage storage = user.get(BackwardsBlockStorage.class);
Integer blockId = storage.get(position);
if (blockId == null) {
if (Via.getManager().isDebug()) {
ViaBackwards.getPlatform().getLogger().warning("Handled BlockEntity does not have a stored block :( " + id + " full tag: " + tag);
}
return tag;
}
return handler.transform(user, blockId, tag);
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag in project ViaBackwards by ViaVersion.
the class EntityPackets1_17 method registerPackets.
@Override
protected void registerPackets() {
registerTrackerWithData(ClientboundPackets1_17.SPAWN_ENTITY, Entity1_17Types.FALLING_BLOCK);
registerSpawnTracker(ClientboundPackets1_17.SPAWN_MOB);
registerTracker(ClientboundPackets1_17.SPAWN_EXPERIENCE_ORB, Entity1_17Types.EXPERIENCE_ORB);
registerTracker(ClientboundPackets1_17.SPAWN_PAINTING, Entity1_17Types.PAINTING);
registerTracker(ClientboundPackets1_17.SPAWN_PLAYER, Entity1_17Types.PLAYER);
registerMetadataRewriter(ClientboundPackets1_17.ENTITY_METADATA, Types1_17.METADATA_LIST, Types1_16.METADATA_LIST);
protocol.registerClientbound(ClientboundPackets1_17.REMOVE_ENTITY, ClientboundPackets1_16_2.DESTROY_ENTITIES, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int entityId = wrapper.read(Type.VAR_INT);
tracker(wrapper.user()).removeEntity(entityId);
// Write into single value array
int[] array = { entityId };
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, array);
});
}
});
protocol.registerClientbound(ClientboundPackets1_17.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
// Entity ID
map(Type.INT);
// Hardcore
map(Type.BOOLEAN);
// Gamemode
map(Type.UNSIGNED_BYTE);
// Previous Gamemode
map(Type.BYTE);
// Worlds
map(Type.STRING_ARRAY);
// Dimension registry
map(Type.NBT);
// Current dimension data
map(Type.NBT);
// World
map(Type.STRING);
handler(wrapper -> {
byte previousGamemode = wrapper.get(Type.BYTE, 0);
if (previousGamemode == -1) {
// "Unset" gamemode removed
wrapper.set(Type.BYTE, 0, (byte) 0);
}
});
handler(getTrackerHandler(Entity1_17Types.PLAYER, Type.INT));
handler(worldDataTrackerHandler(1));
handler(wrapper -> {
CompoundTag registry = wrapper.get(Type.NBT, 0);
CompoundTag biomeRegistry = registry.get("minecraft:worldgen/biome");
ListTag biomes = biomeRegistry.get("value");
for (Tag biome : biomes) {
CompoundTag biomeCompound = ((CompoundTag) biome).get("element");
StringTag category = biomeCompound.get("category");
if (category.getValue().equalsIgnoreCase("underground")) {
category.setValue("none");
}
}
CompoundTag dimensionRegistry = registry.get("minecraft:dimension_type");
ListTag dimensions = dimensionRegistry.get("value");
for (Tag dimension : dimensions) {
CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element");
reduceExtendedHeight(dimensionCompound, false);
}
reduceExtendedHeight(wrapper.get(Type.NBT, 1), true);
});
}
});
protocol.registerClientbound(ClientboundPackets1_17.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
// Dimension data
map(Type.NBT);
// World
map(Type.STRING);
handler(worldDataTrackerHandler(0));
handler(wrapper -> reduceExtendedHeight(wrapper.get(Type.NBT, 0), true));
}
});
protocol.registerClientbound(ClientboundPackets1_17.PLAYER_POSITION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.DOUBLE);
map(Type.FLOAT);
map(Type.FLOAT);
map(Type.BYTE);
map(Type.VAR_INT);
handler(wrapper -> {
// Dismount vehicle ¯\_(ツ)_/¯
wrapper.read(Type.BOOLEAN);
});
}
});
protocol.registerClientbound(ClientboundPackets1_17.ENTITY_PROPERTIES, new PacketRemapper() {
@Override
public void registerMap() {
// Entity id
map(Type.VAR_INT);
handler(wrapper -> {
// Collection length
wrapper.write(Type.INT, wrapper.read(Type.VAR_INT));
});
}
});
// TODO translatables
protocol.mergePacket(ClientboundPackets1_17.COMBAT_ENTER, ClientboundPackets1_16_2.COMBAT_EVENT, 0);
protocol.mergePacket(ClientboundPackets1_17.COMBAT_END, ClientboundPackets1_16_2.COMBAT_EVENT, 1);
protocol.mergePacket(ClientboundPackets1_17.COMBAT_KILL, ClientboundPackets1_16_2.COMBAT_EVENT, 2);
}
Aggregations