Skip to content

Commit 1cb66b9

Browse files
committed
Feat: added close consumer
1 parent 6c3782d commit 1cb66b9

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

commons/src/main/java/com/envyful/api/gui/Gui.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.envyful.api.player.EnvyPlayer;
55
import com.envyful.api.player.PlayerManager;
66

7+
import java.util.function.Consumer;
8+
79
/**
810
*
911
* An interface representing chest GUIs for the platform specific implementation
@@ -63,6 +65,15 @@ interface Builder {
6365
*/
6466
Builder setPlayerManager(PlayerManager<?, ?> playerManager);
6567

68+
/**
69+
*
70+
* Sets the consumer for when the GUI is closed.
71+
*
72+
* @param consumer The close consumer
73+
* @return The builder
74+
*/
75+
Builder setCloseConsumer(Consumer<EnvyPlayer<?>> consumer);
76+
6677
/**
6778
*
6879
* Builds the GUI from the given specifications

forge/src/main/java/com/envyful/api/forge/gui/ForgeGui.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import scala.xml.dtd.REQUIRED;
2525

2626
import java.util.List;
27+
import java.util.function.Consumer;
2728

2829
/**
2930
*
@@ -35,15 +36,18 @@ public class ForgeGui implements Gui {
3536
private final ITextComponent title;
3637
private final int height;
3738
private final PlayerManager<ForgeEnvyPlayer, EntityPlayerMP> playerManager;
39+
private final Consumer<ForgeEnvyPlayer> closeConsumer;
3840
private final ForgeSimplePane parentPane;
3941
private final ForgeSimplePane[] panes;
4042

4143
private final List<ForgeGuiContainer> containers = Lists.newArrayList();
4244

43-
ForgeGui(String title, int height, PlayerManager<ForgeEnvyPlayer, EntityPlayerMP> playerManager, Pane... panes) {
45+
ForgeGui(String title, int height, PlayerManager<ForgeEnvyPlayer, EntityPlayerMP> playerManager,
46+
Consumer<ForgeEnvyPlayer> closeConsumer, Pane... panes) {
4447
this.title = new TextComponentString(title);
4548
this.height = height;
4649
this.playerManager = playerManager;
50+
this.closeConsumer = closeConsumer;
4751
this.parentPane = (ForgeSimplePane) new ForgeSimplePane.Builder().height(height).topLeftX(0).topLeftY(0).width(9).build();
4852
this.panes = new ForgeSimplePane[panes.length];
4953
int i = 0;
@@ -254,6 +258,11 @@ public void onContainerClosed(EntityPlayer playerIn) {
254258
super.onContainerClosed(playerIn);
255259

256260
EnvyPlayer<?> player = this.gui.playerManager.getPlayer(playerIn.getUniqueID());
261+
262+
if (this.gui.closeConsumer != null) {
263+
this.gui.closeConsumer.accept((ForgeEnvyPlayer) player);
264+
}
265+
257266
ForgeGuiTracker.removePlayer(player);
258267
}
259268
}

forge/src/main/java/com/envyful/api/forge/gui/ForgeGuiBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import com.envyful.api.forge.player.ForgeEnvyPlayer;
44
import com.envyful.api.gui.Gui;
55
import com.envyful.api.gui.pane.Pane;
6+
import com.envyful.api.player.EnvyPlayer;
67
import com.envyful.api.player.PlayerManager;
78
import com.google.common.collect.Lists;
89
import net.minecraft.entity.player.EntityPlayerMP;
910

1011
import java.util.List;
12+
import java.util.function.Consumer;
1113

1214
/**
1315
*
@@ -20,6 +22,7 @@ public class ForgeGuiBuilder implements Gui.Builder {
2022
private int height = 5;
2123
private List<Pane> panes = Lists.newArrayList();
2224
private PlayerManager<ForgeEnvyPlayer, EntityPlayerMP> playerManager;
25+
private Consumer<EnvyPlayer<?>> closeConsumer = null;
2326

2427
@Override
2528
public Gui.Builder title(String title) {
@@ -45,12 +48,19 @@ public Gui.Builder setPlayerManager(PlayerManager<?, ?> playerManager) {
4548
return this;
4649
}
4750

51+
@Override
52+
public Gui.Builder setCloseConsumer(Consumer<EnvyPlayer<?>> consumer) {
53+
this.closeConsumer = consumer;
54+
return this;
55+
}
56+
4857
@Override
4958
public Gui build() {
5059
if (this.playerManager == null) {
5160
throw new RuntimeException("Cannot build GUI without PlayerManager being set");
5261
}
5362

54-
return new ForgeGui(this.title, this.height, this.playerManager, this.panes.toArray(new Pane[0]));
63+
return new ForgeGui(this.title, this.height, this.playerManager,
64+
forgeEnvyPlayer -> this.closeConsumer.accept(forgeEnvyPlayer), this.panes.toArray(new Pane[0]));
5565
}
5666
}

0 commit comments

Comments
 (0)