From f732d2b7eb6fb3590ed5c277b4962b688626ca04 Mon Sep 17 00:00:00 2001 From: sisby-folk Date: Mon, 29 Jan 2024 12:55:11 +1100 Subject: [PATCH] implement anti footgun maybe solves #33 --- gradle.properties | 2 +- .../TinkerersSmithingLoader.java | 4 +- .../mixin/IngredientMixin.java | 58 +++++++++++++++++++ .../resources/tinkerers_smithing.mixins.json | 1 + 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/main/java/folk/sisby/tinkerers_smithing/mixin/IngredientMixin.java diff --git a/gradle.properties b/gradle.properties index 7c5e82d..1d79134 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,6 @@ org.gradle.configureondemand=true # Enable advanced multi-module optimizations (share tiny-remaper instance between projects) fabric.loom.multiProjectOptimisation=true # Mod Properties -baseVersion = 2.6.2 +baseVersion = 2.6.3 defaultBranch = 1.19 branch = 1.19 diff --git a/src/main/java/folk/sisby/tinkerers_smithing/TinkerersSmithingLoader.java b/src/main/java/folk/sisby/tinkerers_smithing/TinkerersSmithingLoader.java index 46d0ce7..b6dbd89 100644 --- a/src/main/java/folk/sisby/tinkerers_smithing/TinkerersSmithingLoader.java +++ b/src/main/java/folk/sisby/tinkerers_smithing/TinkerersSmithingLoader.java @@ -328,9 +328,9 @@ public static Identifier appendId(Identifier id, String name) { public static Identifier repairRecipeId(Item baseItem, Ingredient ingredient) { if (ingredient.entries.length == 0) { - TinkerersSmithing.LOGGER.error("Ingredients for Tinkerer's Smithing recipes can't be empty! When repairing item {}", Registry.ITEM.getId(baseItem), new IllegalArgumentException("Ingredient entries are empty!")); + throw new IllegalArgumentException("Ingredients for Tinkerer's Smithing recipes can't be empty! When repairing item %s".formatted(Registry.ITEM.getId(baseItem))); } - Identifier ingredientId = ingredient.entries.length != 0 && ingredient.entries[0] instanceof Ingredient.StackEntry se ? Registry.ITEM.getId(se.stack.getItem()) : (ingredient.entries.length != 0 && ingredient.entries[0] instanceof Ingredient.TagEntry te ? te.tag.id() : new Identifier("ERROR")); + Identifier ingredientId = ingredient.entries[0] instanceof Ingredient.StackEntry se ? Registry.ITEM.getId(se.stack.getItem()) : ingredient.entries[0] instanceof Ingredient.TagEntry te ? te.tag.id() : new Identifier("ERROR"); return recipeId("repair", Registry.ITEM.getId(baseItem), ingredientId); } } diff --git a/src/main/java/folk/sisby/tinkerers_smithing/mixin/IngredientMixin.java b/src/main/java/folk/sisby/tinkerers_smithing/mixin/IngredientMixin.java new file mode 100644 index 0000000..7957ae0 --- /dev/null +++ b/src/main/java/folk/sisby/tinkerers_smithing/mixin/IngredientMixin.java @@ -0,0 +1,58 @@ +package folk.sisby.tinkerers_smithing.mixin; + +import folk.sisby.tinkerers_smithing.TinkerersSmithing; +import it.unimi.dsi.fastutil.ints.IntArrayList; +import it.unimi.dsi.fastutil.ints.IntList; +import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.recipe.Ingredient; +import net.minecraft.util.registry.Registry; +import org.spongepowered.asm.mixin.Mixin; +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.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(Ingredient.class) +public class IngredientMixin { + @Unique private boolean isFootgun() { + if (Registry.ITEM.streamTagsAndEntries().toList().isEmpty()) { + TinkerersSmithing.LOGGER.error("[Tinkerer's Smithing] Cowardly refusing to access ingredient while tags are unloaded", new IllegalStateException("An ingredient was accessed before item tags are loaded - This would normally break all tag recipes! Please report this to the mod in the trace below.")); + return true; + } + return false; + } + + @Inject(method = "getMatchingStacks", at = @At("HEAD"), cancellable = true) + public void getMatchingStacks(CallbackInfoReturnable cir) { + if (isFootgun()) { + cir.setReturnValue(new ItemStack[]{}); + cir.cancel(); + } + } + + @Inject(method = "test(Lnet/minecraft/item/ItemStack;)Z", at = @At("HEAD"), cancellable = true) + public void test(CallbackInfoReturnable cir) { + if (isFootgun()) { + cir.setReturnValue(false); + cir.cancel(); + } + } + + @Inject(method = "getMatchingItemIds", at = @At("HEAD"), cancellable = true) + public void getMatchingItemIds(CallbackInfoReturnable cir) { + if (isFootgun()) { + cir.setReturnValue(new IntArrayList()); + cir.cancel(); + } + } + + @Inject(method = "write", at = @At("HEAD"), cancellable = true) + public void write(PacketByteBuf buf, CallbackInfo ci) { + if (isFootgun()) { + buf.writeVarInt(0); + ci.cancel(); + } + } +} diff --git a/src/main/resources/tinkerers_smithing.mixins.json b/src/main/resources/tinkerers_smithing.mixins.json index cc4a06a..9405b9d 100644 --- a/src/main/resources/tinkerers_smithing.mixins.json +++ b/src/main/resources/tinkerers_smithing.mixins.json @@ -6,6 +6,7 @@ "mixins": [ "AnvilScreenHandlerMixin", "EnchantmentHelperMixin", + "IngredientMixin", "ItemPredicateMixin", "ItemStackMixin", "PlayerManagerMixin",