Skip to content

Commit

Permalink
implement anti footgun
Browse files Browse the repository at this point in the history
maybe solves #33
  • Loading branch information
sisby-folk committed Jan 29, 2024
1 parent 3c6e96c commit f732d2b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ItemStack[]> 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<Boolean> cir) {
if (isFootgun()) {
cir.setReturnValue(false);
cir.cancel();
}
}

@Inject(method = "getMatchingItemIds", at = @At("HEAD"), cancellable = true)
public void getMatchingItemIds(CallbackInfoReturnable<IntList> 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();
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/tinkerers_smithing.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
"AnvilScreenHandlerMixin",
"EnchantmentHelperMixin",
"IngredientMixin",
"ItemPredicateMixin",
"ItemStackMixin",
"PlayerManagerMixin",
Expand Down

0 comments on commit f732d2b

Please sign in to comment.