Skip to content

Commit

Permalink
Convert some Strings to ResourceLocations to allow for non-MI namespa…
Browse files Browse the repository at this point in the history
…ces (#838)

Co-authored-by: Technici4n <[email protected]>
  • Loading branch information
Swedz and Technici4n authored Aug 21, 2024
1 parent 433bdc4 commit 16af829
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package aztech.modern_industrialization.compat.viewer.impl.emi;

import aztech.modern_industrialization.MI;
import aztech.modern_industrialization.inventory.ConfigurableItemStack;
import aztech.modern_industrialization.machines.gui.MachineMenuClient;
import aztech.modern_industrialization.machines.gui.MachineMenuCommon;
Expand All @@ -37,6 +36,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.crafting.RecipeHolder;
import org.apache.commons.lang3.mutable.MutableBoolean;
Expand Down Expand Up @@ -105,12 +105,12 @@ private boolean lockSlots(EmiRecipe recipe, AbstractContainerScreen<MachineMenuC

private boolean canApply(MachineMenuCommon handler, ViewerCategoryEmi<?> category) {
// Check if the block is in the worktables - it's a hack but it should work. :P
String blockId = handler.guiParams.blockId;
ResourceLocation blockId = handler.guiParams.blockId;
MutableBoolean hasWorkstation = new MutableBoolean(false);

category.wrapped.buildWorkstations(items -> {
for (var item : items) {
if (BuiltInRegistries.ITEM.getKey(item.asItem()).equals(MI.id(blockId))) {
if (BuiltInRegistries.ITEM.getKey(item.asItem()).equals(blockId)) {
hasWorkstation.setTrue();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import mezz.jei.api.runtime.IJeiRuntime;
import mezz.jei.api.runtime.IRecipesGui;
import net.minecraft.client.renderer.Rect2i;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.fluids.FluidStack;
Expand All @@ -63,7 +64,7 @@ public List<Rect2i> getGuiExtraAreas(MachineScreen screen) {
public Collection<IGuiClickableArea> getGuiClickableAreas(MachineScreen screen, double guiMouseX, double guiMouseY) {

MachineMenuClient screenHandler = screen.getMenu();
String blockId = screenHandler.guiParams.blockId;
ResourceLocation blockId = screenHandler.guiParams.blockId;
List<ReiMachineRecipes.ClickAreaCategory> categories = ReiMachineRecipes.machineToClickAreaCategory.getOrDefault(blockId,
Collections.emptyList());
Rectangle rectangle = ReiMachineRecipes.machineToClickArea.get(blockId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package aztech.modern_industrialization.compat.viewer.impl.jei;

import aztech.modern_industrialization.MI;
import aztech.modern_industrialization.machines.gui.MachineMenuClient;
import aztech.modern_industrialization.machines.guicomponents.ReiSlotLockingClient;
import aztech.modern_industrialization.machines.recipe.MachineRecipe;
Expand All @@ -38,6 +37,7 @@
import mezz.jei.api.recipe.transfer.IRecipeTransferHandlerHelper;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.crafting.RecipeHolder;
Expand Down Expand Up @@ -95,14 +95,14 @@ public mezz.jei.api.recipe.RecipeType<RecipeHolder<MachineRecipe>> getRecipeType

private boolean canApply(MachineMenuClient handler) {
// Check if the block is in the worktables - it's a hack but it should work. :P
String blockId = handler.guiParams.blockId;
ResourceLocation blockId = handler.guiParams.blockId;

var lookup = getLookup();
if (lookup == null) {
return false;
}

var item = BuiltInRegistries.ITEM.get(MI.id(blockId));
var item = BuiltInRegistries.ITEM.get(blockId);
return lookup.getItemStack().anyMatch(is -> is.is(item));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class ManualPluginJei implements IModPlugin {

private IJeiRuntime jeiRuntime;

private RecipeType<RecipeHolder<MachineRecipe>> getMachineCategory(String category) {
return RecipeType.create(MI.ID, category, (Class) RecipeHolder.class);
private RecipeType<RecipeHolder<MachineRecipe>> getMachineCategory(ResourceLocation category) {
return RecipeType.create(category.getNamespace(), category.getPath(), (Class) RecipeHolder.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
import me.shedaniel.rei.api.client.registry.screen.ClickArea;
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.Display;
import net.minecraft.resources.ResourceLocation;

class MachineClickAreaHandler implements ClickArea<MachineScreen> {
@Override
public Result handle(ClickAreaContext<MachineScreen> context) {
MachineMenuClient screenHandler = context.getScreen().getMenu();
String blockId = screenHandler.guiParams.blockId;
ResourceLocation blockId = screenHandler.guiParams.blockId;
List<ReiMachineRecipes.ClickAreaCategory> categories = ReiMachineRecipes.machineToClickAreaCategory.getOrDefault(blockId,
Collections.emptyList());
Rectangle rectangle = ReiMachineRecipes.machineToClickArea.get(blockId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package aztech.modern_industrialization.compat.viewer.impl.rei;

import aztech.modern_industrialization.MI;
import aztech.modern_industrialization.machines.gui.MachineMenuClient;
import aztech.modern_industrialization.machines.guicomponents.ReiSlotLockingClient;
import aztech.modern_industrialization.machines.recipe.MachineRecipe;
Expand All @@ -35,6 +34,7 @@
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.entry.EntryStack;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;
Expand All @@ -61,12 +61,12 @@ class MachineSlotLockingHandler implements TransferHandler {

private boolean canApply(MachineMenuClient handler, CategoryIdentifier<?> category) {
// Check if the block is in the worktables - it's a hack but it should work. :P
String blockId = handler.guiParams.blockId;
ResourceLocation blockId = handler.guiParams.blockId;
List<EntryIngredient> workstations = CategoryRegistry.getInstance().get(category).getWorkstations();
for (EntryIngredient workstationEntries : workstations) {
for (EntryStack<?> entry : workstationEntries) {
Item item = entry.<ItemStack>cast().getValue().getItem();
if (BuiltInRegistries.ITEM.getKey(item).equals(MI.id(blockId))) {
if (BuiltInRegistries.ITEM.getKey(item).equals(blockId)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public static MachineCategory create(MachineCategoryParams params) {
private final MachineCategoryParams params;

private MachineCategory(MachineCategoryParams params, int width, int height) {
super((Class) RecipeHolder.class, MI.id(params.category),
Component.translatable("rei_categories.modern_industrialization." + params.category),
super((Class) RecipeHolder.class, params.category,
Component.translatable("rei_categories.%s.%s".formatted(params.category.getNamespace(), params.category.getPath())),
BuiltInRegistries.ITEM.get(params.workstations.get(0)).getDefaultInstance(), width, height);

this.params = params;
Expand All @@ -108,26 +108,28 @@ public void buildRecipes(RecipeManager recipeManager, RegistryAccess registryAcc
.forEach(consumer);

// converted recipes
switch (params.category) {
case "bronze_furnace" -> {
recipeManager.getAllRecipesFor(RecipeType.SMELTING)
.stream()
.map(r -> RecipeConversions.ofSmelting(r, MIMachineRecipeTypes.FURNACE, registryAccess))
.forEach(consumer);
}
case "bronze_cutting_machine" -> {
recipeManager.getAllRecipesFor(RecipeType.STONECUTTING)
.stream()
.map(r -> RecipeConversions.ofStonecutting(r, MIMachineRecipeTypes.CUTTING_MACHINE, registryAccess))
.forEach(consumer);
}
case "centrifuge" -> {
ComposterBlock.COMPOSTABLES.keySet()
.stream()
.map(RecipeConversions::ofCompostable)
.filter(Objects::nonNull)
.forEach(consumer);
}
if (params.category.getNamespace().equals(MI.ID)) {
switch (params.category.getPath()) {
case "bronze_furnace" -> {
recipeManager.getAllRecipesFor(RecipeType.SMELTING)
.stream()
.map(r -> RecipeConversions.ofSmelting(r, MIMachineRecipeTypes.FURNACE, registryAccess))
.forEach(consumer);
}
case "bronze_cutting_machine" -> {
recipeManager.getAllRecipesFor(RecipeType.STONECUTTING)
.stream()
.map(r -> RecipeConversions.ofStonecutting(r, MIMachineRecipeTypes.CUTTING_MACHINE, registryAccess))
.forEach(consumer);
}
case "centrifuge" -> {
ComposterBlock.COMPOSTABLES.keySet()
.stream()
.map(RecipeConversions::ofCompostable)
.filter(Objects::nonNull)
.forEach(consumer);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected static class Recipe {
public final List<ItemStack> materials = new ArrayList<>();
public final ResourceLocation id;

public Recipe(String controller, ShapeTemplate shapeTemplate, @Nullable String alternative) {
this.controller = BuiltInRegistries.ITEM.get(MI.id(controller)).getDefaultInstance();
public Recipe(ResourceLocation controller, ShapeTemplate shapeTemplate, @Nullable String alternative) {
this.controller = BuiltInRegistries.ITEM.get(controller).getDefaultInstance();
this.materials.add(this.controller);
SortedMap<Item, Integer> materials = new TreeMap<>(Comparator.comparing(BuiltInRegistries.ITEM::getKey));

Expand All @@ -108,7 +108,8 @@ public Recipe(String controller, ShapeTemplate shapeTemplate, @Nullable String a
for (var entry : materials.entrySet()) {
this.materials.add(new ItemStack(entry.getKey(), entry.getValue()));
}
this.id = MI.id(controller + "/" + materials.size() + (alternative == null ? "" : "/" + alternative));
this.id = ResourceLocation.fromNamespaceAndPath(controller.getNamespace(),
controller.getPath() + "/" + materials.size() + (alternative == null ? "" : "/" + alternative));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class MachineCategoryParams {
public final String englishName;
public final String category;
public final ResourceLocation category;
public final SlotPositions itemInputs;
public final SlotPositions itemOutputs;
public final SlotPositions fluidInputs;
Expand All @@ -44,9 +44,9 @@ public class MachineCategoryParams {
public final SteamMode steamMode;
public final List<ResourceLocation> workstations = new ArrayList<>();

public MachineCategoryParams(String englishName, String category, SlotPositions itemInputs, SlotPositions itemOutputs, SlotPositions fluidInputs,
SlotPositions fluidOutputs, ProgressBar.Parameters progressBarParams, Predicate<MachineRecipe> recipePredicate, boolean isMultiblock,
SteamMode steamMode) {
public MachineCategoryParams(String englishName, ResourceLocation category, SlotPositions itemInputs, SlotPositions itemOutputs,
SlotPositions fluidInputs, SlotPositions fluidOutputs, ProgressBar.Parameters progressBarParams, Predicate<MachineRecipe> recipePredicate,
boolean isMultiblock, SteamMode steamMode) {
this.englishName = englishName;
this.category = category;
this.itemInputs = itemInputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,56 +35,64 @@
* called even if REI is not loaded, and should not reference the REI API.
*/
public class ReiMachineRecipes {
public static final Map<String, MachineCategoryParams> categories = new TreeMap<>();
public static final Map<ResourceLocation, MachineCategoryParams> categories = new TreeMap<>();
/**
* Maps a machine block id to the list of recipe categories.
*/
public static final Map<String, List<ClickAreaCategory>> machineToClickAreaCategory = new HashMap<>();
public static final Map<ResourceLocation, List<ClickAreaCategory>> machineToClickAreaCategory = new HashMap<>();
/**
* Maps a machine block id to the parameters of the click area for the recipe.
*/
public static final Map<String, Rectangle> machineToClickArea = new HashMap<>();
public static final Map<ResourceLocation, Rectangle> machineToClickArea = new HashMap<>();
/**
* List of registered multiblock shape "recipes".
*/
public static final List<MultiblockShape> multiblockShapes = new ArrayList<>();

public static void registerCategory(String machine, MachineCategoryParams params) {
public static void registerCategory(ResourceLocation machine, MachineCategoryParams params) {
if (categories.put(machine, params) != null) {
throw new IllegalStateException("Machine was already registered: " + machine);
}
}

public static void registerWorkstation(String machine, ResourceLocation item) {
public static void registerWorkstation(ResourceLocation machine, ResourceLocation item) {
MachineCategoryParams params = categories.get(machine);
if (params == null) {
throw new NullPointerException("Machine params may not be null for machine " + machine);
}
params.workstations.add(item);
}

public static void registerRecipeCategoryForMachine(String machine, String category) {
public static void registerRecipeCategoryForMachine(ResourceLocation machine, ResourceLocation category) {
registerRecipeCategoryForMachine(machine, category, MachineScreenPredicate.ANY);
}

public static void registerRecipeCategoryForMachine(String machine, String category,
public static void registerRecipeCategoryForMachine(ResourceLocation machine, ResourceLocation category,
MachineScreenPredicate screenPredicate) {
machineToClickAreaCategory.computeIfAbsent(machine, k -> new ArrayList<>())
.add(new ClickAreaCategory(MI.id(category), screenPredicate));
.add(new ClickAreaCategory(category, screenPredicate));
}

public static void registerMachineClickArea(String machine, Rectangle clickArea) {
public static void registerMachineClickArea(ResourceLocation machine, Rectangle clickArea) {
machineToClickArea.put(machine, clickArea);
}

public static void registerMultiblockShape(String machine, ShapeTemplate shapeTemplate) {
public static void registerMultiblockShape(ResourceLocation machine, ShapeTemplate shapeTemplate) {
registerMultiblockShape(machine, shapeTemplate, null);
}

public static void registerMultiblockShape(String machine, ShapeTemplate shapeTemplate, @Nullable String alternative) {
public static void registerMultiblockShape(String machine, ShapeTemplate shapeTemplate) {
registerMultiblockShape(MI.id(machine), shapeTemplate);
}

public static void registerMultiblockShape(ResourceLocation machine, ShapeTemplate shapeTemplate, @Nullable String alternative) {
multiblockShapes.add(new MultiblockShape(machine, shapeTemplate, alternative));
}

public static void registerMultiblockShape(String machine, ShapeTemplate shapeTemplate, @Nullable String alternative) {
registerMultiblockShape(MI.id(machine), shapeTemplate, alternative);
}

public static class ClickAreaCategory {
public final ResourceLocation category;
public final MachineScreenPredicate predicate;
Expand All @@ -100,6 +108,6 @@ public enum MachineScreenPredicate {
MULTIBLOCK,
}

public record MultiblockShape(String machine, ShapeTemplate shapeTemplate, @Nullable String alternative) {
public record MultiblockShape(ResourceLocation machine, ShapeTemplate shapeTemplate, @Nullable String alternative) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import net.minecraft.Util;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -155,7 +156,7 @@ public <T, R> R mapComponentOrDefault(Class<T> clazz, Function<? super T, ? exte

@Override
public final Component getDisplayName() {
return Component.translatable("block.modern_industrialization." + guiParams.blockId);
return Component.translatable(Util.makeDescriptionId("block", guiParams.blockId));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
*/
package aztech.modern_industrialization.machines.gui;

import aztech.modern_industrialization.MI;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;

public class MachineGuiParameters {
public final String blockId;
public final ResourceLocation blockId;
public final int playerInventoryX, playerInventoryY;
public final int backgroundWidth, backgroundHeight;
public final boolean lockButton;

private MachineGuiParameters(String blockId, int playerInventoryX, int playerInventoryY, int backgroundWidth, int backgroundHeight,
private MachineGuiParameters(ResourceLocation blockId, int playerInventoryX, int playerInventoryY, int backgroundWidth, int backgroundHeight,
boolean lockButton) {
this.blockId = blockId;
this.playerInventoryX = playerInventoryX;
Expand All @@ -42,7 +44,7 @@ private MachineGuiParameters(String blockId, int playerInventoryX, int playerInv
}

public void write(FriendlyByteBuf buf) {
buf.writeUtf(blockId);
buf.writeResourceLocation(blockId);
buf.writeInt(playerInventoryX);
buf.writeInt(playerInventoryY);
buf.writeInt(backgroundWidth);
Expand All @@ -51,20 +53,24 @@ public void write(FriendlyByteBuf buf) {
}

public static MachineGuiParameters read(FriendlyByteBuf buf) {
return new MachineGuiParameters(buf.readUtf(), buf.readInt(), buf.readInt(), buf.readInt(), buf.readInt(), buf.readBoolean());
return new MachineGuiParameters(buf.readResourceLocation(), buf.readInt(), buf.readInt(), buf.readInt(), buf.readInt(), buf.readBoolean());
}

public static class Builder {
private final String blockId;
private final ResourceLocation blockId;
public int playerInventoryX = 8, playerInventoryY = 84;
private int backgroundSizeX = 176, backgroundSizeY = 166;
public final boolean lockButton;

public Builder(String blockId, boolean lockButton) {
public Builder(ResourceLocation blockId, boolean lockButton) {
this.blockId = blockId;
this.lockButton = lockButton;
}

public Builder(String blockId, boolean lockButton) {
this(MI.id(blockId), lockButton);
}

public Builder backgroundHeight(int height) {
this.backgroundSizeY = height;
this.playerInventoryY = height - 82;
Expand Down
Loading

0 comments on commit 16af829

Please sign in to comment.