diff --git a/src/main/java/turniplabs/halplibe/helper/gui/GuiHelper.java b/src/main/java/turniplabs/halplibe/helper/gui/GuiHelper.java index 4132b1c..0bc79a5 100644 --- a/src/main/java/turniplabs/halplibe/helper/gui/GuiHelper.java +++ b/src/main/java/turniplabs/halplibe/helper/gui/GuiHelper.java @@ -3,6 +3,7 @@ import net.minecraft.core.data.registry.Registry; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import turniplabs.halplibe.HalpLibe; import turniplabs.halplibe.helper.gui.factory.IGuiFactory; import turniplabs.halplibe.helper.gui.registered.RegisteredGui; @@ -29,7 +30,8 @@ public static RegisteredGui getGui(@NotNull String modId, @NotNull String guiId) return REGISTRY.getItem(modId + ":" + guiId); } - public static RegisteredGui getGui(@NotNull String namespace) { + public static RegisteredGui getGui(@Nullable String namespace) { + if(namespace == null) return null; return REGISTRY.getItem(namespace); } diff --git a/src/main/java/turniplabs/halplibe/helper/gui/registered/RegisteredGui.java b/src/main/java/turniplabs/halplibe/helper/gui/registered/RegisteredGui.java index d8c511a..55c5f61 100644 --- a/src/main/java/turniplabs/halplibe/helper/gui/registered/RegisteredGui.java +++ b/src/main/java/turniplabs/halplibe/helper/gui/registered/RegisteredGui.java @@ -1,15 +1,12 @@ package turniplabs.halplibe.helper.gui.registered; import net.minecraft.client.Minecraft; -import net.minecraft.client.entity.player.EntityClientPlayerMP; import net.minecraft.client.entity.player.EntityPlayerSP; -import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.core.block.entity.TileEntity; import net.minecraft.core.entity.player.EntityPlayer; import net.minecraft.core.item.ItemStack; import net.minecraft.core.player.inventory.Container; -import net.minecraft.core.util.helper.Listener; import net.minecraft.server.entity.player.EntityPlayerMP; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -21,11 +18,9 @@ import turniplabs.halplibe.helper.gui.factory.block.BlockGuiFactory; import turniplabs.halplibe.helper.gui.factory.block.TileGuiFactory; import turniplabs.halplibe.helper.gui.factory.item.ItemGuiFactory; -import turniplabs.halplibe.helper.gui.packet.PacketGuiButtonClick; import turniplabs.halplibe.helper.gui.packet.PacketOpenGui; +import turniplabs.halplibe.mixin.accessors.gui.GuiScreenAccessor; -import java.util.ArrayList; -import java.util.List; import java.util.Random; import java.util.regex.Pattern; @@ -85,8 +80,7 @@ protected void open(@NotNull EntityPlayer player, @Nullable ItemStack itemStack, if (player instanceof EntityPlayerSP) { EntityPlayerSP playerSP = (EntityPlayerSP) player; - GuiScreen gui = factory.createGui(this, playerSP, itemStack, x, y, z); - Minecraft.getMinecraft(RegisteredGui.class).displayGuiScreen(addButtonListeners(gui, playerSP)); + setupAndOpenGui(factory.createGui(this, playerSP, itemStack, x, y, z)); return; } @@ -110,39 +104,22 @@ protected void open(@NotNull EntityPlayer player, @Nullable ItemStack itemStack, HalpLibe.LOGGER.warn("Tried to open a gui for invalid EntityPlayer: " + player); } - private GuiScreen addButtonListeners(GuiScreen gui, EntityPlayerSP playerSP) { - List ids = new ArrayList<>(gui.controlList.size()); - for (GuiButton button : gui.controlList) { - if (ids.contains(button.id)) - throw new IllegalStateException(String.format("Found button in gui '%s' with duplicate id: '%s'.", gui, button.id)); - - final Listener oldListener = button.listener; - button.setListener(aButton -> { - if (playerSP instanceof EntityClientPlayerMP) { - EntityClientPlayerMP playerMP = (EntityClientPlayerMP) playerSP; - playerMP.sendQueue.addToSendQueue(new PacketGuiButtonClick(getNamespace(), aButton.id)); - } - - factory.onButtonPress(this, playerSP, aButton.id); - if(oldListener != null) oldListener.listen(aButton); - }); - - ids.add(button.id); - } - - return gui; - } - @ApiStatus.Internal public void handleOpenPacket(@NotNull PacketOpenGui packet) { Minecraft mc = Minecraft.getMinecraft(RegisteredGui.class); EntityPlayerSP player = mc.thePlayer; - GuiScreen gui = factory.createGui(this, player, packet); - - mc.displayGuiScreen(addButtonListeners(gui, player)); + setupAndOpenGui(factory.createGui(this, player, packet)); player.craftingInventory.windowId = packet.windowId; } + private void setupAndOpenGui(GuiScreen gui) { + ((GuiScreenAccessor) gui).setRegisteredGui(this); + Minecraft mc = Minecraft.getMinecraft(RegisteredGui.class); + mc.displayGuiScreen(gui); + } + + + public String getModId() { return modId; diff --git a/src/main/java/turniplabs/halplibe/mixin/accessors/gui/GuiScreenAccessor.java b/src/main/java/turniplabs/halplibe/mixin/accessors/gui/GuiScreenAccessor.java new file mode 100644 index 0000000..3ea301e --- /dev/null +++ b/src/main/java/turniplabs/halplibe/mixin/accessors/gui/GuiScreenAccessor.java @@ -0,0 +1,20 @@ +package turniplabs.halplibe.mixin.accessors.gui; + +import turniplabs.halplibe.helper.gui.GuiHelper; +import turniplabs.halplibe.helper.gui.registered.RegisteredGui; + +public interface GuiScreenAccessor { + + void setGuiNamespace(String namespace); + + String getGuiNamespace(); + + default void setRegisteredGui(RegisteredGui gui) { + setGuiNamespace(gui.getNamespace()); + } + + default RegisteredGui getRegisteredGui() { + return GuiHelper.getGui(getGuiNamespace()); + } + +} diff --git a/src/main/java/turniplabs/halplibe/mixin/mixins/gui/GuiScreenMixin.java b/src/main/java/turniplabs/halplibe/mixin/mixins/gui/GuiScreenMixin.java new file mode 100644 index 0000000..2c3493e --- /dev/null +++ b/src/main/java/turniplabs/halplibe/mixin/mixins/gui/GuiScreenMixin.java @@ -0,0 +1,74 @@ +package turniplabs.halplibe.mixin.mixins.gui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.player.EntityClientPlayerMP; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.core.util.helper.Listener; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import turniplabs.halplibe.helper.gui.packet.PacketGuiButtonClick; +import turniplabs.halplibe.helper.gui.registered.RegisteredGui; +import turniplabs.halplibe.mixin.accessors.gui.GuiScreenAccessor; + +import java.util.ArrayList; +import java.util.List; + +@SuppressWarnings("AddedMixinMembersNamePattern") +@Mixin( + value = GuiScreen.class, + remap = false +) +public class GuiScreenMixin implements GuiScreenAccessor { + + @Unique + private String guiNamespace; + + @Shadow + public List controlList; + + + @Inject( + method = "init", + at = @At("RETURN") + ) + public void injectInit(CallbackInfo ci) { + Minecraft mc = Minecraft.getMinecraft(GuiScreenMixin.class); + RegisteredGui gui = getRegisteredGui(); + if(gui == null) return; + + List ids = new ArrayList<>(controlList.size()); + for (GuiButton button : controlList) { + if (ids.contains(button.id)) + throw new IllegalStateException(String.format("Found button in gui '%s' with duplicate id: '%s'.", gui, button.id)); + + final Listener oldListener = button.listener; + button.setListener(aButton -> { + if (mc.thePlayer instanceof EntityClientPlayerMP) { + EntityClientPlayerMP playerMP = (EntityClientPlayerMP) mc.thePlayer; + playerMP.sendQueue.addToSendQueue(new PacketGuiButtonClick(getGuiNamespace(), aButton.id)); + } + + gui.getFactory().onButtonPress(gui, mc.thePlayer, aButton.id); + if(oldListener != null) oldListener.listen(aButton); + }); + + ids.add(button.id); + } + } + + @Override + public void setGuiNamespace(String namespace) { + if(guiNamespace != null) throw new IllegalStateException("Gui namespace has already been set!"); + this.guiNamespace = namespace; + } + + @Override + public String getGuiNamespace() { + return guiNamespace; + } +} diff --git a/src/main/resources/halplibe.mixins.json b/src/main/resources/halplibe.mixins.json index 763b684..656f6f9 100644 --- a/src/main/resources/halplibe.mixins.json +++ b/src/main/resources/halplibe.mixins.json @@ -32,6 +32,7 @@ "mixins.MinecraftMixin", "mixins.NetClientHandlerMixin", "mixins.commands.CommandsClientMixin", + "mixins.gui.GuiScreenMixin", "mixins.models.BlockColorDispatcherMixin", "mixins.models.BlockModelDispatcherMixin", "mixins.models.EntityRenderDispatcherMixin",