Skip to content

Commit

Permalink
✨ AnvilApi
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Oct 28, 2023
1 parent bbc7340 commit 2fc26a4
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,5 +26,6 @@ public static void PreInit(FMLPreInitializationEvent event) {
ModForms.initForms();
ModSubEntity.initSubEntity();
ModSpellcards.initSpellCard();
ModAnvilReceipes.init();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, AnvilReceipe> 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;
}
}
}

}
Original file line number Diff line number Diff line change
@@ -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<ItemStack> left;
public Predicate<ItemStack> right;

public AnvilReceipe(Predicate<ItemStack> left, Predicate<ItemStack> right) {
this.left = left;
this.right = right;
}

public AnvilReceipe() {
}

public AnvilReceipe setLeft(Predicate<ItemStack> left) {
this.left = left;
return this;
}

public AnvilReceipe setRight(Predicate<ItemStack> 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);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 2fc26a4

Please sign in to comment.