Fix display tag setting and doubled item id rewriting

This commit is contained in:
Nassim Jahnke 2024-03-19 12:05:37 +01:00
parent 3fef71db2e
commit 60b782455d
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
3 changed files with 46 additions and 18 deletions

View File

@ -98,4 +98,13 @@ public class StructuredItem implements Item {
result = 31 * result + amount;
return result;
}
@Override
public String toString() {
return "StructuredItem{" +
"data=" + data +
", identifier=" + identifier +
", amount=" + amount +
'}';
}
}

View File

@ -442,8 +442,12 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
return item;
}
private int toItemId(final String name) {
final int unmappedId = protocol.getMappingData().itemId(name);
private int unmappedItemId(final String name) {
return protocol.getMappingData().itemId(name);
}
private int toMappedItemId(final String name) {
final int unmappedId = unmappedItemId(name);
return unmappedId != -1 ? protocol.getMappingData().getNewItemId(unmappedId) : -1;
}
@ -616,10 +620,12 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
if (assetNameTag == null || ingredientTag == null) {
return;
}
final int ingredientId = toItemId(ingredientTag.getValue());
final int ingredientId = toMappedItemId(ingredientTag.getValue());
if (ingredientId == -1) {
return;
}
final NumberTag itemModelIndexTag = materialCompoundTag.getNumberTag("item_model_index");
final CompoundTag overrideArmorMaterialsTag = materialCompoundTag.get("override_armor_materials");
final Tag descriptionTag = materialCompoundTag.get("description");
@ -663,10 +669,12 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
if (assetId == null || templateItem == null) {
return;
}
final int templateItemId = toItemId(templateItem);
final int templateItemId = toMappedItemId(templateItem);
if (templateItemId == -1) {
return;
}
final Tag descriptionTag = patternCompoundTag.get("description");
final boolean decal = patternCompoundTag.getBoolean("decal");
patternHolder = Holder.of(new ArmorTrimPattern(
@ -817,13 +825,11 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
}
private void updateItemList(final StructuredDataContainer data, final CompoundTag tag, final String key, final StructuredDataKey<Item[]> dataKey) {
final ListTag<CompoundTag> chargedProjectiles = tag.getListTag(key, CompoundTag.class);
if (chargedProjectiles == null) {
return;
final ListTag<CompoundTag> itemsTag = tag.getListTag(key, CompoundTag.class);
if (itemsTag != null) {
final Item[] items = itemsTag.stream().map(this::itemFromTag).filter(Objects::nonNull).toArray(Item[]::new);
data.set(dataKey, items);
}
final Item[] items = chargedProjectiles.stream().map(this::itemFromTag).filter(Objects::nonNull).toArray(Item[]::new);
data.set(dataKey, items);
}
private @Nullable Item itemFromTag(final CompoundTag item) {
@ -832,7 +838,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
return null;
}
final int itemId = toItemId(id);
final int itemId = unmappedItemId(id);
if (itemId == -1) {
return null;
}
@ -1000,7 +1006,12 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final String rightSherd = sherdsTag.get(2).getValue();
final String frontSherd = sherdsTag.get(3).getValue();
data.set(StructuredDataKey.POT_DECORATIONS, new PotDecorations(toItemId(backSherd), toItemId(leftSherd), toItemId(rightSherd), toItemId(frontSherd)));
data.set(StructuredDataKey.POT_DECORATIONS, new PotDecorations(
toMappedItemId(backSherd),
toMappedItemId(leftSherd),
toMappedItemId(rightSherd),
toMappedItemId(frontSherd)
));
}
final StringTag noteBlockSoundTag = tag.getStringTag("note_block_sound");

View File

@ -84,13 +84,13 @@ final class StructuredDataConverter {
putHideFlag(tag, HIDE_UNBREAKABLE);
}
});
register(StructuredDataKey.CUSTOM_NAME, (data, tag) -> tag.putString("CustomName", ComponentUtil.tagToJsonString(data)));
register(StructuredDataKey.CUSTOM_NAME, (data, tag) -> getDisplayTag(tag).putString("Name", ComponentUtil.tagToJsonString(data)));
register(StructuredDataKey.LORE, (data, tag) -> {
final ListTag<StringTag> lore = new ListTag<>(StringTag.class);
for (final Tag loreEntry : data) {
lore.add(new StringTag(ComponentUtil.tagToJsonString(loreEntry)));
}
tag.put("Lore", lore);
getDisplayTag(tag).put("Lore", lore);
});
register(StructuredDataKey.ENCHANTMENTS, (data, tag) -> convertEnchantments(data, tag, false));
register(StructuredDataKey.STORED_ENCHANTMENTS, (data, tag) -> convertEnchantments(data, tag, true));
@ -120,12 +120,12 @@ final class StructuredDataConverter {
register(StructuredDataKey.HIDE_ADDITIONAL_TOOLTIP, (data, tag) -> putHideFlag(tag, 0x20));
register(StructuredDataKey.REPAIR_COST, (data, tag) -> tag.putInt("RepairCost", data));
register(StructuredDataKey.DYED_COLOR, (data, tag) -> {
tag.putInt("color", data.rgb());
getDisplayTag(tag).putInt("color", data.rgb());
if (!data.showInTooltip()) {
putHideFlag(tag, HIDE_DYE_COLOR);
}
});
register(StructuredDataKey.MAP_COLOR, (data, tag) -> tag.putInt("MapColor", data));
register(StructuredDataKey.MAP_COLOR, (data, tag) -> getDisplayTag(tag).putInt("MapColor", data));
register(StructuredDataKey.MAP_ID, (data, tag) -> tag.putInt("map", data));
register(StructuredDataKey.MAP_DECORATIONS, (data, tag) -> {
final ListTag<CompoundTag> decorations = new ListTag<>(CompoundTag.class);
@ -447,10 +447,18 @@ final class StructuredDataConverter {
// If multiple item components which previously were stored in BlockEntityTag are present, we need to merge them
private static CompoundTag getBlockEntityTag(final CompoundTag tag) {
CompoundTag subTag = tag.getCompoundTag("BlockEntityTag");
return getOrCreate(tag, "BlockEntityTag");
}
private static CompoundTag getDisplayTag(final CompoundTag tag) {
return getOrCreate(tag, "display");
}
private static CompoundTag getOrCreate(final CompoundTag tag, final String key) {
CompoundTag subTag = tag.getCompoundTag(key);
if (subTag == null) {
subTag = new CompoundTag();
tag.put("BlockEntityTag", subTag);
tag.put(key, subTag);
}
return subTag;
}