From 01a006549097fa10648fc947c3cabb6de27bc7b3 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Fri, 6 Mar 2015 23:41:56 -0500 Subject: [PATCH] Fix client controller not connecting with ExU pipes; Fix controller connected block sync issue. --- .../blocks/drawers_controller_front.png | Bin 575 -> 577 bytes .../storagedrawers/StorageDrawers.java | 9 +- .../api/inventory/IDrawerInventory.java | 2 + .../api/inventory/IInventoryAdapter.java | 2 + .../api/storage/IDrawerGroup.java | 2 + .../block/tile/TileEntityController.java | 53 +++++++++-- .../block/tile/TileEntityDrawers.java | 10 +++ .../inventory/InventoryStack.java | 11 +++ .../inventory/StorageInventory.java | 16 ++++ .../network/ControllerUpdateMessage.java | 82 ++++++++++++++++++ .../storage/BaseDrawerData.java | 4 + 11 files changed, 183 insertions(+), 8 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/network/ControllerUpdateMessage.java diff --git a/resources/assets/storagedrawers/textures/blocks/drawers_controller_front.png b/resources/assets/storagedrawers/textures/blocks/drawers_controller_front.png index 2ce78ab75b541ec3747afd6018b86e279f830bde..5f9f7ee8a3b0389ffaf29216ed8ff631154cb2ca 100644 GIT binary patch delta 451 zcmV;!0X+V{1i=K5WC#ZYC=jwJP$7|QD1VqqL_t(|+Le+^t}#&*g{w-HX9N*6!~nD; zS`zvai7}XvhK>ms0ux{eIvVsPA`z5FQHuNhwf#VcQR;v{RfngZ$ z_Z#Q)`QUK8-|v-5B?!&)yyx?o%jI6LmrbnK>v6^*z;`yA8KLcVyMDi)OeS@V$A9C5 z?9=IVKA)S-CJ<$y^0V2j>$;BPD4VjyV!?15OC%Bs@SV@+<#IWn&u=yxklXFnXf$wq z->=nbkH^E#WHMyXfPMtp?e=oHBu|YyosRKstJR7!i@g1Q4|^vx0&1B~r|b2)7n&Oy zY{CEWkxG%lp2iX&Lgb*KO&tWL(|;+I+wB(i)oPX4dc7`{O0igsb|f4QheDx%vL*yl zsnqYE!sa&^3~2U~6_>eQujnCt1aj5MWMUyJ9xTV>@$q=X84iaaWQ;~5h6te^5LqqS za1BYJ$7UuIip3%b8A7DZe_1sbNDeba1H=yvfVuTD1VkoL_t(|+Le;auE9_ghKtrUiU9^@!6|K&hVTyXxH`M|N8fi*|r^qp{8li=M(4k zdf^aWE|+Sx8Y&Bd;C8ze3Wdkxp;PPk`@}dD_)aDhP1$z49SjEPbXvk>GD%549)FLg z)5)?dQjC!mKcCNgo@W?_bTc-a%{W}gQmK>__)e$ON~KaP7G2jRb3UKj?KaN+ey`W- z*Xvc!<#KcY03L;Ix9d0#eFEI;^|T*btyYZ1>D%pgus4)Z5Xfvc+iW%iWu7wFlK=1# zmA(dhfJK0BMjSNMnS;Q1JZ5sU*?+*kR;y84uh->rIgv;JN5kQ8D3wYm8-qY5lll8q z==_GmAz;5*v5fEg=pp(@l2u2eQ5;$EVCg6mf3aB1=W|*i=_&()QY0&Da3Zoa8Vzdr zolZwCn6TAqAt)ilB3b2J(Vj2xLSwmH5=x0SR*ht3Ik}_#eh;?H)4_rA#x5ku3yNfA rltX1Rkze?HuR=o8G(WQb_j$hoon2TMo;*X#00000NkvXXu0mjfs!7;) diff --git a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java index a2c831f..7ed91db 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/StorageDrawers.java @@ -7,6 +7,7 @@ import com.jaquadro.minecraft.storagedrawers.config.RecipeHandlerRegistry; import com.jaquadro.minecraft.storagedrawers.core.*; import com.jaquadro.minecraft.storagedrawers.integration.IntegrationRegistry; import com.jaquadro.minecraft.storagedrawers.network.BlockClickMessage; +import com.jaquadro.minecraft.storagedrawers.network.ControllerUpdateMessage; import com.jaquadro.minecraft.storagedrawers.network.CountUpdateMessage; import cpw.mods.fml.client.event.ConfigChangedEvent; import cpw.mods.fml.common.FMLCommonHandler; @@ -56,10 +57,14 @@ public class StorageDrawers network = NetworkRegistry.INSTANCE.newSimpleChannel(MOD_ID); network.registerMessage(BlockClickMessage.Handler.class, BlockClickMessage.class, 0, Side.SERVER); - if (FMLCommonHandler.instance().getSide() == Side.CLIENT) + if (FMLCommonHandler.instance().getSide() == Side.CLIENT) { network.registerMessage(CountUpdateMessage.Handler.class, CountUpdateMessage.class, 1, Side.CLIENT); - else + network.registerMessage(ControllerUpdateMessage.Handler.class, ControllerUpdateMessage.class, 2, Side.CLIENT); + } + else { network.registerMessage(CountUpdateMessage.HandlerStub.class, CountUpdateMessage.class, 1, Side.CLIENT); + network.registerMessage(ControllerUpdateMessage.HandlerStub.class, ControllerUpdateMessage.class, 2, Side.CLIENT); + } compRegistry = new CompTierRegistry(); oreDictRegistry = new OreDictRegistry(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/inventory/IDrawerInventory.java b/src/com/jaquadro/minecraft/storagedrawers/api/inventory/IDrawerInventory.java index dc13071..47396c7 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/inventory/IDrawerInventory.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/inventory/IDrawerInventory.java @@ -30,4 +30,6 @@ public interface IDrawerInventory extends ISidedInventory boolean canInsertItem (int slot, ItemStack stack); boolean canExtractItem (int slot, ItemStack stack); + + boolean syncInventoryIfNeeded (); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/inventory/IInventoryAdapter.java b/src/com/jaquadro/minecraft/storagedrawers/api/inventory/IInventoryAdapter.java index e069d50..0e3d176 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/inventory/IInventoryAdapter.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/inventory/IInventoryAdapter.java @@ -7,4 +7,6 @@ public interface IInventoryAdapter ItemStack getInventoryStack (SlotType slotType); void syncInventory (); + + boolean syncInventoryIfNeeded (); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java index 4477695..1f1eefe 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup.java @@ -20,4 +20,6 @@ public interface IDrawerGroup boolean isDrawerEnabled (int slot); IDrawerInventory getDrawerInventory (); + + boolean markDirtyIfNeeded (); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index bf8f42b..1296bf6 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -1,9 +1,13 @@ package com.jaquadro.minecraft.storagedrawers.block.tile; +import com.jaquadro.minecraft.storagedrawers.StorageDrawers; import com.jaquadro.minecraft.storagedrawers.api.inventory.IDrawerInventory; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroupInteractive; +import com.jaquadro.minecraft.storagedrawers.network.ControllerUpdateMessage; +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.simpleimpl.IMessage; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; @@ -65,7 +69,6 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS private int[] autoSides = new int[] { 0, 1 }; private int direction; - private int inventorySize = 0; private int drawerSize = 0; private long lastClickTime; @@ -87,7 +90,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS } public int interactPutItemsIntoInventory (EntityPlayer player) { - if (inventorySize == 0) + if (inventorySlots.length == 0) updateCache(); boolean dumpInventory = worldObj.getTotalWorldTime() - lastClickTime < 10 && player.getPersistentID().equals(lastClickUUID); @@ -128,7 +131,6 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS invSlotList.clear(); drawerBlockList.clear(); drawerSlotList.clear(); - inventorySize = 0; drawerSize = 0; } @@ -172,6 +174,9 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS if (invBlockList.get(i) != null) inventorySlots[j++] = i; } + + if (!worldObj.isRemote) + syncClient(); } private void populateNode (int x, int y, int z, int depth) { @@ -216,7 +221,6 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS drawerSlotList.add(i); } - inventorySize += invStorageSize.get(coord); drawerSize += drawerStorageSize.get(coord); if (depth == DEPTH_LIMIT) @@ -315,6 +319,17 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS getWorldObj().func_147479_m(xCoord, yCoord, zCoord); // markBlockForRenderUpdate } + private void syncClient () { + IMessage message = new ControllerUpdateMessage(xCoord, yCoord, zCoord, inventorySlots); + NetworkRegistry.TargetPoint targetPoint = new NetworkRegistry.TargetPoint(worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 500); + + StorageDrawers.network.sendToAllAround(message, targetPoint); + } + + public void clientUpdate (int[] inventorySlots) { + this.inventorySlots = inventorySlots; + } + @Override public IDrawerInventory getDrawerInventory () { return null; @@ -322,7 +337,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS @Override public int getDrawerCount () { - if (inventorySize == 0) + if (inventorySlots.length == 0) updateCache(); return drawerSlotList.size(); @@ -346,6 +361,31 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS return group.isDrawerEnabled(getDrawerSlotForGroup(slot)); } + @Override + public void markDirty () { + for (IDrawerGroup group : storage.values()) { + if (group != null && group.getDrawerInventory() != null) + group.markDirtyIfNeeded(); + } + + super.markDirty(); + } + + @Override + public boolean markDirtyIfNeeded () { + boolean synced = false; + + for (IDrawerGroup group : storage.values()) { + if (group != null && group.getDrawerInventory() != null) + synced |= group.markDirtyIfNeeded(); + } + + if (synced) + super.markDirty(); + + return synced; + } + @Override public int[] getAccessibleSlotsFromSide (int side) { for (int aside : autoSides) { @@ -376,7 +416,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS @Override public int getSizeInventory () { - return inventorySize; + return inventorySlots.length; } @Override @@ -413,6 +453,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS return; inventory.setInventorySlotContents(getDrawerSlotForInv(slot), stack); + inventory.markDirty(); } @Override diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index ab69559..cb2ede3 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -309,6 +309,16 @@ public abstract class TileEntityDrawers extends TileEntity implements IDrawerGro super.markDirty(); } + @Override + public boolean markDirtyIfNeeded () { + if (inventory.syncInventoryIfNeeded()) { + super.markDirty(); + return true; + } + + return false; + } + public void clientUpdateCount (int slot, int count) { IDrawer drawer = getDrawer(slot); if (drawer.getStoredItemCount() != count) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryStack.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryStack.java index d9be05d..1ccfbef 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryStack.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/InventoryStack.java @@ -50,6 +50,17 @@ public abstract class InventoryStack refresh(); } + public boolean markDirtyIfNeeded () { + int diff = getDiff(); + if (diff != 0) { + applyDiff(diff); + refresh(); + return true; + } + + return false; + } + public int getDiff () { if (nativeStack == null) return 0; diff --git a/src/com/jaquadro/minecraft/storagedrawers/inventory/StorageInventory.java b/src/com/jaquadro/minecraft/storagedrawers/inventory/StorageInventory.java index ccfb3fb..d8aa32c 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/inventory/StorageInventory.java +++ b/src/com/jaquadro/minecraft/storagedrawers/inventory/StorageInventory.java @@ -268,6 +268,22 @@ public class StorageInventory implements IDrawerInventory } } + @Override + public boolean syncInventoryIfNeeded () { + boolean synced = false; + + for (int i = 0, n = group.getDrawerCount(); i < n; i++) { + if (!group.isDrawerEnabled(i)) + continue; + + IDrawer drawer = group.getDrawer(i); + if (drawer instanceof IInventoryAdapter) + synced |= ((IInventoryAdapter) drawer).syncInventoryIfNeeded(); + } + + return synced; + } + @Override public boolean isUseableByPlayer (EntityPlayer player) { return false; diff --git a/src/com/jaquadro/minecraft/storagedrawers/network/ControllerUpdateMessage.java b/src/com/jaquadro/minecraft/storagedrawers/network/ControllerUpdateMessage.java new file mode 100644 index 0000000..7d72b43 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/network/ControllerUpdateMessage.java @@ -0,0 +1,82 @@ +package com.jaquadro.minecraft.storagedrawers.network; + +import com.jaquadro.minecraft.storagedrawers.StorageDrawers; +import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityController; +import cpw.mods.fml.common.FMLLog; +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import io.netty.buffer.ByteBuf; +import net.minecraft.client.Minecraft; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import org.apache.logging.log4j.Level; + +public class ControllerUpdateMessage implements IMessage +{ + private int x; + private int y; + private int z; + private int[] inventorySlots; + + public ControllerUpdateMessage () { } + + public ControllerUpdateMessage (int x, int y, int z, int[] inventorySlots) { + this.x = x; + this.y = y; + this.z = z; + this.inventorySlots = inventorySlots; + } + + @Override + public void fromBytes (ByteBuf buf) { + x = buf.readInt(); + y = buf.readShort(); + z = buf.readInt(); + + int count = buf.readShort(); + inventorySlots = new int[count]; + + for (int i = 0; i < count; i++) + inventorySlots[i] = buf.readShort(); + } + + @Override + public void toBytes (ByteBuf buf) { + buf.writeInt(x); + buf.writeShort(y); + buf.writeInt(z); + + buf.writeShort(inventorySlots.length); + for (int i = 0; i < inventorySlots.length; i++) + buf.writeShort(inventorySlots[i]); + } + + @SideOnly(Side.CLIENT) + public static class Handler implements IMessageHandler + { + @Override + public IMessage onMessage (ControllerUpdateMessage message, MessageContext ctx) { + if (ctx.side == Side.CLIENT) { + World world = Minecraft.getMinecraft().theWorld; + TileEntity tileEntity = world.getTileEntity(message.x, message.y, message.z); + if (tileEntity instanceof TileEntityController) { + ((TileEntityController) tileEntity).clientUpdate(message.inventorySlots); + } + } + + return null; + } + } + + public static class HandlerStub implements IMessageHandler + { + @Override + public IMessage onMessage (ControllerUpdateMessage message, MessageContext ctx) { + FMLLog.log(StorageDrawers.MOD_ID, Level.WARN, "ControllerUpdateMessage stub handler called."); + return null; + } + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java index 877a97d..d7b53b5 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java @@ -85,6 +85,10 @@ public abstract class BaseDrawerData implements IDrawer, IInventoryAdapter inventoryStack.markDirty(); } + public boolean syncInventoryIfNeeded () { + return inventoryStack.markDirtyIfNeeded(); + } + public static boolean areItemsEqual (ItemStack stack1, ItemStack stack2) { if (stack1 == null || stack2 == null) return false;