Keep data component types fully immutable

This commit is contained in:
Nassim Jahnke 2024-06-14 00:27:56 +02:00
parent ddf6df8097
commit 00088a90fd
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
7 changed files with 37 additions and 20 deletions

View File

@ -22,6 +22,8 @@
*/
package com.viaversion.viaversion.api.minecraft;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
/**
* Set of ids that either holds a string tag key or an array of ids.
*/
@ -76,4 +78,12 @@ public interface HolderSet {
* @return true if this holder set has direct ids, false if it has a tag key
*/
boolean hasIds();
/**
* Returns a new holder set with the ids rewritten.
*
* @param idRewriter the id rewriter
* @return a new holder set with the ids rewritten, or self if it has a tag key
*/
HolderSet rewrite(Int2IntFunction idRewriter);
}

View File

@ -23,6 +23,7 @@
package com.viaversion.viaversion.api.minecraft;
import com.viaversion.viaversion.util.EitherImpl;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
final class HolderSetImpl extends EitherImpl<String, int[]> implements HolderSet {
@ -53,4 +54,18 @@ final class HolderSetImpl extends EitherImpl<String, int[]> implements HolderSet
public boolean hasIds() {
return isRight();
}
@Override
public HolderSet rewrite(final Int2IntFunction idRewriter) {
if (hasTagKey()) {
return this;
}
final int[] ids = ids();
final int[] mappedIds = new int[ids.length];
for (int i = 0; i < mappedIds.length; i++) {
mappedIds[i] = idRewriter.apply(ids[i]);
}
return new HolderSetImpl(mappedIds);
}
}

View File

@ -44,9 +44,10 @@ public record AdventureModePredicate(BlockPredicate[] predicates, boolean showIn
};
public AdventureModePredicate rewrite(final Int2IntFunction blockIdRewriter) {
final BlockPredicate[] predicates = new BlockPredicate[this.predicates.length];
for (int i = 0; i < predicates.length; i++) {
predicates[i] = predicates[i].rewrite(blockIdRewriter);
predicates[i] = this.predicates[i].rewrite(blockIdRewriter);
}
return this;
return new AdventureModePredicate(predicates, showInTooltip);
}
}

View File

@ -62,10 +62,7 @@ public record BlockPredicate(@Nullable HolderSet holderSet, StatePropertyMatcher
return this;
}
final int[] ids = holderSet.ids();
for (int i = 0; i < ids.length; i++) {
ids[i] = blockIdRewriter.apply(ids[i]);
}
return this;
final HolderSet updatedHolders = holderSet.rewrite(blockIdRewriter);
return new BlockPredicate(updatedHolders, propertyMatchers, tag);
}
}

View File

@ -76,9 +76,10 @@ 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++) {
itemIds[i] = idRewriteFunction.apply(itemIds[i]);
newItems[i] = idRewriteFunction.applyAsInt(itemIds[i]);
}
return this;
return new PotDecorations(newItems);
}
}

View File

@ -47,9 +47,10 @@ public record ToolProperties(ToolRule[] rules, float defaultMiningSpeed, int dam
};
public ToolProperties rewrite(final Int2IntFunction blockIdRewriter) {
final ToolRule[] rules = new ToolRule[this.rules.length];
for (int i = 0; i < rules.length; i++) {
rules[i] = rules[i].rewrite(blockIdRewriter);
rules[i] = this.rules[i].rewrite(blockIdRewriter);
}
return this;
return new ToolProperties(rules, defaultMiningSpeed, damagePerBlock);
}
}

View File

@ -51,14 +51,6 @@ 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;
return blocks.hasIds() ? new ToolRule(blocks.rewrite(blockIdRewriter), speed, correctForDrops) : this;
}
}