use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.
the class Protocol1_17To1_17_1 method registerPackets.
@Override
protected void registerPackets() {
registerClientbound(ClientboundPackets1_17_1.REMOVE_ENTITIES, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
wrapper.cancel();
for (int entityId : entityIds) {
// Send individual remove packets
PacketWrapper newPacket = wrapper.create(ClientboundPackets1_17.REMOVE_ENTITY);
newPacket.write(Type.VAR_INT, entityId);
newPacket.send(Protocol1_17To1_17_1.class);
}
});
}
});
registerClientbound(ClientboundPackets1_17_1.CLOSE_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
});
}
});
registerClientbound(ClientboundPackets1_17_1.SET_SLOT, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
int stateId = wrapper.read(Type.VAR_INT);
wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId);
});
}
});
registerClientbound(ClientboundPackets1_17_1.WINDOW_ITEMS, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
int stateId = wrapper.read(Type.VAR_INT);
wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId);
// Length is encoded as a var int in 1.17.1
wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT));
// Carried item - should work without adding it to the array above
wrapper.read(Type.FLAT_VAR_INT_ITEM);
});
}
});
registerServerbound(ServerboundPackets1_17.CLOSE_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
});
}
});
registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
int stateId = wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
wrapper.write(Type.VAR_INT, stateId == Integer.MAX_VALUE ? 0 : stateId);
});
}
});
registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM);
boolean signing = wrapper.read(Type.BOOLEAN);
// Slot comes first
wrapper.passthrough(Type.VAR_INT);
CompoundTag tag = item.tag();
ListTag pagesTag;
StringTag titleTag = null;
// Sanity checks
if (tag == null || (pagesTag = tag.get("pages")) == null || (signing && (titleTag = tag.get("title")) == null)) {
// Pages length
wrapper.write(Type.VAR_INT, 0);
// Optional title
wrapper.write(Type.BOOLEAN, false);
return;
}
// Write pages - limit them first
if (pagesTag.size() > MAX_PAGES) {
pagesTag = new ListTag(pagesTag.getValue().subList(0, MAX_PAGES));
}
wrapper.write(Type.VAR_INT, pagesTag.size());
for (Tag pageTag : pagesTag) {
String page = ((StringTag) pageTag).getValue();
// Limit page length
if (page.length() > MAX_PAGE_LENGTH) {
page = page.substring(0, MAX_PAGE_LENGTH);
}
wrapper.write(Type.STRING, page);
}
// Write optional title
wrapper.write(Type.BOOLEAN, signing);
if (signing) {
if (titleTag == null) {
titleTag = tag.get("title");
}
// Limit title length
String title = titleTag.getValue();
if (title.length() > MAX_TITLE_LENGTH) {
title = title.substring(0, MAX_TITLE_LENGTH);
}
wrapper.write(Type.STRING, title);
}
});
}
});
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.
the class BlockItemPackets1_11 method handleItemToClient.
@Override
public Item handleItemToClient(Item item) {
if (item == null)
return null;
super.handleItemToClient(item);
CompoundTag tag = item.tag();
if (tag == null)
return item;
// Rewrite spawn eggs (id checks are done in the method itself)
EntityIdRewriter.toClientItem(item, true);
if (tag.get("ench") instanceof ListTag) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
}
if (tag.get("StoredEnchantments") instanceof ListTag) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
}
return item;
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.
the class ItemPackets1_11_1 method handleItemToClient.
@Override
public Item handleItemToClient(Item item) {
if (item == null)
return null;
super.handleItemToClient(item);
CompoundTag tag = item.tag();
if (tag == null)
return item;
if (tag.get("ench") instanceof ListTag) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
}
if (tag.get("StoredEnchantments") instanceof ListTag) {
enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
}
return item;
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.
the class ItemRewriterBase method saveListTag.
protected void saveListTag(CompoundTag displayTag, ListTag original, String name) {
// Multiple places might try to backup data
String backupName = nbtTagName + "|o" + name;
if (!displayTag.contains(backupName)) {
// Clone all tag entries
ListTag listTag = new ListTag();
for (Tag tag : original.getValue()) {
listTag.add(tag.clone());
}
displayTag.put(backupName, listTag);
}
}
use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.
the class EnchantmentRewriter method rewriteEnchantmentsToClient.
public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) {
String key = storedEnchant ? "StoredEnchantments" : "Enchantments";
ListTag enchantments = tag.get(key);
List<Tag> loreToAdd = new ArrayList<>();
boolean changed = false;
Iterator<Tag> iterator = enchantments.iterator();
while (iterator.hasNext()) {
CompoundTag enchantmentEntry = (CompoundTag) iterator.next();
Tag idTag = enchantmentEntry.get("id");
if (!(idTag instanceof StringTag))
continue;
String enchantmentId = ((StringTag) idTag).getValue();
String remappedName = enchantmentMappings.get(enchantmentId);
if (remappedName != null) {
if (!changed) {
// Backup original before doing modifications
itemRewriter.saveListTag(tag, enchantments, key);
changed = true;
}
iterator.remove();
int level = ((NumberTag) enchantmentEntry.get("lvl")).asInt();
String loreValue = remappedName + " " + getRomanNumber(level);
if (jsonFormat) {
loreValue = ChatRewriter.legacyTextToJsonString(loreValue);
}
loreToAdd.add(new StringTag(loreValue));
}
}
if (!loreToAdd.isEmpty()) {
// Add dummy enchant for the glow effect if there are no actual enchantments left
if (!storedEnchant && enchantments.size() == 0) {
CompoundTag dummyEnchantment = new CompoundTag();
dummyEnchantment.put("id", new StringTag());
dummyEnchantment.put("lvl", new ShortTag((short) 0));
enchantments.add(dummyEnchantment);
}
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));
} else {
// Save original lore
itemRewriter.saveListTag(display, loreTag, "Lore");
}
loreToAdd.addAll(loreTag.getValue());
loreTag.setValue(loreToAdd);
}
}
Aggregations