From 5188e39b5745269cc23d2e4302acc303274b64f1 Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Mon, 19 Jan 2015 10:01:08 -0500 Subject: [PATCH] Fix up issues with breaking chisels (Fixes #162) --- .../cricketcraft/chisel/api/IChiselItem.java | 3 ++- .../block/tileentity/TileEntityAutoChisel.java | 7 ++++++- .../chisel/carving/CarvableHelper.java | 3 --- .../chisel/inventory/SlotChiselSelection.java | 7 ++++++- .../chisel/item/chisel/ChiselController.java | 8 -------- .../chisel/item/chisel/ChiselMode.java | 17 ++++++++++++++--- .../chisel/item/chisel/ItemChisel.java | 11 ++--------- .../network/message/MessageChiselSound.java | 13 +++++++++++-- 8 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/cricketcraft/chisel/api/IChiselItem.java b/src/main/java/com/cricketcraft/chisel/api/IChiselItem.java index 8063cf74..f3015a93 100644 --- a/src/main/java/com/cricketcraft/chisel/api/IChiselItem.java +++ b/src/main/java/com/cricketcraft/chisel/api/IChiselItem.java @@ -39,8 +39,9 @@ public interface IChiselItem { * The {@link ItemStack} representing the chisel * @param target * The {@link ItemStack} representing the target item + * @return True if the chisel should be damaged. False otherwise. */ - void onChisel(World world, IInventory inv, int slot, ItemStack chisel, ICarvingVariation target); + boolean onChisel(World world, IInventory inv, int slot, ItemStack chisel, ICarvingVariation target); /** * Called to check if this {@link ItemStack} can be chiseled in this chisel. If not, there will be no possible variants displayed in the GUI. 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 33e229db..d330dd1f 100644 --- a/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java +++ b/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java @@ -209,7 +209,12 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory /** Calls IChiselItem#onChisel() and sends the chisel packet for sound/animation */ private void chiselItem(int chiseled) { if (!worldObj.isRemote) { - ((IChiselItem) inventory[CHISEL].getItem()).onChisel(worldObj, this, CHISEL, inventory[CHISEL], General.getVariation(inventory[TARGET])); + if (((IChiselItem) inventory[CHISEL].getItem()).onChisel(worldObj, this, CHISEL, inventory[CHISEL], General.getVariation(inventory[TARGET]))) { + inventory[CHISEL].setItemDamage(inventory[CHISEL].getItemDamage() - 1); + if (inventory[CHISEL].getItemDamage() >= inventory[CHISEL].getMaxDamage()) { + inventory[CHISEL] = null; + } + } PacketHandler.INSTANCE.sendToDimension(new MessageAutoChisel(this, chiseled, true), worldObj.provider.dimensionId); } else { GeneralChiselClient.spawnAutoChiselFX(this, lastBase != null ? lastBase : inventory[BASE]); diff --git a/src/main/java/com/cricketcraft/chisel/carving/CarvableHelper.java b/src/main/java/com/cricketcraft/chisel/carving/CarvableHelper.java index 7b37b1cc..cbbc938e 100644 --- a/src/main/java/com/cricketcraft/chisel/carving/CarvableHelper.java +++ b/src/main/java/com/cricketcraft/chisel/carving/CarvableHelper.java @@ -38,9 +38,6 @@ public class CarvableHelper { public static final int V4 = 7; public static final int CTMX = 8; - public CarvableHelper() { - } - public ArrayList variations = new ArrayList(); CarvableVariation[] map = new CarvableVariation[16]; public boolean forbidChiseling = false; diff --git a/src/main/java/com/cricketcraft/chisel/inventory/SlotChiselSelection.java b/src/main/java/com/cricketcraft/chisel/inventory/SlotChiselSelection.java index 481ba578..f40f4c0f 100644 --- a/src/main/java/com/cricketcraft/chisel/inventory/SlotChiselSelection.java +++ b/src/main/java/com/cricketcraft/chisel/inventory/SlotChiselSelection.java @@ -60,7 +60,12 @@ public class SlotChiselSelection extends Slot { selInventory.updateItems(); - ((IChiselItem)container.chisel.getItem()).onChisel(player.worldObj, player.inventory, player.inventory.currentItem, container.chisel, General.getVariation(crafted)); + if (((IChiselItem) container.chisel.getItem()).onChisel(player.worldObj, player.inventory, player.inventory.currentItem, container.chisel, General.getVariation(crafted))) { + container.chisel.damageItem(1, player); + if (container.chisel.stackSize <= 0) { + player.inventory.mainInventory[player.inventory.currentItem] = null; + } + } if (player.worldObj.isRemote) { String sound = Carving.chisel.getVariationSound(crafted.getItem(), crafted.getItemDamage()); 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 eb4770fd..4df6578c 100644 --- a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java +++ b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java @@ -4,7 +4,6 @@ import java.util.List; import net.minecraft.block.Block; import net.minecraft.item.ItemStack; -import net.minecraft.util.ChatComponentText; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.event.entity.player.PlayerInteractEvent; @@ -91,13 +90,6 @@ public final class ChiselController { } else { lastTickClick = event.world.getTotalWorldTime(); } - - if (!event.world.isRemote && event.entityPlayer.isSneaking()) { - ChiselMode mode = General.getChiselMode(held); - General.setChiselMode(held, ChiselMode.values()[(mode.ordinal() + 1) % 3]); - event.entityPlayer.addChatMessage(new ChatComponentText(General.getChiselMode(held).toString())); - break; - } if (!event.world.isRemote && chisel.canOpenGui(event.world, event.entityPlayer, held)) { event.entityPlayer.openGui(Chisel.instance, 0, event.world, 0, 0, 0); } diff --git a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselMode.java b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselMode.java index 43bf16b2..6efcab48 100644 --- a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselMode.java +++ b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselMode.java @@ -3,6 +3,7 @@ package com.cricketcraft.chisel.item.chisel; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; import net.minecraft.util.MathHelper; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; @@ -93,6 +94,7 @@ public enum ChiselMode { private static void setVariation(EntityPlayer player, World world, int x, int y, int z, Block origBlock, int origMeta, ICarvingVariation v) { Block block = world.getBlock(x, y, z); int meta = world.getBlockMetadata(x, y, z); + ItemStack held = player.getCurrentEquippedItem(); if (block == v.getBlock() && meta == v.getBlockMeta()) { return; // don't chisel to the same thing } @@ -100,9 +102,18 @@ public enum ChiselMode { return; // don't chisel if this doesn't match the target block (for the AOE modes) } - PacketHandler.INSTANCE.sendTo(new MessageChiselSound(x, y, z, v), (EntityPlayerMP) player); - world.setBlock(x, y, z, v.getBlock(), v.getBlockMeta(), 3); - ((IChiselItem) player.getCurrentEquippedItem().getItem()).onChisel(world, player.inventory, player.inventory.currentItem, player.getCurrentEquippedItem(), v); + if (held != null && held.getItem() instanceof IChiselItem) { + world.setBlock(x, y, z, v.getBlock(), v.getBlockMeta(), 3); + boolean breakChisel = false; + if (((IChiselItem) player.getCurrentEquippedItem().getItem()).onChisel(world, player.inventory, player.inventory.currentItem, player.getCurrentEquippedItem(), v)) { + player.getCurrentEquippedItem().damageItem(1, player); + if (player.getCurrentEquippedItem().stackSize <= 0) { + player.inventory.mainInventory[player.inventory.currentItem] = null; + breakChisel = true; + } + } + PacketHandler.INSTANCE.sendTo(new MessageChiselSound(x, y, z, v, breakChisel), (EntityPlayerMP) player); + } } public ChiselMode next() { diff --git a/src/main/java/com/cricketcraft/chisel/item/chisel/ItemChisel.java b/src/main/java/com/cricketcraft/chisel/item/chisel/ItemChisel.java index f740e6e0..7d14dd67 100644 --- a/src/main/java/com/cricketcraft/chisel/item/chisel/ItemChisel.java +++ b/src/main/java/com/cricketcraft/chisel/item/chisel/ItemChisel.java @@ -7,7 +7,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.util.MathHelper; import net.minecraft.util.StatCollector; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; @@ -17,7 +16,6 @@ import com.cricketcraft.chisel.api.IChiselItem; import com.cricketcraft.chisel.api.carving.ICarvingVariation; import com.cricketcraft.chisel.carving.Carving; import com.cricketcraft.chisel.config.Configurations; -import com.cricketcraft.chisel.utils.GeneralClient; public class ItemChisel extends Item implements IChiselItem { @@ -88,13 +86,8 @@ public class ItemChisel extends Item implements IChiselItem { } @Override - public void onChisel(World world, IInventory inv, int slot, ItemStack chisel, ICarvingVariation target) { - if (Configurations.allowChiselDamage) { - chisel.setItemDamage(chisel.getItemDamage() + 1); - if (chisel.getItemDamage() >= chisel.getMaxDamage()) { - inv.decrStackSize(slot, 1); - } - } + public boolean onChisel(World world, IInventory inv, int slot, ItemStack chisel, ICarvingVariation target) { + return Configurations.allowChiselDamage; } @Override diff --git a/src/main/java/com/cricketcraft/chisel/network/message/MessageChiselSound.java b/src/main/java/com/cricketcraft/chisel/network/message/MessageChiselSound.java index 781e3da6..a910eda5 100644 --- a/src/main/java/com/cricketcraft/chisel/network/message/MessageChiselSound.java +++ b/src/main/java/com/cricketcraft/chisel/network/message/MessageChiselSound.java @@ -2,9 +2,10 @@ package com.cricketcraft.chisel.network.message; import io.netty.buffer.ByteBuf; import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import com.cricketcraft.chisel.Chisel; import com.cricketcraft.chisel.api.carving.ICarvingVariation; -import com.cricketcraft.chisel.carving.CarvingVariation; import com.cricketcraft.chisel.client.GeneralChiselClient; import com.cricketcraft.chisel.item.chisel.ItemChisel; import com.cricketcraft.chisel.network.message.base.MessageCoords; @@ -21,11 +22,13 @@ public class MessageChiselSound extends MessageCoords { private int block; private byte meta; + private boolean breakChisel; - public MessageChiselSound(int x, int y, int z, ICarvingVariation v) { + public MessageChiselSound(int x, int y, int z, ICarvingVariation v, boolean breakChisel) { super(x, y, z); this.block = Block.getIdFromBlock(v.getBlock()); this.meta = (byte) v.getBlockMeta(); + this.breakChisel = breakChisel; } @Override @@ -33,6 +36,7 @@ public class MessageChiselSound extends MessageCoords { super.toBytes(buf); buf.writeInt(block); buf.writeByte(meta); + buf.writeBoolean(breakChisel); } @Override @@ -40,6 +44,7 @@ public class MessageChiselSound extends MessageCoords { super.fromBytes(buf); block = buf.readInt(); meta = buf.readByte(); + breakChisel = buf.readBoolean(); } public static class Handler implements IMessageHandler { @@ -48,6 +53,10 @@ public class MessageChiselSound extends MessageCoords { public IMessage onMessage(MessageChiselSound message, MessageContext ctx) { String sound = ItemChisel.carving.getVariationSound(Block.getBlockById(message.block), message.meta); GeneralChiselClient.spawnChiselEffect(message.x, message.y, message.z, sound); + if (message.breakChisel) { + EntityPlayer player = Chisel.proxy.getClientPlayer(); + player.renderBrokenItemStack(player.getCurrentEquippedItem()); + } return null; } }