diff --git a/src/main/java/tconstruct/library/event/SmelteryEvent.java b/src/main/java/tconstruct/library/event/SmelteryEvent.java index 44ac8201..6a90d10b 100644 --- a/src/main/java/tconstruct/library/event/SmelteryEvent.java +++ b/src/main/java/tconstruct/library/event/SmelteryEvent.java @@ -1,5 +1,6 @@ package tconstruct.library.event; +import cpw.mods.fml.common.eventhandler.Cancelable; import cpw.mods.fml.common.eventhandler.Event; import mantle.blocks.abstracts.InventoryLogic; import net.minecraft.entity.player.EntityPlayer; @@ -19,8 +20,10 @@ public class SmelteryEvent extends Event this.z = z; } + @Cancelable public static class ItemInsertedIntoCasting extends SmelteryEvent { + /** Item that will be inserted into the casting block */ public ItemStack item; public EntityPlayer player; @@ -34,6 +37,7 @@ public class SmelteryEvent extends Event public static class ItemRemovedFromCasting extends SmelteryEvent { + /** Item that will be returned to the player */ public ItemStack item; public EntityPlayer player; diff --git a/src/main/java/tconstruct/smeltery/blocks/SearedBlock.java b/src/main/java/tconstruct/smeltery/blocks/SearedBlock.java index 50641650..6d9c9e1d 100644 --- a/src/main/java/tconstruct/smeltery/blocks/SearedBlock.java +++ b/src/main/java/tconstruct/smeltery/blocks/SearedBlock.java @@ -76,14 +76,12 @@ public class SearedBlock extends InventoryBlock public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int side, float clickX, float clickY, float clickZ) { int md = world.getBlockMetadata(x, y, z); - if (md == 0) + // casting table or basin + if (md == 0 || md == 2) { - return activateCastingTable(world, x, y, z, player); - } - if (md == 2) - { - return activateCastingBasin(world, x, y, z, player); + return activateCastingBlock(world, x, y, z, player); } + // faucet else if (md == 1) { if (player.isSneaking()) @@ -97,93 +95,17 @@ public class SearedBlock extends InventoryBlock return super.onBlockActivated(world, x, y, z, player, side, clickX, clickY, clickZ); } - boolean activateCastingTable (World world, int x, int y, int z, EntityPlayer player) + boolean activateCastingBlock (World world, int x, int y, int z, EntityPlayer player) { if (!world.isRemote) { - CastingTableLogic logic = (CastingTableLogic) world.getTileEntity(x, y, z); - if (logic.liquid != null) - return true; - - if (!logic.isStackInSlot(0) && !logic.isStackInSlot(1)) - { - ItemStack stack = player.getCurrentEquippedItem(); - stack = player.inventory.decrStackSize(player.inventory.currentItem, 1); - logic.setInventorySlotContents(0, stack); - MinecraftForge.EVENT_BUS.post(new SmelteryEvent.ItemInsertedIntoCasting(logic, x, y, z, stack, player)); - } - else - { - if (logic.isStackInSlot(1)) - { - MinecraftForge.EVENT_BUS.post(new SmelteryEvent.ItemRemovedFromCasting(logic, x, y, z, logic.getStackInSlot(1), player)); - ItemStack stack = logic.decrStackSize(1, 1); - if (stack != null) - addItemToInventory(player, world, x, y, z, stack); - } - else if (logic.isStackInSlot(0)) - { - MinecraftForge.EVENT_BUS.post(new SmelteryEvent.ItemRemovedFromCasting(logic, x, y, z, logic.getStackInSlot(0), player)); - ItemStack stack = logic.decrStackSize(0, 1); - if (stack != null) - addItemToInventory(player, world, x, y, z, stack); - } - } - + CastingBlockLogic logic = (CastingBasinLogic) world.getTileEntity(x, y, z); + logic.interact(player); world.markBlockForUpdate(x, y, z); } return true; } - boolean activateCastingBasin (World world, int x, int y, int z, EntityPlayer player) - { - if (!world.isRemote) - { - CastingBasinLogic logic = (CastingBasinLogic) world.getTileEntity(x, y, z); - if (logic.liquid != null) - return true; - - if (!logic.isStackInSlot(0) && !logic.isStackInSlot(1)) - { - ItemStack stack = player.getCurrentEquippedItem(); - stack = player.inventory.decrStackSize(player.inventory.currentItem, 1); - logic.setInventorySlotContents(0, stack); - MinecraftForge.EVENT_BUS.post(new SmelteryEvent.ItemInsertedIntoCasting(logic, x, y, z, stack, player)); - } - else - { - if (logic.isStackInSlot(1)) - { - MinecraftForge.EVENT_BUS.post(new SmelteryEvent.ItemRemovedFromCasting(logic, x, y, z, logic.getStackInSlot(1), player)); - ItemStack stack = logic.decrStackSize(1, 1); - if (stack != null) - addItemToInventory(player, world, x, y, z, stack); - } - else if (logic.isStackInSlot(0)) - { - MinecraftForge.EVENT_BUS.post(new SmelteryEvent.ItemRemovedFromCasting(logic, x, y, z, logic.getStackInSlot(0), player)); - ItemStack stack = logic.decrStackSize(0, 1); - if (stack != null) - addItemToInventory(player, world, x, y, z, stack); - } - } - - world.markBlockForUpdate(x, y, z); - } - return true; - } - - public void addItemToInventory (EntityPlayer player, World world, int x, int y, int z, ItemStack stack) - { - AbilityHelper.spawnItemAtPlayer(player, stack); - /*if (!world.isRemote) - { - EntityItem entityitem = new EntityItem(world, (double) x + 0.5D, (double) y + 0.9325D, (double) z + 0.5D, stack); - world.spawnEntityInWorld(entityitem); - entityitem.onCollideWithPlayer(player); - }*/ - } - /* Rendering */ @Override public int getRenderType () diff --git a/src/main/java/tconstruct/smeltery/logic/CastingBlockLogic.java b/src/main/java/tconstruct/smeltery/logic/CastingBlockLogic.java index 813f715c..6944fdb5 100644 --- a/src/main/java/tconstruct/smeltery/logic/CastingBlockLogic.java +++ b/src/main/java/tconstruct/smeltery/logic/CastingBlockLogic.java @@ -2,6 +2,7 @@ package tconstruct.smeltery.logic; import cpw.mods.fml.common.eventhandler.Event; import mantle.blocks.abstracts.InventoryLogic; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.*; import net.minecraft.item.ItemStack; @@ -15,6 +16,7 @@ import net.minecraftforge.fluids.*; import tconstruct.TConstruct; import tconstruct.library.crafting.*; import tconstruct.library.event.*; +import tconstruct.library.tools.AbilityHelper; import tconstruct.library.util.IPattern; public abstract class CastingBlockLogic extends InventoryLogic implements IFluidTank, IFluidHandler, ISidedInventory @@ -132,6 +134,7 @@ public abstract class CastingBlockLogic extends InventoryLogic implements IFluid return liquid != null ? liquid.amount : 0; } + /** Returns the current amount of the liquid FOR RENDERING */ public int getLiquidAmount () { return liquid.amount - renderOffset; @@ -267,6 +270,63 @@ public abstract class CastingBlockLogic extends InventoryLogic implements IFluid /* Inventory, inserting/extracting */ + public void interact(EntityPlayer player) + { + // only server side + if(worldObj.isRemote) + return; + + // can't interact with liquid inside + // todo: maybe let it interact with a bucket or tank! + if(liquid != null) + return; + + // put stuff in? + if(!isStackInSlot(0) && !isStackInSlot(1)) + { + ItemStack stack = player.inventory.decrStackSize(player.inventory.currentItem, stackSizeLimit); + SmelteryEvent.ItemInsertedIntoCasting event = new SmelteryEvent.ItemInsertedIntoCasting(this, xCoord, yCoord, zCoord, stack, player); + MinecraftForge.EVENT_BUS.post(event); + if(!event.isCanceled()) + setInventorySlotContents(0, event.item); + else + player.inventory.addItemStackToInventory(stack); // should never return false, since the itemstack was taken from the inventory + } + // take stuff out. + else + { + int slot = 0; + // output-slot has higher priority + if(isStackInSlot(1)) + slot = 1; + + // Additional Info: Only 1 item can only be put into the casting block usually, however recipies + // can have multiple blocks as output (compressed gravel -> brownstone for example) + // we therefore spill the whole contents on extraction + + SmelteryEvent.ItemRemovedFromCasting event = new SmelteryEvent.ItemRemovedFromCasting(this, xCoord, yCoord, zCoord, getStackInSlot(slot), player); + MinecraftForge.EVENT_BUS.post(event); + + + /* + // try to transfer thes tack to the player inventory + ItemStack output = event.item; + if(!player.inventory.addItemStackToInventory(output)) + { + // drop the rest as an item + AbilityHelper.spawnItemAtPlayer(player, output); + } + // added to inventory, update inventory + else + player.inventory.markDirty(); + */ + AbilityHelper.spawnItemAtPlayer(player, event.item); + + // remove inventory contents, since we spilled the full contents of the slot + inventory[slot] = null; + } + } + @Override public ItemStack decrStackSize (int slot, int quantity) {