JEI integration

This commit is contained in:
Ell 2022-06-27 14:19:51 +02:00
parent 27e25fbedf
commit 4e05fe660e
6 changed files with 106 additions and 45 deletions

View file

@ -108,7 +108,7 @@ dependencies {
minecraft 'net.minecraftforge:forge:1.19-41.0.45' minecraft 'net.minecraftforge:forge:1.19-41.0.45'
embed 'org.jgrapht:jgrapht-core:1.5.1' 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") runtimeOnly fg.deobf("mezz.jei:jei-1.19-forge:11.0.0.206")
// to test the rf requiring and crafting stuff // to test the rf requiring and crafting stuff

View file

@ -1,7 +1,26 @@
package de.ellpeck.prettypipes.compat.jei; 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<CraftingModuleContainer, CraftingRecipe> { public class CraftingModuleTransferHandler implements IRecipeTransferHandler<CraftingModuleContainer, CraftingRecipe> {
@Override @Override
@ -10,23 +29,27 @@ public class CraftingModuleTransferHandler implements IRecipeTransferHandler<Cra
} }
@Override @Override
public Class<CraftingRecipe> getRecipeClass() { public Optional<MenuType<CraftingModuleContainer>> getMenuType() {
return CraftingRecipe.class; return Optional.of(Registry.craftingModuleContainer);
} }
@Override @Override
public IRecipeTransferError transferRecipe(CraftingModuleContainer container, CraftingRecipe recipe, IRecipeLayout recipeLayout, Player player, boolean maxTransfer, boolean doTransfer) { public RecipeType<CraftingRecipe> getRecipeType() {
return RecipeTypes.CRAFTING;
}
@Override
public @Nullable IRecipeTransferError transferRecipe(CraftingModuleContainer container, CraftingRecipe recipe, IRecipeSlotsView slots, Player player, boolean maxTransfer, boolean doTransfer) {
if (!doTransfer) if (!doTransfer)
return null; return null;
var ingredients = recipeLayout.getItemStacks().getGuiIngredients();
var inputs = new ArrayList<ItemStack>(); var inputs = new ArrayList<ItemStack>();
var outputs = new ArrayList<ItemStack>(); var outputs = new ArrayList<ItemStack>();
for (var entry : ingredients.entrySet()) { for (var entry : slots.getSlotViews()) {
var allIngredients = entry.getValue().getAllIngredients(); var allIngredients = entry.getIngredients(VanillaTypes.ITEM_STACK).toList();
if (allIngredients.isEmpty()) if (allIngredients.isEmpty())
continue; continue;
var remain = allIngredients.get(0).copy(); 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) { for (var stack : toAdd) {
if (ItemEquality.compareItems(stack, remain)) { if (ItemEquality.compareItems(stack, remain)) {
var fits = Math.min(stack.getMaxStackSize() - stack.getCount(), remain.getCount()); var fits = Math.min(stack.getMaxStackSize() - stack.getCount(), remain.getCount());
@ -43,4 +66,3 @@ public class CraftingModuleTransferHandler implements IRecipeTransferHandler<Cra
return null; return null;
} }
} }
*/

View file

@ -1,7 +1,26 @@
package de.ellpeck.prettypipes.compat.jei; package de.ellpeck.prettypipes.compat.jei;
// TODO JEI import de.ellpeck.prettypipes.Registry;
/* import de.ellpeck.prettypipes.packets.PacketGhostSlot;
import de.ellpeck.prettypipes.packets.PacketHandler;
import de.ellpeck.prettypipes.terminal.containers.CraftingTerminalContainer;
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.crafting.CraftingRecipe;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class CraftingTerminalTransferHandler implements IRecipeTransferHandler<CraftingTerminalContainer, CraftingRecipe> { public class CraftingTerminalTransferHandler implements IRecipeTransferHandler<CraftingTerminalContainer, CraftingRecipe> {
@Override @Override
@ -10,23 +29,24 @@ public class CraftingTerminalTransferHandler implements IRecipeTransferHandler<C
} }
@Override @Override
public Class<CraftingRecipe> getRecipeClass() { public Optional<MenuType<CraftingTerminalContainer>> getMenuType() {
return CraftingRecipe.class; return Optional.of(Registry.craftingTerminalContainer);
} }
@Nullable
@Override @Override
public IRecipeTransferError transferRecipe(CraftingTerminalContainer container, CraftingRecipe recipe, IRecipeLayout recipeLayout, Player player, boolean maxTransfer, boolean doTransfer) { public RecipeType<CraftingRecipe> getRecipeType() {
return RecipeTypes.CRAFTING;
}
@Override
public @Nullable IRecipeTransferError transferRecipe(CraftingTerminalContainer container, CraftingRecipe recipe, IRecipeSlotsView slots, Player player, boolean maxTransfer, boolean doTransfer) {
if (!doTransfer) if (!doTransfer)
return null; return null;
Map<Integer, PacketGhostSlot.Entry> stacks = new HashMap<>(); List<PacketGhostSlot.Entry> stacks = new ArrayList<>();
var ingredients = recipeLayout.getItemStacks().getGuiIngredients(); var ingredients = slots.getSlotViews(RecipeIngredientRole.INPUT);
for (var entry : ingredients.entrySet()) { for (var entry : ingredients)
if (entry.getValue().isInput()) stacks.add(new PacketGhostSlot.Entry(entry.getIngredients(VanillaTypes.ITEM_STACK).collect(Collectors.toList())));
stacks.put(entry.getKey() - 1, new PacketGhostSlot.Entry(entry.getValue().getAllIngredients()));
}
PacketHandler.sendToServer(new PacketGhostSlot(container.getTile().getBlockPos(), stacks)); PacketHandler.sendToServer(new PacketGhostSlot(container.getTile().getBlockPos(), stacks));
return null; return null;
} }
} }
*/

View file

@ -1,7 +1,30 @@
package de.ellpeck.prettypipes.compat.jei; package de.ellpeck.prettypipes.compat.jei;
// TODO JEI import de.ellpeck.prettypipes.PrettyPipes;
/*@JeiPlugin 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 { public class JEIPrettyPipesPlugin implements IModPlugin {
private IJeiRuntime runtime; private IJeiRuntime runtime;
@ -25,7 +48,7 @@ public class JEIPrettyPipesPlugin implements IModPlugin {
@Override @Override
public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) { public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) {
registration.addRecipeTransferHandler(new CraftingTerminalTransferHandler(), VanillaRecipeCategoryUid.CRAFTING); registration.addRecipeTransferHandler(new CraftingTerminalTransferHandler(), RecipeTypes.CRAFTING);
registration.addUniversalRecipeTransferHandler(new CraftingModuleTransferHandler()); registration.addUniversalRecipeTransferHandler(new CraftingModuleTransferHandler());
} }
@ -50,7 +73,7 @@ public class JEIPrettyPipesPlugin implements IModPlugin {
var screen = event.getScreen(); var screen = event.getScreen();
if (!(screen instanceof ItemTerminalGui terminal)) if (!(screen instanceof ItemTerminalGui terminal))
return; 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(); var preferences = PlayerPrefs.get();
preferences.syncJei = !preferences.syncJei; preferences.syncJei = !preferences.syncJei;
preferences.save(); preferences.save();
@ -68,14 +91,14 @@ public class JEIPrettyPipesPlugin implements IModPlugin {
var sync = PlayerPrefs.get().syncJei; var sync = PlayerPrefs.get().syncJei;
if (event instanceof ScreenEvent.DrawScreenEvent.Post) { if (event instanceof ScreenEvent.DrawScreenEvent.Post) {
if (this.jeiSyncButton.isHoveredOrFocused()) 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) { } 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 @SubscribeEvent
public void onClientTick(ClientTickEvent event) { public void onClientTick(TickEvent.ClientTickEvent event) {
if (!PlayerPrefs.get().syncJei) if (!PlayerPrefs.get().syncJei)
return; return;
@ -99,4 +122,4 @@ public class JEIPrettyPipesPlugin implements IModPlugin {
filter.setFilterText(terminalText); filter.setFilterText(terminalText);
} }
} }
}*/ }

View file

@ -17,9 +17,7 @@ import net.minecraft.world.item.Items;
import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.NetworkEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -27,9 +25,9 @@ import java.util.stream.Collectors;
public class PacketGhostSlot { public class PacketGhostSlot {
private BlockPos pos; private BlockPos pos;
private Map<Integer, Entry> stacks; private List<Entry> stacks;
public PacketGhostSlot(BlockPos pos, Map<Integer, Entry> stacks) { public PacketGhostSlot(BlockPos pos, List<Entry> stacks) {
this.pos = pos; this.pos = pos;
this.stacks = stacks; this.stacks = stacks;
} }
@ -41,19 +39,17 @@ public class PacketGhostSlot {
public static PacketGhostSlot fromBytes(FriendlyByteBuf buf) { public static PacketGhostSlot fromBytes(FriendlyByteBuf buf) {
var packet = new PacketGhostSlot(); var packet = new PacketGhostSlot();
packet.pos = buf.readBlockPos(); packet.pos = buf.readBlockPos();
packet.stacks = new HashMap<>(); packet.stacks = new ArrayList<>();
for (var i = buf.readInt(); i > 0; i--) for (var i = buf.readInt(); i > 0; i--)
packet.stacks.put(buf.readInt(), new Entry(buf)); packet.stacks.add(new Entry(buf));
return packet; return packet;
} }
public static void toBytes(PacketGhostSlot packet, FriendlyByteBuf buf) { public static void toBytes(PacketGhostSlot packet, FriendlyByteBuf buf) {
buf.writeBlockPos(packet.pos); buf.writeBlockPos(packet.pos);
buf.writeInt(packet.stacks.size()); buf.writeInt(packet.stacks.size());
for (var entry : packet.stacks.entrySet()) { for (var entry : packet.stacks)
buf.writeInt(entry.getKey()); entry.write(buf);
entry.getValue().write(buf);
}
} }
@SuppressWarnings("Convert2Lambda") @SuppressWarnings("Convert2Lambda")

View file

@ -58,7 +58,7 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity {
return this.craftItems.getStackInSlot(slot).isEmpty() && !this.ghostItems.getStackInSlot(slot).isEmpty(); return this.craftItems.getStackInSlot(slot).isEmpty() && !this.ghostItems.getStackInSlot(slot).isEmpty();
} }
public void setGhostItems(Map<Integer, PacketGhostSlot.Entry> stacks) { public void setGhostItems(List<PacketGhostSlot.Entry> stacks) {
this.updateItems(); this.updateItems();
for (var i = 0; i < this.ghostItems.getSlots(); i++) { for (var i = 0; i < this.ghostItems.getSlots(); i++) {
var items = stacks.get(i).getStacks(); var items = stacks.get(i).getStacks();
@ -95,9 +95,9 @@ public class CraftingTerminalBlockEntity extends ItemTerminalBlockEntity {
} }
if (!this.level.isClientSide) { if (!this.level.isClientSide) {
Map<Integer, PacketGhostSlot.Entry> clients = new HashMap<>(); List<PacketGhostSlot.Entry> clients = new ArrayList<>();
for (var i = 0; i < this.ghostItems.getSlots(); i++) 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)); PacketHandler.sendToAllLoaded(this.level, this.getBlockPos(), new PacketGhostSlot(this.getBlockPos(), clients));
} }
} }