From 00088a90fdad57bb4df5c32e1979520ef4a7f378 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 14 Jun 2024 00:27:56 +0200 Subject: [PATCH] Keep data component types fully immutable --- .../viaversion/api/minecraft/HolderSet.java | 10 ++++++++++ .../viaversion/api/minecraft/HolderSetImpl.java | 15 +++++++++++++++ .../item/data/AdventureModePredicate.java | 5 +++-- .../api/minecraft/item/data/BlockPredicate.java | 7 ++----- .../api/minecraft/item/data/PotDecorations.java | 5 +++-- .../api/minecraft/item/data/ToolProperties.java | 5 +++-- .../api/minecraft/item/data/ToolRule.java | 10 +--------- 7 files changed, 37 insertions(+), 20 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSet.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSet.java index 1b72aef84..087cdf07b 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSet.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSet.java @@ -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); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSetImpl.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSetImpl.java index 6bdba6bdf..7d024799c 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSetImpl.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSetImpl.java @@ -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 implements HolderSet { @@ -53,4 +54,18 @@ final class HolderSetImpl extends EitherImpl 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); + } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AdventureModePredicate.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AdventureModePredicate.java index 06b0e0bd2..c6cff3cd6 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AdventureModePredicate.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AdventureModePredicate.java @@ -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); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java index 15c4ffc69..d19eb7726 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java @@ -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); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java index 5189db024..8b610e978 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotDecorations.java @@ -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); } } \ No newline at end of file diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ToolProperties.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ToolProperties.java index 22d01bdb9..ce7c1ab20 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ToolProperties.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ToolProperties.java @@ -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); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ToolRule.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ToolRule.java index 1a87d071f..9232808e9 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ToolRule.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ToolRule.java @@ -51,14 +51,6 @@ public record ToolRule(HolderSet blocks, @Nullable Float speed, @Nullable Boolea public static final Type 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; } }