use of com.github.steveice10.opennbt.tag.builtin.ShortTag in project ViaVersion by ViaVersion.
the class InventoryPackets method handleItemToClient.
@Override
public Item handleItemToClient(Item item) {
if (item == null)
return null;
CompoundTag tag = item.tag();
// Save original id
int originalId = (item.identifier() << 16 | item.data() & 0xFFFF);
int rawId = (item.identifier() << 4 | item.data() & 0xF);
// NBT Additions
if (isDamageable(item.identifier())) {
if (tag == null)
item.setTag(tag = new CompoundTag());
tag.put("Damage", new IntTag(item.data()));
}
if (item.identifier() == 358) {
// map
if (tag == null)
item.setTag(tag = new CompoundTag());
tag.put("map", new IntTag(item.data()));
}
// NBT Changes
if (tag != null) {
// Invert banner/shield color id
boolean banner = item.identifier() == 425;
if (banner || item.identifier() == 442) {
if (tag.get("BlockEntityTag") instanceof CompoundTag) {
CompoundTag blockEntityTag = tag.get("BlockEntityTag");
if (blockEntityTag.get("Base") instanceof IntTag) {
IntTag base = blockEntityTag.get("Base");
// Set banner item id according to nbt
if (banner) {
rawId = 6800 + base.asInt();
}
base.setValue(15 - base.asInt());
}
if (blockEntityTag.get("Patterns") instanceof ListTag) {
for (Tag pattern : (ListTag) blockEntityTag.get("Patterns")) {
if (pattern instanceof CompoundTag) {
IntTag c = ((CompoundTag) pattern).get("Color");
// Invert color id
c.setValue(15 - c.asInt());
}
}
}
}
}
// Display Name now uses JSON
if (tag.get("display") instanceof CompoundTag) {
CompoundTag display = tag.get("display");
if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name");
display.put(NBT_TAG_NAME + "|Name", new StringTag(name.getValue()));
name.setValue(ChatRewriter.legacyTextToJsonString(name.getValue(), true));
}
}
// ench is now Enchantments and now uses identifiers
if (tag.get("ench") instanceof ListTag) {
ListTag ench = tag.get("ench");
ListTag enchantments = new ListTag(CompoundTag.class);
for (Tag enchEntry : ench) {
NumberTag idTag;
if (enchEntry instanceof CompoundTag && (idTag = ((CompoundTag) enchEntry).get("id")) != null) {
CompoundTag enchantmentEntry = new CompoundTag();
short oldId = idTag.asShort();
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(((NumberTag) ((CompoundTag) enchEntry).get("lvl")).asShort()));
enchantments.add(enchantmentEntry);
}
}
tag.remove("ench");
tag.put("Enchantments", enchantments);
}
if (tag.get("StoredEnchantments") instanceof ListTag) {
ListTag storedEnch = tag.get("StoredEnchantments");
ListTag newStoredEnch = new ListTag(CompoundTag.class);
for (Tag enchEntry : storedEnch) {
if (enchEntry instanceof CompoundTag) {
CompoundTag enchantmentEntry = new CompoundTag();
short oldId = ((NumberTag) ((CompoundTag) enchEntry).get("id")).asShort();
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(((NumberTag) ((CompoundTag) enchEntry).get("lvl")).asShort()));
newStoredEnch.add(enchantmentEntry);
}
}
tag.remove("StoredEnchantments");
tag.put("StoredEnchantments", newStoredEnch);
}
if (tag.get("CanPlaceOn") instanceof ListTag) {
ListTag old = tag.get("CanPlaceOn");
ListTag newCanPlaceOn = new ListTag(StringTag.class);
// There will be data losing
tag.put(NBT_TAG_NAME + "|CanPlaceOn", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(old)));
for (Tag oldTag : old) {
Object value = oldTag.getValue();
String oldId = value.toString().replace("minecraft:", "");
String numberConverted = BlockIdData.numberIdToString.get(Ints.tryParse(oldId));
if (numberConverted != null) {
oldId = numberConverted;
}
String[] newValues = BlockIdData.blockIdMapping.get(oldId.toLowerCase(Locale.ROOT));
if (newValues != null) {
for (String newValue : newValues) {
newCanPlaceOn.add(new StringTag(newValue));
}
} else {
newCanPlaceOn.add(new StringTag(oldId.toLowerCase(Locale.ROOT)));
}
}
tag.put("CanPlaceOn", newCanPlaceOn);
}
if (tag.get("CanDestroy") instanceof ListTag) {
ListTag old = tag.get("CanDestroy");
ListTag newCanDestroy = new ListTag(StringTag.class);
// There will be data losing
tag.put(NBT_TAG_NAME + "|CanDestroy", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(old)));
for (Tag oldTag : old) {
Object value = oldTag.getValue();
String oldId = value.toString().replace("minecraft:", "");
String numberConverted = BlockIdData.numberIdToString.get(Ints.tryParse(oldId));
if (numberConverted != null) {
oldId = numberConverted;
}
String[] newValues = BlockIdData.blockIdMapping.get(oldId.toLowerCase(Locale.ROOT));
if (newValues != null) {
for (String newValue : newValues) {
newCanDestroy.add(new StringTag(newValue));
}
} else {
newCanDestroy.add(new StringTag(oldId.toLowerCase(Locale.ROOT)));
}
}
tag.put("CanDestroy", newCanDestroy);
}
// Handle SpawnEggs
if (item.identifier() == 383) {
if (tag.get("EntityTag") instanceof CompoundTag) {
CompoundTag entityTag = tag.get("EntityTag");
if (entityTag.get("id") instanceof StringTag) {
StringTag identifier = entityTag.get("id");
rawId = SpawnEggRewriter.getSpawnEggId(identifier.getValue());
if (rawId == -1) {
// Bat fallback
rawId = 25100288;
} else {
entityTag.remove("id");
if (entityTag.isEmpty())
tag.remove("EntityTag");
}
} else {
// Fallback to bat
rawId = 25100288;
}
} else {
// Fallback to bat
rawId = 25100288;
}
}
if (tag.isEmpty()) {
item.setTag(tag = null);
}
}
if (!Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId)) {
if (!isDamageable(item.identifier()) && item.identifier() != 358) {
// Map
if (tag == null)
item.setTag(tag = new CompoundTag());
// Data will be lost, saving original id
tag.put(NBT_TAG_NAME, new IntTag(originalId));
}
if (item.identifier() == 31 && item.data() == 0) {
// Shrub was removed
// Dead Bush
rawId = 32 << 4;
} else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId & ~0xF)) {
// Remove data
rawId &= ~0xF;
} else {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Failed to get 1.13 item for " + item.identifier());
}
// Stone
rawId = 16;
}
}
item.setIdentifier(Protocol1_13To1_12_2.MAPPINGS.getItemMappings().get(rawId));
item.setData((short) 0);
return item;
}
use of com.github.steveice10.opennbt.tag.builtin.ShortTag 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.ShortTag 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.ShortTag in project ViaVersion by ViaVersion.
the class ComponentRewriter1_13 method handleHoverEvent.
@Override
protected void handleHoverEvent(JsonObject hoverEvent) {
super.handleHoverEvent(hoverEvent);
String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (!action.equals("show_item"))
return;
JsonElement value = hoverEvent.get("value");
if (value == null)
return;
String text = findItemNBT(value);
if (text == null)
return;
CompoundTag tag;
try {
tag = BinaryTagIO.readString(text);
} catch (Exception e) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Error reading NBT in show_item:" + text);
e.printStackTrace();
}
return;
}
CompoundTag itemTag = tag.get("tag");
ShortTag damageTag = tag.get("Damage");
// Call item converter
short damage = damageTag != null ? damageTag.asShort() : 0;
Item item = new DataItem();
item.setData(damage);
item.setTag(itemTag);
protocol.getItemRewriter().handleItemToClient(item);
// Serialize again
if (damage != item.data()) {
tag.put("Damage", new ShortTag(item.data()));
}
if (itemTag != null) {
tag.put("tag", itemTag);
}
JsonArray array = new JsonArray();
JsonObject object = new JsonObject();
array.add(object);
String serializedNBT;
try {
serializedNBT = BinaryTagIO.writeString(tag);
object.addProperty("text", serializedNBT);
hoverEvent.add("value", array);
} catch (IOException e) {
Via.getPlatform().getLogger().warning("Error writing NBT in show_item:" + text);
e.printStackTrace();
}
}
use of com.github.steveice10.opennbt.tag.builtin.ShortTag in project ViaVersion by ViaVersion.
the class InventoryPackets method handleItemToServer.
@Override
public Item handleItemToServer(Item item) {
if (item == null)
return null;
Integer rawId = null;
boolean gotRawIdFromTag = false;
CompoundTag tag = item.tag();
// Use tag to get original ID and data
if (tag != null) {
// Check for valid tag
if (tag.get(NBT_TAG_NAME) instanceof IntTag) {
rawId = ((NumberTag) tag.get(NBT_TAG_NAME)).asInt();
// Remove the tag
tag.remove(NBT_TAG_NAME);
gotRawIdFromTag = true;
}
}
if (rawId == null) {
int oldId = Protocol1_13To1_12_2.MAPPINGS.getItemMappings().inverse().get(item.identifier());
if (oldId != -1) {
// Handle spawn eggs
Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
if (eggEntityId.isPresent()) {
rawId = 383 << 16;
if (tag == null)
item.setTag(tag = new CompoundTag());
if (!tag.contains("EntityTag")) {
CompoundTag entityTag = new CompoundTag();
entityTag.put("id", new StringTag(eggEntityId.get()));
tag.put("EntityTag", entityTag);
}
} else {
rawId = (oldId >> 4) << 16 | oldId & 0xF;
}
}
}
if (rawId == null) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Failed to get 1.12 item for " + item.identifier());
}
// Stone
rawId = 0x10000;
}
item.setIdentifier((short) (rawId >> 16));
item.setData((short) (rawId & 0xFFFF));
// NBT changes
if (tag != null) {
if (isDamageable(item.identifier())) {
if (tag.get("Damage") instanceof IntTag) {
if (!gotRawIdFromTag) {
item.setData((short) (int) tag.get("Damage").getValue());
}
tag.remove("Damage");
}
}
if (item.identifier() == 358) {
// map
if (tag.get("map") instanceof IntTag) {
if (!gotRawIdFromTag) {
item.setData((short) (int) tag.get("map").getValue());
}
tag.remove("map");
}
}
if (item.identifier() == 442 || item.identifier() == 425) {
// shield / banner
if (tag.get("BlockEntityTag") instanceof CompoundTag) {
CompoundTag blockEntityTag = tag.get("BlockEntityTag");
if (blockEntityTag.get("Base") instanceof IntTag) {
IntTag base = blockEntityTag.get("Base");
// invert color id
base.setValue(15 - base.asInt());
}
if (blockEntityTag.get("Patterns") instanceof ListTag) {
for (Tag pattern : (ListTag) blockEntityTag.get("Patterns")) {
if (pattern instanceof CompoundTag) {
IntTag c = ((CompoundTag) pattern).get("Color");
// Invert color id
c.setValue(15 - c.asInt());
}
}
}
}
}
// Display Name now uses JSON
if (tag.get("display") instanceof CompoundTag) {
CompoundTag display = tag.get("display");
if (display.get("Name") instanceof StringTag) {
StringTag name = display.get("Name");
StringTag via = display.remove(NBT_TAG_NAME + "|Name");
name.setValue(via != null ? via.getValue() : ChatRewriter.jsonToLegacyText(name.getValue()));
}
}
// ench is now Enchantments and now uses identifiers
if (tag.get("Enchantments") instanceof ListTag) {
ListTag enchantments = tag.get("Enchantments");
ListTag ench = new ListTag(CompoundTag.class);
for (Tag enchantmentEntry : enchantments) {
if (enchantmentEntry instanceof CompoundTag) {
CompoundTag enchEntry = new CompoundTag();
String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
if (oldId != null) {
enchEntry.put("id", new ShortTag(oldId));
enchEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort()));
ench.add(enchEntry);
}
}
}
tag.remove("Enchantments");
tag.put("ench", ench);
}
if (tag.get("StoredEnchantments") instanceof ListTag) {
ListTag storedEnch = tag.get("StoredEnchantments");
ListTag newStoredEnch = new ListTag(CompoundTag.class);
for (Tag enchantmentEntry : storedEnch) {
if (enchantmentEntry instanceof CompoundTag) {
CompoundTag enchEntry = new CompoundTag();
String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
if (oldId != null) {
enchEntry.put("id", new ShortTag(oldId));
enchEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort()));
newStoredEnch.add(enchEntry);
}
}
}
tag.remove("StoredEnchantments");
tag.put("StoredEnchantments", newStoredEnch);
}
if (tag.get(NBT_TAG_NAME + "|CanPlaceOn") instanceof ListTag) {
tag.put("CanPlaceOn", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(tag.get(NBT_TAG_NAME + "|CanPlaceOn"))));
tag.remove(NBT_TAG_NAME + "|CanPlaceOn");
} else if (tag.get("CanPlaceOn") instanceof ListTag) {
ListTag old = tag.get("CanPlaceOn");
ListTag newCanPlaceOn = new ListTag(StringTag.class);
for (Tag oldTag : old) {
Object value = oldTag.getValue();
String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String ? ((String) value).replace("minecraft:", "") : null);
if (newValues != null) {
for (String newValue : newValues) {
newCanPlaceOn.add(new StringTag(newValue));
}
} else {
newCanPlaceOn.add(oldTag);
}
}
tag.put("CanPlaceOn", newCanPlaceOn);
}
if (tag.get(NBT_TAG_NAME + "|CanDestroy") instanceof ListTag) {
tag.put("CanDestroy", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(tag.get(NBT_TAG_NAME + "|CanDestroy"))));
tag.remove(NBT_TAG_NAME + "|CanDestroy");
} else if (tag.get("CanDestroy") instanceof ListTag) {
ListTag old = tag.get("CanDestroy");
ListTag newCanDestroy = new ListTag(StringTag.class);
for (Tag oldTag : old) {
Object value = oldTag.getValue();
String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String ? ((String) value).replace("minecraft:", "") : null);
if (newValues != null) {
for (String newValue : newValues) {
newCanDestroy.add(new StringTag(newValue));
}
} else {
newCanDestroy.add(oldTag);
}
}
tag.put("CanDestroy", newCanDestroy);
}
}
return item;
}
Aggregations