3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-20 06:50:08 +01:00

Keep data component types fully immutable

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-06-14 00:27:56 +02:00
Ursprung ddf6df8097
Commit 00088a90fd
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
7 geänderte Dateien mit 37 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -22,6 +22,8 @@
*/ */
package com.viaversion.viaversion.api.minecraft; 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. * 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 * @return true if this holder set has direct ids, false if it has a tag key
*/ */
boolean hasIds(); 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);
} }

Datei anzeigen

@ -23,6 +23,7 @@
package com.viaversion.viaversion.api.minecraft; package com.viaversion.viaversion.api.minecraft;
import com.viaversion.viaversion.util.EitherImpl; import com.viaversion.viaversion.util.EitherImpl;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
final class HolderSetImpl extends EitherImpl<String, int[]> implements HolderSet { 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() { public boolean hasIds() {
return isRight(); 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);
}
} }

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

@ -76,9 +76,10 @@ public final class PotDecorations {
} }
public PotDecorations rewrite(final Int2IntFunction idRewriteFunction) { public PotDecorations rewrite(final Int2IntFunction idRewriteFunction) {
final int[] newItems = new int[itemIds.length];
for (int i = 0; i < itemIds.length; i++) { 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);
} }
} }

Datei anzeigen

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

Datei anzeigen

@ -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 static final Type<ToolRule[]> ARRAY_TYPE = new ArrayType<>(TYPE);
public ToolRule rewrite(final Int2IntFunction blockIdRewriter) { public ToolRule rewrite(final Int2IntFunction blockIdRewriter) {
if (blocks.hasTagKey()) { return blocks.hasIds() ? new ToolRule(blocks.rewrite(blockIdRewriter), speed, correctForDrops) : this;
return this;
}
final int[] ids = blocks.ids();
for (int i = 0; i < ids.length; i++) {
ids[i] = blockIdRewriter.apply(ids[i]);
}
return this;
} }
} }