Skip to content

Commit cba4806

Browse files
committed
ability to open synced ui from client & baubles test
1 parent 573080d commit cba4806

File tree

14 files changed

+186
-44
lines changed

14 files changed

+186
-44
lines changed

src/main/java/com/cleanroommc/modularui/ClientProxy.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
package com.cleanroommc.modularui;
22

33
import com.cleanroommc.modularui.drawable.DrawableSerialization;
4+
import com.cleanroommc.modularui.factory.GuiFactories;
5+
import com.cleanroommc.modularui.factory.inventory.InventoryTypes;
46
import com.cleanroommc.modularui.holoui.HoloScreenEntity;
57
import com.cleanroommc.modularui.holoui.ScreenEntityRender;
68
import com.cleanroommc.modularui.keybind.KeyBindHandler;
79
import com.cleanroommc.modularui.overlay.OverlayManager;
810
import com.cleanroommc.modularui.screen.ClientScreenHandler;
911
import com.cleanroommc.modularui.test.EventHandler;
1012
import com.cleanroommc.modularui.test.OverlayTest;
13+
import com.cleanroommc.modularui.test.TestItem;
1114
import com.cleanroommc.modularui.theme.ThemeManager;
1215
import com.cleanroommc.modularui.theme.ThemeReloadCommand;
1316

1417
import net.minecraft.client.Minecraft;
1518
import net.minecraft.client.resources.IReloadableResourceManager;
19+
import net.minecraft.client.settings.KeyBinding;
1620
import net.minecraft.util.Timer;
1721
import net.minecraftforge.client.ClientCommandHandler;
22+
import net.minecraftforge.client.settings.KeyConflictContext;
1823
import net.minecraftforge.common.MinecraftForge;
24+
import net.minecraftforge.fml.client.registry.ClientRegistry;
1925
import net.minecraftforge.fml.client.registry.RenderingRegistry;
2026
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
2127
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
28+
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
29+
import net.minecraftforge.fml.common.gameevent.InputEvent;
2230
import net.minecraftforge.fml.relauncher.Side;
2331
import net.minecraftforge.fml.relauncher.SideOnly;
2432

33+
import org.lwjgl.input.Keyboard;
34+
2535
@SideOnly(Side.CLIENT)
2636
@SuppressWarnings("unused")
2737
public class ClientProxy extends CommonProxy {
2838

2939
private final Timer timer60Fps = new Timer(60f);
40+
public static KeyBinding testKey;
3041

3142
@Override
3243
void preInit(FMLPreInitializationEvent event) {
@@ -38,6 +49,8 @@ void preInit(FMLPreInitializationEvent event) {
3849

3950
if (ModularUIConfig.enableTestGuis) {
4051
MinecraftForge.EVENT_BUS.register(EventHandler.class);
52+
testKey = new KeyBinding("key.test", KeyConflictContext.IN_GAME, Keyboard.KEY_NUMPAD4, "key.categories.modularui");
53+
ClientRegistry.registerKeyBinding(testKey);
4154
}
4255
if (ModularUIConfig.enableTestOverlays) {
4356
OverlayTest.init();
@@ -60,6 +73,19 @@ void postInit(FMLPostInitializationEvent event) {
6073
((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(new ThemeManager());
6174
}
6275

76+
@SubscribeEvent
77+
public void onKeyboard(InputEvent.KeyInputEvent event) {
78+
if (testKey.isPressed() && ModularUI.isBaubleLoaded()) {
79+
InventoryTypes.BAUBLES.visitAll(Minecraft.getMinecraft().player, (type, index, stack) -> {
80+
if (stack.getItem() instanceof TestItem) {
81+
GuiFactories.playerInventory().openFromBaublesClient(index);
82+
return true;
83+
}
84+
return false;
85+
});
86+
}
87+
}
88+
6389
@Override
6490
public Timer getTimer60Fps() {
6591
return this.timer60Fps;

src/main/java/com/cleanroommc/modularui/CommonProxy.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.cleanroommc.modularui;
22

3-
import com.cleanroommc.modularui.factory.*;
3+
import com.cleanroommc.modularui.factory.GuiFactories;
4+
import com.cleanroommc.modularui.factory.GuiManager;
45
import com.cleanroommc.modularui.holoui.HoloScreenEntity;
56
import com.cleanroommc.modularui.network.NetworkHandler;
67
import com.cleanroommc.modularui.screen.ModularContainer;
78
import com.cleanroommc.modularui.test.ItemEditorGui;
89
import com.cleanroommc.modularui.test.TestBlock;
9-
1010
import com.cleanroommc.modularui.value.sync.ModularSyncManager;
1111

1212
import net.minecraft.util.Timer;
@@ -29,7 +29,7 @@
2929
public class CommonProxy {
3030

3131
void preInit(FMLPreInitializationEvent event) {
32-
MinecraftForge.EVENT_BUS.register(CommonProxy.class);
32+
MinecraftForge.EVENT_BUS.register(this);
3333
MinecraftForge.EVENT_BUS.register(GuiManager.class);
3434

3535
if (ModularUIConfig.enableTestGuis) {
@@ -54,7 +54,7 @@ public Timer getTimer60Fps() {
5454
}
5555

5656
@SubscribeEvent
57-
public static void registerBlocks(RegistryEvent.Register<EntityEntry> event) {
57+
public void registerBlocks(RegistryEvent.Register<EntityEntry> event) {
5858
IForgeRegistry<EntityEntry> registry = event.getRegistry();
5959
registry.register(EntityEntryBuilder.create()
6060
.id("modular_screen", 0)
@@ -65,7 +65,7 @@ public static void registerBlocks(RegistryEvent.Register<EntityEntry> event) {
6565
}
6666

6767
@SubscribeEvent
68-
public static void onCloseContainer(PlayerContainerEvent.Open event) {
68+
public void onCloseContainer(PlayerContainerEvent.Open event) {
6969
if (event.getContainer() instanceof ModularContainer container) {
7070
ModularSyncManager syncManager = container.getSyncManager();
7171
if (syncManager != null) {
@@ -75,7 +75,7 @@ public static void onCloseContainer(PlayerContainerEvent.Open event) {
7575
}
7676

7777
@SubscribeEvent
78-
public static void onConfigChange(ConfigChangedEvent.OnConfigChangedEvent event) {
78+
public void onConfigChange(ConfigChangedEvent.OnConfigChangedEvent event) {
7979
if (event.getModID().equals(ModularUI.ID)) {
8080
ConfigManager.sync(ModularUI.ID, Config.Type.INSTANCE);
8181
}

src/main/java/com/cleanroommc/modularui/factory/AbstractUIFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.cleanroommc.modularui.api.UIFactory;
55
import com.cleanroommc.modularui.screen.ModularPanel;
66
import com.cleanroommc.modularui.screen.ModularScreen;
7-
87
import com.cleanroommc.modularui.screen.UISettings;
98
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
109

10+
import net.minecraft.client.entity.EntityPlayerSP;
1111
import net.minecraft.entity.player.EntityPlayer;
1212
import net.minecraft.entity.player.EntityPlayerMP;
1313

@@ -20,7 +20,13 @@ public abstract class AbstractUIFactory<T extends GuiData> implements UIFactory<
2020
protected static EntityPlayerMP verifyServerSide(EntityPlayer player) {
2121
if (player == null) throw new NullPointerException("Can't open UI for null player!");
2222
if (player instanceof EntityPlayerMP entityPlayerMP) return entityPlayerMP;
23-
throw new IllegalStateException("Synced GUI must be opened from server side!");
23+
throw new IllegalStateException("Expected server player to open UI on server!");
24+
}
25+
26+
protected static EntityPlayerSP verifyClientSide(EntityPlayer player) {
27+
if (player == null) throw new NullPointerException("Can't open UI for null player!");
28+
if (player instanceof EntityPlayerSP entityPlayerMP) return entityPlayerMP;
29+
throw new IllegalStateException("Expected client player to open UI on client side!");
2430
}
2531

2632
private final String name;

src/main/java/com/cleanroommc/modularui/factory/GuiManager.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import com.cleanroommc.modularui.api.UIFactory;
77
import com.cleanroommc.modularui.network.NetworkHandler;
88
import com.cleanroommc.modularui.network.packets.OpenGuiPacket;
9-
import com.cleanroommc.modularui.screen.*;
9+
import com.cleanroommc.modularui.screen.GuiContainerWrapper;
10+
import com.cleanroommc.modularui.screen.GuiScreenWrapper;
11+
import com.cleanroommc.modularui.screen.ModularContainer;
12+
import com.cleanroommc.modularui.screen.ModularPanel;
13+
import com.cleanroommc.modularui.screen.ModularScreen;
14+
import com.cleanroommc.modularui.screen.UISettings;
1015
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
1116
import com.cleanroommc.modularui.widget.WidgetTree;
1217

@@ -28,6 +33,7 @@
2833
import io.netty.buffer.Unpooled;
2934
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
3035
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
36+
import org.jetbrains.annotations.ApiStatus;
3137
import org.jetbrains.annotations.NotNull;
3238

3339
import java.util.ArrayList;
@@ -90,8 +96,9 @@ public static <T extends GuiData> void open(@NotNull UIFactory<T> factory, @NotN
9096
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, container));
9197
}
9298

99+
@ApiStatus.Internal
93100
@SideOnly(Side.CLIENT)
94-
public static <T extends GuiData> void open(int windowId, @NotNull UIFactory<T> factory, @NotNull PacketBuffer data, @NotNull EntityPlayerSP player) {
101+
public static <T extends GuiData> void openFromClient(int windowId, @NotNull UIFactory<T> factory, @NotNull PacketBuffer data, @NotNull EntityPlayerSP player) {
95102
T guiData = factory.readGuiData(player, data);
96103
UISettings settings = new UISettings();
97104
settings.defaultCanInteractWith(factory, guiData);
@@ -112,6 +119,13 @@ public static <T extends GuiData> void open(int windowId, @NotNull UIFactory<T>
112119
player.openContainer = guiContainer.inventorySlots;
113120
}
114121

122+
@SideOnly(Side.CLIENT)
123+
public static <T extends GuiData> void openFromClient(@NotNull UIFactory<T> factory, @NotNull T guiData) {
124+
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
125+
factory.writeGuiData(guiData, buffer);
126+
NetworkHandler.sendToServer(new OpenGuiPacket<>(0, factory, buffer));
127+
}
128+
115129
@SideOnly(Side.CLIENT)
116130
static void openScreen(ModularScreen screen, UISettings settings) {
117131
screen.getContext().setSettings(settings);

src/main/java/com/cleanroommc/modularui/factory/PlayerInventoryGuiFactory.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import com.cleanroommc.modularui.ModularUI;
44
import com.cleanroommc.modularui.api.IGuiHolder;
55
import com.cleanroommc.modularui.factory.inventory.InventoryType;
6-
76
import com.cleanroommc.modularui.factory.inventory.InventoryTypes;
7+
import com.cleanroommc.modularui.utils.Platform;
88

99
import net.minecraft.entity.player.EntityPlayer;
1010
import net.minecraft.network.PacketBuffer;
11-
1211
import net.minecraft.util.EnumHand;
12+
import net.minecraftforge.fml.relauncher.Side;
13+
import net.minecraftforge.fml.relauncher.SideOnly;
1314

1415
import org.jetbrains.annotations.NotNull;
1516

@@ -40,6 +41,30 @@ public void open(EntityPlayer player, InventoryType type, int index) {
4041
GuiManager.open(this, new PlayerInventoryGuiData(player, type, index), verifyServerSide(player));
4142
}
4243

44+
@SideOnly(Side.CLIENT)
45+
public void openFromPlayerInventoryClient(int index) {
46+
GuiManager.openFromClient(this, new PlayerInventoryGuiData(Platform.getClientPlayer(), InventoryTypes.PLAYER, index));
47+
}
48+
49+
@SideOnly(Side.CLIENT)
50+
public void openFromHandClient(EnumHand hand) {
51+
openFromPlayerInventoryClient(hand == EnumHand.OFF_HAND ? 40 : Platform.getClientPlayer().inventory.currentItem);
52+
}
53+
54+
@SideOnly(Side.CLIENT)
55+
public void openFromBaublesClient(int index) {
56+
if (!ModularUI.isBaubleLoaded()) {
57+
throw new IllegalArgumentException("Can't open UI for baubles item when bauble is not loaded!");
58+
}
59+
GuiManager.openFromClient(
60+
this, new PlayerInventoryGuiData(Platform.getClientPlayer(), InventoryTypes.BAUBLES, index));
61+
}
62+
63+
@SideOnly(Side.CLIENT)
64+
public void openClient(InventoryType type, int index) {
65+
GuiManager.openFromClient(this, new PlayerInventoryGuiData(Platform.getClientPlayer(), type, index));
66+
}
67+
4368
private PlayerInventoryGuiFactory() {
4469
super("mui:player_inv");
4570
}

src/main/java/com/cleanroommc/modularui/factory/SidedTileEntityGuiFactory.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.cleanroommc.modularui.factory;
22

33
import com.cleanroommc.modularui.api.IGuiHolder;
4+
import com.cleanroommc.modularui.utils.Platform;
45

56
import net.minecraft.entity.player.EntityPlayer;
67
import net.minecraft.entity.player.EntityPlayerMP;
78
import net.minecraft.network.PacketBuffer;
89
import net.minecraft.tileentity.TileEntity;
910
import net.minecraft.util.EnumFacing;
1011
import net.minecraft.util.math.BlockPos;
12+
import net.minecraftforge.fml.relauncher.Side;
13+
import net.minecraftforge.fml.relauncher.SideOnly;
1114

1215
import org.jetbrains.annotations.NotNull;
1316

@@ -19,15 +22,8 @@ public class SidedTileEntityGuiFactory extends AbstractUIFactory<SidedPosGuiData
1922

2023
public <T extends TileEntity & IGuiHolder<SidedPosGuiData>> void open(EntityPlayer player, T tile, EnumFacing facing) {
2124
Objects.requireNonNull(player);
22-
Objects.requireNonNull(tile);
2325
Objects.requireNonNull(facing);
24-
if (tile.isInvalid()) {
25-
throw new IllegalArgumentException("Can't open invalid TileEntity GUI!");
26-
}
27-
if (player.world != tile.getWorld()) {
28-
throw new IllegalArgumentException("TileEntity must be in same dimension as the player!");
29-
}
30-
BlockPos pos = tile.getPos();
26+
BlockPos pos = TileEntityGuiFactory.getPosFromTile(tile);
3127
SidedPosGuiData data = new SidedPosGuiData(player, pos.getX(), pos.getY(), pos.getZ(), facing);
3228
GuiManager.open(this, data, (EntityPlayerMP) player);
3329
}
@@ -40,6 +36,22 @@ public void open(EntityPlayer player, BlockPos pos, EnumFacing facing) {
4036
GuiManager.open(this, data, (EntityPlayerMP) player);
4137
}
4238

39+
@SideOnly(Side.CLIENT)
40+
public <T extends TileEntity & IGuiHolder<SidedPosGuiData>> void openClient(T tile, EnumFacing facing) {
41+
Objects.requireNonNull(facing);
42+
BlockPos pos = TileEntityGuiFactory.getPosFromTile(tile);
43+
SidedPosGuiData data = new SidedPosGuiData(Platform.getClientPlayer(), pos.getX(), pos.getY(), pos.getZ(), facing);
44+
GuiManager.openFromClient(this, data);
45+
}
46+
47+
@SideOnly(Side.CLIENT)
48+
public void openClient(BlockPos pos, EnumFacing facing) {
49+
Objects.requireNonNull(pos);
50+
Objects.requireNonNull(facing);
51+
SidedPosGuiData data = new SidedPosGuiData(Platform.getClientPlayer(), pos.getX(), pos.getY(), pos.getZ(), facing);
52+
GuiManager.openFromClient(this, data);
53+
}
54+
4355
private SidedTileEntityGuiFactory() {
4456
super("mui:sided_tile");
4557
}

src/main/java/com/cleanroommc/modularui/factory/SimpleGuiFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.cleanroommc.modularui.factory;
22

33
import com.cleanroommc.modularui.api.IGuiHolder;
4+
import com.cleanroommc.modularui.api.MCHelper;
45

56
import net.minecraft.entity.player.EntityPlayer;
67
import net.minecraft.entity.player.EntityPlayerMP;
78
import net.minecraft.network.PacketBuffer;
9+
import net.minecraftforge.fml.relauncher.Side;
10+
import net.minecraftforge.fml.relauncher.SideOnly;
811

912
import org.jetbrains.annotations.NotNull;
1013

@@ -50,6 +53,11 @@ public void open(EntityPlayerMP player) {
5053
GuiManager.open(this, new GuiData(player), player);
5154
}
5255

56+
@SideOnly(Side.CLIENT)
57+
public void openClient() {
58+
GuiManager.openFromClient(this, new GuiData(MCHelper.getPlayer()));
59+
}
60+
5361
@Override
5462
public void writeGuiData(GuiData guiData, PacketBuffer buffer) {}
5563

src/main/java/com/cleanroommc/modularui/factory/TileEntityGuiFactory.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.cleanroommc.modularui.factory;
22

33
import com.cleanroommc.modularui.api.IGuiHolder;
4+
import com.cleanroommc.modularui.api.MCHelper;
5+
import com.cleanroommc.modularui.utils.Platform;
46

57
import net.minecraft.entity.player.EntityPlayer;
68
import net.minecraft.entity.player.EntityPlayerMP;
79
import net.minecraft.network.PacketBuffer;
810
import net.minecraft.tileentity.TileEntity;
911
import net.minecraft.util.math.BlockPos;
12+
import net.minecraftforge.fml.relauncher.Side;
13+
import net.minecraftforge.fml.relauncher.SideOnly;
1014

1115
import org.jetbrains.annotations.NotNull;
1216

@@ -22,14 +26,7 @@ private TileEntityGuiFactory() {
2226

2327
public <T extends TileEntity & IGuiHolder<PosGuiData>> void open(EntityPlayer player, T tile) {
2428
Objects.requireNonNull(player);
25-
Objects.requireNonNull(tile);
26-
if (tile.isInvalid()) {
27-
throw new IllegalArgumentException("Can't open invalid TileEntity GUI!");
28-
}
29-
if (player.world != tile.getWorld()) {
30-
throw new IllegalArgumentException("TileEntity must be in same dimension as the player!");
31-
}
32-
BlockPos pos = tile.getPos();
29+
BlockPos pos = getPosFromTile(tile);
3330
PosGuiData data = new PosGuiData(player, pos.getX(), pos.getY(), pos.getZ());
3431
GuiManager.open(this, data, (EntityPlayerMP) player);
3532
}
@@ -41,6 +38,18 @@ public void open(EntityPlayer player, BlockPos pos) {
4138
GuiManager.open(this, data, (EntityPlayerMP) player);
4239
}
4340

41+
@SideOnly(Side.CLIENT)
42+
public <T extends TileEntity & IGuiHolder<PosGuiData>> void openClient(T tile) {
43+
BlockPos pos = getPosFromTile(tile);
44+
GuiManager.openFromClient(this, new PosGuiData(MCHelper.getPlayer(), pos.getX(), pos.getY(), pos.getZ()));
45+
}
46+
47+
@SideOnly(Side.CLIENT)
48+
public void openClient(BlockPos pos) {
49+
Objects.requireNonNull(pos);
50+
GuiManager.openFromClient(this, new PosGuiData(MCHelper.getPlayer(), pos.getX(), pos.getY(), pos.getZ()));
51+
}
52+
4453
@Override
4554
public @NotNull IGuiHolder<PosGuiData> getGuiHolder(PosGuiData data) {
4655
return Objects.requireNonNull(castGuiHolder(data.getTileEntity()), "Found TileEntity is not a gui holder!");
@@ -62,4 +71,15 @@ public void writeGuiData(PosGuiData guiData, PacketBuffer buffer) {
6271
public @NotNull PosGuiData readGuiData(EntityPlayer player, PacketBuffer buffer) {
6372
return new PosGuiData(player, buffer.readVarInt(), buffer.readVarInt(), buffer.readVarInt());
6473
}
74+
75+
public static BlockPos getPosFromTile(TileEntity tile) {
76+
Objects.requireNonNull(tile);
77+
if (tile.isInvalid()) {
78+
throw new IllegalArgumentException("Can't open invalid TileEntity GUI!");
79+
}
80+
if (Platform.getClientPlayer().world != tile.getWorld()) {
81+
throw new IllegalArgumentException("TileEntity must be in same dimension as the player!");
82+
}
83+
return tile.getPos();
84+
}
6585
}

0 commit comments

Comments
 (0)