Skip to content

Commit

Permalink
Match empty tags with null tags (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
IzzelAliz committed Feb 3, 2024
1 parent 55932e4 commit 0e2ef46
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.izzel.arclight.common.bridge.core.entity.player.ServerPlayerEntityBridge;
import io.izzel.arclight.common.bridge.core.item.ItemStackBridge;
import io.izzel.arclight.i18n.ArclightConfig;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
Expand All @@ -21,12 +22,16 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import javax.annotation.Nullable;
import java.util.Objects;
import java.util.function.Consumer;

@Mixin(ItemStack.class)
Expand All @@ -39,6 +44,14 @@ public abstract class ItemStackMixin extends CapabilityProvider<ItemStack> imple
@Mutable @Shadow(remap = false) @Final private net.minecraft.core.Holder.Reference<Item> delegate;
// @formatter:on

@Shadow
public abstract boolean is(Item p_150931_);

@Shadow
public abstract Item getItem();

@Shadow @Nullable private CompoundTag tag;

protected ItemStackMixin(Class<ItemStack> baseClass) {
super(baseClass);
}
Expand Down Expand Up @@ -98,4 +111,43 @@ public void setItem(Item item) {
this.item = item;
this.delegate = ForgeRegistries.ITEMS.getDelegateOrThrow(item);
}

@Unique
private static boolean arclight$lenientItemMatch(Object a, Object b) {
if (ArclightConfig.spec().getCompat().isLenientItemTagMatch()) {
var tagA = (CompoundTag) a;
var tagB = (CompoundTag) b;
if (tagB != null) {
var tmp = tagA;
tagA = tagB;
tagB = tmp;
}
return tagA == null || (tagA.isEmpty() ? (tagB == null || tagB.isEmpty()) : tagA.equals(tagB));
} else {
return Objects.equals(a, b);
}
}

/**
* @author IzzelAliz
* @reason
*/
@Overwrite
public static boolean tagMatches(ItemStack itemStack, ItemStack itemStack2) {
return arclight$lenientItemMatch(itemStack.getTag(), itemStack2.getTag())
&& itemStack.areCapsCompatible(itemStack2);
}

/**
* @author IzzelAliz
* @reason
*/
@Overwrite
private boolean matches(ItemStack other) {
if (this.count != other.getCount()) {
return false;
} else if (!this.is(other.getItem())) {
return false;
} else return tagMatches((ItemStack) (Object) this, other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class CompatSpec {
@Setting("valid-username-regex")
private String validUsernameRegex;

@Setting("lenient-item-tag-match")
private boolean lenientItemTagMatch;

public Map<String, MaterialPropertySpec> getMaterials() {
return materials;
}
Expand Down Expand Up @@ -64,4 +67,8 @@ public boolean isForwardPermissionReverse() {
public String getValidUsernameRegex() {
return validUsernameRegex;
}

public boolean isLenientItemTagMatch() {
return lenientItemTagMatch;
}
}
1 change: 1 addition & 0 deletions i18n-config/src/main/resources/META-INF/arclight.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ compatibility {
]
forward-permission = true
valid-username-regex = ""
lenient-item-tag-match = true
}
async-catcher {
dump = true
Expand Down
3 changes: 3 additions & 0 deletions i18n-config/src/main/resources/META-INF/i18n/en_us.conf
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,8 @@ comments {
"Following allows any username to login:"
"valid-username-regex = \".+\""
]
lenient-item-tag-match.comment = [
"Allows items with an empty nbt tag stack on no tag items"
]
}
}
3 changes: 3 additions & 0 deletions i18n-config/src/main/resources/META-INF/i18n/zh_cn.conf
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,8 @@ comments {
"如果允许任何用户名可以使用"
"valid-username-regex = \".+\""
]
lenient-item-tag-match.comment = [
"允许空 NBT 标签的物品和没有 NBT 标签的物品堆叠"
]
}
}

0 comments on commit 0e2ef46

Please sign in to comment.