diff --git a/src/main/java/nmd/primal/forgecraft/blocks/Firebox.java b/src/main/java/nmd/primal/forgecraft/blocks/Firebox.java index 35f5d3ed..eee43b98 100644 --- a/src/main/java/nmd/primal/forgecraft/blocks/Firebox.java +++ b/src/main/java/nmd/primal/forgecraft/blocks/Firebox.java @@ -5,8 +5,15 @@ import net.minecraft.block.BlockContainer; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.InventoryHelper; +import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; +import net.minecraft.tileentity.TileEntityFurnace; import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; @@ -15,11 +22,13 @@ import net.minecraftforge.fml.relauncher.SideOnly; import nmd.primal.forgecraft.ModInfo; import nmd.primal.forgecraft.tiles.TileFirebox; +import javax.annotation.Nullable; + /** * Created by kitsu on 11/26/2016. */ -//public class Firebox extends BlockContainer implements ITileEntityProvider { -public class Firebox extends Block { +public class Firebox extends BlockContainer implements ITileEntityProvider { +//public class Firebox extends Block { public Firebox(Material material) { super(material); setUnlocalizedName(ModInfo.ForgecraftBlocks.FIREBOX.getUnlocalizedName()); @@ -27,11 +36,66 @@ public class Firebox extends Block { setCreativeTab(ModInfo.TAB_FORGECRAFT); } - //@Override - //public TileEntity createNewTileEntity(World worldIn, int meta) - //{ - // return new TileFirebox(); - //} + @Override + public TileEntity createNewTileEntity(World worldIn, int meta) + { + return new TileFirebox(); + } + + @Override + public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) + { + if (!world.isRemote) + { + TileFirebox tile = (TileFirebox) world.getTileEntity(pos); + if (tile != null) + { + if (tile.getStackInSlot(0) == null){ + if (player.inventory.getCurrentItem()!=null) { + tile.setInventorySlotContents(0, player.inventory.getCurrentItem()); + player.inventory.setInventorySlotContents(player.inventory.currentItem, null); + return true; + } + } + if (tile.getStackInSlot(0) != null){ + if (player.inventory.getCurrentItem()==null) { + player.inventory.setInventorySlotContents(player.inventory.currentItem, tile.getStackInSlot(0)); + tile.setInventorySlotContents(0, null); + return true; + } + } + } + } + + return false; + } + + /** + * Called serverside after this block is replaced with another in Chunk, but before the Tile Entity is updated + */ + @Override + public void breakBlock(World worldIn, BlockPos pos, IBlockState state) + { + if (!worldIn.isRemote && worldIn.getGameRules().getBoolean("doTileDrops")) + { + TileEntity tileentity = worldIn.getTileEntity(pos); + + if (tileentity instanceof TileFirebox) + { + InventoryHelper.dropInventoryItems(worldIn, pos, (TileFirebox)tileentity); + worldIn.updateComparatorOutputLevel(pos, this); + } + } + + super.breakBlock(worldIn, pos, state); + } + + @Override + public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack){ + if (stack.hasDisplayName()){ + ((TileFirebox) world.getTileEntity(pos)).setCustomName(stack.getDisplayName()); + } + } @Override public boolean isFullCube(IBlockState state) diff --git a/src/main/java/nmd/primal/forgecraft/tiles/TileFirebox.java b/src/main/java/nmd/primal/forgecraft/tiles/TileFirebox.java index e911bbfd..ad054c62 100644 --- a/src/main/java/nmd/primal/forgecraft/tiles/TileFirebox.java +++ b/src/main/java/nmd/primal/forgecraft/tiles/TileFirebox.java @@ -1,11 +1,205 @@ package nmd.primal.forgecraft.tiles; +import net.minecraft.client.gui.GuiChat; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.tileentity.TileEntity; /** * Created by mminaie on 11/30/16. */ -public class TileFirebox extends TileEntity { +public class TileFirebox extends TileEntity implements IInventory { + private ItemStack[] inventory; + private String customName; + + public TileFirebox() { + this.inventory = new ItemStack[this.getSizeInventory()]; + } + + public String getCustomName() { + return customName; + } + + public void setCustomName (String customName){ + this.customName = customName; + } + + @Override + public String getName(){ + //if custName is true return this.customName if false return --v + // ? means if true : means if false + return this.hasCustomName() ? this.customName : "container.firebox"; + } + + @Override + public boolean hasCustomName() { + return this.customName != null && !this.customName.equals(""); + } + + public int getSizeInventory() { + return 1; + } + + @Override + public ItemStack getStackInSlot(int index) { + if (index < 0 || index >= this.getSizeInventory()) + return null; + return this.inventory[index]; + } + + @Override + public ItemStack decrStackSize(int index, int count) { + if (this.getStackInSlot(index) != null) { + ItemStack itemstack; + + if (this.getStackInSlot(index).stackSize <= count) { + itemstack = this.getStackInSlot(index); + this.setInventorySlotContents(index, null); + this.markDirty(); + return itemstack; + } else { + itemstack = this.getStackInSlot(index).splitStack(count); + + if (this.getStackInSlot(index).stackSize <= 0) { + this.setInventorySlotContents(index, null); + } else { + //Just to show that changes happened + this.setInventorySlotContents(index, this.getStackInSlot(index)); + } + + this.markDirty(); + return itemstack; + } + } else { + return null; + } + } + + @Override + public void setInventorySlotContents(int index, ItemStack stack) { + if (index < 0 || index >= this.getSizeInventory()) + return; + + if (stack != null && stack.stackSize > this.getInventoryStackLimit()) + stack.stackSize = this.getInventoryStackLimit(); + + if (stack != null && stack.stackSize == 0) + stack = null; + + this.inventory[index] = stack; + this.markDirty(); + } + + @Override + public ItemStack removeStackFromSlot(int index) { + ItemStack stack = this.getStackInSlot(index); + this.setInventorySlotContents(index, null); + return stack; + } + + @Override + public int getInventoryStackLimit() { + return 64; + } + + @Override + public boolean isUseableByPlayer(EntityPlayer player){ + return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5f)) <= 64; + } + + @Override + public void openInventory(EntityPlayer player) { + + } + + public void closeInventory(EntityPlayer player){ + + } + + @Override + public boolean isItemValidForSlot(int index, ItemStack stack) { + return true; + } + + public int getField(int id) { + return 0; + } + + @Override + public void setField(int id, int value) { + + } + + @Override + public int getFieldCount() { + return 0; + } + + @Override + public void clear() { + for(int i = 0; i < this.getSizeInventory(); i++){ + this.setInventorySlotContents(i, null); + } + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound nbt) { + super.writeToNBT(nbt); + + NBTTagList list = new NBTTagList(); + for (int i = 0; i < this.getSizeInventory(); ++i) { + if (this.getStackInSlot(i) != null) { + NBTTagCompound stackTag = new NBTTagCompound(); + stackTag.setByte("Slot", (byte) i); + this.getStackInSlot(i).writeToNBT(stackTag); + list.appendTag(stackTag); + } + } + nbt.setTag("Items", list); + + if (this.hasCustomName()) { + nbt.setString("CustomName", this.getCustomName()); + } + return nbt; + } + + + @Override + public void readFromNBT(NBTTagCompound nbt) { + super.readFromNBT(nbt); + + NBTTagList list = nbt.getTagList("Items", 10); + for (int i = 0; i < list.tagCount(); ++i) { + NBTTagCompound stackTag = list.getCompoundTagAt(i); + int slot = stackTag.getByte("Slot") & 255; + this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag)); + } + + if (nbt.hasKey("CustomName", 8)) { + this.setCustomName(nbt.getString("CustomName")); + } + } + + @Override + public NBTTagCompound getUpdateTag() + { + return writeToNBT(new NBTTagCompound()); + } + + @Override + public SPacketUpdateTileEntity getUpdatePacket() { + return new SPacketUpdateTileEntity(this.pos, 0, this.writeToNBT(new NBTTagCompound())); + } + + @Override + public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) { + this.readFromNBT(packet.getNbtCompound()); + } }