Explain backup tags (#1)

This commit is contained in:
EnZaXD 2024-04-08 16:59:33 +02:00 committed by GitHub
parent 4b780b92ee
commit 7932b79080
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 22 deletions

View File

@ -509,6 +509,15 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
return item;
}
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;
}
private void restoreFromBackupTag(final CompoundTag backupTag, final StructuredDataContainer data) {
final CompoundTag instrument = backupTag.getCompoundTag("instrument");
if (instrument != null) {
@ -586,7 +595,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
final CompoundTag soundEventCompound = (CompoundTag) soundEventTag;
final StringTag identifier = soundEventCompound.getStringTag("identifier");
if (identifier == null) {
return; // Nothing we can do about
return;
}
soundEvent = Holder.of(new SoundEvent(
identifier.getValue(),
@ -594,7 +603,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
soundEventCompound.getFloat("fixed_range") : null
));
} else {
return; // Nothing we can do about
return;
}
data.set(StructuredDataKey.INSTRUMENT, Holder.of(new Instrument(soundEvent, useDuration, range)));
}
@ -613,7 +622,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
for (final CompoundTag effect : possibleEffectsTag) {
final CompoundTag potionEffectTag = effect.getCompoundTag("effect");
if (potionEffectTag == null) {
continue; // Nothing we can do about
continue;
}
possibleEffects.add(new FoodEffect(
new PotionEffect(
@ -643,7 +652,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
}
}
if (blocks == null) {
continue; // Nothing we can do about
continue;
}
rules.add(new ToolRule(
blocks,
@ -663,7 +672,7 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
for (final CompoundTag tag : bannerPatterns) {
final CompoundTag patternTag = tag.getCompoundTag("pattern");
if (patternTag == null) {
continue; // Nothing we can do about
continue;
}
final String assetId = patternTag.getString("asset_id");
final String translationKey = patternTag.getString("translation_key");
@ -673,15 +682,6 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
data.set(StructuredDataKey.BANNER_PATTERNS, patternLayer.toArray(new BannerPatternLayer[0]));
}
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;
}
private AdventureModePredicate updateBlockPredicates(final ListTag<StringTag> tag, final boolean showInTooltip) {
final BlockPredicate[] predicates = tag.stream()
.map(StringTag::getValue)

View File

@ -79,9 +79,13 @@ public final class StructuredDataConverter {
static final int HIDE_DYE_COLOR = 1 << 6;
static final int HIDE_ARMOR_TRIM = 1 << 7;
// Can't do nicely
// Store invalid/inconvertible data in the backup tag and later
// restore it when converting back to the original version
private static final String BACKUP_TAG_KEY = "VV|DataComponents";
private static final String ITEM_BACKUP_TAG_KEY = "VV|id";
// Store item ids separately to avoid copying whole data tree
// just for the item id
private static final String ITEM_BACKUP_TAG_KEY = "VV|Id";
private final Map<StructuredDataKey<?>, DataConverter<?>> rewriters = new Reference2ObjectOpenHashMap<>();
private final boolean backupInconvertibleData;
@ -301,8 +305,8 @@ public final class StructuredDataConverter {
for (final int id : data.itemIds()) {
final String name = toMappedItemName(id);
if (name.isEmpty()) {
// Backup whole data if one of the sherds is inconvertible
// Since we don't want to break the order of the sherds
// Backup whole data if one of the entries is inconvertible
// Since we don't want to break the order of the entries
if (backupInconvertibleData && originalSherds == null) {
originalSherds = new IntArrayTag(data.itemIds());
}
@ -410,8 +414,8 @@ public final class StructuredDataConverter {
register(StructuredDataKey.BANNER_PATTERNS, (data, tag) -> {
final ListTag<CompoundTag> originalPatterns = new ListTag<>(CompoundTag.class);
if (backupInconvertibleData) {
// Backup whole data if one of the patterns is inconvertible
// Since we don't want to break the order of the patterns
// Backup whole data if one of the entries is inconvertible
// Since we don't want to break the order of the entries
if (Arrays.stream(data).anyMatch(layer -> layer.pattern().isDirect())) {
for (final BannerPatternLayer layer : data) {
final CompoundTag layerTag = new CompoundTag();
@ -645,6 +649,7 @@ public final class StructuredDataConverter {
return backupTag;
}
// Check if item name could be mapped, if not, locate original/backup item id and use it
static int removeItemBackupTag(final CompoundTag tag, final int unmappedId) {
if (unmappedId != -1) {
return unmappedId;
@ -674,8 +679,10 @@ public final class StructuredDataConverter {
} else {
for (final int id : holders.ids()) {
final String name = toMappedItemName(id);
if (backupInconvertibleData && name.isEmpty()) {
// TODO HANDLE
if (name.isEmpty()) {
if (backupInconvertibleData) {
// TODO Backup
}
continue;
}
predicatedListTag.add(serializeBlockPredicate(predicate, name));