Skip to content

Commit

Permalink
custom brewing recipes by json
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyof429 committed May 6, 2024
1 parent 963f4c0 commit 5a0e0d6
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/main/java/net/lyof/sortilege/Sortilege.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package net.lyof.sortilege;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.lyof.sortilege.brewing.BetterBrewingRegistry;
import net.lyof.sortilege.configs.ModJsonConfigs;
import net.lyof.sortilege.enchants.ModEnchants;
import net.lyof.sortilege.item.ModItemGroups;
import net.lyof.sortilege.item.ModItems;
import net.lyof.sortilege.setup.ReloadListener;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -23,7 +26,7 @@ public void onInitialize() {

ModEnchants.register();

BetterBrewingRegistry.register();
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new ReloadListener());
}

public static Identifier makeID(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,28 @@ public static void register() {
}


private static List<BetterBrewingRecipe> RECIPES = new ArrayList<>();
private static List<IBetterBrewingRecipe> RECIPES = new ArrayList<>();

public static List<BetterBrewingRecipe> getAll() {
public static List<IBetterBrewingRecipe> getAll() {
return RECIPES;
}

public static void register(BetterBrewingRecipe recipe) {
public static void clear() {
RECIPES.clear();
}

public static void register(IBetterBrewingRecipe recipe) {
RECIPES.add(recipe);
Sortilege.log(RECIPES);
}


public static boolean isRecipe(ItemStack input, ItemStack ingredient) {
return findRecipe(input, ingredient) != null;
}

public static BetterBrewingRecipe findRecipe(ItemStack input, ItemStack ingredient) {
for (BetterBrewingRecipe recipe : BetterBrewingRegistry.getAll()) {
public static IBetterBrewingRecipe findRecipe(ItemStack input, ItemStack ingredient) {
for (IBetterBrewingRecipe recipe : BetterBrewingRegistry.getAll()) {
if (recipe.isIngredient(ingredient) && recipe.isInput(input)) return recipe;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.item.ItemStack;

public interface BetterBrewingRecipe {
public interface IBetterBrewingRecipe {
// Bottom Slots
boolean isInput(ItemStack stack);
// Top Slot
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.lyof.sortilege.brewing.custom;

import net.lyof.sortilege.brewing.BetterBrewingRecipe;
import net.lyof.sortilege.brewing.IBetterBrewingRecipe;
import net.lyof.sortilege.item.ModItems;
import net.lyof.sortilege.item.custom.potion.AntidotePotionItem;
import net.lyof.sortilege.utils.PotionHelper;
Expand All @@ -11,7 +11,7 @@
import net.minecraft.potion.PotionUtil;
import net.minecraft.potion.Potions;

public class AntidoteBrewingRecipe implements BetterBrewingRecipe {
public class AntidoteBrewingRecipe implements IBetterBrewingRecipe {
@Override
public boolean isInput(ItemStack stack) {
return stack.getItem() instanceof PotionItem &&
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/net/lyof/sortilege/brewing/custom/BrewingRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.lyof.sortilege.brewing.custom;

import net.lyof.sortilege.brewing.BetterBrewingRegistry;
import net.lyof.sortilege.brewing.IBetterBrewingRecipe;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

import java.util.Map;

public class BrewingRecipe implements IBetterBrewingRecipe {
@Override
public boolean isInput(ItemStack stack) {
return stack.isOf(this.input);
}

@Override
public boolean isIngredient(ItemStack stack) {
return stack.isOf(this.ingredient);
}

@Override
public ItemStack craft(ItemStack input, ItemStack ingredient) {
return this.output.getDefaultStack();
}

@Override
public String toString() {
return "BrewingRecipe{" +
"input=" + input +
", ingredient=" + ingredient +
", output=" + output +
'}';
}

public Item input;
public Item ingredient;
public Item output;

public BrewingRecipe(Item in, Item add, Item out) {
this.input = in;
this.ingredient = add;
this.output = out;
}

public static void read(Map<String, ?> json) {
if (json.containsKey("input") && json.containsKey("ingredient") && json.containsKey("output")) {
Item in = Registries.ITEM.get(new Identifier(String.valueOf(json.get("input"))));
Item add = Registries.ITEM.get(new Identifier(String.valueOf(json.get("ingredient"))));
Item out = Registries.ITEM.get(new Identifier(String.valueOf(json.get("output"))));

BetterBrewingRegistry.register(new BrewingRecipe(in, add, out));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package net.lyof.sortilege.brewing.custom;

import net.lyof.sortilege.brewing.BetterBrewingRecipe;
import net.lyof.sortilege.item.ModItems;
import net.lyof.sortilege.brewing.IBetterBrewingRecipe;
import net.lyof.sortilege.item.custom.potion.AntidotePotionItem;
import net.lyof.sortilege.utils.PotionHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.PotionItem;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionUtil;
import net.minecraft.potion.Potions;

public class PotionBrewingRecipe implements BetterBrewingRecipe {
public class PotionBrewingRecipe implements IBetterBrewingRecipe {
@Override
public boolean isInput(ItemStack stack) {
return stack.getItem() instanceof AntidotePotionItem &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package net.lyof.sortilege.mixin;

import net.lyof.sortilege.Sortilege;
import net.lyof.sortilege.brewing.BetterBrewingRecipe;
import net.lyof.sortilege.brewing.IBetterBrewingRecipe;
import net.lyof.sortilege.brewing.BetterBrewingRegistry;
import net.minecraft.block.entity.BrewingStandBlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.BrewingRecipeRegistry;
import net.minecraft.screen.BrewingStandScreenHandler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
Expand All @@ -17,7 +13,7 @@
public abstract class BrewingRecipeRegistryMixin {
@Inject(method = "isValidIngredient", at = @At("RETURN"), cancellable = true)
private static void isIngredient(ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
for (BetterBrewingRecipe recipe : BetterBrewingRegistry.getAll()) {
for (IBetterBrewingRecipe recipe : BetterBrewingRegistry.getAll()) {
if (recipe.isIngredient(stack)) cir.setReturnValue(true);
}
}
Expand All @@ -29,7 +25,7 @@ private static void hasRecipe(ItemStack input, ItemStack ingredient, CallbackInf

@Inject(method = "craft", at = @At("RETURN"), cancellable = true)
private static void craft(ItemStack ingredient, ItemStack input, CallbackInfoReturnable<ItemStack> cir) {
BetterBrewingRecipe recipe = BetterBrewingRegistry.findRecipe(input, ingredient);
IBetterBrewingRecipe recipe = BetterBrewingRegistry.findRecipe(input, ingredient);
if (recipe == null) return;

cir.setReturnValue(recipe.craft(input, ingredient));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.lyof.sortilege.mixin;

import net.lyof.sortilege.brewing.BetterBrewingRecipe;
import net.lyof.sortilege.brewing.IBetterBrewingRecipe;
import net.lyof.sortilege.brewing.BetterBrewingRegistry;
import net.minecraft.block.entity.BrewingStandBlockEntity;
import net.minecraft.item.ItemStack;
Expand All @@ -14,7 +14,7 @@ public class BrewingStandBlockEntityMixin {
@Inject(method = "isValid", at = @At("RETURN"), cancellable = true)
public void isValid(int slot, ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
if (slot != 3 && slot != 4) {
for (BetterBrewingRecipe recipe : BetterBrewingRegistry.getAll()) {
for (IBetterBrewingRecipe recipe : BetterBrewingRegistry.getAll()) {
if (recipe.isInput(stack)) cir.setReturnValue(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.lyof.sortilege.mixin;

import net.lyof.sortilege.brewing.BetterBrewingRecipe;
import net.lyof.sortilege.brewing.IBetterBrewingRecipe;
import net.lyof.sortilege.brewing.BetterBrewingRegistry;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -12,7 +12,7 @@
public class BrewingStandScreenHandlerMixin {
@Inject(method = "matches", at = @At("HEAD"), cancellable = true)
private static void matches(ItemStack stack, CallbackInfoReturnable<Boolean> cir){
for (BetterBrewingRecipe recipe : BetterBrewingRegistry.getAll()) {
for (IBetterBrewingRecipe recipe : BetterBrewingRegistry.getAll()) {
if (recipe.isInput(stack)) cir.setReturnValue(true);
}
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/net/lyof/sortilege/setup/ReloadListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.lyof.sortilege.setup;

import com.google.gson.Gson;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.lyof.sortilege.Sortilege;
import net.lyof.sortilege.brewing.BetterBrewingRegistry;
import net.lyof.sortilege.brewing.custom.BrewingRecipe;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Objects;

public class ReloadListener implements SimpleSynchronousResourceReloadListener {
@Override
public Identifier getFabricId() {
return Sortilege.makeID("reload_listener");
}

@Override
public void reload(ResourceManager manager) {
BetterBrewingRegistry.clear();
BetterBrewingRegistry.register();

for (Map.Entry<Identifier, Resource> entry : manager.findResources("recipes",
path -> path.toString().endsWith(".json")).entrySet()) {

try {
Resource resource = entry.getValue();

String content = new String(resource.getInputStream().readAllBytes());
Map<String, ?> json = new Gson().fromJson(content, Map.class);

if (!json.containsKey("type") || !Objects.equals(String.valueOf(json.get("type")), Sortilege.MOD_ID + ":brewing")) continue;

BrewingRecipe.read(json);

} catch (IOException e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 5a0e0d6

Please sign in to comment.