Skip to content

Commit c69cec4

Browse files
authored
Sync refactor 2 (#56)
* start * add center() method * center panels by default * synced panel fixes * allow custom player slots * r * try fix close panel issue * sync closePanelInternal() to server * refactor client panel opening & remove deprecated methods * add container customizer * fix compile issue * fix container customizer stackoverflow * check contains for gui actions and jei ghost slots * support for non phantom oversized slots * remove default mode stuff
1 parent c06d3da commit c69cec4

49 files changed

Lines changed: 1148 additions & 655 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import com.cleanroommc.modularui.screen.ModularContainer;
1010
import com.cleanroommc.modularui.test.ItemEditorGui;
1111
import com.cleanroommc.modularui.test.TestBlock;
12-
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
12+
13+
import com.cleanroommc.modularui.value.sync.ModularSyncManager;
1314

1415
import net.minecraft.util.Timer;
1516
import net.minecraftforge.common.MinecraftForge;
@@ -72,7 +73,7 @@ public static void registerBlocks(RegistryEvent.Register<EntityEntry> event) {
7273
@SubscribeEvent
7374
public static void onCloseContainer(PlayerContainerEvent.Open event) {
7475
if (event.getContainer() instanceof ModularContainer container) {
75-
GuiSyncManager syncManager = container.getSyncManager();
76+
ModularSyncManager syncManager = container.getSyncManager();
7677
if (syncManager != null) {
7778
syncManager.onOpen();
7879
}

src/main/java/com/cleanroommc/modularui/api/IGuiHolder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import com.cleanroommc.modularui.factory.GuiData;
44
import com.cleanroommc.modularui.screen.ModularPanel;
55
import com.cleanroommc.modularui.screen.ModularScreen;
6-
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
6+
7+
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
78

89
import net.minecraftforge.fml.relauncher.Side;
910
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -18,7 +19,7 @@ public interface IGuiHolder<T extends GuiData> {
1819
* Only called on client side.
1920
*
2021
* @param data information about the creation context
21-
* @param mainPanel the panel created in {@link #buildUI(GuiData, GuiSyncManager)}
22+
* @param mainPanel the panel created in {@link #buildUI(GuiData, PanelSyncManager)}
2223
* @return a modular screen instance with the given panel
2324
*/
2425
@SideOnly(Side.CLIENT)
@@ -29,10 +30,10 @@ default ModularScreen createScreen(T data, ModularPanel mainPanel) {
2930
/**
3031
* Called on server and client. Create only the main panel here. Only here you can add sync handlers to widgets directly.
3132
* If the widget to be synced is not in this panel yet (f.e. in another panel) the sync handler must be registered here
32-
* with {@link GuiSyncManager}.
33+
* with {@link PanelSyncManager}.
3334
*
3435
* @param data information about the creation context
3536
* @param syncManager sync handler where widget sync handlers should be registered
3637
*/
37-
ModularPanel buildUI(T data, GuiSyncManager syncManager);
38+
ModularPanel buildUI(T data, PanelSyncManager syncManager);
3839
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.cleanroommc.modularui.api;
2+
3+
import com.cleanroommc.modularui.screen.ModularPanel;
4+
import com.cleanroommc.modularui.screen.SecondaryPanel;
5+
import com.cleanroommc.modularui.value.sync.PanelSyncHandler;
6+
7+
import org.jetbrains.annotations.ApiStatus;
8+
9+
/**
10+
* This class can handle opening and closing of a {@link ModularPanel}. It makes sure, that the same panel is not created multiple times and instead reused.
11+
* Using {@link #openPanel()} is the only way to open multiple panels.
12+
* Panels can be closed with {@link #closePanel()}, but also with {@link ModularPanel#closeIfOpen(boolean)} and {@link ModularPanel#animateClose()}.
13+
* They are the same. Synced panels must be registered in the main {@link com.cleanroommc.modularui.value.sync.PanelSyncManager PanelSyncManager}.
14+
* It is recommended to use {@link com.cleanroommc.modularui.value.sync.PanelSyncManager#panel(String, ModularPanel, PanelSyncHandler.IPanelBuilder) PanelSyncManager#panel(String, ModularPanel, PanelSyncHandler.IPanelBuilder)}.
15+
*/
16+
public interface IPanelHandler {
17+
18+
/**
19+
* Creates a synced panel handler. It MUST be registered in a {@link com.cleanroommc.modularui.value.sync.PanelSyncManager PanelSyncManager} on
20+
* both sides to work.
21+
*
22+
* @param mainPanel the main panel of the GUI
23+
* @param panelBuilder the panel builder, that will create the new panel. It must not return null or the main panel.
24+
* @return a synced panel handler.
25+
* @throws NullPointerException if the build panel of the builder is null
26+
* @throws IllegalArgumentException if the build panel of the builder is the main panel
27+
*/
28+
static IPanelHandler synced(ModularPanel mainPanel, PanelSyncHandler.IPanelBuilder panelBuilder) {
29+
return new PanelSyncHandler(mainPanel, panelBuilder);
30+
}
31+
32+
/**
33+
* Creates a non synced panel handler. Trying to use synced values anyway will result in a crash.
34+
*
35+
* @param mainPanel the main panel of the GUI
36+
* @param provider the panel builder, that will create the new panel. It must not return null or the main panel.
37+
* @return a simple panel handler.
38+
* @throws NullPointerException if the build panel of the builder is null
39+
* @throws IllegalArgumentException if the build panel of the builder is the main panel or there are synced values in the panel
40+
*/
41+
static IPanelHandler simple(ModularPanel mainPanel, SecondaryPanel.IPanelBuilder provider) {
42+
return new SecondaryPanel(mainPanel, provider);
43+
}
44+
45+
/**
46+
* Opens the panel. If there is no cached panel, one will be created.
47+
* Can be called on both sides if this handler is synced.
48+
*/
49+
void openPanel();
50+
51+
/**
52+
* Initiates the closing animation if the panel is open.
53+
* Can be called on both sides if this handler is synced.
54+
*/
55+
void closePanel();
56+
57+
/**
58+
* Called internally after the panel is closed.
59+
*/
60+
@ApiStatus.OverrideOnly
61+
void closePanelInternal();
62+
63+
/**
64+
* Deletes the current cached panel. Should not be used frequently.
65+
* This only works on non synced panels. Otherwise, it crashes.
66+
*
67+
* @throws UnsupportedOperationException if this handler is synced
68+
*/
69+
void deleteCachedPanel();
70+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cleanroommc.modularui.api;
2+
3+
import com.cleanroommc.modularui.value.sync.SyncHandler;
4+
5+
import net.minecraft.entity.player.EntityPlayer;
6+
7+
import org.jetbrains.annotations.ApiStatus;
8+
9+
public interface IPanelSyncManager {
10+
11+
@ApiStatus.Internal
12+
void detectAndSendChanges(boolean init);
13+
14+
void onClose();
15+
16+
void onOpen();
17+
18+
EntityPlayer getPlayer();
19+
20+
IPanelSyncManager syncValue(String key, int id, SyncHandler syncHandler);
21+
22+
}

src/main/java/com/cleanroommc/modularui/api/UIFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import com.cleanroommc.modularui.factory.GuiData;
44
import com.cleanroommc.modularui.screen.ModularPanel;
55
import com.cleanroommc.modularui.screen.ModularScreen;
6-
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
6+
7+
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
78

89
import net.minecraft.entity.player.EntityPlayer;
910
import net.minecraft.network.PacketBuffer;
@@ -37,13 +38,13 @@ public interface UIFactory<D extends GuiData> {
3738
* @return new main panel
3839
*/
3940
@ApiStatus.OverrideOnly
40-
ModularPanel createPanel(D guiData, GuiSyncManager syncManager);
41+
ModularPanel createPanel(D guiData, PanelSyncManager syncManager);
4142

4243
/**
4344
* Creates the screen for the GUI. Is only called on client side.
4445
*
4546
* @param guiData gui data
46-
* @param mainPanel main panel created in {@link #createPanel(GuiData, GuiSyncManager)}
47+
* @param mainPanel main panel created in {@link #createPanel(GuiData, PanelSyncManager)}
4748
* @return new main panel
4849
*/
4950
@SideOnly(Side.CLIENT)

src/main/java/com/cleanroommc/modularui/api/drawable/IDrawable.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.cleanroommc.modularui.api.drawable;
22

3-
import com.cleanroommc.modularui.api.ITheme;
43
import com.cleanroommc.modularui.drawable.Icon;
54
import com.cleanroommc.modularui.screen.viewport.GuiContext;
6-
import com.cleanroommc.modularui.theme.ThemeAPI;
75
import com.cleanroommc.modularui.theme.WidgetTheme;
8-
import com.cleanroommc.modularui.utils.Color;
96
import com.cleanroommc.modularui.widget.Widget;
107
import com.cleanroommc.modularui.widget.sizer.Area;
118

129
import net.minecraftforge.fml.relauncher.Side;
1310
import net.minecraftforge.fml.relauncher.SideOnly;
1411

1512
import com.google.gson.JsonObject;
16-
import org.jetbrains.annotations.ApiStatus;
1713

1814
/**
1915
* An object which can be drawn. This is mainly used for backgrounds and overlays in
@@ -104,18 +100,6 @@ default void drawAtZero(GuiContext context, Area area, WidgetTheme widgetTheme)
104100
draw(context, 0, 0, area.width, area.height, widgetTheme);
105101
}
106102

107-
/**
108-
* Applies a theme color before drawing. Do not call, only override.
109-
*
110-
* @param theme theme to apply color of
111-
* @param widgetTheme widget theme to apply color of
112-
*/
113-
@Deprecated
114-
@ApiStatus.ScheduledForRemoval(inVersion = "2.6.0")
115-
default void applyThemeColor(ITheme theme, WidgetTheme widgetTheme) {
116-
Color.setGlColorOpaque(Color.WHITE.main);
117-
}
118-
119103
/**
120104
* @return if theme color can be applied on this drawable
121105
*/
@@ -168,7 +152,7 @@ public DrawableWidget(IDrawable drawable) {
168152
@SideOnly(Side.CLIENT)
169153
@Override
170154
public void draw(GuiContext context, WidgetTheme widgetTheme) {
171-
this.drawable.drawAtZero(context, getArea());
155+
this.drawable.drawAtZero(context, getArea(), widgetTheme);
172156
}
173157
}
174158
}

src/main/java/com/cleanroommc/modularui/api/widget/IPositioned.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ default W align(Alignment alignment) {
376376
alignY(alignment.y);
377377
}
378378

379+
default W center() {
380+
return align(Alignment.Center);
381+
}
382+
379383
default W flex(Consumer<Flex> flexConsumer) {
380384
flexConsumer.accept(flex());
381385
return getThis();

src/main/java/com/cleanroommc/modularui/api/widget/ISynced.java

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

3-
import com.cleanroommc.modularui.value.sync.GuiSyncManager;
3+
import com.cleanroommc.modularui.value.sync.ModularSyncManager;
44
import com.cleanroommc.modularui.value.sync.SyncHandler;
55

66
import org.jetbrains.annotations.NotNull;
@@ -25,7 +25,7 @@ default W getThis() {
2525
*
2626
* @param syncManager sync manager
2727
*/
28-
void initialiseSyncHandler(GuiSyncManager syncManager);
28+
void initialiseSyncHandler(ModularSyncManager syncManager);
2929

3030
/**
3131
* Checks if the received sync handler is valid for this widget.
@@ -38,6 +38,13 @@ default boolean isValidSyncHandler(SyncHandler syncHandler) {
3838
return false;
3939
}
4040

41+
default <T> T castIfTypeElseNull(SyncHandler syncHandler, Class<T> clazz) {
42+
if (syncHandler != null && clazz.isAssignableFrom(syncHandler.getClass())) {
43+
return (T) syncHandler;
44+
}
45+
return null;
46+
}
47+
4148
/**
4249
* @return true if this widget has a valid sync handler
4350
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package com.cleanroommc.modularui.core.mixin;
22

3+
import net.minecraft.entity.player.EntityPlayer;
34
import net.minecraft.inventory.Container;
45

6+
import net.minecraft.inventory.IInventory;
7+
import net.minecraft.item.ItemStack;
8+
9+
import net.minecraft.world.World;
10+
511
import org.spongepowered.asm.mixin.Mixin;
612
import org.spongepowered.asm.mixin.gen.Accessor;
13+
import org.spongepowered.asm.mixin.gen.Invoker;
714

815
@Mixin(Container.class)
916
public interface ContainerAccessor {
1017

1118
@Accessor
1219
int getDragEvent();
20+
21+
@Invoker
22+
boolean invokeMergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection);
23+
24+
@Invoker
25+
void invokeClearContainer(EntityPlayer playerIn, World worldIn, IInventory inventoryIn);
1326
}

src/main/java/com/cleanroommc/modularui/drawable/IconRenderer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.cleanroommc.modularui.api.drawable.IIcon;
55
import com.cleanroommc.modularui.api.drawable.IKey;
66
import com.cleanroommc.modularui.screen.viewport.GuiContext;
7+
import com.cleanroommc.modularui.theme.WidgetTheme;
78
import com.cleanroommc.modularui.utils.Alignment;
89

910
import net.minecraft.client.Minecraft;
@@ -101,7 +102,7 @@ public void drawMeasuredLines(GuiContext context, List<IIcon> lines) {
101102
for (IIcon icon : lines) {
102103
int x = icon.getWidth() > 0 ? getStartX(icon.getWidth()) : this.x;
103104
if (!this.simulate) {
104-
icon.draw(context, x, y, maxWidth, icon.getHeight());
105+
icon.draw(context, x, y, maxWidth, icon.getHeight(), WidgetTheme.getDefault());
105106
}
106107
y += (int) ((icon.getHeight() + this.linePadding) * this.scale);
107108
}

0 commit comments

Comments
 (0)