From 67276a28b6d24f876c453802ba84a26fb1867747 Mon Sep 17 00:00:00 2001 From: Dragon-Seeker Date: Wed, 10 Aug 2022 16:00:43 -0500 Subject: [PATCH] Auto Import Items for Alloy Forgery Recipes within REI --- .../alloyforgery/AlloyForgeScreenHandler.java | 4 + ...gin.java => AlloyForgeryClientPlugin.java} | 2 +- .../compat/rei/AlloyForgeryCommonPlugin.java | 20 +++++ .../compat/rei/AlloyForgeryMenuInfo.java | 42 ++++++++++ .../compat/rei/AlloyForgingDisplay.java | 81 +++++++++++++++++++ src/main/resources/fabric.mod.json | 5 +- 6 files changed, 152 insertions(+), 2 deletions(-) rename src/main/java/wraith/alloyforgery/compat/rei/{AlloyForgeryPlugin.java => AlloyForgeryClientPlugin.java} (95%) create mode 100644 src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryCommonPlugin.java create mode 100644 src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryMenuInfo.java diff --git a/src/main/java/wraith/alloyforgery/AlloyForgeScreenHandler.java b/src/main/java/wraith/alloyforgery/AlloyForgeScreenHandler.java index ca9a08c..dd6ba4b 100644 --- a/src/main/java/wraith/alloyforgery/AlloyForgeScreenHandler.java +++ b/src/main/java/wraith/alloyforgery/AlloyForgeScreenHandler.java @@ -76,4 +76,8 @@ public int getLavaProgress() { public boolean canUse(PlayerEntity player) { return this.controllerInventory.canPlayerUse(player); } + + public Inventory getControllerInventory(){ + return this.controllerInventory; + } } diff --git a/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryPlugin.java b/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryClientPlugin.java similarity index 95% rename from src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryPlugin.java rename to src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryClientPlugin.java index 00fe3cb..e69ec5e 100644 --- a/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryPlugin.java +++ b/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryClientPlugin.java @@ -10,7 +10,7 @@ import wraith.alloyforgery.forges.ForgeRegistry; import wraith.alloyforgery.recipe.AlloyForgeRecipe; -public class AlloyForgeryPlugin implements REIClientPlugin { +public class AlloyForgeryClientPlugin implements REIClientPlugin { @Override public void registerCategories(CategoryRegistry registry) { diff --git a/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryCommonPlugin.java b/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryCommonPlugin.java new file mode 100644 index 0000000..62fdf7c --- /dev/null +++ b/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryCommonPlugin.java @@ -0,0 +1,20 @@ +package wraith.alloyforgery.compat.rei; + +import me.shedaniel.rei.api.common.display.DisplaySerializerRegistry; +import me.shedaniel.rei.api.common.plugins.REIServerPlugin; +import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry; +import me.shedaniel.rei.api.common.transfer.info.simple.SimpleMenuInfoProvider; +import wraith.alloyforgery.AlloyForgeScreenHandler; + +public class AlloyForgeryCommonPlugin implements REIServerPlugin { + + @Override + public void registerMenuInfo(MenuInfoRegistry registry) { + registry.register(AlloyForgingCategory.ID, AlloyForgeScreenHandler.class, SimpleMenuInfoProvider.of(AlloyForgeryMenuInfo::new)); + } + + @Override + public void registerDisplaySerializer(DisplaySerializerRegistry registry) { + registry.register(AlloyForgingCategory.ID, AlloyForgingDisplay.Serializer.INSTANCE); + } +} diff --git a/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryMenuInfo.java b/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryMenuInfo.java new file mode 100644 index 0000000..ce51efa --- /dev/null +++ b/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgeryMenuInfo.java @@ -0,0 +1,42 @@ +package wraith.alloyforgery.compat.rei; + +import me.shedaniel.rei.api.common.transfer.info.MenuInfoContext; +import me.shedaniel.rei.api.common.transfer.info.clean.InputCleanHandler; +import me.shedaniel.rei.api.common.transfer.info.simple.SimplePlayerInventoryMenuInfo; +import me.shedaniel.rei.api.common.transfer.info.stack.SlotAccessor; +import net.minecraft.inventory.Inventory; +import wraith.alloyforgery.AlloyForgeScreenHandler; + +import java.util.ArrayList; +import java.util.List; + +public record AlloyForgeryMenuInfo(AlloyForgingDisplay display) implements SimplePlayerInventoryMenuInfo { + + @Override + public Iterable getInputSlots(MenuInfoContext context) { + Inventory inventory = context.getMenu().getControllerInventory(); + + List list = new ArrayList<>(inventory.size() - 2); + for (int i = 0; i < inventory.size() - 2; i++) { + list.add(SlotAccessor.fromContainer(inventory, i)); + } + + return list; + } + + @Override + public AlloyForgingDisplay getDisplay() { + return this.display; + } + + @Override + public InputCleanHandler getInputCleanHandler() { + return context -> { + for (SlotAccessor gridStack : getInputSlots(context)) { + InputCleanHandler.returnSlotsToPlayerInventory(context, getDumpHandler(), gridStack); + } + + clearInputSlots(context.getMenu()); + }; + } +} diff --git a/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgingDisplay.java b/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgingDisplay.java index 57236b4..0370157 100644 --- a/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgingDisplay.java +++ b/src/main/java/wraith/alloyforgery/compat/rei/AlloyForgingDisplay.java @@ -1,10 +1,16 @@ package wraith.alloyforgery.compat.rei; +import com.google.common.collect.ImmutableMap; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; +import me.shedaniel.rei.api.common.display.DisplaySerializer; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.util.EntryIngredients; +import net.fabricmc.fabric.api.util.NbtType; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtHelper; +import net.minecraft.nbt.NbtList; import net.minecraft.recipe.Ingredient; import wraith.alloyforgery.recipe.AlloyForgeRecipe; @@ -46,6 +52,16 @@ public AlloyForgingDisplay(AlloyForgeRecipe recipe) { this.overrides = recipe.getTierOverrides(); } + public AlloyForgingDisplay(List inputs, EntryIngredient output, int minForgeTier, int requiredFuel, Map overrides) { + this.inputs = inputs; + this.output = output; + + this.minForgeTier = minForgeTier; + this.requiredFuel = requiredFuel; + + this.overrides = overrides; + } + @Override public List getInputEntries() { return inputs; @@ -60,4 +76,69 @@ public List getOutputEntries() { public CategoryIdentifier getCategoryIdentifier() { return AlloyForgingCategory.ID; } + + public enum Serializer implements DisplaySerializer { + + INSTANCE; + + @Override + public NbtCompound save(NbtCompound tag, AlloyForgingDisplay display) { + // Store the fuel per tick + tag.putInt("fuel_per_tick", display.requiredFuel); + + // Save the minimum Forge Tier + tag.putInt("min_forge_tier", display.minForgeTier); + + // Store the recipe inputs + NbtList inputs = new NbtList(); + inputs.addAll(display.inputs.stream().map(EntryIngredient::save).toList()); + tag.put("inputs", inputs); + + // Store the recipe output + tag.put("output", display.output.save()); + + NbtList overrides = new NbtList(); + display.overrides.forEach((overrideRange, itemStack) -> { + NbtCompound overrideTag = new NbtCompound(); + + overrideTag.putInt("lower", overrideRange.lowerBound()); + overrideTag.putInt("upper", overrideRange.upperBound()); + overrideTag.put("stack", itemStack.getOrCreateNbt()); + + overrides.add(overrideTag); + }); + tag.put("overrides", overrides); + + return tag; + } + + @Override + public AlloyForgingDisplay read(NbtCompound tag) { + // Get the fuel per tick + int requiredFuel = tag.getInt("fuel_per_tick"); + + // Get the minimum Forge Tier + int minForgeTier = tag.getInt("fuel_per_tick"); + + // We get a list of all the recipe inputs + List input = new ArrayList<>(); + tag.getList("inputs", NbtType.LIST).forEach(nbtElement -> input.add(EntryIngredient.read((NbtList) nbtElement))); + + // We get the single recipe output + EntryIngredient output = EntryIngredient.read(tag.getList("output", NbtType.LIST)); + + // Last thing we grab is the recipes Override Range Values + ImmutableMap.Builder builder = new ImmutableMap.Builder<>(); + tag.getList("overrides", NbtType.COMPOUND).forEach(nbtElement -> { + NbtCompound overrideTag = (NbtCompound) nbtElement; + + AlloyForgeRecipe.OverrideRange range = new AlloyForgeRecipe.OverrideRange(overrideTag.getInt("lower"), overrideTag.getInt("upper")); + ItemStack stack = ItemStack.fromNbt(overrideTag.getCompound("stack")); + + builder.put(range, stack); + }); + + return new AlloyForgingDisplay(input, output, minForgeTier, requiredFuel, builder.build()); + } + } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 88b0aa0..f1994ab 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -28,7 +28,10 @@ "wraith.alloyforgery.AlloyForgery" ], "rei_client": [ - "wraith.alloyforgery.compat.rei.AlloyForgeryPlugin" + "wraith.alloyforgery.compat.rei.AlloyForgeryClientPlugin" + ], + "rei_common": [ + "wraith.alloyforgery.compat.rei.AlloyForgeryCommonPlugin" ] }, "mixins": [