Skip to content

Commit

Permalink
Auto Import Items for Alloy Forgery Recipes within REI
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon-Seeker committed Aug 10, 2022
1 parent 3a028be commit 67276a2
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ public int getLavaProgress() {
public boolean canUse(PlayerEntity player) {
return this.controllerInventory.canPlayerUse(player);
}

public Inventory getControllerInventory(){
return this.controllerInventory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<AlloyForgeScreenHandler, AlloyForgingDisplay> {

@Override
public Iterable<SlotAccessor> getInputSlots(MenuInfoContext<AlloyForgeScreenHandler, ?, AlloyForgingDisplay> context) {
Inventory inventory = context.getMenu().getControllerInventory();

List<SlotAccessor> 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<AlloyForgeScreenHandler, AlloyForgingDisplay> getInputCleanHandler() {
return context -> {
for (SlotAccessor gridStack : getInputSlots(context)) {
InputCleanHandler.returnSlotsToPlayerInventory(context, getDumpHandler(), gridStack);
}

clearInputSlots(context.getMenu());
};
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -46,6 +52,16 @@ public AlloyForgingDisplay(AlloyForgeRecipe recipe) {
this.overrides = recipe.getTierOverrides();
}

public AlloyForgingDisplay(List<EntryIngredient> inputs, EntryIngredient output, int minForgeTier, int requiredFuel, Map<AlloyForgeRecipe.OverrideRange, ItemStack> overrides) {
this.inputs = inputs;
this.output = output;

this.minForgeTier = minForgeTier;
this.requiredFuel = requiredFuel;

this.overrides = overrides;
}

@Override
public List<EntryIngredient> getInputEntries() {
return inputs;
Expand All @@ -60,4 +76,69 @@ public List<EntryIngredient> getOutputEntries() {
public CategoryIdentifier<?> getCategoryIdentifier() {
return AlloyForgingCategory.ID;
}

public enum Serializer implements DisplaySerializer<AlloyForgingDisplay> {

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<EntryIngredient> 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<AlloyForgeRecipe.OverrideRange, ItemStack> 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());
}
}
}
5 changes: 4 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit 67276a2

Please sign in to comment.