From 2764367e21b78bd70493659a014d9c1fc3078b78 Mon Sep 17 00:00:00 2001 From: andi-makes Date: Sat, 22 Jun 2024 11:27:24 +0200 Subject: [PATCH] Add Potion of Small recipe, fix custom containers --- src/main/java/dev/schmarrn/BeegSmol.java | 3 ++ src/main/java/dev/schmarrn/MyMobEffects.java | 6 +++- .../brewing/CustomBrewingRecipes.java | 28 ++++++++++++++++++- .../dev/schmarrn/brewing/IBrewingRecipe.java | 2 +- src/main/java/dev/schmarrn/items/MyItems.java | 2 +- .../mixin/BrewingStandBlockEntityMixin.java | 26 +++++++++++++++++ .../schmarrn/mixin/BrewingStandMenuMixin.java | 18 ++++++++++++ .../schmarrn/mixin/PotionBrewingMixin.java | 14 ++++++---- src/main/resources/beeg-smol.mixins.json | 18 ++++++------ 9 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 src/main/java/dev/schmarrn/mixin/BrewingStandBlockEntityMixin.java create mode 100644 src/main/java/dev/schmarrn/mixin/BrewingStandMenuMixin.java diff --git a/src/main/java/dev/schmarrn/BeegSmol.java b/src/main/java/dev/schmarrn/BeegSmol.java index f0286ea..f14158e 100644 --- a/src/main/java/dev/schmarrn/BeegSmol.java +++ b/src/main/java/dev/schmarrn/BeegSmol.java @@ -1,5 +1,6 @@ package dev.schmarrn; +import dev.schmarrn.brewing.CustomBrewingRecipes; import dev.schmarrn.components.MyComponents; import dev.schmarrn.items.MyItems; import net.fabricmc.api.ModInitializer; @@ -21,6 +22,8 @@ public void onInitialize() { MyMobEffects.init(); MyComponents.init(); + CustomBrewingRecipes.init(); + MyItems.init(); } } \ No newline at end of file diff --git a/src/main/java/dev/schmarrn/MyMobEffects.java b/src/main/java/dev/schmarrn/MyMobEffects.java index 26723ae..1152656 100644 --- a/src/main/java/dev/schmarrn/MyMobEffects.java +++ b/src/main/java/dev/schmarrn/MyMobEffects.java @@ -13,7 +13,11 @@ public class MyMobEffects { public static final Holder BEEG = Registry.registerForHolder(BuiltInRegistries.MOB_EFFECT, BeegSmol.rl("beeg"), new MyMobEffect(MobEffectCategory.NEUTRAL, 0xFF00FF).addAttributeModifier(Attributes.SCALE, BeegSmol.rl("effect.beeg"), 0.5, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL)); public static final Holder SMOL = Registry.registerForHolder(BuiltInRegistries.MOB_EFFECT, BeegSmol.rl("smol"), new MyMobEffect(MobEffectCategory.NEUTRAL, 0xFF00FF).addAttributeModifier(Attributes.SCALE, BeegSmol.rl("effect.smol"), -0.5, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL)); - public static MobEffectInstance getMobEffectInstance(Holder effect, int amplifier) { + public static final MobEffectInstance BIG_INSTANCE = getMobEffectInstance(BEEG, 0); + public static final MobEffectInstance SMALL_INSTANCE = getMobEffectInstance(SMOL, 0); + public static final MobEffectInstance SMOL_INSTANCE = getMobEffectInstance(SMOL, 1); + + private static MobEffectInstance getMobEffectInstance(Holder effect, int amplifier) { int duration = -1; boolean ambient = false; boolean visible = false; diff --git a/src/main/java/dev/schmarrn/brewing/CustomBrewingRecipes.java b/src/main/java/dev/schmarrn/brewing/CustomBrewingRecipes.java index 8b4e2ef..2c11622 100644 --- a/src/main/java/dev/schmarrn/brewing/CustomBrewingRecipes.java +++ b/src/main/java/dev/schmarrn/brewing/CustomBrewingRecipes.java @@ -1,6 +1,8 @@ package dev.schmarrn.brewing; import dev.schmarrn.MyMobEffects; +import dev.schmarrn.components.MobEffectInstancesComponent; +import dev.schmarrn.components.MyComponents; import dev.schmarrn.items.Vial; import net.minecraft.core.Holder; import net.minecraft.core.component.DataComponents; @@ -18,6 +20,7 @@ public class CustomBrewingRecipes { public static final List RECIPES = new ArrayList<>(); static { + // Potion of Beeg register(new IBrewingRecipe() { @Override public boolean isBase(ItemStack base) { @@ -32,7 +35,26 @@ public boolean isIngredient(ItemStack itemStack) { @Override public ItemStack getOutput(ItemStack base, ItemStack ingredient) { - return Vial.getWithEffect(MyMobEffects.getMobEffectInstance(MyMobEffects.BEEG, 0)); + return Vial.getWithEffect(MyMobEffects.BIG_INSTANCE); + } + }); + + // Potion of small + register(new IBrewingRecipe() { + @Override + public boolean isBase(ItemStack base) { + var mobEffects = base.getOrDefault(MyComponents.MOB_EFFECTS, MobEffectInstancesComponent.EMPTY).effectInstances(); + return mobEffects.size() == 1 && mobEffects.getFirst().equals(MyMobEffects.BIG_INSTANCE); + } + + @Override + public boolean isIngredient(ItemStack itemStack) { + return itemStack.is(Items.FERMENTED_SPIDER_EYE); + } + + @Override + public ItemStack getOutput(ItemStack base, ItemStack ingredient) { + return Vial.getWithEffect(MyMobEffects.SMALL_INSTANCE); } }); } @@ -59,4 +81,8 @@ public static boolean isBasis(ItemStack stack) { private static void register(IBrewingRecipe recipe) { RECIPES.add(recipe); } + + public static void init() { + // no-op + } } diff --git a/src/main/java/dev/schmarrn/brewing/IBrewingRecipe.java b/src/main/java/dev/schmarrn/brewing/IBrewingRecipe.java index 67402ed..0ac4ee6 100644 --- a/src/main/java/dev/schmarrn/brewing/IBrewingRecipe.java +++ b/src/main/java/dev/schmarrn/brewing/IBrewingRecipe.java @@ -6,7 +6,7 @@ * Inspired by neoforge: https://github.com/neoforged/NeoForge/blob/f923568cc0595b90034739ecc96af367ecacbdd5/src/main/java/net/neoforged/neoforge/common/brewing/IBrewingRecipe.java */ public interface IBrewingRecipe { - boolean isBase(ItemStack itemStack); + boolean isBase(ItemStack base); boolean isIngredient(ItemStack itemStack); ItemStack getOutput(ItemStack base, ItemStack ingredient); } diff --git a/src/main/java/dev/schmarrn/items/MyItems.java b/src/main/java/dev/schmarrn/items/MyItems.java index 2f67369..221a244 100644 --- a/src/main/java/dev/schmarrn/items/MyItems.java +++ b/src/main/java/dev/schmarrn/items/MyItems.java @@ -6,7 +6,7 @@ import net.minecraft.world.item.Item; public class MyItems { - public static final Item POTION = Registry.register(BuiltInRegistries.ITEM, BeegSmol.rl("vial"), new Vial(new Item.Properties())); + public static final Item POTION = Registry.register(BuiltInRegistries.ITEM, BeegSmol.rl("vial"), new Vial(new Item.Properties().stacksTo(1))); public static void init() { // no-op diff --git a/src/main/java/dev/schmarrn/mixin/BrewingStandBlockEntityMixin.java b/src/main/java/dev/schmarrn/mixin/BrewingStandBlockEntityMixin.java new file mode 100644 index 0000000..9cdb0cd --- /dev/null +++ b/src/main/java/dev/schmarrn/mixin/BrewingStandBlockEntityMixin.java @@ -0,0 +1,26 @@ +package dev.schmarrn.mixin; + +import dev.schmarrn.BeegSmol; +import dev.schmarrn.brewing.CustomBrewingRecipes; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.entity.BrewingStandBlockEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(BrewingStandBlockEntity.class) +public class BrewingStandBlockEntityMixin { + @Inject( + method = "canPlaceItem", + at = @At(value="HEAD"), + cancellable = true + ) + private void bs$canPlaceItem(int i, ItemStack stack, CallbackInfoReturnable cir) { + BeegSmol.LOGGER.info("Is basis? {}, {}", stack, CustomBrewingRecipes.isBasis(stack)); + + if (i != 3 && i != 4 && CustomBrewingRecipes.isBasis(stack)) { + cir.setReturnValue(true); + } + } +} diff --git a/src/main/java/dev/schmarrn/mixin/BrewingStandMenuMixin.java b/src/main/java/dev/schmarrn/mixin/BrewingStandMenuMixin.java new file mode 100644 index 0000000..8689df4 --- /dev/null +++ b/src/main/java/dev/schmarrn/mixin/BrewingStandMenuMixin.java @@ -0,0 +1,18 @@ +package dev.schmarrn.mixin; + +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import dev.schmarrn.brewing.CustomBrewingRecipes; +import net.minecraft.world.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +@Mixin(targets = "net.minecraft.world.inventory.BrewingStandMenu$PotionSlot") +public class BrewingStandMenuMixin { + @ModifyReturnValue( + method = "mayPlaceItem", + at = @At("RETURN") + ) + private static boolean bs$mayPlaceItem(boolean original, ItemStack stack) { + return original || CustomBrewingRecipes.isBasis(stack); + } +} diff --git a/src/main/java/dev/schmarrn/mixin/PotionBrewingMixin.java b/src/main/java/dev/schmarrn/mixin/PotionBrewingMixin.java index 7c9fe54..d2a2308 100644 --- a/src/main/java/dev/schmarrn/mixin/PotionBrewingMixin.java +++ b/src/main/java/dev/schmarrn/mixin/PotionBrewingMixin.java @@ -2,14 +2,8 @@ import dev.schmarrn.brewing.IBrewingRecipe; import dev.schmarrn.brewing.CustomBrewingRecipes; -import net.minecraft.core.Holder; -import net.minecraft.core.component.DataComponents; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.alchemy.Potion; import net.minecraft.world.item.alchemy.PotionBrewing; -import net.minecraft.world.item.alchemy.PotionContents; - -import java.util.Optional; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -26,6 +20,14 @@ public class PotionBrewingMixin { return original || CustomBrewingRecipes.isIngredient(ingredient); } + @ModifyReturnValue( + method = "isContainer", + at = @At("RETURN") + ) + private boolean bs$isContainer(boolean original, ItemStack ingredient) { + return original || CustomBrewingRecipes.isBasis(ingredient); + } + @ModifyReturnValue( method = "hasPotionMix", at = @At("RETURN") diff --git a/src/main/resources/beeg-smol.mixins.json b/src/main/resources/beeg-smol.mixins.json index 89c5070..4df9cbd 100644 --- a/src/main/resources/beeg-smol.mixins.json +++ b/src/main/resources/beeg-smol.mixins.json @@ -1,11 +1,13 @@ { - "required": true, - "package": "dev.schmarrn.mixin", - "compatibilityLevel": "JAVA_21", - "mixins": [ - "PotionBrewingMixin" - ], - "injectors": { - "defaultRequire": 1 + "required": true, + "package": "dev.schmarrn.mixin", + "compatibilityLevel": "JAVA_21", + "mixins": [ + "BrewingStandBlockEntityMixin", + "BrewingStandMenuMixin", + "PotionBrewingMixin" + ], + "injectors": { + "defaultRequire": 1 } } \ No newline at end of file