Paper/src/main/java/net/minecraft/server/BlockDispenser.java

190 Zeilen
6.3 KiB
Java

2011-02-01 22:55:30 +01:00
package net.minecraft.server;
import java.util.Random;
2011-05-14 16:29:42 +02:00
2011-02-01 22:55:30 +01:00
public class BlockDispenser extends BlockContainer {
public static final IRegistry a = new RegistryDefault(new DispenseBehaviorItem());
2013-03-13 23:33:27 +01:00
protected Random b = new Random();
public static boolean eventFired = false; // CraftBukkit
2011-05-26 14:48:22 +02:00
2011-02-01 22:55:30 +01:00
protected BlockDispenser(int i) {
2011-02-01 23:01:37 +01:00
super(i, Material.STONE);
2012-07-29 09:33:13 +02:00
this.a(CreativeModeTab.d);
2011-02-01 22:55:30 +01:00
}
2013-03-13 23:33:27 +01:00
public int a(World world) {
2011-02-01 22:55:30 +01:00
return 4;
}
public void onPlace(World world, int i, int j, int k) {
super.onPlace(world, i, j, k);
2013-03-13 23:33:27 +01:00
this.k(world, i, j, k);
2011-02-01 22:55:30 +01:00
}
2013-03-13 23:33:27 +01:00
private void k(World world, int i, int j, int k) {
2011-04-20 22:47:26 +02:00
if (!world.isStatic) {
int l = world.getTypeId(i, j, k - 1);
int i1 = world.getTypeId(i, j, k + 1);
int j1 = world.getTypeId(i - 1, j, k);
int k1 = world.getTypeId(i + 1, j, k);
byte b0 = 3;
2013-07-01 13:03:00 +02:00
if (Block.t[l] && !Block.t[i1]) {
2011-04-20 22:47:26 +02:00
b0 = 3;
}
2013-07-01 13:03:00 +02:00
if (Block.t[i1] && !Block.t[l]) {
2011-04-20 22:47:26 +02:00
b0 = 2;
}
2013-07-01 13:03:00 +02:00
if (Block.t[j1] && !Block.t[k1]) {
2011-04-20 22:47:26 +02:00
b0 = 5;
}
2013-07-01 13:03:00 +02:00
if (Block.t[k1] && !Block.t[j1]) {
2011-04-20 22:47:26 +02:00
b0 = 4;
}
2013-03-13 23:33:27 +01:00
world.setData(i, j, k, b0, 2);
2011-02-01 22:55:30 +01:00
}
}
2012-07-29 09:33:13 +02:00
public boolean interact(World world, int i, int j, int k, EntityHuman entityhuman, int l, float f, float f1, float f2) {
2011-02-01 23:01:37 +01:00
if (world.isStatic) {
2011-02-01 22:55:30 +01:00
return true;
} else {
2011-02-01 23:01:37 +01:00
TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);
2011-02-01 22:55:30 +01:00
2011-09-15 02:23:52 +02:00
if (tileentitydispenser != null) {
2012-02-29 22:31:04 +01:00
entityhuman.openDispenser(tileentitydispenser);
2011-09-15 02:23:52 +02:00
}
2011-02-01 22:55:30 +01:00
return true;
}
}
// CraftBukkit - private -> public
public void dispense(World world, int i, int j, int k) {
SourceBlock sourceblock = new SourceBlock(world, i, j, k);
TileEntityDispenser tileentitydispenser = (TileEntityDispenser) sourceblock.getTileEntity();
2011-05-14 16:29:42 +02:00
2011-09-15 02:23:52 +02:00
if (tileentitydispenser != null) {
2013-03-13 23:33:27 +01:00
int l = tileentitydispenser.j();
2011-09-15 02:23:52 +02:00
if (l < 0) {
2012-02-29 22:31:04 +01:00
world.triggerEffect(1001, i, j, k, 0);
2011-02-01 22:55:30 +01:00
} else {
ItemStack itemstack = tileentitydispenser.getItem(l);
2013-03-13 23:33:27 +01:00
IDispenseBehavior idispensebehavior = this.a(itemstack);
2012-08-02 23:54:21 +02:00
if (idispensebehavior != IDispenseBehavior.a) {
ItemStack itemstack1 = idispensebehavior.a(sourceblock, itemstack);
eventFired = false; // CraftBukkit - reset event status
2012-08-02 23:54:21 +02:00
tileentitydispenser.setItem(l, itemstack1.count == 0 ? null : itemstack1);
2011-09-15 02:23:52 +02:00
}
}
2011-02-01 22:55:30 +01:00
}
}
2013-03-13 23:33:27 +01:00
protected IDispenseBehavior a(ItemStack itemstack) {
return (IDispenseBehavior) a.a(itemstack.getItem());
}
2011-02-01 22:55:30 +01:00
2013-03-13 23:33:27 +01:00
public void doPhysics(World world, int i, int j, int k, int l) {
boolean flag = world.isBlockIndirectlyPowered(i, j, k) || world.isBlockIndirectlyPowered(i, j + 1, k);
int i1 = world.getData(i, j, k);
boolean flag1 = (i1 & 8) != 0;
if (flag && !flag1) {
world.a(i, j, k, this.id, this.a(world));
world.setData(i, j, k, i1 | 8, 4);
} else if (!flag && flag1) {
world.setData(i, j, k, i1 & -9, 4);
2011-02-01 22:55:30 +01:00
}
}
2013-03-13 23:33:27 +01:00
public void a(World world, int i, int j, int k, Random random) {
if (!world.isStatic) {
this.dispense(world, i, j, k);
2011-02-01 22:55:30 +01:00
}
}
2013-03-13 23:33:27 +01:00
public TileEntity b(World world) {
2011-02-01 23:01:37 +01:00
return new TileEntityDispenser();
2011-02-01 22:55:30 +01:00
}
2013-03-13 23:33:27 +01:00
public void postPlace(World world, int i, int j, int k, EntityLiving entityliving, ItemStack itemstack) {
int l = BlockPiston.a(world, i, j, k, entityliving);
2011-02-01 23:01:37 +01:00
2013-03-13 23:33:27 +01:00
world.setData(i, j, k, l, 2);
if (itemstack.hasName()) {
((TileEntityDispenser) world.getTileEntity(i, j, k)).a(itemstack.getName());
2011-02-01 22:55:30 +01:00
}
}
2011-05-26 14:48:22 +02:00
2012-07-29 09:33:13 +02:00
public void remove(World world, int i, int j, int k, int l, int i1) {
2011-05-26 14:48:22 +02:00
TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);
2011-09-15 02:23:52 +02:00
if (tileentitydispenser != null) {
2012-07-29 09:33:13 +02:00
for (int j1 = 0; j1 < tileentitydispenser.getSize(); ++j1) {
ItemStack itemstack = tileentitydispenser.getItem(j1);
2011-05-26 14:48:22 +02:00
2011-09-15 02:23:52 +02:00
if (itemstack != null) {
float f = this.b.nextFloat() * 0.8F + 0.1F;
float f1 = this.b.nextFloat() * 0.8F + 0.1F;
float f2 = this.b.nextFloat() * 0.8F + 0.1F;
2011-05-26 14:48:22 +02:00
2011-09-15 02:23:52 +02:00
while (itemstack.count > 0) {
int k1 = this.b.nextInt(21) + 10;
2011-05-26 14:48:22 +02:00
2012-07-29 09:33:13 +02:00
if (k1 > itemstack.count) {
k1 = itemstack.count;
2011-09-15 02:23:52 +02:00
}
2011-05-26 14:48:22 +02:00
2012-07-29 09:33:13 +02:00
itemstack.count -= k1;
EntityItem entityitem = new EntityItem(world, (double) ((float) i + f), (double) ((float) j + f1), (double) ((float) k + f2), new ItemStack(itemstack.id, k1, itemstack.getData()));
if (itemstack.hasTag()) {
2012-12-20 05:03:52 +01:00
entityitem.getItemStack().setTag((NBTTagCompound) itemstack.getTag().clone());
}
2011-09-15 02:23:52 +02:00
float f3 = 0.05F;
2011-05-26 14:48:22 +02:00
entityitem.motX = (double) ((float) this.b.nextGaussian() * f3);
entityitem.motY = (double) ((float) this.b.nextGaussian() * f3 + 0.2F);
entityitem.motZ = (double) ((float) this.b.nextGaussian() * f3);
2011-09-15 02:23:52 +02:00
world.addEntity(entityitem);
}
2011-05-26 14:48:22 +02:00
}
}
2013-03-20 21:09:23 +01:00
world.m(i, j, k, l);
2011-05-26 14:48:22 +02:00
}
2012-07-29 09:33:13 +02:00
super.remove(world, i, j, k, l, i1);
}
public static IPosition a(ISourceBlock isourceblock) {
2013-07-01 13:03:00 +02:00
EnumFacing enumfacing = l_(isourceblock.h());
2012-12-20 05:03:52 +01:00
double d0 = isourceblock.getX() + 0.7D * (double) enumfacing.c();
2013-03-13 23:33:27 +01:00
double d1 = isourceblock.getY() + 0.7D * (double) enumfacing.d();
2012-12-20 05:03:52 +01:00
double d2 = isourceblock.getZ() + 0.7D * (double) enumfacing.e();
2012-07-29 09:33:13 +02:00
return new Position(d0, d1, d2);
2011-05-26 14:48:22 +02:00
}
2013-03-13 23:33:27 +01:00
2013-07-01 13:03:00 +02:00
public static EnumFacing l_(int i) {
2013-03-13 23:33:27 +01:00
return EnumFacing.a(i & 7);
}
public boolean q_() {
return true;
}
public int b_(World world, int i, int j, int k, int l) {
return Container.b((IInventory) world.getTileEntity(i, j, k));
}
2011-02-02 00:32:18 +01:00
}