Skip to content

Commit 95c176f

Browse files
authored
Sync fixes (#50)
* try some shit * fix desyncs * undo testing stuff * fix
1 parent 9cf888b commit 95c176f

8 files changed

Lines changed: 102 additions & 10 deletions

File tree

src/main/java/com/cleanroommc/modularui/factory/GuiManager.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010

1111
import net.minecraft.client.Minecraft;
1212
import net.minecraft.client.entity.EntityPlayerSP;
13+
import net.minecraft.entity.player.EntityPlayer;
1314
import net.minecraft.entity.player.EntityPlayerMP;
1415
import net.minecraft.network.PacketBuffer;
1516
import net.minecraftforge.client.event.GuiOpenEvent;
17+
import net.minecraftforge.common.MinecraftForge;
18+
import net.minecraftforge.common.util.FakePlayer;
19+
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
1620
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
21+
import net.minecraftforge.fml.common.gameevent.TickEvent;
1722
import net.minecraftforge.fml.relauncher.Side;
1823
import net.minecraftforge.fml.relauncher.SideOnly;
1924

@@ -22,6 +27,8 @@
2227
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
2328
import org.jetbrains.annotations.NotNull;
2429

30+
import java.util.ArrayList;
31+
import java.util.List;
2532
import java.util.NoSuchElementException;
2633
import java.util.Objects;
2734

@@ -30,6 +37,7 @@ public class GuiManager {
3037
private static final Object2ObjectMap<String, UIFactory<?>> FACTORIES = new Object2ObjectOpenHashMap<>(16);
3138

3239
private static GuiScreenWrapper lastMui;
40+
private static final List<EntityPlayer> openedContainers = new ArrayList<>(4);
3341

3442
public static void registerFactory(UIFactory<?> factory) {
3543
Objects.requireNonNull(factory);
@@ -50,6 +58,8 @@ public static void registerFactory(UIFactory<?> factory) {
5058
}
5159

5260
public static <T extends GuiData> void open(@NotNull UIFactory<T> factory, @NotNull T guiData, EntityPlayerMP player) {
61+
if (player instanceof FakePlayer || openedContainers.contains(player)) return;
62+
openedContainers.add(player);
5363
// create panel, collect sync handlers and create container
5464
guiData.setJeiSettings(JeiSettings.DUMMY);
5565
GuiSyncManager syncManager = new GuiSyncManager(player);
@@ -67,6 +77,8 @@ public static <T extends GuiData> void open(@NotNull UIFactory<T> factory, @NotN
6777
PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
6878
factory.writeGuiData(guiData, buffer);
6979
NetworkHandler.sendToPlayer(new OpenGuiPacket<>(windowId, factory, buffer), player);
80+
// finally invoke event
81+
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, container));
7082
}
7183

7284
@SideOnly(Side.CLIENT)
@@ -82,6 +94,7 @@ public static <T extends GuiData> void open(int windowId, @NotNull UIFactory<T>
8294
GuiScreenWrapper guiScreenWrapper = new GuiScreenWrapper(new ModularContainer(syncManager), screen);
8395
guiScreenWrapper.inventorySlots.windowId = windowId;
8496
Minecraft.getMinecraft().displayGuiScreen(guiScreenWrapper);
97+
player.openContainer = guiScreenWrapper.inventorySlots;
8598
}
8699

87100
@SideOnly(Side.CLIENT)
@@ -91,6 +104,13 @@ static void openScreen(ModularScreen screen, JeiSettingsImpl jeiSettings) {
91104
Minecraft.getMinecraft().displayGuiScreen(screenWrapper);
92105
}
93106

107+
@SubscribeEvent
108+
public static void onTick(TickEvent.ServerTickEvent event) {
109+
if (event.phase == TickEvent.Phase.END) {
110+
openedContainers.clear();
111+
}
112+
}
113+
94114
@SubscribeEvent
95115
public static void onGuiOpen(GuiOpenEvent event) {
96116
if (lastMui != null && event.getGui() == null) {

src/main/java/com/cleanroommc/modularui/network/NetworkHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.cleanroommc.modularui.network;
22

33
import com.cleanroommc.modularui.ModularUI;
4-
import com.cleanroommc.modularui.network.packets.OpenGuiPacket;
5-
import com.cleanroommc.modularui.network.packets.PacketSyncHandler;
6-
import com.cleanroommc.modularui.network.packets.SClipboard;
7-
import com.cleanroommc.modularui.network.packets.SyncConfig;
4+
import com.cleanroommc.modularui.network.packets.*;
85

96
import net.minecraft.client.network.NetHandlerPlayClient;
107
import net.minecraft.entity.player.EntityPlayerMP;
@@ -28,6 +25,7 @@ public static void init() {
2825
registerC2S(PacketSyncHandler.class);
2926
registerC2S(SyncConfig.class);
3027
registerS2C(OpenGuiPacket.class);
28+
registerC2S(OpenGuiHandshake.class);
3129
}
3230

3331
private static void registerC2S(Class<? extends IPacket> clazz) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.cleanroommc.modularui.network.packets;
2+
3+
import com.cleanroommc.modularui.network.IPacket;
4+
import com.cleanroommc.modularui.screen.ModularContainer;
5+
6+
import net.minecraft.network.NetHandlerPlayServer;
7+
import net.minecraft.network.PacketBuffer;
8+
9+
import org.jetbrains.annotations.Nullable;
10+
11+
import java.io.IOException;
12+
13+
public class OpenGuiHandshake implements IPacket {
14+
15+
private int windowId;
16+
17+
public OpenGuiHandshake() {
18+
}
19+
20+
public OpenGuiHandshake(int windowId) {
21+
this.windowId = windowId;
22+
}
23+
24+
@Override
25+
public void write(PacketBuffer buf) throws IOException {
26+
buf.writeVarInt(this.windowId);
27+
}
28+
29+
@Override
30+
public void read(PacketBuffer buf) throws IOException {
31+
this.windowId = buf.readVarInt();
32+
}
33+
34+
@Override
35+
public @Nullable IPacket executeServer(NetHandlerPlayServer handler) {
36+
if (handler.player.openContainer instanceof ModularContainer container && container.windowId == this.windowId) {
37+
container.onHandshake();
38+
}
39+
return null;
40+
}
41+
}

src/main/java/com/cleanroommc/modularui/network/packets/OpenGuiPacket.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.cleanroommc.modularui.factory.GuiData;
55
import com.cleanroommc.modularui.factory.GuiManager;
66
import com.cleanroommc.modularui.network.IPacket;
7+
import com.cleanroommc.modularui.network.NetworkHandler;
78
import com.cleanroommc.modularui.network.NetworkUtils;
89

910
import net.minecraft.client.Minecraft;
@@ -49,6 +50,7 @@ public void read(PacketBuffer buf) {
4950
@Override
5051
public @Nullable IPacket executeClient(NetHandlerPlayClient handler) {
5152
GuiManager.open(this.windowId, this.factory, this.data, Minecraft.getMinecraft().player);
53+
NetworkHandler.sendToServer(new OpenGuiHandshake(this.windowId));
5254
return null;
5355
}
5456
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static ModularContainer getCurrent(EntityPlayer player) {
4646
}
4747

4848
private final GuiSyncManager guiSyncManager;
49-
private boolean init = true;
49+
private boolean init = true, handshake = false;
5050
private final List<ModularSlot> slots = new ArrayList<>();
5151
private final List<ModularSlot> shiftClickSlots = new ArrayList<>();
5252

@@ -72,14 +72,17 @@ public ContainerAccessor acc() {
7272
@Override
7373
public void onContainerClosed(@NotNull EntityPlayer playerIn) {
7474
super.onContainerClosed(playerIn);
75+
this.handshake = false;
7576
if (this.guiSyncManager != null) {
7677
this.guiSyncManager.onClose();
7778
}
7879
}
7980

8081
@Override
8182
public void detectAndSendChanges() {
82-
super.detectAndSendChanges();
83+
if (this.handshake) {
84+
super.detectAndSendChanges();
85+
}
8386
this.guiSyncManager.detectAndSendChanges(this.init);
8487
this.init = false;
8588
}
@@ -363,4 +366,8 @@ public void buildSortingContext(ISortingContextBuilder builder) {
363366
public IPosSetter getPlayerButtonPosSetter() {
364367
return null;
365368
}
369+
370+
public void onHandshake() {
371+
this.handshake = true;
372+
}
366373
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cleanroommc.modularui.test;
22

3-
import com.cleanroommc.modularui.ModularUI;
43
import com.cleanroommc.modularui.api.IGuiHolder;
54
import com.cleanroommc.modularui.api.drawable.IKey;
65
import com.cleanroommc.modularui.drawable.*;
@@ -23,15 +22,17 @@
2322
import com.cleanroommc.modularui.widgets.layout.Row;
2423
import com.cleanroommc.modularui.widgets.textfield.TextFieldWidget;
2524

26-
import net.minecraft.client.Minecraft;
2725
import net.minecraft.init.Items;
2826
import net.minecraft.item.ItemStack;
27+
import net.minecraft.nbt.NBTTagCompound;
2928
import net.minecraft.tileentity.TileEntity;
3029
import net.minecraft.util.ITickable;
3130
import net.minecraftforge.fluids.FluidTank;
3231
import net.minecraftforge.items.IItemHandlerModifiable;
3332
import net.minecraftforge.items.ItemStackHandler;
3433

34+
import org.jetbrains.annotations.NotNull;
35+
3536
import java.util.concurrent.atomic.AtomicReference;
3637
import java.util.function.Function;
3738

@@ -215,7 +216,7 @@ public ModularPanel buildUI(PosGuiData guiData, GuiSyncManager guiSyncManager) {
215216
.size(14, 14)
216217
.length(3)
217218
.texture(GuiTextures.CYCLE_BUTTON_DEMO)
218-
.value(new IntValue.Dynamic(() -> this.val2, val -> this.val2 = val))
219+
.value(new IntSyncValue(() -> this.val2, val -> this.val2 = val))
219220
.margin(8, 0))
220221
.child(IKey.str("Hello World").asWidget().height(18)))
221222
.child(new SpecialButton(IKey.str("A very long string that looks cool when animated").withAnimation())
@@ -354,12 +355,29 @@ public void update() {
354355
if (this.time++ % 20 == 0) {
355356
this.val++;
356357
}
358+
} else {
359+
if (this.time++ % 20 == 0 && ++this.val2 == 3) {
360+
this.val2 = 0;
361+
}
357362
}
358363
if (++this.progress == this.duration) {
359364
this.progress = 0;
360365
}
361366
}
362367

368+
@Override
369+
public @NotNull NBTTagCompound writeToNBT(@NotNull NBTTagCompound compound) {
370+
compound = super.writeToNBT(compound);
371+
compound.setTag("item_inv", this.bigInventory.serializeNBT());
372+
return compound;
373+
}
374+
375+
@Override
376+
public void readFromNBT(@NotNull NBTTagCompound compound) {
377+
super.readFromNBT(compound);
378+
this.bigInventory.deserializeNBT(compound.getCompoundTag("item_inv"));
379+
}
380+
363381
private static class SpecialButton extends ButtonWidget<SpecialButton> {
364382

365383
private final AnimatedText animatedKey;

src/main/java/com/cleanroommc/modularui/widgets/FluidSlot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void draw(GuiContext context, WidgetTheme widgetTheme) {
143143
GuiDraw.drawFluidTexture(content, this.contentOffsetX, y, getArea().width - this.contentOffsetX * 2, height, 0);
144144
}
145145
if (this.overlayTexture != null) {
146-
this.overlayTexture.draw(context, getArea());
146+
this.overlayTexture.drawAtZero(context, getArea(), widgetTheme);
147147
}
148148
if (content != null && this.syncHandler.controlsAmount()) {
149149
String s = NumberFormat.formatWithMaxDigits(getBaseUnitAmount(content.amount)) + getBaseUnit();

src/main/java/com/cleanroommc/modularui/widgets/ItemSlot.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import net.minecraftforge.fml.relauncher.Side;
3333
import net.minecraftforge.fml.relauncher.SideOnly;
3434

35+
import net.minecraftforge.items.IItemHandlerModifiable;
36+
3537
import org.jetbrains.annotations.NotNull;
3638
import org.jetbrains.annotations.Nullable;
3739

@@ -169,6 +171,10 @@ public ItemSlot slot(ModularSlot slot) {
169171
return this;
170172
}
171173

174+
public ItemSlot slot(IItemHandlerModifiable itemHandler, int index) {
175+
return slot(new ModularSlot(itemHandler, index));
176+
}
177+
172178
@SideOnly(Side.CLIENT)
173179
private void drawSlot(Slot slotIn) {
174180
GuiScreenWrapper guiScreen = getScreen().getScreenWrapper();

0 commit comments

Comments
 (0)