From dfbf5fe3d0fac9fc2f1883d7d18b007e0d2eb1f1 Mon Sep 17 00:00:00 2001
From: hea3ven <hea3venmc@gmail.com>
Date: Mon, 14 Jan 2019 14:25:09 -0300
Subject: [PATCH] Fix container desync in the client (#61) (#67)

---
 build.gradle                                  |  2 +-
 .../impl/client/gui/GuiProviderImpl.java      |  1 +
 .../fabric/containers/ContainerMod.java       | 39 +++++++++++++++++--
 .../fabric/containers/ContainerModClient.java | 22 +++++++++++
 4 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/build.gradle b/build.gradle
index 25a23374f..87bcc407c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -38,7 +38,7 @@ minecraft {
 
 dependencies {
 	minecraft "com.mojang:minecraft:$mcVersion"
-	mappings "net.fabricmc:yarn:$mcVersion.12"
+	mappings "net.fabricmc:yarn:$mcVersion.24"
 	modCompile "net.fabricmc:fabric-loader:0.3.2.92"
 }
 
diff --git a/src/main/java/net/fabricmc/fabric/impl/client/gui/GuiProviderImpl.java b/src/main/java/net/fabricmc/fabric/impl/client/gui/GuiProviderImpl.java
index 18d32fb32..90af1010e 100644
--- a/src/main/java/net/fabricmc/fabric/impl/client/gui/GuiProviderImpl.java
+++ b/src/main/java/net/fabricmc/fabric/impl/client/gui/GuiProviderImpl.java
@@ -74,6 +74,7 @@ public class GuiProviderImpl implements GuiProviderRegistry {
 					return;
 				}
 				ContainerGui gui = factory.create(syncId, identifier, packetContext.getPlayer(), packetByteBuf);
+				packetContext.getPlayer().container = gui.getContainer();
 				MinecraftClient.getInstance().openGui(gui);
 			});
 		});
diff --git a/src/test/java/net/fabricmc/fabric/containers/ContainerMod.java b/src/test/java/net/fabricmc/fabric/containers/ContainerMod.java
index 474b5aebd..2ad7b818a 100644
--- a/src/test/java/net/fabricmc/fabric/containers/ContainerMod.java
+++ b/src/test/java/net/fabricmc/fabric/containers/ContainerMod.java
@@ -19,8 +19,9 @@ package net.fabricmc.fabric.containers;
 import net.fabricmc.api.ModInitializer;
 import net.fabricmc.fabric.api.container.ContainerProviderRegistry;
 import net.fabricmc.fabric.commands.CommandRegistry;
-import net.minecraft.class_3917;
 import net.minecraft.container.Container;
+import net.minecraft.container.ContainerType;
+import net.minecraft.container.Slot;
 import net.minecraft.entity.player.PlayerEntity;
 import net.minecraft.entity.player.PlayerInventory;
 import net.minecraft.server.command.ServerCommandManager;
@@ -31,6 +32,7 @@ public class ContainerMod implements ModInitializer {
 
 	public static final Identifier EXAMPLE_CONTAINER = new Identifier("fabric_container", "example_container");
 	public static final Identifier EXAMPLE_CONTAINER_2 = new Identifier("fabric_container", "example_container_2");
+	public static final Identifier EXAMPLE_INVENTORY_CONTAINER = new Identifier("fabric_container", "example_inventory_container");
 
 	@Override
 	public void onInitialize() {
@@ -42,7 +44,7 @@ public class ContainerMod implements ModInitializer {
 					BlockPos pos = context.getSource().getEntity().getPos();
 
 					//Opens a container, sending the block pos
-					ContainerProviderRegistry.INSTANCE.openContainer(EXAMPLE_CONTAINER, context.getSource().getPlayer(), buf -> buf.writeBlockPos(pos));
+					ContainerProviderRegistry.INSTANCE.openContainer(EXAMPLE_INVENTORY_CONTAINER, context.getSource().getPlayer(), buf -> buf.writeBlockPos(pos));
 
 					return 1;
 				})));
@@ -56,6 +58,9 @@ public class ContainerMod implements ModInitializer {
 			BlockPos pos = buf.readBlockPos();
 			return new ExampleContainer(syncId, pos, player);
 		});
+		ContainerProviderRegistry.INSTANCE.registerFactory(EXAMPLE_INVENTORY_CONTAINER, (syncId, identifier, player, buf) -> {
+			return new ExampleInventoryContainer(syncId, player);
+		});
 	}
 
 	//A basic container that prints to console when opened, this should print on the client + server
@@ -71,7 +76,7 @@ public class ContainerMod implements ModInitializer {
 		}
 
 		@Override
-		public class_3917<?> method_17358() {
+		public ContainerType<?> getType() {
 			return null;
 		}
 
@@ -81,4 +86,32 @@ public class ContainerMod implements ModInitializer {
 		}
 	}
 
+	public static class ExampleInventoryContainer extends Container {
+		public final PlayerInventory playerInventory;
+		BlockPos pos;
+
+		public ExampleInventoryContainer(int syncId, PlayerEntity playerEntity) {
+			super(syncId);
+			this.playerInventory = playerEntity.inventory;
+			for(int i = 0; i < 3; ++i) {
+				for(int j = 0; j < 9; ++j) {
+					this.addSlot(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
+				}
+			}
+
+			for(int j = 0; j < 9; ++j) {
+				this.addSlot(new Slot(playerInventory, j, 8 + j * 18, 142));
+			}
+		}
+
+		@Override
+		public ContainerType<?> getType() {
+			return null;
+		}
+
+		@Override
+		public boolean canUse(PlayerEntity playerEntity) {
+			return true;
+		}
+	}
 }
diff --git a/src/test/java/net/fabricmc/fabric/containers/ContainerModClient.java b/src/test/java/net/fabricmc/fabric/containers/ContainerModClient.java
index 17c80823c..d1ac74283 100644
--- a/src/test/java/net/fabricmc/fabric/containers/ContainerModClient.java
+++ b/src/test/java/net/fabricmc/fabric/containers/ContainerModClient.java
@@ -16,11 +16,13 @@
 
 package net.fabricmc.fabric.containers;
 
+import com.mojang.blaze3d.platform.GlStateManager;
 import net.fabricmc.api.ClientModInitializer;
 import net.fabricmc.fabric.api.client.gui.GuiProviderRegistry;
 import net.minecraft.client.gui.ContainerGui;
 import net.minecraft.entity.player.PlayerEntity;
 import net.minecraft.text.StringTextComponent;
+import net.minecraft.util.Identifier;
 import net.minecraft.util.math.BlockPos;
 
 public class ContainerModClient implements ClientModInitializer {
@@ -35,6 +37,9 @@ public class ContainerModClient implements ClientModInitializer {
 
 		//Registers a gui factory that opens our example gui, this uses the container created by ContainerProviderRegistry
 		GuiProviderRegistry.INSTANCE.registerFactory(ContainerMod.EXAMPLE_CONTAINER_2, ExampleContainerGui2::new);
+
+		//Registers a gui factory that opens our example inventory gui
+		GuiProviderRegistry.INSTANCE.registerFactory(ContainerMod.EXAMPLE_INVENTORY_CONTAINER, ExampleInventoryContainerGui::new);
 	}
 
 	//A container gui that shows the block pos that was sent
@@ -70,4 +75,21 @@ public class ContainerModClient implements ClientModInitializer {
 		}
 	}
 
+	//A container gui that has the player's inventory
+	public static class ExampleInventoryContainerGui extends ContainerGui<ContainerMod.ExampleInventoryContainer> {
+
+		private static final Identifier BG_TEXTURE = new Identifier("textures/gui/container/horse.png");
+
+		public ExampleInventoryContainerGui(ContainerMod.ExampleInventoryContainer container) {
+			super(container, container.playerInventory, new StringTextComponent("Example Inventory GUI"));
+		}
+
+		@Override
+		protected void drawBackground(float v, int i, int i1) {
+			GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
+			client.getTextureManager().bindTexture(BG_TEXTURE);
+			this.drawTexturedRect(left, top, 0, 0, containerWidth, containerHeight);
+		}
+	}
+
 }