Skip to content

Commit

Permalink
Merge pull request #60 from kill05/7.2
Browse files Browse the repository at this point in the history
Fix vanilla server kick + button logic
  • Loading branch information
MartinSVK12 committed Jun 19, 2024
2 parents 6ec0670 + 77474f7 commit 234c427
Show file tree
Hide file tree
Showing 18 changed files with 294 additions and 532 deletions.
8 changes: 2 additions & 6 deletions src/main/java/turniplabs/halplibe/HalpLibe.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import turniplabs.halplibe.helper.*;
import turniplabs.halplibe.helper.gui.Guis;
import turniplabs.halplibe.helper.gui.packet.PacketGuiButtonClick;
import turniplabs.halplibe.helper.gui.packet.PacketOpenBlockGui;
import turniplabs.halplibe.helper.gui.packet.PacketOpenGui;
import turniplabs.halplibe.helper.gui.packet.PacketOpenItemGui;
Expand Down Expand Up @@ -88,11 +88,7 @@ public void onInitialize() {
NetworkHelper.register(PacketOpenGui.class, false, true);
NetworkHelper.register(PacketOpenItemGui.class, false, true);
NetworkHelper.register(PacketOpenBlockGui.class, false, true);
try {
Class.forName(Guis.class.getName()); // init vanilla guis
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
NetworkHelper.register(PacketGuiButtonClick.class, true, true);

LOGGER.info("HalpLibe initialized.");
}
Expand Down
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
@@ -0,0 +1,19 @@
package turniplabs.halplibe.helper.gui;

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());
}

}
182 changes: 0 additions & 182 deletions src/main/java/turniplabs/halplibe/helper/gui/Guis.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import net.minecraft.client.entity.player.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.player.inventory.Container;
import net.minecraft.server.entity.player.EntityPlayerMP;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import turniplabs.halplibe.helper.gui.packet.PacketOpenBlockGui;
Expand All @@ -14,10 +16,52 @@

public interface IGuiFactory {

/**
* Called to create a gui screen on a client.
*
* @param gui the registered gui that needs to create a gui
* @param player the player that will see the gui
* @param itemStack an item, in case the gui is an item gui
* @param x x coordinate, in case the gui is a block gui
* @param y y coordinate, in case the gui is a block gui
* @param z z coordinate, in case the gui is a block gui
* @return a new gui
*/
@ApiStatus.Internal
@NotNull GuiScreen createGui(@NotNull RegisteredGui gui, @NotNull EntityPlayerSP player, @Nullable ItemStack itemStack, int x, int y, int z);

/**
* Called to create a gui container on a server.
*
* @param gui the registered gui that needs to create a container
* @param player the player that will open the container
* @param itemStack an item, in case the gui is an item gui
* @param x x coordinate, in case the gui is a block gui
* @param y y coordinate, in case the gui is a block gui
* @param z z coordinate, in case the gui is a block gui
* @return a new container
*/
@ApiStatus.Internal
@Nullable Container createContainer(@NotNull RegisteredGui gui, @NotNull EntityPlayerMP player, @Nullable ItemStack itemStack, int x, int y, int z);

/**
* Called when a gui interaction happens.<p>
* The player and clicker parameter are not the same when a client acknowledges
* that another player has pressed a button.<p>
* As an example, if player1 clicked a button on a server,
* this method will be called on player2's client
* with player2 as the player argument and player1 as the clicker argument
*
* @param gui the registered gui that was clicked
* @param player the player that acknowledges that a player has pressed a button
* @param clicker the player that clicked the button
* @param buttonId the id of the button
*/
default void onButtonClick(@NotNull RegisteredGui gui, @NotNull EntityPlayer player, @NotNull EntityPlayer clicker, int buttonId) {

}

@ApiStatus.Internal
default @NotNull GuiScreen createGui(@NotNull RegisteredGui gui, @NotNull EntityPlayerSP player, @NotNull PacketOpenGui packet) {
if(!gui.isServerSide()) throw new IllegalStateException("Gui is client side!");

Expand All @@ -33,6 +77,7 @@ public interface IGuiFactory {
return createGui(gui, player, null, 0, -100, 0);
}

@ApiStatus.Internal
default @NotNull PacketOpenGui createGuiPacket(@NotNull RegisteredGui gui, int windowId, @NotNull EntityPlayerMP player, @Nullable ItemStack itemStack, int x, int y, int z) {
if(!gui.isServerSide()) throw new IllegalStateException("Gui is client side!");

Expand All @@ -47,6 +92,7 @@ public interface IGuiFactory {
return new PacketOpenGui(gui, windowId);
}

@ApiStatus.Internal
default void throwInvalidException(String message) {
throw new IllegalArgumentException(String.format("Invalid arguments for gui factory '%s': %s", getClass(), message));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ default GuiScreen createGui(@NotNull RegisteredGui gui, @NotNull EntityPlayerSP

@Override
default @Nullable Container createContainer(@NotNull RegisteredGui gui, @NotNull EntityPlayerMP player, @Nullable ItemStack itemStack, int x, int y, int z) {
if(y < 0) throwInvalidException("y value of block coordinates must be set to a positive value. This either means a wrong value has been used as an argument or that the wrong gui method has been called.");
return createContainer(gui, player, x, y, z);
}
}
Loading

0 comments on commit 234c427

Please sign in to comment.