diff --git a/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/Patchouli.java b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/Patchouli.java index e348938..9d0fe62 100644 --- a/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/Patchouli.java +++ b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/Patchouli.java @@ -6,6 +6,7 @@ import io.github.teamgensouspark.grimoireofpatchouli.libs.danmaku.ModSpellcards; import io.github.teamgensouspark.grimoireofpatchouli.libs.danmaku.ModSubEntity; import io.github.teamgensouspark.grimoireofpatchouli.proxy.CommonProxy; +import io.github.teamgensouspark.grimoireofpatchouli.recepies.ModAnvilReceipes; import io.github.teamgensouspark.kekkai.utils.ModResource; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; @@ -25,5 +26,6 @@ public static void PreInit(FMLPreInitializationEvent event) { ModForms.initForms(); ModSubEntity.initSubEntity(); ModSpellcards.initSpellCard(); + ModAnvilReceipes.init(); } } diff --git a/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/api/anvil/AnvilApi.java b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/api/anvil/AnvilApi.java new file mode 100644 index 0000000..9e37f91 --- /dev/null +++ b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/api/anvil/AnvilApi.java @@ -0,0 +1,38 @@ +package io.github.teamgensouspark.grimoireofpatchouli.api.anvil; + +import java.util.HashMap; + +import io.github.teamgensouspark.grimoireofpatchouli.PatchouliModInfo; +import net.minecraft.item.ItemStack; +import net.minecraftforge.event.AnvilUpdateEvent; +import net.minecraftforge.fml.common.Mod.EventBusSubscriber; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +@EventBusSubscriber(modid = PatchouliModInfo.MODID) +public class AnvilApi { + private static HashMap RECEPIES = new HashMap<>(); + + public static void regReceipe(String name, AnvilReceipe receipe) { + AnvilApi.RECEPIES.put(name, receipe); + } + + public static void unregReceipe(String name) { + AnvilApi.RECEPIES.remove(name); + } + + public static boolean hasRecepie(String name) { + return AnvilApi.RECEPIES.containsKey(name); + } + + @SubscribeEvent + public static void _InternalListener(AnvilUpdateEvent event) { + ItemStack left = event.getLeft(); + ItemStack right = event.getRight(); + for (AnvilReceipe R : AnvilApi.RECEPIES.values()) { + if (R.matchThen(left, right, () -> R.apply(event))) { + break; + } + } + } + +} diff --git a/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/api/anvil/AnvilReceipe.java b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/api/anvil/AnvilReceipe.java new file mode 100644 index 0000000..4c74858 --- /dev/null +++ b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/api/anvil/AnvilReceipe.java @@ -0,0 +1,69 @@ +package io.github.teamgensouspark.grimoireofpatchouli.api.anvil; + +import java.util.function.Predicate; + +import net.minecraft.item.ItemStack; +import net.minecraftforge.event.AnvilUpdateEvent; + +public class AnvilReceipe { + public int cost = 10; + public boolean isShape = false; + public ItemStack result = ItemStack.EMPTY; + public Predicate left; + public Predicate right; + + public AnvilReceipe(Predicate left, Predicate right) { + this.left = left; + this.right = right; + } + + public AnvilReceipe() { + } + + public AnvilReceipe setLeft(Predicate left) { + this.left = left; + return this; + } + + public AnvilReceipe setRight(Predicate right) { + this.right = right; + return this; + } + + public AnvilReceipe setCost(int cost) { + this.cost = cost; + return this; + } + + public AnvilReceipe setShape(boolean isShape) { + this.isShape = isShape; + return this; + } + + public AnvilReceipe setResult(ItemStack itemStack) { + this.result = itemStack; + return this; + } + + public boolean match(ItemStack left, ItemStack right) { + return isShape ? this.left.test(left) && this.right.test(right) + : this.left.test(left) && this.right.test(right) || this.left.test(right) && this.right.test(left); + } + + public boolean matchThen(ItemStack left, ItemStack right, Runnable F) { + if (match(left, right)) { + F.run(); + return true; + } + return false; + } + + public void apply(AnvilUpdateEvent event) { + ItemStack result = this.result.copy(); + if (result != ItemStack.EMPTY && result != null) { + event.setOutput(result); + event.setCost(cost); + } + } + +} diff --git a/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/recepies/AnvilReceipe.java b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/recepies/AnvilReceipe.java deleted file mode 100644 index 70deb39..0000000 --- a/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/recepies/AnvilReceipe.java +++ /dev/null @@ -1,37 +0,0 @@ -package io.github.teamgensouspark.grimoireofpatchouli.recepies; - -import arekkuusu.grimoireofalice.common.item.ModItems; -import io.github.teamgensouspark.grimoireofpatchouli.PatchouliModInfo; -import io.github.teamgensouspark.grimoireofpatchouli.utils.ModCompat; -import net.minecraft.init.Enchantments; -import net.minecraft.init.Items; -import net.minecraft.item.ItemStack; -import net.minecraftforge.event.AnvilUpdateEvent; -import net.minecraftforge.fml.common.Mod.EventBusSubscriber; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; - -@EventBusSubscriber(modid = PatchouliModInfo.MODID) -public class AnvilReceipe { - @SubscribeEvent - public static void onAnvilUpdate(AnvilUpdateEvent event) { - if (ModCompat.GOA.isLoaded()) { - if (event.getLeft().getItem() == ModItems.HAKUROUKEN && event.getRight().getItem() == ModItems.ROUKANKEN) { - ItemStack output = new ItemStack( - io.github.teamgensouspark.grimoireofpatchouli.libs.vanilia.ModItems.BLACK_SAKURA); - output.addEnchantment(Enchantments.SMITE, 10); - event.setOutput(output); - event.setCost(30); - - } - } else { - if (event.getLeft().getItem() == Items.DIAMOND_SWORD && event.getRight().getItem() == Items.NETHER_STAR) { - ItemStack output = new ItemStack( - io.github.teamgensouspark.grimoireofpatchouli.libs.vanilia.ModItems.BLACK_SAKURA); - output.addEnchantment(Enchantments.SMITE, 10); - event.setOutput(output); - event.setCost(30); - - } - } - } -} diff --git a/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/recepies/ModAnvilReceipes.java b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/recepies/ModAnvilReceipes.java new file mode 100644 index 0000000..8811fa6 --- /dev/null +++ b/src/main/java/io/github/teamgensouspark/grimoireofpatchouli/recepies/ModAnvilReceipes.java @@ -0,0 +1,28 @@ +package io.github.teamgensouspark.grimoireofpatchouli.recepies; + +import arekkuusu.grimoireofalice.common.item.ModItems; +import io.github.teamgensouspark.grimoireofpatchouli.api.anvil.AnvilApi; +import io.github.teamgensouspark.grimoireofpatchouli.api.anvil.AnvilReceipe; +import io.github.teamgensouspark.grimoireofpatchouli.utils.ModCompat; +import net.minecraft.init.Enchantments; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; + +public class ModAnvilReceipes { + public static void init() { + ItemStack BLACK_SAKURA = new ItemStack( + io.github.teamgensouspark.grimoireofpatchouli.libs.vanilia.ModItems.BLACK_SAKURA); + BLACK_SAKURA.addEnchantment(Enchantments.SMITE, 10); + if (ModCompat.GOA.isLoaded()) { + AnvilApi.regReceipe("black_sakura", + new AnvilReceipe().setCost(30).setShape(false) + .setLeft((stack) -> stack.getItem() == ModItems.HAKUROUKEN) + .setRight((stack) -> stack.getItem() == ModItems.ROUKANKEN).setResult(BLACK_SAKURA)); + } else { + AnvilApi.regReceipe("black_sakura", + new AnvilReceipe().setCost(30).setShape(false) + .setLeft((stack) -> stack.getItem() == Items.DIAMOND_SWORD) + .setRight((stack) -> stack.getItem() == Items.NETHER_STAR).setResult(BLACK_SAKURA)); + } + } +}