Skip to content

Commit

Permalink
Fixed button click handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kill05 committed Jun 17, 2024
1 parent 619b962 commit dec317d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/main/java/turniplabs/halplibe/helper/gui/GuiHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -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<Integer> 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<GuiButton> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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<GuiButton> 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<Integer> 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<GuiButton> 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;
}
}
1 change: 1 addition & 0 deletions src/main/resources/halplibe.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"mixins.MinecraftMixin",
"mixins.NetClientHandlerMixin",
"mixins.commands.CommandsClientMixin",
"mixins.gui.GuiScreenMixin",
"mixins.models.BlockColorDispatcherMixin",
"mixins.models.BlockModelDispatcherMixin",
"mixins.models.EntityRenderDispatcherMixin",
Expand Down

0 comments on commit dec317d

Please sign in to comment.