Skip to content

Commit f5a532e

Browse files
committed
fix #182
1 parent cf3462d commit f5a532e

File tree

8 files changed

+48
-27
lines changed

8 files changed

+48
-27
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.cleanroommc.modularui.screen.ModularContainer;
99
import com.cleanroommc.modularui.test.ItemEditorGui;
1010
import com.cleanroommc.modularui.test.TestBlock;
11-
import com.cleanroommc.modularui.value.sync.ModularSyncManager;
1211

1312
import net.minecraft.util.Timer;
1413
import net.minecraftforge.common.MinecraftForge;
@@ -67,12 +66,16 @@ public void registerBlocks(RegistryEvent.Register<EntityEntry> event) {
6766
}
6867

6968
@SubscribeEvent
70-
public void onCloseContainer(PlayerContainerEvent.Open event) {
69+
public void onOpenContainer(PlayerContainerEvent.Open event) {
7170
if (event.getContainer() instanceof ModularContainer container) {
72-
ModularSyncManager syncManager = container.getSyncManager();
73-
if (syncManager != null) {
74-
syncManager.onOpen();
75-
}
71+
container.onModularContainerOpened();
72+
}
73+
}
74+
75+
@SubscribeEvent
76+
public void onCloseContainer(PlayerContainerEvent.Close event) {
77+
if (event.getContainer() instanceof ModularContainer container) {
78+
container.onModularContainerClosed();
7679
}
7780
}
7881

src/main/java/com/cleanroommc/modularui/screen/ClientScreenHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private static void invalidateCurrentScreen() {
226226
if (lastMui != null) {
227227
((GuiScreenAccessor) lastMui.getGuiScreen()).setEventButton(-1);
228228
((GuiScreenAccessor) lastMui.getGuiScreen()).setLastMouseEvent(-1);
229-
lastMui.getScreen().getPanelManager().closeAll();
229+
lastMui.getScreen().getPanelManager().closeScreen();
230230
lastMui = null;
231231
}
232232
currentScreen = null;

src/main/java/com/cleanroommc/modularui/screen/ModularContainer.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.cleanroommc.bogosorter.api.ISortableContainer;
1414
import com.cleanroommc.bogosorter.api.ISortingContextBuilder;
1515

16+
import net.minecraft.client.gui.inventory.GuiContainer;
1617
import net.minecraft.entity.player.EntityPlayer;
1718
import net.minecraft.entity.player.InventoryPlayer;
1819
import net.minecraft.inventory.ClickType;
@@ -101,11 +102,21 @@ public ContainerAccessor acc() {
101102
}
102103

103104
@MustBeInvokedByOverriders
104-
@Override
105-
public void onContainerClosed(@NotNull EntityPlayer playerIn) {
106-
super.onContainerClosed(playerIn);
105+
public void onModularContainerOpened() {
106+
if (this.syncManager != null) {
107+
this.syncManager.onOpen();
108+
}
109+
}
110+
111+
/**
112+
* Called when this container closes. This is different to {@link Container#onContainerClosed(EntityPlayer)}, since that one is also
113+
* called from {@link GuiContainer#onGuiClosed()}, which means it is called even when the container may still exist.
114+
* This happens when a temporary client screen takes over (like JEI,NEI,etc.). This is only called when the container actually closes.
115+
*/
116+
@MustBeInvokedByOverriders
117+
public void onModularContainerClosed() {
107118
if (this.syncManager != null) {
108-
this.syncManager.onClose();
119+
this.syncManager.dispose();
109120
}
110121
}
111122

@@ -119,7 +130,7 @@ public void detectAndSendChanges() {
119130
this.init = false;
120131
}
121132

122-
@ApiStatus.Internal
133+
@MustBeInvokedByOverriders
123134
public void onUpdate() {
124135
// detectAndSendChanges is potentially called multiple times per tick, while this method is called exactly once per tick
125136
if (this.syncManager != null) {

src/main/java/com/cleanroommc/modularui/screen/ModularPanel.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,8 @@ public void onOpen(ModularScreen screen) {
244244
this.state = State.OPEN;
245245
}
246246

247-
boolean reopen(boolean strict) {
248-
if (this.state != State.CLOSED) {
249-
if (strict) throw new IllegalStateException();
250-
return false;
251-
}
247+
void reopen() {
252248
this.state = State.OPEN;
253-
return true;
254249
}
255250

256251
@MustBeInvokedByOverriders

src/main/java/com/cleanroommc/modularui/screen/PanelManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ boolean tryInit() {
6060
throw new IllegalStateException("Tried to reopen closed screen, but all panels are disposed!");
6161
}
6262
// set all stored panels to be open
63-
this.panels.forEach(p -> p.reopen(true));
63+
this.panels.forEach(ModularPanel::reopen);
6464
this.disposal.removeIf(this.panels::contains);
6565
setState(State.REOPENED);
6666
yield true;
@@ -226,8 +226,17 @@ public boolean closeAll() {
226226
return false;
227227
}
228228

229+
void closeScreen() {
230+
// only close the screen without closing the panels
231+
// this is useful when we expect the screen to reopen at some point and the sync managers are still available
232+
if (this.state.isOpen) {
233+
setState(State.CLOSED);
234+
this.screen.onClose();
235+
}
236+
}
237+
229238
private void finalizePanel(ModularPanel panel) {
230-
panel.onClose();
239+
if (panel.isOpen()) panel.onClose();
231240
if (!this.disposal.contains(panel)) {
232241
if (this.disposal.size() == DISPOSAL_CAPACITY) {
233242
this.disposal.removeFirst().dispose();
@@ -258,6 +267,8 @@ public void dispose() {
258267
setState(State.WAIT_DISPOSAL);
259268
return;
260269
}
270+
// make sure every panel gets closed before disposing
271+
this.panels.forEach(this::finalizePanel);
261272
setState(State.CLOSED);
262273
this.disposal.forEach(ModularPanel::dispose);
263274
this.disposal.clear();
@@ -449,7 +460,7 @@ public enum State {
449460
*/
450461
REOPENED(true),
451462
/**
452-
* Screen is closed after it was open.
463+
* Screen is closed after it was open. Panels may still be considered open in some cases.
453464
*/
454465
CLOSED(false),
455466
/**

src/main/java/com/cleanroommc/modularui/test/TestTile.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
158158

159159
Rectangle colorPickerBackground = new Rectangle().setColor(Color.RED.main);
160160
ModularPanel panel = new ModularPanel("test_tile");
161-
IPanelHandler panelSyncHandler = syncManager.panel("other_panel", this::openSecondWindow, true);
161+
IPanelHandler panelSyncHandler = syncManager.syncedPanel("other_panel", true, this::openSecondWindow);
162162
IPanelHandler colorPicker = IPanelHandler.simple(panel, (mainPanel, player) -> new ColorPickerDialog(colorPickerBackground::setColor, colorPickerBackground.getColor(), true)
163163
.setDraggable(true)
164164
.relative(panel)
@@ -502,8 +502,8 @@ public ModularPanel openSecondWindow(PanelSyncManager syncManager, IPanelHandler
502502
syncManager.registerSlotGroup(slotGroup);
503503
AtomicInteger number = new AtomicInteger(0);
504504
syncManager.syncValue("int_value", new IntSyncValue(number::get, number::set));
505-
IPanelHandler panelSyncHandler = syncManager.panel("other_panel_2", (syncManager1, syncHandler1) ->
506-
openThirdWindow(syncManager1, syncHandler1, number), true);
505+
IPanelHandler panelSyncHandler = syncManager.syncedPanel("other_panel_2", true, (syncManager1, syncHandler1) ->
506+
openThirdWindow(syncManager1, syncHandler1, number));
507507
panel.child(ButtonWidget.panelCloseButton())
508508
.child(new ButtonWidget<>()
509509
.size(10).top(14).right(4)

src/main/java/com/cleanroommc/modularui/test/TestTile2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ModularPanel buildUI(PosGuiData data, PanelSyncManager syncManager, UISet
5454
.slot(new ModularSlot(this.itemHandler, i)));
5555
}
5656
ModularPanel panel = ModularPanel.defaultPanel("test_tile_2", 176, 13 * 18 + 14 + 10 + 20);
57-
IPanelHandler otherPanel = syncManager.panel("2nd panel", (syncManager1, syncHandler) -> {
57+
IPanelHandler otherPanel = syncManager.syncedPanel("2nd panel", true, (syncManager1, syncHandler) -> {
5858
ModularPanel panel1 = new Dialog<>("Option Selection").setDisablePanelsBelow(false).setDraggable(true).size(4 * 18 + 8, 4 * 18 + 8);
5959
return panel1
6060
.child(SlotGroupWidget.builder()
@@ -66,7 +66,7 @@ public ModularPanel buildUI(PosGuiData data, PanelSyncManager syncManager, UISet
6666
.build()
6767
.pos(4, 4)
6868
);
69-
}, true);
69+
});
7070
return panel
7171
.bindPlayerInventory()
7272
.child(sw)

src/main/java/com/cleanroommc/modularui/value/sync/ModularSyncManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public void detectAndSendChanges(boolean init) {
6161
this.panelSyncManagerMap.values().forEach(psm -> psm.detectAndSendChanges(init));
6262
}
6363

64-
public void onClose() {
64+
public void dispose() {
6565
this.panelSyncManagerMap.values().forEach(PanelSyncManager::onClose);
66+
this.panelSyncManagerMap.clear();
6667
}
6768

6869
public void onOpen() {

0 commit comments

Comments
 (0)