From c6d86a69f60f86bdb8d24424b466a0344836185a Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Sun, 11 Jan 2015 17:21:53 -0500 Subject: [PATCH] Fix autochisel inventory syncing and change autochisel block bounds --- .../java/com/cricketcraft/chisel/Chisel.java | 1 + .../cricketcraft/chisel/PacketHandler.java | 15 ----- .../chisel/block/BlockAutoChisel.java | 3 +- .../tileentity/TileEntityAutoChisel.java | 22 ++++--- .../chisel/item/chisel/ChiselController.java | 57 +------------------ .../chisel/network/PacketHandler.java | 20 +++++++ 6 files changed, 41 insertions(+), 77 deletions(-) delete mode 100644 src/main/java/com/cricketcraft/chisel/PacketHandler.java create mode 100644 src/main/java/com/cricketcraft/chisel/network/PacketHandler.java diff --git a/src/main/java/com/cricketcraft/chisel/Chisel.java b/src/main/java/com/cricketcraft/chisel/Chisel.java index c137aa4d..f0a4a3e0 100644 --- a/src/main/java/com/cricketcraft/chisel/Chisel.java +++ b/src/main/java/com/cricketcraft/chisel/Chisel.java @@ -33,6 +33,7 @@ import com.cricketcraft.chisel.inventory.ContainerChisel; import com.cricketcraft.chisel.inventory.ContainerPresent; import com.cricketcraft.chisel.inventory.InventoryChiselSelection; import com.cricketcraft.chisel.item.chisel.ChiselController; +import com.cricketcraft.chisel.network.PacketHandler; import com.cricketcraft.chisel.proxy.CommonProxy; import com.cricketcraft.chisel.utils.General; import com.cricketcraft.chisel.world.GeneratorChisel; diff --git a/src/main/java/com/cricketcraft/chisel/PacketHandler.java b/src/main/java/com/cricketcraft/chisel/PacketHandler.java deleted file mode 100644 index 7c65050d..00000000 --- a/src/main/java/com/cricketcraft/chisel/PacketHandler.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.cricketcraft.chisel; - -import com.cricketcraft.chisel.item.chisel.ChiselController; - -import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; -import cpw.mods.fml.relauncher.Side; - -public class PacketHandler { - - public static final SimpleNetworkWrapper INSTANCE = new SimpleNetworkWrapper(Chisel.MOD_ID); - - public static void init() { - INSTANCE.registerMessage(ChiselController.PlaySoundHandler.class, ChiselController.PacketPlaySound.class, 0, Side.CLIENT); - } -} \ No newline at end of file diff --git a/src/main/java/com/cricketcraft/chisel/block/BlockAutoChisel.java b/src/main/java/com/cricketcraft/chisel/block/BlockAutoChisel.java index 9bf227e8..7127ed37 100644 --- a/src/main/java/com/cricketcraft/chisel/block/BlockAutoChisel.java +++ b/src/main/java/com/cricketcraft/chisel/block/BlockAutoChisel.java @@ -20,6 +20,7 @@ public class BlockAutoChisel extends BlockContainer { public BlockAutoChisel() { super(Material.rock); setHardness(1F); + setBlockBounds(0.125f, 0, 0.125f, 0.875f, 0.8125f, 0.875f); } @Override @@ -46,7 +47,7 @@ public class BlockAutoChisel extends BlockContainer { if (tile != null && tile instanceof TileEntityAutoChisel && !player.isSneaking()) { player.openGui(Chisel.instance, 1, world, x, y, z); } - + return true; } diff --git a/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java b/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java index e286ba93..63963c7d 100644 --- a/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java +++ b/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java @@ -17,6 +17,8 @@ import net.minecraft.util.StatCollector; import com.cricketcraft.chisel.api.IChiselItem; import com.cricketcraft.chisel.carving.Carving; import com.cricketcraft.chisel.init.ChiselItems; +import com.cricketcraft.chisel.network.PacketHandler; +import com.cricketcraft.chisel.network.message.MessageSlotUpdate; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -122,22 +124,20 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory if (canMerge(chiseled)) { // if our output is empty, just use the current result if (output == null) { - inventory[OUTPUT] = chiseled; + setInventorySlotContents(OUTPUT, chiseled); } else { // otherwise just add our result to the existing stack inventory[OUTPUT].stackSize += chiseled.stackSize; + slotChanged(OUTPUT); } // remove what we made from the stack base.stackSize -= chiseled.stackSize; if (base.stackSize <= 0) { - inventory[BASE] = null; // clear out 0 size itemstacks + setInventorySlotContents(BASE, null); // clear out 0 size itemstacks } // TODO here we need to send a packet to the client so that the animation and sound can be played ((IChiselItem) inventory[CHISEL].getItem()).onChisel(worldObj, this, CHISEL, inventory[CHISEL], inventory[TARGET]); - - // we changed something, so we need to tell the chunk to save - markDirty(); } } } @@ -221,6 +221,14 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory if (stack != null && stack.stackSize > getInventoryStackLimit()) { stack.stackSize = getInventoryStackLimit(); } + + if (!worldObj.isRemote) { + slotChanged(slot); + } + } + + private void slotChanged(int slot) { + PacketHandler.INSTANCE.sendToDimension(new MessageSlotUpdate(this, slot, inventory[slot]), worldObj.provider.dimensionId); markDirty(); } @@ -246,7 +254,7 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory @Override public int[] getAccessibleSlotsFromSide(int side) { - return new int[] { BASE, OUTPUT }; + return new int[] { BASE, TARGET, OUTPUT, CHISEL }; } @Override @@ -270,7 +278,7 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory @Override public boolean canInsertItem(int slot, ItemStack itemStack, int side) { - return hasUpgrade(Upgrade.AUTOMATION) && slot == BASE; + return hasUpgrade(Upgrade.AUTOMATION) && (slot == BASE || slot == TARGET || slot == CHISEL); } @Override diff --git a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java index 96db8015..41d740e0 100644 --- a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java +++ b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java @@ -1,6 +1,5 @@ package com.cricketcraft.chisel.item.chisel; -import io.netty.buffer.ByteBuf; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; @@ -12,17 +11,14 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent; import org.apache.commons.lang3.ArrayUtils; import com.cricketcraft.chisel.Chisel; -import com.cricketcraft.chisel.PacketHandler; import com.cricketcraft.chisel.api.IChiselItem; import com.cricketcraft.chisel.carving.Carving; import com.cricketcraft.chisel.carving.CarvingVariation; -import com.cricketcraft.chisel.client.GeneralChiselClient; +import com.cricketcraft.chisel.network.PacketHandler; +import com.cricketcraft.chisel.network.message.MessageChiselSound; import com.cricketcraft.chisel.utils.General; import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.network.simpleimpl.IMessage; -import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; -import cpw.mods.fml.common.network.simpleimpl.MessageContext; public final class ChiselController { @@ -92,56 +88,9 @@ public final class ChiselController { * Assumes that the player is holding a chisel */ private void setVariation(EntityPlayer player, World world, int x, int y, int z, CarvingVariation v, ItemStack target) { - PacketHandler.INSTANCE.sendTo(new PacketPlaySound(x, y, z, v), (EntityPlayerMP) player); + PacketHandler.INSTANCE.sendTo(new MessageChiselSound(x, y, z, v), (EntityPlayerMP) player); world.setBlock(x, y, z, v.block); world.setBlockMetadataWithNotify(x, y, z, v.meta, 3); ((IChiselItem) player.getCurrentEquippedItem().getItem()).onChisel(world, player.inventory, player.inventory.currentItem, player.getCurrentEquippedItem(), target); } - - public static class PacketPlaySound implements IMessage { - - public PacketPlaySound() { - } - - private int x, y, z; - private int block; - private byte meta; - - public PacketPlaySound(int x, int y, int z, CarvingVariation v) { - this.x = x; - this.y = y; - this.z = z; - this.block = Block.getIdFromBlock(v.block); - this.meta = (byte) v.meta; - } - - @Override - public void toBytes(ByteBuf buf) { - buf.writeInt(x); - buf.writeInt(y); - buf.writeInt(z); - buf.writeInt(block); - buf.writeByte(meta); - } - - @Override - public void fromBytes(ByteBuf buf) { - x = buf.readInt(); - y = buf.readInt(); - z = buf.readInt(); - block = buf.readInt(); - meta = buf.readByte(); - } - } - - public static class PlaySoundHandler implements IMessageHandler { - - @Override - public IMessage onMessage(PacketPlaySound message, MessageContext ctx) { - String sound = ItemChisel.carving.getVariationSound(Block.getBlockById(message.block), message.meta); - GeneralChiselClient.spawnChiselEffect(message.x, message.y, message.z, sound); - return null; - } - } - } diff --git a/src/main/java/com/cricketcraft/chisel/network/PacketHandler.java b/src/main/java/com/cricketcraft/chisel/network/PacketHandler.java new file mode 100644 index 00000000..d83930da --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/network/PacketHandler.java @@ -0,0 +1,20 @@ +package com.cricketcraft.chisel.network; + +import com.cricketcraft.chisel.Chisel; +import com.cricketcraft.chisel.network.message.MessageChiselSound; +import com.cricketcraft.chisel.network.message.MessageSlotUpdate; + +import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; +import cpw.mods.fml.relauncher.Side; + +public class PacketHandler { + + public static final SimpleNetworkWrapper INSTANCE = new SimpleNetworkWrapper(Chisel.MOD_ID); + + public static void init() { + int id = 0; + + INSTANCE.registerMessage(MessageChiselSound.Handler.class, MessageChiselSound.class, id++, Side.CLIENT); + INSTANCE.registerMessage(MessageSlotUpdate.Handler.class, MessageSlotUpdate.class, id++, Side.CLIENT); + } +} \ No newline at end of file