From 4e05fe660ee17e0363c20c809b0a3128f7c20cdc Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 27 Jun 2022 14:19:51 +0200 Subject: [PATCH] JEI integration --- build.gradle | 2 +- .../jei/CraftingModuleTransferHandler.java | 42 +++++++++++++---- .../jei/CraftingTerminalTransferHandler.java | 46 +++++++++++++------ .../compat/jei/JEIPrettyPipesPlugin.java | 39 ++++++++++++---- .../prettypipes/packets/PacketGhostSlot.java | 16 +++---- .../terminal/CraftingTerminalBlockEntity.java | 6 +-- 6 files changed, 106 insertions(+), 45 deletions(-) diff --git a/build.gradle b/build.gradle index b6e1bf7..cbf9708 100644 --- a/build.gradle +++ b/build.gradle @@ -108,7 +108,7 @@ dependencies { minecraft 'net.minecraftforge:forge:1.19-41.0.45' embed 'org.jgrapht:jgrapht-core:1.5.1' - compileOnly fg.deobf("mezz.jei:jei-1.19-forge-api:11.0.0.206") + compileOnly fg.deobf("mezz.jei:jei-1.19-common-api:11.0.0.206") runtimeOnly fg.deobf("mezz.jei:jei-1.19-forge:11.0.0.206") // to test the rf requiring and crafting stuff diff --git a/src/main/java/de/ellpeck/prettypipes/compat/jei/CraftingModuleTransferHandler.java b/src/main/java/de/ellpeck/prettypipes/compat/jei/CraftingModuleTransferHandler.java index 49766c7..6c714e9 100644 --- a/src/main/java/de/ellpeck/prettypipes/compat/jei/CraftingModuleTransferHandler.java +++ b/src/main/java/de/ellpeck/prettypipes/compat/jei/CraftingModuleTransferHandler.java @@ -1,7 +1,26 @@ package de.ellpeck.prettypipes.compat.jei; -// TODO JEI -/* +import de.ellpeck.prettypipes.Registry; +import de.ellpeck.prettypipes.misc.ItemEquality; +import de.ellpeck.prettypipes.packets.PacketCraftingModuleTransfer; +import de.ellpeck.prettypipes.packets.PacketHandler; +import de.ellpeck.prettypipes.pipe.modules.craft.CraftingModuleContainer; +import mezz.jei.api.constants.RecipeTypes; +import mezz.jei.api.constants.VanillaTypes; +import mezz.jei.api.gui.ingredient.IRecipeSlotsView; +import mezz.jei.api.recipe.RecipeIngredientRole; +import mezz.jei.api.recipe.RecipeType; +import mezz.jei.api.recipe.transfer.IRecipeTransferError; +import mezz.jei.api.recipe.transfer.IRecipeTransferHandler; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.MenuType; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.CraftingRecipe; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Optional; + public class CraftingModuleTransferHandler implements IRecipeTransferHandler { @Override @@ -10,23 +29,27 @@ public class CraftingModuleTransferHandler implements IRecipeTransferHandler getRecipeClass() { - return CraftingRecipe.class; + public Optional> getMenuType() { + return Optional.of(Registry.craftingModuleContainer); } @Override - public IRecipeTransferError transferRecipe(CraftingModuleContainer container, CraftingRecipe recipe, IRecipeLayout recipeLayout, Player player, boolean maxTransfer, boolean doTransfer) { + public RecipeType getRecipeType() { + return RecipeTypes.CRAFTING; + } + + @Override + public @Nullable IRecipeTransferError transferRecipe(CraftingModuleContainer container, CraftingRecipe recipe, IRecipeSlotsView slots, Player player, boolean maxTransfer, boolean doTransfer) { if (!doTransfer) return null; - var ingredients = recipeLayout.getItemStacks().getGuiIngredients(); var inputs = new ArrayList(); var outputs = new ArrayList(); - for (var entry : ingredients.entrySet()) { - var allIngredients = entry.getValue().getAllIngredients(); + for (var entry : slots.getSlotViews()) { + var allIngredients = entry.getIngredients(VanillaTypes.ITEM_STACK).toList(); if (allIngredients.isEmpty()) continue; var remain = allIngredients.get(0).copy(); - var toAdd = entry.getValue().isInput() ? inputs : outputs; + var toAdd = entry.getRole() == RecipeIngredientRole.INPUT ? inputs : outputs; for (var stack : toAdd) { if (ItemEquality.compareItems(stack, remain)) { var fits = Math.min(stack.getMaxStackSize() - stack.getCount(), remain.getCount()); @@ -43,4 +66,3 @@ public class CraftingModuleTransferHandler implements IRecipeTransferHandler { @Override @@ -10,23 +29,24 @@ public class CraftingTerminalTransferHandler implements IRecipeTransferHandler getRecipeClass() { - return CraftingRecipe.class; + public Optional> getMenuType() { + return Optional.of(Registry.craftingTerminalContainer); } - @Nullable @Override - public IRecipeTransferError transferRecipe(CraftingTerminalContainer container, CraftingRecipe recipe, IRecipeLayout recipeLayout, Player player, boolean maxTransfer, boolean doTransfer) { + public RecipeType getRecipeType() { + return RecipeTypes.CRAFTING; + } + + @Override + public @Nullable IRecipeTransferError transferRecipe(CraftingTerminalContainer container, CraftingRecipe recipe, IRecipeSlotsView slots, Player player, boolean maxTransfer, boolean doTransfer) { if (!doTransfer) return null; - Map stacks = new HashMap<>(); - var ingredients = recipeLayout.getItemStacks().getGuiIngredients(); - for (var entry : ingredients.entrySet()) { - if (entry.getValue().isInput()) - stacks.put(entry.getKey() - 1, new PacketGhostSlot.Entry(entry.getValue().getAllIngredients())); - } + List stacks = new ArrayList<>(); + var ingredients = slots.getSlotViews(RecipeIngredientRole.INPUT); + for (var entry : ingredients) + stacks.add(new PacketGhostSlot.Entry(entry.getIngredients(VanillaTypes.ITEM_STACK).collect(Collectors.toList()))); PacketHandler.sendToServer(new PacketGhostSlot(container.getTile().getBlockPos(), stacks)); return null; } } -*/ diff --git a/src/main/java/de/ellpeck/prettypipes/compat/jei/JEIPrettyPipesPlugin.java b/src/main/java/de/ellpeck/prettypipes/compat/jei/JEIPrettyPipesPlugin.java index 31a9bd6..467f6fb 100644 --- a/src/main/java/de/ellpeck/prettypipes/compat/jei/JEIPrettyPipesPlugin.java +++ b/src/main/java/de/ellpeck/prettypipes/compat/jei/JEIPrettyPipesPlugin.java @@ -1,7 +1,30 @@ package de.ellpeck.prettypipes.compat.jei; -// TODO JEI -/*@JeiPlugin +import de.ellpeck.prettypipes.PrettyPipes; +import de.ellpeck.prettypipes.misc.PlayerPrefs; +import de.ellpeck.prettypipes.terminal.containers.ItemTerminalGui; +import mezz.jei.api.IModPlugin; +import mezz.jei.api.JeiPlugin; +import mezz.jei.api.constants.RecipeTypes; +import mezz.jei.api.gui.handlers.IGuiContainerHandler; +import mezz.jei.api.registration.IGuiHandlerRegistration; +import mezz.jei.api.registration.IRecipeTransferRegistration; +import mezz.jei.api.runtime.IJeiRuntime; +import net.minecraft.ChatFormatting; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.components.Button; +import net.minecraft.client.renderer.Rect2i; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraftforge.client.event.ScreenEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; + +import java.util.ArrayList; +import java.util.List; + +@JeiPlugin public class JEIPrettyPipesPlugin implements IModPlugin { private IJeiRuntime runtime; @@ -25,7 +48,7 @@ public class JEIPrettyPipesPlugin implements IModPlugin { @Override public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) { - registration.addRecipeTransferHandler(new CraftingTerminalTransferHandler(), VanillaRecipeCategoryUid.CRAFTING); + registration.addRecipeTransferHandler(new CraftingTerminalTransferHandler(), RecipeTypes.CRAFTING); registration.addUniversalRecipeTransferHandler(new CraftingModuleTransferHandler()); } @@ -50,7 +73,7 @@ public class JEIPrettyPipesPlugin implements IModPlugin { var screen = event.getScreen(); if (!(screen instanceof ItemTerminalGui terminal)) return; - terminal.addRenderableWidget(this.jeiSyncButton = new Button(terminal.getGuiLeft() - 22, terminal.getGuiTop() + 44, 20, 20, new TextComponent(""), button -> { + terminal.addRenderableWidget(this.jeiSyncButton = new Button(terminal.getGuiLeft() - 22, terminal.getGuiTop() + 44, 20, 20, Component.literal(""), button -> { var preferences = PlayerPrefs.get(); preferences.syncJei = !preferences.syncJei; preferences.save(); @@ -68,14 +91,14 @@ public class JEIPrettyPipesPlugin implements IModPlugin { var sync = PlayerPrefs.get().syncJei; if (event instanceof ScreenEvent.DrawScreenEvent.Post) { if (this.jeiSyncButton.isHoveredOrFocused()) - terminal.renderTooltip(event.getPoseStack(), new TranslatableComponent("info." + PrettyPipes.ID + ".sync_jei." + (sync ? "on" : "off")), event.getMouseX(), event.getMouseY()); + terminal.renderTooltip(event.getPoseStack(), Component.translatable("info." + PrettyPipes.ID + ".sync_jei." + (sync ? "on" : "off")), event.getMouseX(), event.getMouseY()); } else if (event instanceof ScreenEvent.DrawScreenEvent.Pre) { - this.jeiSyncButton.setMessage(new TextComponent((sync ? ChatFormatting.GREEN : ChatFormatting.RED) + "J")); + this.jeiSyncButton.setMessage(Component.literal((sync ? ChatFormatting.GREEN : ChatFormatting.RED) + "J")); } } @SubscribeEvent - public void onClientTick(ClientTickEvent event) { + public void onClientTick(TickEvent.ClientTickEvent event) { if (!PlayerPrefs.get().syncJei) return; @@ -99,4 +122,4 @@ public class JEIPrettyPipesPlugin implements IModPlugin { filter.setFilterText(terminalText); } } -}*/ +} diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketGhostSlot.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketGhostSlot.java index 2411b72..ebc1c24 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketGhostSlot.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketGhostSlot.java @@ -17,9 +17,7 @@ import net.minecraft.world.item.Items; import net.minecraftforge.network.NetworkEvent; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.function.Consumer; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -27,9 +25,9 @@ import java.util.stream.Collectors; public class PacketGhostSlot { private BlockPos pos; - private Map stacks; + private List stacks; - public PacketGhostSlot(BlockPos pos, Map stacks) { + public PacketGhostSlot(BlockPos pos, List stacks) { this.pos = pos; this.stacks = stacks; } @@ -41,19 +39,17 @@ public class PacketGhostSlot { public static PacketGhostSlot fromBytes(FriendlyByteBuf buf) { var packet = new PacketGhostSlot(); packet.pos = buf.readBlockPos(); - packet.stacks = new HashMap<>(); + packet.stacks = new ArrayList<>(); for (var i = buf.readInt(); i > 0; i--) - packet.stacks.put(buf.readInt(), new Entry(buf)); + packet.stacks.add(new Entry(buf)); return packet; } public static void toBytes(PacketGhostSlot packet, FriendlyByteBuf buf) { buf.writeBlockPos(packet.pos); buf.writeInt(packet.stacks.size()); - for (var entry : packet.stacks.entrySet()) { - buf.writeInt(entry.getKey()); - entry.getValue().write(buf); - } + for (var entry : packet.stacks) + entry.write(buf); } @SuppressWarnings("Convert2Lambda") diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java b/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java index 4746678..87ecb0b 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/CraftingTerminalBlockEntity.java @@ -58,7 +58,7 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity { return this.craftItems.getStackInSlot(slot).isEmpty() && !this.ghostItems.getStackInSlot(slot).isEmpty(); } - public void setGhostItems(Map stacks) { + public void setGhostItems(List stacks) { this.updateItems(); for (var i = 0; i < this.ghostItems.getSlots(); i++) { var items = stacks.get(i).getStacks(); @@ -95,9 +95,9 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity { } if (!this.level.isClientSide) { - Map clients = new HashMap<>(); + List clients = new ArrayList<>(); for (var i = 0; i < this.ghostItems.getSlots(); i++) - clients.put(i, new PacketGhostSlot.Entry(Collections.singletonList(this.ghostItems.getStackInSlot(i)))); + clients.add(new PacketGhostSlot.Entry(Collections.singletonList(this.ghostItems.getStackInSlot(i)))); PacketHandler.sendToAllLoaded(this.level, this.getBlockPos(), new PacketGhostSlot(this.getBlockPos(), clients)); } }