use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.
the class Protocol1_18_2To1_18 method registerPackets.
@Override
protected void registerPackets() {
final TagRewriter tagRewriter = new TagRewriter(this);
tagRewriter.addEmptyTag(RegistryType.BLOCK, "minecraft:fall_damage_resetting");
tagRewriter.registerGeneric(ClientboundPackets1_18.TAGS);
registerClientbound(ClientboundPackets1_18.ENTITY_EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Entity id
map(Type.VAR_INT);
// Effect id
map(Type.BYTE, Type.VAR_INT);
}
});
registerClientbound(ClientboundPackets1_18.REMOVE_ENTITY_EFFECT, new PacketRemapper() {
@Override
public void registerMap() {
// Entity id
map(Type.VAR_INT);
// Effect id
map(Type.BYTE, Type.VAR_INT);
}
});
registerClientbound(ClientboundPackets1_18.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);
// World List
map(Type.STRING_ARRAY);
// Registry
map(Type.NBT);
// Current dimension data
map(Type.NBT);
handler(wrapper -> {
final CompoundTag registry = wrapper.get(Type.NBT, 0);
final CompoundTag dimensionsHolder = registry.get("minecraft:dimension_type");
final ListTag dimensions = dimensionsHolder.get("value");
for (final Tag dimension : dimensions) {
addTagPrefix(((CompoundTag) dimension).get("element"));
}
addTagPrefix(wrapper.get(Type.NBT, 1));
});
}
});
registerClientbound(ClientboundPackets1_18.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> addTagPrefix(wrapper.passthrough(Type.NBT)));
}
});
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project DragonProxy by DragonetMC.
the class ItemBlockTranslator method translateRawNBT.
@SuppressWarnings("unchecked")
public static org.dragonet.common.data.nbt.tag.CompoundTag translateRawNBT(int id, Tag pcTag, org.dragonet.common.data.nbt.tag.CompoundTag target) {
if (pcTag != null) {
String name = pcTag.getName() != null ? pcTag.getName() : "";
if (target == null)
target = new org.dragonet.common.data.nbt.tag.CompoundTag(name);
switch(pcTag.getClass().getSimpleName()) {
case "ByteArrayTag":
target.putByteArray(name, (byte[]) pcTag.getValue());
break;
case "ByteTag":
target.putByte(name, (byte) pcTag.getValue());
break;
case "DoubleTag":
target.putDouble(name, (double) pcTag.getValue());
break;
case "FloatTag":
target.putFloat(name, (float) pcTag.getValue());
break;
case "IntArrayTag":
target.putIntArray(name, (int[]) pcTag.getValue());
break;
case "IntTag":
target.putInt(name, (int) pcTag.getValue());
break;
case "LongTag":
target.putLong(name, (long) pcTag.getValue());
break;
case "ShortTag":
target.putShort(name, (short) pcTag.getValue());
break;
case "StringTag":
target.putString(name, (String) pcTag.getValue());
break;
case "CompoundTag":
for (String subName : ((CompoundTag) pcTag).getValue().keySet()) translateRawNBT(0, ((CompoundTag) pcTag).getValue().get(subName), target);
break;
case "ListTag":
ListTag listTag = new ListTag();
for (Tag subTag : (List<Tag>) pcTag.getValue()) listTag.add(translateRawNBT(0, subTag, new org.dragonet.common.data.nbt.tag.CompoundTag()));
target.putList(listTag);
break;
default:
System.out.println("TAG not implemented : " + pcTag.getClass().getSimpleName());
break;
}
}
return target;
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.
the class BedrockBookEditTranslator method translate.
@Override
public void translate(GeyserSession session, BookEditPacket packet) {
if (packet.getText() != null && !packet.getText().isEmpty() && packet.getText().getBytes(StandardCharsets.UTF_8).length > MAXIMUM_PAGE_LENGTH) {
session.getGeyser().getLogger().warning("Page length greater than server allowed!");
return;
}
GeyserItemStack itemStack = session.getPlayerInventory().getItemInHand();
if (itemStack != null) {
CompoundTag tag = itemStack.getNbt() != null ? itemStack.getNbt() : new CompoundTag("");
ItemStack bookItem = new ItemStack(itemStack.getJavaId(), itemStack.getAmount(), tag);
List<Tag> pages = tag.contains("pages") ? new LinkedList<>(((ListTag) tag.get("pages")).getValue()) : new LinkedList<>();
int page = packet.getPageNumber();
switch(packet.getAction()) {
case ADD_PAGE:
{
// Add empty pages in between
for (int i = pages.size(); i < page; i++) {
pages.add(i, new StringTag("", ""));
}
pages.add(page, new StringTag("", packet.getText()));
break;
}
// Called whenever a page is modified
case REPLACE_PAGE:
{
if (page < pages.size()) {
pages.set(page, new StringTag("", packet.getText()));
} else {
// Add empty pages in between
for (int i = pages.size(); i < page; i++) {
pages.add(i, new StringTag("", ""));
}
pages.add(page, new StringTag("", packet.getText()));
}
break;
}
case DELETE_PAGE:
{
if (page < pages.size()) {
pages.remove(page);
}
break;
}
case SWAP_PAGES:
{
int page2 = packet.getSecondaryPageNumber();
if (page < pages.size() && page2 < pages.size()) {
Collections.swap(pages, page, page2);
}
break;
}
case SIGN_BOOK:
{
tag.put(new StringTag("author", packet.getAuthor()));
tag.put(new StringTag("title", packet.getTitle()));
break;
}
default:
return;
}
// Remove empty pages at the end
while (pages.size() > 0) {
StringTag currentPage = (StringTag) pages.get(pages.size() - 1);
if (currentPage.getValue() == null || currentPage.getValue().isEmpty()) {
pages.remove(pages.size() - 1);
} else {
break;
}
}
tag.put(new ListTag("pages", pages));
// Update local copy
session.getPlayerInventory().setItem(36 + session.getPlayerInventory().getHeldItemSlot(), GeyserItemStack.from(bookItem), session);
session.getInventoryTranslator().updateInventory(session, session.getPlayerInventory());
List<String> networkPages = new ArrayList<>();
for (Tag pageTag : pages) {
networkPages.add(((StringTag) pageTag).getValue());
}
String title;
if (packet.getAction() == BookEditPacket.Action.SIGN_BOOK) {
// Add title to packet so the server knows we're signing
if (packet.getTitle().getBytes(StandardCharsets.UTF_8).length > MAXIMUM_TITLE_LENGTH) {
session.getGeyser().getLogger().warning("Book title larger than server allows!");
return;
}
title = packet.getTitle();
} else {
title = null;
}
session.getBookEditCache().setPacket(new ServerboundEditBookPacket(session.getPlayerInventory().getHeldItemSlot(), networkPages, title));
// There won't be any more book updates after this, so we can try sending the edit packet immediately
if (packet.getAction() == BookEditPacket.Action.SIGN_BOOK) {
session.getBookEditCache().checkForSend();
}
}
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.
the class CrossbowTranslator method translateToBedrock.
@Override
public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemMapping mapping) {
if (itemTag.get("ChargedProjectiles") != null) {
ListTag chargedProjectiles = itemTag.get("ChargedProjectiles");
if (!chargedProjectiles.getValue().isEmpty()) {
CompoundTag projectile = (CompoundTag) chargedProjectiles.getValue().get(0);
ItemMapping projectileMapping = session.getItemMappings().getMapping((String) projectile.get("id").getValue());
if (projectileMapping == null)
return;
CompoundTag tag = projectile.get("tag");
ItemStack itemStack = new ItemStack(mapping.getJavaId(), (byte) projectile.get("Count").getValue(), tag);
ItemData itemData = ItemTranslator.translateToBedrock(session, itemStack);
CompoundTag newProjectile = new CompoundTag("chargedItem");
newProjectile.put(new ByteTag("Count", (byte) itemData.getCount()));
newProjectile.put(new StringTag("Name", projectileMapping.getBedrockIdentifier()));
newProjectile.put(new ShortTag("Damage", (short) itemData.getDamage()));
itemTag.put(newProjectile);
}
}
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.
the class EnchantedBookTranslator method translateToJava.
@Override
public void translateToJava(CompoundTag itemTag, ItemMapping mapping) {
if (!itemTag.contains("Enchantments")) {
return;
}
Tag enchTag = itemTag.get("Enchantments");
if (enchTag instanceof ListTag) {
enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue());
itemTag.remove("Enchantments");
itemTag.put(enchTag);
}
}
Aggregations