| 1 | package net.minecraft.entity.item;
|
| 2 |
|
| 3 | import net.minecraft.entity.player.EntityPlayer;
|
| 4 | import net.minecraft.inventory.Container;
|
| 5 | import net.minecraft.inventory.InventoryHelper;
|
| 6 | import net.minecraft.item.ItemStack;
|
| 7 | import net.minecraft.nbt.NBTTagCompound;
|
| 8 | import net.minecraft.nbt.NBTTagList;
|
| 9 | import net.minecraft.util.DamageSource;
|
| 10 | import net.minecraft.world.ILockableContainer;
|
| 11 | import net.minecraft.world.LockCode;
|
| 12 | import net.minecraft.world.World;
|
| 13 |
|
| 14 | public abstract class EntityMinecartContainer extends EntityMinecart implements ILockableContainer
|
| 15 | {
|
| 16 | private ItemStack[] minecartContainerItems = new ItemStack[36];
|
| 17 |
|
| 18 | /**
|
| 19 | * When set to true, the minecart will drop all items when setDead() is called. When false (such as when travelling
|
| 20 | * dimensions) it preserves its contents.
|
| 21 | */
|
| 22 | private boolean dropContentsWhenDead = true;
|
| 23 |
|
| 24 | public EntityMinecartContainer(World worldIn)
|
| 25 | {
|
| 26 | super(worldIn);
|
| 27 | }
|
| 28 |
|
| 29 | public EntityMinecartContainer(World worldIn, double x, double y, double z)
|
| 30 | {
|
| 31 | super(worldIn, x, y, z);
|
| 32 | }
|
| 33 |
|
| 34 | public void killMinecart(DamageSource source)
|
| 35 | {
|
| 36 | super.killMinecart(source);
|
| 37 |
|
| 38 | if (this.worldObj.getGameRules().getBoolean("doEntityDrops"))
|
| 39 | {
|
| 40 | InventoryHelper.dropInventoryItems(this.worldObj, this, this);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | /**
|
| 45 | * Returns the stack in the given slot.
|
| 46 | */
|
| 47 | public ItemStack getStackInSlot(int index)
|
| 48 | {
|
| 49 | return this.minecartContainerItems[index];
|
| 50 | }
|
| 51 |
|
| 52 | /**
|
| 53 | * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
|
| 54 | */
|
| 55 | public ItemStack decrStackSize(int index, int count)
|
| 56 | {
|
| 57 | if (this.minecartContainerItems[index] != null)
|
| 58 | {
|
| 59 | if (this.minecartContainerItems[index].stackSize <= count)
|
| 60 | {
|
| 61 | ItemStack itemstack1 = this.minecartContainerItems[index];
|
| 62 | this.minecartContainerItems[index] = null;
|
| 63 | return itemstack1;
|
| 64 | }
|
| 65 | else
|
| 66 | {
|
| 67 | ItemStack itemstack = this.minecartContainerItems[index].splitStack(count);
|
| 68 |
|
| 69 | if (this.minecartContainerItems[index].stackSize == 0)
|
| 70 | {
|
| 71 | this.minecartContainerItems[index] = null;
|
| 72 | }
|
| 73 |
|
| 74 | return itemstack;
|
| 75 | }
|
| 76 | }
|
| 77 | else
|
| 78 | {
|
| 79 | return null;
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | /**
|
| 84 | * Removes a stack from the given slot and returns it.
|
| 85 | */
|
| 86 | public ItemStack removeStackFromSlot(int index)
|
| 87 | {
|
| 88 | if (this.minecartContainerItems[index] != null)
|
| 89 | {
|
| 90 | ItemStack itemstack = this.minecartContainerItems[index];
|
| 91 | this.minecartContainerItems[index] = null;
|
| 92 | return itemstack;
|
| 93 | }
|
| 94 | else
|
| 95 | {
|
| 96 | return null;
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | /**
|
| 101 | * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
|
| 102 | */
|
| 103 | public void setInventorySlotContents(int index, ItemStack stack)
|
| 104 | {
|
| 105 | this.minecartContainerItems[index] = stack;
|
| 106 |
|
| 107 | if (stack != null && stack.stackSize > this.getInventoryStackLimit())
|
| 108 | {
|
| 109 | stack.stackSize = this.getInventoryStackLimit();
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | /**
|
| 114 | * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
|
| 115 | * hasn't changed and skip it.
|
| 116 | */
|
| 117 | public void markDirty()
|
| 118 | {
|
| 119 | }
|
| 120 |
|
| 121 | /**
|
| 122 | * Do not make give this method the name canInteractWith because it clashes with Container
|
| 123 | */
|
| 124 | public boolean isUseableByPlayer(EntityPlayer player)
|
| 125 | {
|
| 126 | return this.isDead ? false : player.getDistanceSqToEntity(this) <= 64.0D;
|
| 127 | }
|
| 128 |
|
| 129 | public void openInventory(EntityPlayer player)
|
| 130 | {
|
| 131 | }
|
| 132 |
|
| 133 | public void closeInventory(EntityPlayer player)
|
| 134 | {
|
| 135 | }
|
| 136 |
|
| 137 | /**
|
| 138 | * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
|
| 139 | */
|
| 140 | public boolean isItemValidForSlot(int index, ItemStack stack)
|
| 141 | {
|
| 142 | return true;
|
| 143 | }
|
| 144 |
|
| 145 | /**
|
| 146 | * Get the name of this object. For players this returns their username
|
| 147 | */
|
| 148 | public String getName()
|
| 149 | {
|
| 150 | return this.hasCustomName() ? this.getCustomNameTag() : "container.minecart";
|
| 151 | }
|
| 152 |
|
| 153 | /**
|
| 154 | * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
|
| 155 | */
|
| 156 | public int getInventoryStackLimit()
|
| 157 | {
|
| 158 | return 64;
|
| 159 | }
|
| 160 |
|
| 161 | /**
|
| 162 | * Teleports the entity to another dimension. Params: Dimension number to teleport to
|
| 163 | */
|
| 164 | public void travelToDimension(int dimensionId)
|
| 165 | {
|
| 166 | this.dropContentsWhenDead = false;
|
| 167 | super.travelToDimension(dimensionId);
|
| 168 | }
|
| 169 |
|
| 170 | /**
|
| 171 | * Will get destroyed next tick.
|
| 172 | */
|
| 173 | public void setDead()
|
| 174 | {
|
| 175 | if (this.dropContentsWhenDead)
|
| 176 | {
|
| 177 | InventoryHelper.dropInventoryItems(this.worldObj, this, this);
|
| 178 | }
|
| 179 |
|
| 180 | super.setDead();
|
| 181 | }
|
| 182 |
|
| 183 | /**
|
| 184 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 185 | */
|
| 186 | protected void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 187 | {
|
| 188 | super.writeEntityToNBT(tagCompound);
|
| 189 | NBTTagList nbttaglist = new NBTTagList();
|
| 190 |
|
| 191 | for (int i = 0; i < this.minecartContainerItems.length; ++i)
|
| 192 | {
|
| 193 | if (this.minecartContainerItems[i] != null)
|
| 194 | {
|
| 195 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 196 | nbttagcompound.setByte("Slot", (byte)i);
|
| 197 | this.minecartContainerItems[i].writeToNBT(nbttagcompound);
|
| 198 | nbttaglist.appendTag(nbttagcompound);
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | tagCompound.setTag("Items", nbttaglist);
|
| 203 | }
|
| 204 |
|
| 205 | /**
|
| 206 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 207 | */
|
| 208 | protected void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 209 | {
|
| 210 | super.readEntityFromNBT(tagCompund);
|
| 211 | NBTTagList nbttaglist = tagCompund.getTagList("Items", 10);
|
| 212 | this.minecartContainerItems = new ItemStack[this.getSizeInventory()];
|
| 213 |
|
| 214 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 215 | {
|
| 216 | NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
|
| 217 | int j = nbttagcompound.getByte("Slot") & 255;
|
| 218 |
|
| 219 | if (j >= 0 && j < this.minecartContainerItems.length)
|
| 220 | {
|
| 221 | this.minecartContainerItems[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | /**
|
| 227 | * First layer of player interaction
|
| 228 | */
|
| 229 | public boolean interactFirst(EntityPlayer playerIn)
|
| 230 | {
|
| 231 | if (!this.worldObj.isRemote)
|
| 232 | {
|
| 233 | playerIn.displayGUIChest(this);
|
| 234 | }
|
| 235 |
|
| 236 | return true;
|
| 237 | }
|
| 238 |
|
| 239 | protected void applyDrag()
|
| 240 | {
|
| 241 | int i = 15 - Container.calcRedstoneFromInventory(this);
|
| 242 | float f = 0.98F + (float)i * 0.001F;
|
| 243 | this.motionX *= (double)f;
|
| 244 | this.motionY *= 0.0D;
|
| 245 | this.motionZ *= (double)f;
|
| 246 | }
|
| 247 |
|
| 248 | public int getField(int id)
|
| 249 | {
|
| 250 | return 0;
|
| 251 | }
|
| 252 |
|
| 253 | public void setField(int id, int value)
|
| 254 | {
|
| 255 | }
|
| 256 |
|
| 257 | public int getFieldCount()
|
| 258 | {
|
| 259 | return 0;
|
| 260 | }
|
| 261 |
|
| 262 | public boolean isLocked()
|
| 263 | {
|
| 264 | return false;
|
| 265 | }
|
| 266 |
|
| 267 | public void setLockCode(LockCode code)
|
| 268 | {
|
| 269 | }
|
| 270 |
|
| 271 | public LockCode getLockCode()
|
| 272 | {
|
| 273 | return LockCode.EMPTY_CODE;
|
| 274 | }
|
| 275 |
|
| 276 | public void clear()
|
| 277 | {
|
| 278 | for (int i = 0; i < this.minecartContainerItems.length; ++i)
|
| 279 | {
|
| 280 | this.minecartContainerItems[i] = null;
|
| 281 | }
|
| 282 | }
|
| 283 | }
|
| 284 |
|