From 0bb8fbaf5c32f293c4d8d3aba2d6e0dae259ccd1 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 8 May 2020 21:19:32 +0200 Subject: [PATCH] added jei syncing to the terminal --- .../compat/jei/JEIPrettyPipesPlugin.java | 106 ++++++++++++++++++ .../ellpeck/prettypipes/misc/PlayerPrefs.java | 1 + .../terminal/containers/ItemTerminalGui.java | 4 +- .../assets/prettypipes/lang/en_us.json | 4 +- 4 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/main/java/de/ellpeck/prettypipes/compat/jei/JEIPrettyPipesPlugin.java diff --git a/src/main/java/de/ellpeck/prettypipes/compat/jei/JEIPrettyPipesPlugin.java b/src/main/java/de/ellpeck/prettypipes/compat/jei/JEIPrettyPipesPlugin.java new file mode 100644 index 0000000..d832f04 --- /dev/null +++ b/src/main/java/de/ellpeck/prettypipes/compat/jei/JEIPrettyPipesPlugin.java @@ -0,0 +1,106 @@ +package de.ellpeck.prettypipes.compat.jei; + +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.runtime.IIngredientFilter; +import mezz.jei.api.runtime.IJeiRuntime; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.button.Button; +import net.minecraft.client.resources.I18n; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.text.TextFormatting; +import net.minecraftforge.client.event.GuiScreenEvent; +import net.minecraftforge.client.event.GuiScreenEvent.DrawScreenEvent; +import net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.event.TickEvent.ClientTickEvent; +import net.minecraftforge.event.world.PistonEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; + +import java.util.Locale; + +@JeiPlugin +public class JEIPrettyPipesPlugin implements IModPlugin { + + private IJeiRuntime runtime; + private String lastTerminalText; + private String lastJeiText; + private Button jeiSyncButton; + + public JEIPrettyPipesPlugin() { + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public ResourceLocation getPluginUid() { + return new ResourceLocation(PrettyPipes.ID, "jei_plugin"); + } + + @Override + public void onRuntimeAvailable(IJeiRuntime jeiRuntime) { + this.runtime = jeiRuntime; + } + + @SubscribeEvent + public void onInitGui(InitGuiEvent.Post event) { + Screen screen = event.getGui(); + if (!(screen instanceof ItemTerminalGui)) + return; + ItemTerminalGui terminal = (ItemTerminalGui) screen; + event.addWidget(this.jeiSyncButton = new Button(terminal.getGuiLeft() - 22, terminal.getGuiTop() + 44, 20, 20, "", button -> { + PlayerPrefs prefs = PlayerPrefs.get(); + prefs.syncJei = !prefs.syncJei; + prefs.save(); + terminal.updateWidgets(); + })); + if (PlayerPrefs.get().syncJei) + terminal.search.setText(this.runtime.getIngredientFilter().getFilterText()); + } + + @SubscribeEvent + public void onRenderGui(DrawScreenEvent event) { + Screen screen = event.getGui(); + if (!(screen instanceof ItemTerminalGui)) + return; + ItemTerminalGui terminal = (ItemTerminalGui) screen; + boolean sync = PlayerPrefs.get().syncJei; + if (event instanceof DrawScreenEvent.Post) { + if (this.jeiSyncButton.isHovered()) + terminal.renderTooltip(I18n.format("info." + PrettyPipes.ID + ".sync_jei." + (sync ? "on" : "off")), event.getMouseX(), event.getMouseY()); + } else if (event instanceof DrawScreenEvent.Pre) { + this.jeiSyncButton.setMessage((sync ? TextFormatting.GREEN : TextFormatting.RED) + "J"); + } + } + + @SubscribeEvent + public void onClientTick(ClientTickEvent event) { + if (!PlayerPrefs.get().syncJei) + return; + + Screen screen = Minecraft.getInstance().currentScreen; + if (!(screen instanceof ItemTerminalGui)) { + this.lastTerminalText = null; + this.lastJeiText = null; + return; + } + ItemTerminalGui terminal = (ItemTerminalGui) screen; + IIngredientFilter filter = this.runtime.getIngredientFilter(); + String terminalText = terminal.search.getText(); + String jeiText = filter.getFilterText(); + + if (!jeiText.equals(this.lastJeiText)) { + this.lastTerminalText = jeiText; + this.lastJeiText = jeiText; + terminal.search.setText(jeiText); + } else if (!terminalText.equals(this.lastTerminalText)) { + this.lastTerminalText = terminalText; + this.lastJeiText = terminalText; + filter.setFilterText(terminalText); + } + } +} diff --git a/src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java b/src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java index a665aa1..dcedb8c 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/PlayerPrefs.java @@ -16,6 +16,7 @@ public class PlayerPrefs { public ItemOrder terminalItemOrder = ItemOrder.AMOUNT; public boolean terminalAscending; + public boolean syncJei = true; public void save() { File file = getFile(); diff --git a/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java b/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java index 1c7826c..0350047 100644 --- a/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java +++ b/src/main/java/de/ellpeck/prettypipes/terminal/containers/ItemTerminalGui.java @@ -33,10 +33,10 @@ public class ItemTerminalGui extends ContainerScreen { private Button requestButton; private Button orderButton; private Button ascendingButton; - private TextFieldWidget search; private String lastSearchText; private int requestAmount = 1; private int scrollOffset; + public TextFieldWidget search; public ItemTerminalGui(ItemTerminalContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) { super(screenContainer, inv, titleIn); @@ -129,7 +129,7 @@ public class ItemTerminalGui extends ContainerScreen { this.updateWidgets(); } - private void updateWidgets() { + public void updateWidgets() { PlayerPrefs prefs = PlayerPrefs.get(); this.ascendingButton.setMessage(prefs.terminalAscending ? "^" : "v"); this.orderButton.setMessage(prefs.terminalItemOrder.name().substring(0, 1)); diff --git a/src/main/resources/assets/prettypipes/lang/en_us.json b/src/main/resources/assets/prettypipes/lang/en_us.json index 48af1ab..be4cb6d 100644 --- a/src/main/resources/assets/prettypipes/lang/en_us.json +++ b/src/main/resources/assets/prettypipes/lang/en_us.json @@ -55,5 +55,7 @@ "info.prettypipes.order.name": "Name", "info.prettypipes.order.mod": "Mod", "info.prettypipes.ascending": "Ascending", - "info.prettypipes.descending": "Descending" + "info.prettypipes.descending": "Descending", + "info.prettypipes.sync_jei.on": "Sync JEI Search", + "info.prettypipes.sync_jei.off": "Don't Sync JEI Search" } \ No newline at end of file