summaryrefslogtreecommitdiff
path: root/src/main/java/gmail/Lance5057/blocks
diff options
context:
space:
mode:
authorLance5057 <Lance5057@gmail.com>2015-02-09 08:04:46 -0600
committerLance5057 <Lance5057@gmail.com>2015-02-09 08:04:46 -0600
commit0fe568fc1800274fa3f0c68a09e0ad8d6b5e3e62 (patch)
tree32770788690fc72e2a147c80e732647f1c4ffa09 /src/main/java/gmail/Lance5057/blocks
parentd0dd1569124f26af121ba0a8bf039b1157006579 (diff)
Attempt at making crest mount have an inventory.
A failed attempt
Diffstat (limited to 'src/main/java/gmail/Lance5057/blocks')
-rw-r--r--src/main/java/gmail/Lance5057/blocks/CrestMount.java80
-rw-r--r--src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java219
2 files changed, 293 insertions, 6 deletions
diff --git a/src/main/java/gmail/Lance5057/blocks/CrestMount.java b/src/main/java/gmail/Lance5057/blocks/CrestMount.java
index 66f0762..1401057 100644
--- a/src/main/java/gmail/Lance5057/blocks/CrestMount.java
+++ b/src/main/java/gmail/Lance5057/blocks/CrestMount.java
@@ -4,14 +4,22 @@ import static net.minecraftforge.common.util.ForgeDirection.EAST;
import static net.minecraftforge.common.util.ForgeDirection.NORTH;
import static net.minecraftforge.common.util.ForgeDirection.SOUTH;
import static net.minecraftforge.common.util.ForgeDirection.WEST;
+import gmail.Lance5057.com.mod_TinkersDefense;
+import gmail.Lance5057.gui.Gui_CrestMount;
+
+import java.util.Random;
+
+import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
-import net.minecraft.entity.EntityLivingBase;
+import net.minecraft.entity.item.EntityItem;
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
+import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
-import net.minecraft.tileentity.TileEntityFurnace;
-import net.minecraft.util.MathHelper;
+import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.world.World;
public class CrestMount extends BlockContainer {
@@ -21,6 +29,66 @@ public class CrestMount extends BlockContainer {
super(Material.iron);
this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
}
+
+ @Override
+ public void onBlockAdded(World world, int i, int j, int k)
+ {
+ super.onBlockAdded(world, i, j, k);
+ world.markBlockForUpdate(i, j, k);
+ }
+
+ @Override
+ public boolean onBlockActivated(World world, int x, int y, int z,
+ EntityPlayer player, int metadata, float what, float these, float are) {
+ TileEntity tileEntity = world.getTileEntity(x, y, z);
+ if (tileEntity == null || player.isSneaking()) {
+ return false;
+ }
+ //code to open gui explained later
+ player.openGui(mod_TinkersDefense.instance, 0, world, x, y, z);
+ return true;
+ }
+
+ @Override
+ public void breakBlock(World world, int x, int y, int z, Block par5, int par6) {
+ dropItems(world, x, y, z);
+ super.breakBlock(world, x, y, z, par5, par6);
+ }
+
+ private void dropItems(World world, int x, int y, int z){
+ Random rand = new Random();
+
+ TileEntity tileEntity = world.getTileEntity(x, y, z);
+ if (!(tileEntity instanceof IInventory)) {
+ return;
+ }
+ IInventory inventory = (IInventory) tileEntity;
+
+ for (int i = 0; i < inventory.getSizeInventory(); i++) {
+ ItemStack item = inventory.getStackInSlot(i);
+
+ if (item != null && item.stackSize > 0) {
+ float rx = rand.nextFloat() * 0.8F + 0.1F;
+ float ry = rand.nextFloat() * 0.8F + 0.1F;
+ float rz = rand.nextFloat() * 0.8F + 0.1F;
+
+ EntityItem entityItem = new EntityItem(world,
+ x + rx, y + ry, z + rz,
+ new ItemStack(item.getItem(), item.stackSize, item.getItemDamage()));
+
+ if (item.hasTagCompound()) {
+ entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
+ }
+
+ float factor = 0.05F;
+ entityItem.motionX = rand.nextGaussian() * factor;
+ entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
+ entityItem.motionZ = rand.nextGaussian() * factor;
+ world.spawnEntityInWorld(entityItem);
+ item.stackSize = 0;
+ }
+ }
+}
//You don't want the normal render type, or it wont render properly.
@Override
@@ -46,7 +114,8 @@ public class CrestMount extends BlockContainer {
@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
- return new TileEntity_CrestMount();
+ TileEntity_CrestMount te = new TileEntity_CrestMount();
+ return te;
}
@Override
@@ -75,4 +144,5 @@ public class CrestMount extends BlockContainer {
}
return j1;
- }}
+ }
+}
diff --git a/src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java b/src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java
index 486689c..2e9d848 100644
--- a/src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java
+++ b/src/main/java/gmail/Lance5057/blocks/TileEntity_CrestMount.java
@@ -1,7 +1,224 @@
package gmail.Lance5057.blocks;
+import java.util.Iterator;
+import java.util.List;
+
+import gmail.Lance5057.com.mod_TinkersDefense;
+import gmail.Lance5057.proxy.Handler_CrestMount;
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.inventory.ContainerChest;
+import net.minecraft.inventory.IInventory;
+import net.minecraft.inventory.InventoryLargeChest;
+import net.minecraft.item.ItemStack;
+import net.minecraft.nbt.NBTTagCompound;
+import net.minecraft.nbt.NBTTagList;
+import net.minecraft.network.Packet;
import net.minecraft.tileentity.TileEntity;
+import net.minecraft.util.AxisAlignedBB;
+import net.minecraft.world.World;
+
+public class TileEntity_CrestMount extends TileEntity implements IInventory{
+
+ private ItemStack[] inventory = new ItemStack[4];
+ private String name = "Item";
+ private int facing;
+ private int numUsingPlayers;
+ private int ticksSinceSync;
+
+ @Override
+ public int getSizeInventory() {
+ return inventory.length;
+ }
+
+ @Override
+ public ItemStack getStackInSlot(int slot) {
+ return inventory[slot];
+ }
+
+ @Override
+ public ItemStack decrStackSize(int slot, int amount) {
+ ItemStack stack = getStackInSlot(slot);
+ if(stack != null)
+ {
+ if(stack.stackSize > amount)
+ {
+ stack = stack.splitStack(amount);
+ markDirty();
+ }
+ else
+ {
+ setInventorySlotContents(slot, null);
+ }
+ }
+ return stack;
+ }
+
+ @Override
+ public ItemStack getStackInSlotOnClosing(int slot) {
+ ItemStack stack = getStackInSlot(slot);
+ if(stack != null)
+ {
+ setInventorySlotContents(slot, null);
+ }
+ return stack;
+ }
+
+ @Override
+ public void setInventorySlotContents(int slot, ItemStack itemstack) {
+ this.inventory[slot] = itemstack;
+
+ if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
+ {
+ itemstack.stackSize = this.getInventoryStackLimit();
+ }
+
+ markDirty();
+ }
+
+ @Override
+ public String getInventoryName() {
+ return name;
+ }
+
+ @Override
+ public boolean hasCustomInventoryName() {
+ return name.length() > 0;
+ }
+
+ @Override
+ public int getInventoryStackLimit() {
+ return 1;
+ }
+
+ @Override
+ public boolean isUseableByPlayer(EntityPlayer player) {
+ return true;
+ }
+
+ @Override
+ public void openInventory() {
+ if (worldObj == null) return;
+ numUsingPlayers++;
+ worldObj.addBlockEvent(xCoord, yCoord, zCoord, mod_TinkersDefense.block_CrestMount, 1, numUsingPlayers);
+ }
+
+ @Override
+ public void closeInventory() {
+ if (worldObj == null) return;
+ numUsingPlayers--;
+ worldObj.addBlockEvent(xCoord, yCoord, zCoord, mod_TinkersDefense.block_CrestMount, 1, numUsingPlayers);
+ }
+
+ @Override
+ public boolean receiveClientEvent(int p_145842_1_, int p_145842_2_)
+ {
+ if (p_145842_1_ == 1)
+ {
+ this.numUsingPlayers = p_145842_2_;
+ return true;
+ }
+ else
+ {
+ return super.receiveClientEvent(p_145842_1_, p_145842_2_);
+ }
+ }
+
+ @Override
+ public void updateEntity()
+ {
+ super.updateEntity();
+ ++this.ticksSinceSync;
+ float f;
+
+ if (!this.worldObj.isRemote && this.numUsingPlayers != 0 && (this.ticksSinceSync + this.xCoord + this.yCoord + this.zCoord) % 200 == 0)
+ {
+ this.numUsingPlayers = 0;
+ f = 5.0F;
+ List list = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox((double)((float)this.xCoord - f), (double)((float)this.yCoord - f), (double)((float)this.zCoord - f), (double)((float)(this.xCoord + 1) + f), (double)((float)(this.yCoord + 1) + f), (double)((float)(this.zCoord + 1) + f)));
+ Iterator iterator = list.iterator();
+
+ while (iterator.hasNext())
+ {
+ EntityPlayer entityplayer = (EntityPlayer)iterator.next();
+
+ if (entityplayer.openContainer instanceof ContainerChest)
+ {
+ IInventory iinventory = ((ContainerChest)entityplayer.openContainer).getLowerChestInventory();
+
+ if (iinventory == this || iinventory instanceof InventoryLargeChest && ((InventoryLargeChest)iinventory).isPartOfLargeChest(this))
+ {
+ ++this.numUsingPlayers;
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public boolean isItemValidForSlot(int p_94041_1_, ItemStack itemstack) {
+ return true;
+ }
+
+ public int getFacing()
+ {
+ return this.facing;
+ }
+
+ public void setFacing(int facing_o)
+ {
+ facing = facing_o;
+ }
+
+ @Override
+ public Packet getDescriptionPacket()
+ {
+ return Handler_CrestMount.getPacket(this);
+ }
+
+ public void readFromNBT(NBTTagCompound p_145839_1_)
+ {
+ super.readFromNBT(p_145839_1_);
+ NBTTagList nbttaglist = p_145839_1_.getTagList("Items", 10);
+ this.inventory = new ItemStack[this.getSizeInventory()];
+
+ if (p_145839_1_.hasKey("CustomName", 8))
+ {
+ this.name = p_145839_1_.getString("CustomName");
+ }
+
+ for (int i = 0; i < nbttaglist.tagCount(); ++i)
+ {
+ NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
+ int j = nbttagcompound1.getByte("Slot") & 255;
+
+ if (j >= 0 && j < this.inventory.length)
+ {
+ this.inventory[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
+ }
+ }
+ }
+
+ public void writeToNBT(NBTTagCompound p_145841_1_)
+ {
+ super.writeToNBT(p_145841_1_);
+ NBTTagList nbttaglist = new NBTTagList();
+
+ for (int i = 0; i < this.inventory.length; ++i)
+ {
+ if (this.inventory[i] != null)
+ {
+ NBTTagCompound nbttagcompound1 = new NBTTagCompound();
+ nbttagcompound1.setByte("Slot", (byte)i);
+ this.inventory[i].writeToNBT(nbttagcompound1);
+ nbttaglist.appendTag(nbttagcompound1);
+ }
+ }
-public class TileEntity_CrestMount extends TileEntity {
+ p_145841_1_.setTag("Items", nbttaglist);
+ if (this.hasCustomInventoryName())
+ {
+ p_145841_1_.setString("CustomName", this.name);
+ }
+ }
}