2020-05-08 21:19:32 +02:00
|
|
|
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;
|
2020-05-09 14:56:58 +02:00
|
|
|
import mezz.jei.api.constants.VanillaRecipeCategoryUid;
|
2020-10-18 15:40:02 +02:00
|
|
|
import mezz.jei.api.gui.handlers.IGuiContainerHandler;
|
|
|
|
import mezz.jei.api.registration.IGuiHandlerRegistration;
|
2020-05-09 14:56:58 +02:00
|
|
|
import mezz.jei.api.registration.IRecipeTransferRegistration;
|
2020-05-08 21:19:32 +02:00
|
|
|
import mezz.jei.api.runtime.IJeiRuntime;
|
2021-12-19 17:27:21 +01:00
|
|
|
import net.minecraft.ChatFormatting;
|
2020-05-08 21:19:32 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
2021-12-19 17:27:21 +01:00
|
|
|
import net.minecraft.client.gui.components.Button;
|
|
|
|
import net.minecraft.client.renderer.Rect2i;
|
|
|
|
import net.minecraft.network.chat.TextComponent;
|
|
|
|
import net.minecraft.network.chat.TranslatableComponent;
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2021-12-19 17:27:21 +01:00
|
|
|
import net.minecraftforge.client.event.ScreenEvent;
|
2020-05-08 21:19:32 +02:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.TickEvent.ClientTickEvent;
|
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
|
|
|
2020-10-20 00:52:56 +02:00
|
|
|
import java.util.ArrayList;
|
2020-10-18 15:40:02 +02:00
|
|
|
import java.util.List;
|
2020-05-08 21:19:32 +02:00
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:56:58 +02:00
|
|
|
@Override
|
|
|
|
public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) {
|
|
|
|
registration.addRecipeTransferHandler(new CraftingTerminalTransferHandler(), VanillaRecipeCategoryUid.CRAFTING);
|
2020-10-15 06:07:06 +02:00
|
|
|
registration.addUniversalRecipeTransferHandler(new CraftingModuleTransferHandler());
|
2020-05-09 14:56:58 +02:00
|
|
|
}
|
|
|
|
|
2020-10-18 15:40:02 +02:00
|
|
|
@Override
|
|
|
|
public void registerGuiHandlers(IGuiHandlerRegistration registration) {
|
2021-12-19 17:27:21 +01:00
|
|
|
registration.addGuiContainerHandler(ItemTerminalGui.class, new IGuiContainerHandler<>() {
|
2020-10-18 15:40:02 +02:00
|
|
|
@Override
|
2021-12-19 17:27:21 +01:00
|
|
|
public List<Rect2i> getGuiExtraAreas(ItemTerminalGui containerScreen) {
|
|
|
|
List<Rect2i> ret = new ArrayList<>();
|
2020-10-20 00:52:56 +02:00
|
|
|
// sorting buttons
|
2021-12-19 17:27:21 +01:00
|
|
|
ret.add(new Rect2i(containerScreen.getGuiLeft() - 22, containerScreen.getGuiTop(), 22, 64));
|
2020-10-20 00:52:56 +02:00
|
|
|
// crafting hud
|
|
|
|
if (containerScreen.currentlyCrafting != null && !containerScreen.currentlyCrafting.isEmpty())
|
2021-12-19 17:27:21 +01:00
|
|
|
ret.add(new Rect2i(containerScreen.getGuiLeft() + containerScreen.getXSize(), containerScreen.getGuiTop() + 4, 65, 89));
|
2020-10-20 00:52:56 +02:00
|
|
|
return ret;
|
2020-10-18 15:40:02 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:19:32 +02:00
|
|
|
@SubscribeEvent
|
2021-12-19 17:27:21 +01:00
|
|
|
public void onInitGui(ScreenEvent.InitScreenEvent.Post event) {
|
|
|
|
var screen = event.getScreen();
|
|
|
|
if (!(screen instanceof ItemTerminalGui terminal))
|
2020-05-08 21:19:32 +02:00
|
|
|
return;
|
2021-12-19 17:27:21 +01:00
|
|
|
terminal.addRenderableWidget(this.jeiSyncButton = new Button(terminal.getGuiLeft() - 22, terminal.getGuiTop() + 44, 20, 20, new TextComponent(""), button -> {
|
|
|
|
var preferences = PlayerPrefs.get();
|
|
|
|
preferences.syncJei = !preferences.syncJei;
|
|
|
|
preferences.save();
|
2020-05-08 21:19:32 +02:00
|
|
|
terminal.updateWidgets();
|
|
|
|
}));
|
|
|
|
if (PlayerPrefs.get().syncJei)
|
2021-12-19 17:27:21 +01:00
|
|
|
terminal.search.setValue(this.runtime.getIngredientFilter().getFilterText());
|
2020-05-08 21:19:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2021-12-19 17:27:21 +01:00
|
|
|
public void onRenderGui(ScreenEvent.DrawScreenEvent event) {
|
|
|
|
var screen = event.getScreen();
|
|
|
|
if (!(screen instanceof ItemTerminalGui terminal))
|
2020-05-08 21:19:32 +02:00
|
|
|
return;
|
2021-12-19 17:27:21 +01:00
|
|
|
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());
|
|
|
|
} else if (event instanceof ScreenEvent.DrawScreenEvent.Pre) {
|
|
|
|
this.jeiSyncButton.setMessage(new TextComponent((sync ? ChatFormatting.GREEN : ChatFormatting.RED) + "J"));
|
2020-05-08 21:19:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onClientTick(ClientTickEvent event) {
|
|
|
|
if (!PlayerPrefs.get().syncJei)
|
|
|
|
return;
|
|
|
|
|
2021-12-19 17:27:21 +01:00
|
|
|
var screen = Minecraft.getInstance().screen;
|
|
|
|
if (!(screen instanceof ItemTerminalGui terminal)) {
|
2020-05-08 21:19:32 +02:00
|
|
|
this.lastTerminalText = null;
|
|
|
|
this.lastJeiText = null;
|
|
|
|
return;
|
|
|
|
}
|
2021-12-19 17:27:21 +01:00
|
|
|
var filter = this.runtime.getIngredientFilter();
|
|
|
|
var terminalText = terminal.search.getValue();
|
|
|
|
var jeiText = filter.getFilterText();
|
2020-05-08 21:19:32 +02:00
|
|
|
|
|
|
|
if (!jeiText.equals(this.lastJeiText)) {
|
|
|
|
this.lastTerminalText = jeiText;
|
|
|
|
this.lastJeiText = jeiText;
|
2021-12-19 17:27:21 +01:00
|
|
|
terminal.search.setValue(jeiText);
|
2020-05-08 21:19:32 +02:00
|
|
|
} else if (!terminalText.equals(this.lastTerminalText)) {
|
|
|
|
this.lastTerminalText = terminalText;
|
|
|
|
this.lastJeiText = terminalText;
|
|
|
|
filter.setFilterText(terminalText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|