Handle block ids in tool and adventure more predicate data

This commit is contained in:
Nassim Jahnke 2024-06-13 23:10:25 +02:00
parent e977e8db90
commit cc6f789334
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
9 changed files with 83 additions and 18 deletions

View File

@ -52,6 +52,8 @@ public interface MappingData {
*/
int getNewBlockId(int id);
int getOldBlockId(int id);
/**
* Returns the mapped item id, or -1 if unmapped.
*

View File

@ -39,13 +39,13 @@ public class MappingDataBase implements MappingData {
protected final String unmappedVersion;
protected final String mappedVersion;
protected BiMappings itemMappings;
protected FullMappings argumentTypeMappings;
protected FullMappings entityMappings;
protected FullMappings recipeSerializerMappings;
protected FullMappings itemDataSerializerMappings;
protected ParticleMappings particleMappings;
protected Mappings blockMappings;
protected BiMappings itemMappings;
protected BiMappings blockMappings;
protected Mappings blockStateMappings;
protected Mappings blockEntityMappings;
protected Mappings soundMappings;
@ -68,7 +68,7 @@ public class MappingDataBase implements MappingData {
}
final CompoundTag data = readMappingsFile("mappings-" + unmappedVersion + "to" + mappedVersion + ".nbt");
blockMappings = loadMappings(data, "blocks");
blockMappings = loadBiMappings(data, "blocks");
blockStateMappings = loadMappings(data, "blockstates");
blockEntityMappings = loadMappings(data, "blockentities");
soundMappings = loadMappings(data, "sounds");
@ -178,6 +178,11 @@ public class MappingDataBase implements MappingData {
return checkValidity(id, blockMappings.getNewId(id), "block");
}
@Override
public int getOldBlockId(final int id) {
return blockMappings.getNewIdOrDefault(id, 1);
}
@Override
public int getNewItemId(final int id) {
return checkValidity(id, itemMappings.getNewId(id), "item");

View File

@ -99,6 +99,24 @@ public final class StructuredDataContainer {
return empty;
}
/**
* Updates and returns the structured data by id if not empty.
*
* @param key serializer id
* @param mappingFunction function to update existing data
* @param <T> data type
* @return updated structured data if not empty
*/
public <T> @Nullable StructuredData<T> updateIfPresent(final StructuredDataKey<T> key, final Function<T, T> mappingFunction) {
final StructuredData<T> data = this.getNonEmpty(key);
if (data == null) {
return null;
}
data.setValue(mappingFunction.apply(data.value()));
return data;
}
public <T> void set(final StructuredDataKey<T> key, final T value) {
final int id = serializerId(key);
if (id != -1) {

View File

@ -24,6 +24,7 @@ package com.viaversion.viaversion.api.minecraft.item.data;
import com.viaversion.viaversion.api.type.Type;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
public record AdventureModePredicate(BlockPredicate[] predicates, boolean showInTooltip) {
@ -42,4 +43,10 @@ public record AdventureModePredicate(BlockPredicate[] predicates, boolean showIn
}
};
public AdventureModePredicate rewrite(final Int2IntFunction blockIdRewriter) {
for (int i = 0; i < predicates.length; i++) {
predicates[i] = predicates[i].rewrite(blockIdRewriter);
}
return this;
}
}

View File

@ -28,6 +28,7 @@ import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.ArrayType;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
import org.checkerframework.checker.nullness.qual.Nullable;
public record BlockPredicate(@Nullable HolderSet holderSet, StatePropertyMatcher @Nullable [] propertyMatchers,
@ -56,4 +57,15 @@ public record BlockPredicate(@Nullable HolderSet holderSet, StatePropertyMatcher
};
public static final Type<BlockPredicate[]> ARRAY_TYPE = new ArrayType<>(TYPE);
public BlockPredicate rewrite(final Int2IntFunction blockIdRewriter) {
if (holderSet == null || holderSet.hasTagKey()) {
return this;
}
final int[] ids = holderSet.ids();
for (int i = 0; i < ids.length; i++) {
ids[i] = blockIdRewriter.apply(ids[i]);
}
return this;
}
}

View File

@ -76,10 +76,9 @@ public final class PotDecorations {
}
public PotDecorations rewrite(final Int2IntFunction idRewriteFunction) {
final int[] newItems = new int[itemIds.length];
for (int i = 0; i < itemIds.length; i++) {
newItems[i] = idRewriteFunction.applyAsInt(itemIds[i]);
itemIds[i] = idRewriteFunction.apply(itemIds[i]);
}
return new PotDecorations(newItems);
return this;
}
}

View File

@ -25,6 +25,7 @@ package com.viaversion.viaversion.api.minecraft.item.data;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
public record ToolProperties(ToolRule[] rules, float defaultMiningSpeed, int damagePerBlock) {
@ -45,4 +46,10 @@ public record ToolProperties(ToolRule[] rules, float defaultMiningSpeed, int dam
}
};
public ToolProperties rewrite(final Int2IntFunction blockIdRewriter) {
for (int i = 0; i < rules.length; i++) {
rules[i] = rules[i].rewrite(blockIdRewriter);
}
return this;
}
}

View File

@ -27,6 +27,7 @@ import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.ArrayType;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
import org.checkerframework.checker.nullness.qual.Nullable;
public record ToolRule(HolderSet blocks, @Nullable Float speed, @Nullable Boolean correctForDrops) {
@ -49,4 +50,15 @@ public record ToolRule(HolderSet blocks, @Nullable Float speed, @Nullable Boolea
};
public static final Type<ToolRule[]> ARRAY_TYPE = new ArrayType<>(TYPE);
public ToolRule rewrite(final Int2IntFunction blockIdRewriter) {
if (blocks.hasTagKey()) {
return this;
}
final int[] ids = blocks.ids();
for (int i = 0; i < ids.length; i++) {
ids[i] = blockIdRewriter.apply(ids[i]);
}
return this;
}
}

View File

@ -26,8 +26,6 @@ import com.viaversion.viaversion.api.minecraft.data.StructuredData;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.item.data.ArmorTrim;
import com.viaversion.viaversion.api.minecraft.item.data.PotDecorations;
import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType;
@ -88,7 +86,7 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
}
}
updateItemComponents(connection, dataContainer, this::handleItemToClient, mappingData != null ? mappingData::getNewItemId : null);
updateItemComponents(connection, dataContainer, this::handleItemToClient, mappingData != null ? mappingData::getNewItemId : null, mappingData != null ? mappingData::getNewBlockId : null);
return item;
}
@ -113,20 +111,25 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
}
restoreTextComponents(item);
updateItemComponents(connection, dataContainer, this::handleItemToServer, mappingData != null ? mappingData::getOldItemId : null);
updateItemComponents(connection, dataContainer, this::handleItemToServer, mappingData != null ? mappingData::getOldItemId : null, mappingData != null ? mappingData::getOldBlockId : null);
return item;
}
// Temporary
protected void updateItemComponents(UserConnection connection, StructuredDataContainer container, ItemHandler itemHandler, @Nullable Int2IntFunction idRewriter) {
// Specific types that need deep handling
final StructuredData<ArmorTrim> trimData = container.getNonEmpty(StructuredDataKey.TRIM);
if (trimData != null && idRewriter != null) {
trimData.setValue(trimData.value().rewrite(idRewriter));
}
updateItemComponents(connection, container, itemHandler, idRewriter, null);
}
final StructuredData<PotDecorations> potDecorationsData = container.getNonEmpty(StructuredDataKey.POT_DECORATIONS);
if (potDecorationsData != null && idRewriter != null) {
potDecorationsData.setValue(potDecorationsData.value().rewrite(idRewriter));
protected void updateItemComponents(UserConnection connection, StructuredDataContainer container, ItemHandler itemHandler, @Nullable Int2IntFunction idRewriter, @Nullable Int2IntFunction blockIdRewriter) {
// Specific types that need deep handling
if (idRewriter != null) {
container.updateIfPresent(StructuredDataKey.TRIM, value -> value.rewrite(idRewriter));
container.updateIfPresent(StructuredDataKey.POT_DECORATIONS, value -> value.rewrite(idRewriter));
}
if (blockIdRewriter != null) {
container.updateIfPresent(StructuredDataKey.TOOL, value -> value.rewrite(blockIdRewriter));
container.updateIfPresent(StructuredDataKey.CAN_PLACE_ON, value -> value.rewrite(blockIdRewriter));
container.updateIfPresent(StructuredDataKey.CAN_BREAK, value -> value.rewrite(blockIdRewriter));
}
// Look for item types