Skip to content

Commit 57d7f1a

Browse files
committed
Fix: GUI lag & spam
1 parent 0115620 commit 57d7f1a

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.envyful.api.player.PlayerManager;
1111
import com.envyful.api.type.Pair;
1212
import com.google.common.collect.Lists;
13-
import com.google.common.collect.Maps;
1413
import net.minecraft.entity.player.EntityPlayer;
1514
import net.minecraft.entity.player.EntityPlayerMP;
1615
import net.minecraft.inventory.ClickType;
@@ -22,10 +21,9 @@
2221
import net.minecraft.util.NonNullList;
2322
import net.minecraft.util.text.ITextComponent;
2423
import net.minecraft.util.text.TextComponentString;
24+
import scala.xml.dtd.REQUIRED;
2525

2626
import java.util.List;
27-
import java.util.Map;
28-
import java.util.UUID;
2927

3028
/**
3129
*
@@ -82,7 +80,7 @@ public void open(EnvyPlayer<?> player) {
8280

8381
public void update() {
8482
for (ForgeGuiContainer value : this.containers) {
85-
value.update(this.panes);
83+
value.update(this.panes, false);
8684
}
8785
}
8886

@@ -95,21 +93,33 @@ private final class ForgeGuiContainer extends Container {
9593

9694
private final ForgeGui gui;
9795
private final EntityPlayerMP player;
96+
private final List<EmptySlot> emptySlots = Lists.newArrayList();
9897

9998
public ForgeGuiContainer(ForgeGui gui, EntityPlayerMP player) {
10099
this.windowId = 1;
101100
this.gui = gui;
102101
this.player = player;
103102

104-
this.update(this.gui.panes);
103+
this.update(this.gui.panes, true);
105104
}
106105

107-
public void update(ForgeSimplePane[] panes) {
106+
public void update(ForgeSimplePane[] panes, boolean force) {
108107
this.inventorySlots = Lists.newArrayList();
109108
this.inventoryItemStacks = NonNullList.create();
109+
boolean createEmptySlots = this.emptySlots.isEmpty();
110+
111+
if (!createEmptySlots) {
112+
this.inventorySlots.addAll(this.emptySlots);
113+
}
110114

111115
for (int i = 0; i < (9 * this.gui.height); i++) {
112-
this.inventorySlots.add(new EmptySlot(this.gui.parentPane, i));
116+
if (createEmptySlots) {
117+
EmptySlot emptySlot = new EmptySlot(this.gui.parentPane, i);
118+
119+
this.emptySlots.add(emptySlot);
120+
this.inventorySlots.add(emptySlot);
121+
}
122+
113123
this.inventoryItemStacks.add(ItemStack.EMPTY);
114124
}
115125

@@ -133,13 +143,19 @@ public void update(ForgeSimplePane[] panes) {
133143
}
134144

135145
for (int i = 9; i < 36; i++) {
136-
inventorySlots.add(new Slot(this.player.inventory, i - 9, 0, 0));
137-
inventoryItemStacks.add(this.player.inventory.getStackInSlot(i));
146+
ItemStack itemStack = player.inventory.mainInventory.get(i);
147+
inventorySlots.add(new Slot(player.inventory, i, 0, 0));
148+
inventoryItemStacks.add(itemStack);
138149
}
139-
150+
// Sets the slots for the hotbar.
140151
for (int i = 0; i < 9; i++) {
141-
inventorySlots.add(new Slot(this.player.inventory, i + 27, 0, 0));
142-
inventoryItemStacks.add(this.player.inventory.getStackInSlot(i));
152+
ItemStack itemStack = player.inventory.mainInventory.get(i);
153+
inventorySlots.add(new Slot(player.inventory, i, 0, 0));
154+
inventoryItemStacks.add(itemStack);
155+
}
156+
157+
if (force || ForgeGuiTracker.requiresUpdate(this.player)) {
158+
this.refreshPlayerContents();
143159
}
144160
}
145161

@@ -208,6 +224,7 @@ public ItemStack slotClick(int slot, int dragType, ClickType clickTypeIn, Entity
208224

209225
ForgeSimplePane.SimpleDisplayableSlot simpleDisplayableSlot = pane.getItems()[panePosition.getY()][panePosition.getX()];
210226
simpleDisplayableSlot.getDisplayable().onClick(envyPlayer, clickType);
227+
ForgeGuiTracker.enqueueUpdate(envyPlayer);
211228
}
212229

213230
return ItemStack.EMPTY;
@@ -223,9 +240,8 @@ private Displayable.ClickType convertClickType(int id) {
223240
}
224241

225242
private void refreshPlayerContents() {
226-
player.sendAllContents(player.openContainer, inventoryItemStacks);
227-
player.inventoryContainer.detectAndSendChanges();
228-
player.sendAllContents(player.inventoryContainer, player.inventoryContainer.inventoryItemStacks);
243+
this.player.sendAllContents(this, this.inventoryItemStacks);
244+
ForgeGuiTracker.dequeueUpdate(this.player);
229245
}
230246

231247
private void clearPlayerCursor() {

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.envyful.api.forge.gui;
22

3-
import com.envyful.api.concurrency.UtilConcurrency;
43
import com.envyful.api.forge.listener.LazyListener;
54
import com.envyful.api.player.EnvyPlayer;
65
import com.google.common.collect.Maps;
6+
import com.google.common.collect.Sets;
7+
import net.minecraft.entity.player.EntityPlayerMP;
78
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
89
import net.minecraftforge.fml.common.gameevent.TickEvent;
910

1011
import java.util.Map;
12+
import java.util.Set;
1113
import java.util.UUID;
1214

1315
/**
@@ -17,7 +19,8 @@
1719
*/
1820
public class ForgeGuiTracker {
1921

20-
private static final Map<UUID, ForgeGui> OPEN_GUIS = Maps.newConcurrentMap();
22+
private static final Map<UUID, ForgeGui> OPEN_GUIS = Maps.newHashMap();
23+
private static final Set<UUID> REQUIRED_UPDATE = Sets.newHashSet();
2124

2225
static {
2326
new ForgeGuiTickListener();
@@ -31,6 +34,18 @@ public static void removePlayer(EnvyPlayer<?> player) {
3134
OPEN_GUIS.remove(player.getUuid());
3235
}
3336

37+
public static void enqueueUpdate(EnvyPlayer<?> player) {
38+
REQUIRED_UPDATE.add(player.getUuid());
39+
}
40+
41+
public static boolean requiresUpdate(EntityPlayerMP player) {
42+
return REQUIRED_UPDATE.contains(player.getUniqueID());
43+
}
44+
45+
public static void dequeueUpdate(EntityPlayerMP player) {
46+
REQUIRED_UPDATE.remove(player.getUniqueID());
47+
}
48+
3449
private static final class ForgeGuiTickListener extends LazyListener {
3550

3651
private int tick = 0;
@@ -41,17 +56,15 @@ private ForgeGuiTickListener() {
4156

4257
@SubscribeEvent
4358
public void onServerTick(TickEvent.ServerTickEvent event) {
59+
++tick;
60+
4461
if (tick % 10 != 0) {
4562
return;
4663
}
4764

48-
++tick;
49-
50-
UtilConcurrency.runAsync(() -> {
51-
for (ForgeGui value : OPEN_GUIS.values()) {
52-
value.update();
53-
}
54-
});
65+
for (ForgeGui value : OPEN_GUIS.values()) {
66+
value.update();
67+
}
5568
}
5669
}
5770
}

forge/src/main/java/com/envyful/api/forge/gui/pane/ForgeSimplePane.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import com.envyful.api.gui.pane.Pane;
77
import com.envyful.api.type.Pair;
88
import com.google.common.collect.Lists;
9+
import net.minecraft.inventory.InventoryBasic;
910
import net.minecraft.inventory.Slot;
1011
import net.minecraft.item.ItemStack;
1112
import net.minecraft.util.NonNullList;
13+
import net.minecraft.util.text.TextComponentString;
1214

1315
import java.util.List;
1416

@@ -24,6 +26,7 @@ public class ForgeSimplePane implements Pane {
2426
private final int width;
2527
private final int height;
2628
private final SimpleDisplayableSlot[][] items;
29+
private final InventoryBasic inventoryBasic;
2730

2831
private boolean full = false;
2932
private Pair<Integer, Integer> lastPos = Pair.of(0, 0);
@@ -33,6 +36,7 @@ private ForgeSimplePane(int topLeftX, int topLeftY, int height, int width) {
3336
this.topLeftY = topLeftY;
3437
this.width = width;
3538
this.height = height;
39+
this.inventoryBasic = new InventoryBasic("", false, 1);
3640
this.items = new SimpleDisplayableSlot[height][width];
3741

3842
for (int x = 0; x < this.width; x++) {
@@ -124,7 +128,7 @@ public static class SimpleDisplayableSlot extends Slot {
124128
private final Displayable displayable;
125129

126130
public SimpleDisplayableSlot(ForgeSimplePane pane, Displayable displayable, int xPosition, int yPosition) {
127-
super(null, xPosition + yPosition * 9, pane.topLeftX + xPosition,
131+
super(pane.inventoryBasic, xPosition + yPosition * 9, pane.topLeftX + xPosition,
128132
pane.topLeftY + yPosition);
129133

130134
this.displayable = displayable;

0 commit comments

Comments
 (0)