2011-02-23 03:37:56 +01:00
|
|
|
package net.minecraft.server;
|
|
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
2012-02-29 19:56:35 +01:00
|
|
|
// CraftBukkit start
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bukkit.craftbukkit.entity.CraftHumanEntity;
|
|
|
|
import org.bukkit.entity.HumanEntity;
|
|
|
|
// CraftBukkit end
|
|
|
|
|
2011-02-23 03:37:56 +01:00
|
|
|
public class TileEntityDispenser extends TileEntity implements IInventory {
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
private ItemStack[] items = new ItemStack[9];
|
2011-02-23 03:37:56 +01:00
|
|
|
private Random b = new Random();
|
|
|
|
|
|
|
|
// CraftBukkit start
|
2012-02-29 19:56:35 +01:00
|
|
|
public List<HumanEntity> transaction = new ArrayList<HumanEntity>();
|
|
|
|
|
2011-02-23 03:37:56 +01:00
|
|
|
public ItemStack[] getContents() {
|
2011-06-27 00:25:01 +02:00
|
|
|
return this.items;
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
2012-02-29 19:56:35 +01:00
|
|
|
|
|
|
|
public void onOpen(CraftHumanEntity who) {
|
|
|
|
transaction.add(who);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onClose(CraftHumanEntity who) {
|
|
|
|
transaction.remove(who);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<HumanEntity> getViewers() {
|
|
|
|
return transaction;
|
|
|
|
}
|
2011-02-23 03:37:56 +01:00
|
|
|
// CraftBukkit end
|
|
|
|
|
|
|
|
public TileEntityDispenser() {}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public int getSize() {
|
2011-02-23 03:37:56 +01:00
|
|
|
return 9;
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public ItemStack getItem(int i) {
|
|
|
|
return this.items[i];
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
public ItemStack splitStack(int i, int j) {
|
2011-04-20 19:05:14 +02:00
|
|
|
if (this.items[i] != null) {
|
2011-02-23 03:37:56 +01:00
|
|
|
ItemStack itemstack;
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
if (this.items[i].count <= j) {
|
|
|
|
itemstack = this.items[i];
|
|
|
|
this.items[i] = null;
|
|
|
|
this.update();
|
2011-02-23 03:37:56 +01:00
|
|
|
return itemstack;
|
|
|
|
} else {
|
2011-04-20 19:05:14 +02:00
|
|
|
itemstack = this.items[i].a(j);
|
|
|
|
if (this.items[i].count == 0) {
|
|
|
|
this.items[i] = null;
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
this.update();
|
2011-02-23 03:37:56 +01:00
|
|
|
return itemstack;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 11:49:23 +01:00
|
|
|
public ItemStack splitWithoutUpdate(int i) {
|
|
|
|
if (this.items[i] != null) {
|
|
|
|
ItemStack itemstack = this.items[i];
|
|
|
|
|
|
|
|
this.items[i] = null;
|
|
|
|
return itemstack;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CraftBukkit start - move code out from p_
|
2011-05-08 11:56:40 +02:00
|
|
|
public int findDispenseSlot() {
|
2011-02-23 03:37:56 +01:00
|
|
|
int i = -1;
|
|
|
|
int j = 1;
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
for (int k = 0; k < this.items.length; ++k) {
|
2011-05-26 14:48:22 +02:00
|
|
|
if (this.items[k] != null && this.b.nextInt(j++) == 0) {
|
|
|
|
if (this.items[k].count == 0) continue; // CraftBukkit
|
2011-02-23 03:37:56 +01:00
|
|
|
i = k;
|
|
|
|
}
|
|
|
|
}
|
2011-05-08 11:56:40 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2012-03-01 11:49:23 +01:00
|
|
|
public ItemStack p_() {
|
2011-06-27 00:25:01 +02:00
|
|
|
int i = this.findDispenseSlot();
|
2011-05-14 16:29:42 +02:00
|
|
|
// CraftBukkit end
|
2011-02-23 03:37:56 +01:00
|
|
|
|
|
|
|
if (i >= 0) {
|
2011-06-27 00:25:01 +02:00
|
|
|
return this.splitStack(i, 1);
|
2011-02-23 03:37:56 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void setItem(int i, ItemStack itemstack) {
|
|
|
|
this.items[i] = itemstack;
|
|
|
|
if (itemstack != null && itemstack.count > this.getMaxStackSize()) {
|
|
|
|
itemstack.count = this.getMaxStackSize();
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
this.update();
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public String getName() {
|
2012-03-01 11:49:23 +01:00
|
|
|
return "container.dispenser";
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void a(NBTTagCompound nbttagcompound) {
|
|
|
|
super.a(nbttagcompound);
|
2011-11-30 00:17:43 +01:00
|
|
|
NBTTagList nbttaglist = nbttagcompound.getList("Items");
|
2011-02-23 03:37:56 +01:00
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
this.items = new ItemStack[this.getSize()];
|
2011-02-23 03:37:56 +01:00
|
|
|
|
2011-11-30 00:17:43 +01:00
|
|
|
for (int i = 0; i < nbttaglist.size(); ++i) {
|
|
|
|
NBTTagCompound nbttagcompound1 = (NBTTagCompound) nbttaglist.get(i);
|
|
|
|
int j = nbttagcompound1.getByte("Slot") & 255;
|
2011-02-23 03:37:56 +01:00
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
if (j >= 0 && j < this.items.length) {
|
2011-09-15 02:23:52 +02:00
|
|
|
this.items[j] = ItemStack.a(nbttagcompound1);
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void b(NBTTagCompound nbttagcompound) {
|
|
|
|
super.b(nbttagcompound);
|
|
|
|
NBTTagList nbttaglist = new NBTTagList();
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
for (int i = 0; i < this.items.length; ++i) {
|
|
|
|
if (this.items[i] != null) {
|
2011-02-23 03:37:56 +01:00
|
|
|
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
|
|
|
|
|
2011-11-30 00:17:43 +01:00
|
|
|
nbttagcompound1.setByte("Slot", (byte) i);
|
2012-02-29 22:31:04 +01:00
|
|
|
this.items[i].save(nbttagcompound1);
|
2011-11-30 00:17:43 +01:00
|
|
|
nbttaglist.add(nbttagcompound1);
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 00:17:43 +01:00
|
|
|
nbttagcompound.set("Items", nbttaglist);
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public int getMaxStackSize() {
|
2011-02-23 03:37:56 +01:00
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
public boolean a(EntityHuman entityhuman) {
|
2011-07-08 14:25:53 +02:00
|
|
|
return this.world.getTileEntity(this.x, this.y, this.z) != this ? false : entityhuman.e((double) this.x + 0.5D, (double) this.y + 0.5D, (double) this.z + 0.5D) <= 64.0D;
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
2011-09-15 02:23:52 +02:00
|
|
|
|
2011-11-20 09:01:14 +01:00
|
|
|
public void f() {}
|
2011-09-15 02:23:52 +02:00
|
|
|
|
2011-11-20 09:01:14 +01:00
|
|
|
public void g() {}
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|