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

189 Zeilen
6.2 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());
private 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);
this.textureId = 45;
2012-07-29 09:33:13 +02:00
this.a(CreativeModeTab.d);
2011-02-01 22:55:30 +01:00
}
public int r_() {
2011-02-01 22:55:30 +01:00
return 4;
}
public int getDropType(int i, Random random, int j) {
2011-02-01 23:01:37 +01:00
return Block.DISPENSER.id;
2011-02-01 22:55:30 +01:00
}
public void onPlace(World world, int i, int j, int k) {
super.onPlace(world, i, j, k);
2012-07-29 09:33:13 +02:00
this.l(world, i, j, k);
2011-02-01 22:55:30 +01:00
}
2012-07-29 09:33:13 +02:00
private void l(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;
if (Block.q[l] && !Block.q[i1]) {
2011-04-20 22:47:26 +02:00
b0 = 3;
}
if (Block.q[i1] && !Block.q[l]) {
2011-04-20 22:47:26 +02:00
b0 = 2;
}
if (Block.q[j1] && !Block.q[k1]) {
2011-04-20 22:47:26 +02:00
b0 = 5;
}
if (Block.q[k1] && !Block.q[j1]) {
2011-04-20 22:47:26 +02:00
b0 = 4;
}
world.setData(i, j, k, b0);
2011-02-01 22:55:30 +01:00
}
}
public int a(int i) {
2011-02-01 23:01:37 +01:00
return i == 1 ? this.textureId + 17 : (i == 0 ? this.textureId + 17 : (i == 3 ? this.textureId + 1 : this.textureId));
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) {
int l = tileentitydispenser.i();
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);
IDispenseBehavior idispensebehavior = (IDispenseBehavior) a.a(itemstack.getItem());
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
}
}
public void doPhysics(World world, int i, int j, int k, int l) {
if (l > 0 && Block.byId[l].isPowerSource()) {
boolean flag = world.isBlockIndirectlyPowered(i, j, k) || world.isBlockIndirectlyPowered(i, j + 1, k);
2011-02-01 22:55:30 +01:00
if (flag) {
world.a(i, j, k, this.id, this.r_());
2011-02-01 22:55:30 +01:00
}
}
}
2012-07-29 09:33:13 +02:00
public void b(World world, int i, int j, int k, Random random) {
2011-11-20 09:01:14 +01:00
if (!world.isStatic && (world.isBlockIndirectlyPowered(i, j, k) || world.isBlockIndirectlyPowered(i, j + 1, k))) {
this.dispense(world, i, j, k);
2011-02-01 22:55:30 +01:00
}
}
2012-07-29 09:33:13 +02:00
public TileEntity a(World world) {
2011-02-01 23:01:37 +01:00
return new TileEntityDispenser();
2011-02-01 22:55:30 +01:00
}
public void postPlace(World world, int i, int j, int k, EntityLiving entityliving) {
int l = MathHelper.floor((double) (entityliving.yaw * 4.0F / 360.0F) + 0.5D) & 3;
2011-02-01 22:55:30 +01:00
if (l == 0) {
world.setData(i, j, k, 2);
2011-02-01 22:55:30 +01:00
}
2011-02-01 23:01:37 +01:00
2011-02-01 22:55:30 +01:00
if (l == 1) {
world.setData(i, j, k, 5);
2011-02-01 22:55:30 +01:00
}
2011-02-01 23:01:37 +01:00
2011-02-01 22:55:30 +01:00
if (l == 2) {
world.setData(i, j, k, 3);
2011-02-01 22:55:30 +01:00
}
2011-02-01 23:01:37 +01:00
2011-02-01 22:55:30 +01:00
if (l == 3) {
world.setData(i, j, k, 4);
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()) {
entityitem.itemStack.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
}
}
}
2012-07-29 09:33:13 +02:00
super.remove(world, i, j, k, l, i1);
}
public static IPosition a(ISourceBlock isourceblock) {
EnumFacing enumfacing = EnumFacing.a(isourceblock.h());
double d0 = isourceblock.getX() + 0.6D * (double) enumfacing.c();
double d1 = isourceblock.getY();
double d2 = isourceblock.getZ() + 0.6D * (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
}
2011-02-02 00:32:18 +01:00
}