Skip to content

Commit 5fe7af9

Browse files
committed
Add onScreenHandlerClose and onPlayerClose
1 parent aee2dda commit 5fe7af9

File tree

9 files changed

+67
-278
lines changed

9 files changed

+67
-278
lines changed

gradle.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G
33

44
# Fabric Properties
55
# check these on https://fabricmc.net/versions.html
6-
minecraft_version=1.21.7
7-
yarn_mappings=1.21.7+build.1
6+
minecraft_version=1.21.8
7+
yarn_mappings=1.21.8+build.1
88
loader_version=0.16.14
99

1010
#Fabric api
11-
fabric_version=0.128.2+1.21.7
11+
fabric_version=0.129.0+1.21.8
1212

1313
# Mod Properties
14-
mod_version = 1.10.1+1.21.7
14+
mod_version = 1.10.2+1.21.8
1515
maven_group = eu.pb4
1616
archives_base_name = sgui
1717

src/main/java/eu/pb4/sgui/api/gui/GuiInterface.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import net.minecraft.screen.ScreenHandlerType;
77
import net.minecraft.server.network.ServerPlayerEntity;
88
import net.minecraft.text.Text;
9-
import org.jetbrains.annotations.ApiStatus;
109
import org.jetbrains.annotations.Nullable;
1110

1211
@SuppressWarnings({"unused"})
@@ -71,10 +70,9 @@ public interface GuiInterface {
7170
/**
7271
* Used internally for closing the gui.
7372
*
74-
* @param alreadyClosed Is set to true, if gui's ScreenHandler is already closed
73+
* @param alreadyClosed Is set to true, if gui's ScreenHandler is already closed and close packet shouldn't be sent
7574
* @see GuiInterface#onClose()
7675
*/
77-
@ApiStatus.Internal
7876
void close(boolean alreadyClosed);
7977

8078
/**
@@ -105,11 +103,27 @@ default void onOpen() {
105103
}
106104

107105
/**
108-
* Executes when the screen is closed
106+
* Executes when the screen is closed with GuiInterface#close
107+
* Which can be called by your code or outside of it.
109108
*/
110109
default void onClose() {
111110
}
112111

112+
/**
113+
* Executes when the screen is closed by player
114+
*
115+
* @param success Whatever screen will be actually closed, or forcefully reopened. Depends on value of GuiInterface#canPlayerClose()
116+
*/
117+
default void onPlayerClose(boolean success) {
118+
}
119+
120+
/**
121+
* Executes when the screen handler is closed.
122+
* This method might be executed multiple times.
123+
*/
124+
default void onScreenHandlerClosed() {
125+
}
126+
113127
/**
114128
* Executes each tick while the screen is open
115129
*/

src/main/java/eu/pb4/sgui/api/gui/layered/BackendSimpleGui.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public void onClose() {
2929
this.gui.onClose();
3030
}
3131

32+
@Override
33+
public void onPlayerClose(boolean b) {
34+
this.gui.onPlayerClose(false);
35+
}
36+
37+
@Override
38+
public void onScreenHandlerClosed() {
39+
this.gui.onScreenHandlerClosed();
40+
}
41+
3242
@Override
3343
public void onTick() {
3444
this.gui.onTick();

src/main/java/eu/pb4/sgui/mixin/ServerPlayNetworkHandlerMixin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ public ServerPlayNetworkHandlerMixin(MinecraftServer server, ClientConnection co
138138
}
139139

140140
if (handler.getGui().canPlayerClose()) {
141+
handler.getGui().onPlayerClose(true);
141142
this.sgui$previousScreen = this.player.currentScreenHandler;
142143
} else {
144+
handler.getGui().onPlayerClose(false);
143145
var screenHandler = this.player.currentScreenHandler;
144146
try {
145147
if (screenHandler.getType() != null) {

src/main/java/eu/pb4/sgui/virtual/FakeScreenHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,14 @@ public void sendContentUpdates() {
4242
public ItemStack quickMove(PlayerEntity player, int index) {
4343
return ItemStack.EMPTY;
4444
}
45+
46+
@Override
47+
public void onClosed(PlayerEntity player) {
48+
super.onClosed(player);
49+
try {
50+
this.getGui().onScreenHandlerClosed();
51+
} catch (Throwable e) {
52+
this.getGui().handleException(e);
53+
}
54+
}
4555
}

src/main/java/eu/pb4/sgui/virtual/book/BookScreenHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,14 @@ protected boolean insertItem(ItemStack stack, int startIndex, int endIndex, bool
8585
public BookGui getGui() {
8686
return gui;
8787
}
88+
89+
@Override
90+
public void onClosed(PlayerEntity player) {
91+
super.onClosed(player);
92+
try {
93+
this.getGui().onScreenHandlerClosed();
94+
} catch (Throwable e) {
95+
this.getGui().handleException(e);
96+
}
97+
}
8898
}

src/main/java/eu/pb4/sgui/virtual/inventory/VirtualScreenHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,14 @@ public void setSlot(int index, Slot slot) {
123123
protected boolean insertItem(ItemStack stack, int startIndex, int endIndex, boolean fromLast) {
124124
return this.gui.insertItem(stack, startIndex, endIndex, fromLast);
125125
}
126+
127+
@Override
128+
public void onClosed(PlayerEntity player) {
129+
super.onClosed(player);
130+
try {
131+
this.getGui().onScreenHandlerClosed();
132+
} catch (Throwable e) {
133+
this.getGui().handleException(e);
134+
}
135+
}
126136
}

0 commit comments

Comments
 (0)