Skip to content

Commit

Permalink
Validate items on init (should handle things such as containers, etc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
MerchantPug committed Jun 27, 2024
1 parent c867d17 commit 4baa2fb
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 40 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## Changes
- Any Enchanted Books that have no enchantments as a result of this mod will now turn into regular Books.

## Bugfixes
- [FABRIC] Fixed crash involving platform helper being loaded at the wrong time.
- Fixed setting enchantments operating on an immutable map.
- Caught more edge cases where a disabled enchantment can slip through when playing with the mod serverside.
- Caught more edge cases where a disabled enchantment can be obtained from containers when playing with the mod serverside.

## Internal
- Moved EnchantmentDisableTag#DISABLED_ENCHANTMENT_TAG field to EnchantmentDisabledTags#DISABLED.
- Moved EnchantmentDisableTag#DISABLED_ENCHANTMENT_TAG field to EnchantmentDisabledTags#DISABLED.
- This was unfortunately a required change to fix Fabric platform helper.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.EnchantedBookItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantment;
Expand All @@ -29,21 +28,32 @@ public static EnchantmentDisableTagPlatformHelper getHelper() {
public static ItemStack removeDisabledEnchantments(ItemStack stack) {
if (stack.getTag() == null)
return stack;
if (stack.getTag().contains("Enchantments", Tag.TAG_LIST)) {
removeDisabledEnchantments(stack.getTag().getList("Enchantments", Tag.TAG_COMPOUND));
if (stack.getTag().getList("Enchantments", Tag.TAG_COMPOUND).isEmpty())
stack.removeTagKey("Enchantments");
if (removeDisabledEnchantments(stack.getTag()) && stack.is(Items.ENCHANTED_BOOK)) {
ItemStack book = new ItemStack(Items.BOOK);
book.setTag(stack.getTag());
return book;
}
if (stack.getItem() instanceof EnchantedBookItem && stack.getTag().contains("StoredEnchantments", Tag.TAG_LIST)) {
removeDisabledEnchantments(stack.getTag().getList("StoredEnchantments", Tag.TAG_COMPOUND));
if (stack.getTag().getList("StoredEnchantments", Tag.TAG_COMPOUND).isEmpty())
stack.removeTagKey("StoredEnchantments");
}
if (stack.is(Items.ENCHANTED_BOOK) && EnchantedBookItem.getEnchantments(stack).isEmpty())
return new ItemStack(Items.BOOK);
return stack;
}

public static boolean removeDisabledEnchantments(CompoundTag tag) {
if (tag.contains("Enchantments", Tag.TAG_LIST)) {
removeDisabledEnchantments(tag.getList("Enchantments", Tag.TAG_COMPOUND));
if (tag.getList("Enchantments", Tag.TAG_COMPOUND).isEmpty()) {
tag.remove("Enchantments");
return true;
}
}
if (tag.contains("StoredEnchantments", Tag.TAG_LIST)) {
removeDisabledEnchantments(tag.getList("StoredEnchantments", Tag.TAG_COMPOUND));
if (tag.getList("StoredEnchantments", Tag.TAG_COMPOUND).isEmpty()) {
tag.remove("StoredEnchantments");
return true;
}
}
return false;
}

public static void removeDisabledEnchantments(ListTag list) {
if (list == null)
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.greenhouseteam.enchantmentdisabletag.mixin.fabric;

import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemStack.class)
public class ItemStackMixin {
@Shadow
@Nullable
private CompoundTag tag;

@Mutable @Shadow @Final @Deprecated @Nullable
private Item item;

@Inject(method = "<init>(Lnet/minecraft/nbt/CompoundTag;)V", at = @At(value = "TAIL"))
private void enchantmentdisabletag$verifyEnchantmentTagsFromInit(CompoundTag compoundTag, CallbackInfo ci) {
if (this.tag != null && EnchantmentDisableTag.removeDisabledEnchantments(this.tag) && item == Items.ENCHANTED_BOOK) {
this.item = Items.BOOK;
}
}
}
29 changes: 15 additions & 14 deletions fabric/src/main/resources/enchantmentdisabletag.fabric.mixins.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"required": true,
"minVersion": "0.8",
"package": "dev.greenhouseteam.enchantmentdisabletag.mixin.fabric",
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_17",
"client": [
"client.ClientPacketListenerMixin"
],
"injectors": {
"defaultRequire": 1
},
"mixins": [
"ReloadableServerResourcesMixin"
]
"required": true,
"minVersion": "0.8",
"package": "dev.greenhouseteam.enchantmentdisabletag.mixin.fabric",
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_17",
"client": [
"client.ClientPacketListenerMixin"
],
"injectors": {
"defaultRequire": 1
},
"mixins": [
"ItemStackMixin",
"ReloadableServerResourcesMixin"
]
}
2 changes: 2 additions & 0 deletions forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ minecraft {
mods {
create(mod_id) {
source(sourceSets["main"])
source(sourceSets["test"])
}
}
}
Expand All @@ -58,6 +59,7 @@ minecraft {
mods {
create(mod_id) {
source(sourceSets["main"])
source(sourceSets["test"])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.greenhouseteam.enchantmentdisabletag.mixin.forge;

import dev.greenhouseteam.enchantmentdisabletag.EnchantmentDisableTag;
import net.minecraft.core.Holder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.registries.ForgeRegistries;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import javax.annotation.Nullable;

@Mixin(ItemStack.class)
public class ItemStackMixin {
@Shadow
@Nullable
private CompoundTag tag;

@Mutable
@Shadow @Final @Deprecated @Nullable
private Item item;

@Mutable
@Shadow(remap = false) @Final @org.jetbrains.annotations.Nullable
private Holder.@org.jetbrains.annotations.Nullable Reference<Item> delegate;

@Inject(method = "forgeInit", at = @At(value = "HEAD"), remap = false)
private void enchantmentdisabletag$verifyEnchantmentTagsFromInit(CallbackInfo ci) {
if (this.tag != null && EnchantmentDisableTag.removeDisabledEnchantments(this.tag) && item == Items.ENCHANTED_BOOK) {
this.item = Items.BOOK;
this.delegate = ForgeRegistries.ITEMS.getDelegateOrThrow(Items.BOOK);
}
}
}
23 changes: 12 additions & 11 deletions forge/src/main/resources/enchantmentdisabletag.forge.mixins.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"required": true,
"minVersion": "0.8",
"package": "dev.greenhouseteam.enchantmentdisabletag.mixin",
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_17",
"mixins": [
"forge.CreativeModeTabMixin",
"forge.ForgeRegistryMixin"
],
"injectors": {
"defaultRequire": 1
"required": true,
"minVersion": "0.8",
"package": "dev.greenhouseteam.enchantmentdisabletag.mixin",
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_17",
"mixins": [
"forge.CreativeModeTabMixin",
"forge.ForgeRegistryMixin",
"forge.ItemStackMixin"
],
"injectors": {
"defaultRequire": 1
}
}

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project
mod_version=1.0.3
mod_version=1.1.0
group=dev.greenhouseteam

# Common
Expand Down

0 comments on commit 4baa2fb

Please sign in to comment.