Skip to content

Commit d773454

Browse files
committed
Implements #65
1 parent d9e4e22 commit d773454

File tree

2 files changed

+75
-37
lines changed

2 files changed

+75
-37
lines changed

src/main/java/org/samo_lego/golfiv/mixin/illegal_items/ItemStackMixinCast_ItemStackChecker.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import net.minecraft.item.ItemStack;
88
import net.minecraft.item.Items;
99
import net.minecraft.item.SpawnEggItem;
10+
import net.minecraft.nbt.NbtCompound;
1011
import net.minecraft.nbt.NbtList;
1112
import net.minecraft.potion.PotionUtil;
12-
import net.minecraft.registry.Registries;
13-
import net.minecraft.util.Identifier;
13+
import org.jetbrains.annotations.Nullable;
1414
import org.samo_lego.golfiv.casts.ItemStackChecker;
1515
import org.spongepowered.asm.mixin.Mixin;
1616
import org.spongepowered.asm.mixin.Shadow;
@@ -45,6 +45,8 @@ public abstract class ItemStackMixinCast_ItemStackChecker implements ItemStackCh
4545
@Shadow
4646
public abstract Item getItem();
4747

48+
@Shadow
49+
private @Nullable NbtCompound nbt;
4850
private final ItemStack itemStack = (ItemStack) (Object) this;
4951

5052
/**
@@ -58,55 +60,49 @@ public void makeLegal(boolean survival) {
5860
Map<Enchantment, Integer> enchantments = EnchantmentHelper.fromNbt(this.getEnchantments());
5961

6062
// Checks item enchantments
61-
if((survival && golfConfig.items.survival.checkEnchants) || (!survival && golfConfig.items.creative.checkEnchants))
62-
for(Map.Entry<Enchantment, Integer> ench : enchantments.entrySet()) {
63+
if ((survival && golfConfig.items.survival.checkEnchants) || (!survival && golfConfig.items.creative.checkEnchants))
64+
for (Map.Entry<Enchantment, Integer> ench : enchantments.entrySet()) {
6365
Enchantment enchantment = ench.getKey();
6466
int level = ench.getValue();
6567

6668
Set<Enchantment> otherEnchants = EnchantmentHelper.get(this.itemStack).keySet();
6769
otherEnchants.remove(enchantment);
6870

69-
if(!enchantment.isAcceptableItem(this.itemStack) || !EnchantmentHelper.isCompatible(otherEnchants, enchantment) || level > enchantment.getMaxLevel()) {
71+
if (!enchantment.isAcceptableItem(this.itemStack) ||
72+
!EnchantmentHelper.isCompatible(otherEnchants, enchantment) ||
73+
level > enchantment.getMaxLevel()) {
7074
this.removeSubNbt("Enchantments");
7175
break;
7276
}
7377
}
7478

7579
// Checks potion
76-
if(
77-
(
78-
(survival && golfConfig.items.survival.checkPotionLevels) ||
79-
(!survival && golfConfig.items.creative.checkPotionLevels)
80-
) &&
81-
(
82-
this.itemStack.getItem() == Items.POTION ||
83-
this.itemStack.getItem() == Items.SPLASH_POTION ||
84-
this.itemStack.getItem() == Items.LINGERING_POTION
85-
)
86-
) {
80+
if (((survival && golfConfig.items.survival.checkPotionLevels) || (!survival && golfConfig.items.creative.checkPotionLevels)) &&
81+
(this.itemStack.getItem() == Items.POTION ||
82+
this.itemStack.getItem() == Items.SPLASH_POTION ||
83+
this.itemStack.getItem() == Items.LINGERING_POTION)) {
8784
List<StatusEffectInstance> effects = PotionUtil.getCustomPotionEffects(this.itemStack);
88-
for(StatusEffectInstance effect : effects) {
89-
if(effect.getAmplifier() > 1) {
85+
86+
for (StatusEffectInstance effect : effects) {
87+
if (effect.getAmplifier() > 1) {
9088
this.removeSubNbt("CustomPotionEffects");
9189
this.removeSubNbt("Potion");
9290
break;
9391
}
9492
}
9593
}
9694

97-
Identifier id = Registries.ITEM.getId(this.itemStack.getItem());
98-
if(survival && (golfConfig.items.survival.bannedItems.contains(id.toString()) || (golfConfig.items.survival.bannedItems.contains("minecraft:spawn_egg") && this.itemStack.getItem() instanceof SpawnEggItem))) {
95+
String nbt = golfConfig.items.survival.bannedItems.get(this.itemStack.getItem());
96+
NbtCompound tag = this.itemStack.getNbt();
97+
98+
if (survival && nbt != null && (nbt.isEmpty() || tag != null && tag.toString().equals(nbt)) ||
99+
(golfConfig.items.survival.banSpawnEggs && this.itemStack.getItem() instanceof SpawnEggItem)) {
99100
this.setCount(0);
100-
}
101+
} else if (((survival && golfConfig.items.survival.checkItemCount) ||
102+
(!survival && golfConfig.items.creative.checkItemCount)) &&
103+
this.count > this.getMaxCount()) {
101104

102-
if(
103-
(
104-
(survival && golfConfig.items.survival.checkItemCount) ||
105-
(!survival && golfConfig.items.creative.checkItemCount)
106-
) &&
107-
this.count > this.getMaxCount()
108-
) {
109-
this.count = 1;
105+
this.count = this.getMaxCount();
110106
}
111107
}
112108
}

src/main/java/org/samo_lego/golfiv/storage/GolfConfig.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import net.minecraft.block.Block;
1313
import net.minecraft.block.Blocks;
1414
import net.minecraft.entity.EntityType;
15+
import net.minecraft.item.ItemConvertible;
16+
import net.minecraft.nbt.NbtCompound;
1517
import net.minecraft.registry.Registries;
1618
import net.minecraft.util.Identifier;
1719
import org.samo_lego.golfiv.utils.GolfLogger;
@@ -87,14 +89,22 @@ public static class Survival {
8789

8890
@SerializedName("// Which items should be cleared when clicked in survival inventory")
8991
public final String _comment_bannedSurvivalItems = "";
90-
public ArrayList<String> bannedItems = new ArrayList<>(Arrays.asList(
91-
"minecraft:barrier",
92-
"minecraft:spawner",
93-
"minecraft:structure_void",
94-
"minecraft:bedrock",
95-
"minecraft:command_block",
96-
"minecraft:spawn_egg"
97-
));
92+
@JsonAdapter(BlockMapAdapter.class)
93+
public Map<ItemConvertible, String> bannedItems = Map.ofEntries(
94+
Map.entry(Blocks.BEDROCK, ""),
95+
Map.entry(Blocks.BARRIER, ""),
96+
Map.entry(Blocks.COMMAND_BLOCK, ""),
97+
Map.entry(Blocks.CHAIN_COMMAND_BLOCK, ""),
98+
Map.entry(Blocks.REPEATING_COMMAND_BLOCK, ""),
99+
Map.entry(Blocks.STRUCTURE_BLOCK, ""),
100+
Map.entry(Blocks.STRUCTURE_VOID, ""),
101+
Map.entry(Blocks.JIGSAW, ""),
102+
Map.entry(Blocks.SPAWNER, ""),
103+
Map.entry(Blocks.NETHER_PORTAL, ""),
104+
Map.entry(Blocks.END_PORTAL, ""),
105+
Map.entry(Blocks.END_PORTAL_FRAME, ""),
106+
Map.entry(Blocks.END_GATEWAY, ""));
107+
public boolean banSpawnEggs = true;
98108
public boolean checkEnchants = true;
99109
public boolean checkPotionLevels = true;
100110
public boolean checkItemCount = true;
@@ -341,6 +351,38 @@ public Set<Block> read(JsonReader in) throws IOException {
341351
}
342352
}
343353

354+
355+
/**
356+
* Adapts {@link ItemConvertible} and string {@link NbtCompound} between it and the identifier.
357+
*/
358+
private static final class BlockMapAdapter extends TypeAdapter<Map<ItemConvertible, String>> {
359+
360+
@Override
361+
public void write(JsonWriter out, Map<ItemConvertible, String> value) throws IOException {
362+
out.beginObject();
363+
var reg = Registries.ITEM;
364+
for (var entry : value.entrySet()) {
365+
out.name(reg.getId(entry.getKey().asItem()).toString());
366+
out.value(entry.getValue());
367+
}
368+
out.endObject();
369+
370+
}
371+
372+
@Override
373+
public Map<ItemConvertible, String> read(JsonReader in) throws IOException {
374+
in.beginObject();
375+
var reg = Registries.ITEM;
376+
var map = new HashMap<ItemConvertible, String>();
377+
while (in.hasNext()) {
378+
var key = reg.get(Identifier.tryParse(in.nextName()));
379+
map.put(key, in.nextString());
380+
}
381+
in.endObject();
382+
return map;
383+
}
384+
}
385+
344386
/**
345387
* Adapts {@link EntityType} between it and the identifier.
346388
* <p>

0 commit comments

Comments
 (0)