Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-26 16:12:42 +01:00
Keep data component types fully immutable
Dieser Commit ist enthalten in:
Ursprung
ddf6df8097
Commit
00088a90fd
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren