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

283 Zeilen
11 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
// CraftBukkit start
2011-05-08 11:56:40 +02:00
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.util.Vector;
2011-05-14 16:29:42 +02:00
// CraftBukkit end
2011-02-01 22:55:30 +01:00
public class BlockDispenser extends BlockContainer {
2011-05-26 14:48:22 +02:00
private Random a = new Random();
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;
2011-02-01 22:55:30 +01:00
}
2011-11-20 09:01:14 +01:00
public int d() {
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);
2011-02-01 23:01:37 +01:00
this.g(world, i, j, k);
2011-02-01 22:55:30 +01:00
}
private void g(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;
2012-03-01 11:49:23 +01:00
if (Block.n[l] && !Block.n[i1]) {
2011-04-20 22:47:26 +02:00
b0 = 3;
}
2012-03-01 11:49:23 +01:00
if (Block.n[i1] && !Block.n[l]) {
2011-04-20 22:47:26 +02:00
b0 = 2;
}
2012-03-01 11:49:23 +01:00
if (Block.n[j1] && !Block.n[k1]) {
2011-04-20 22:47:26 +02:00
b0 = 5;
}
2012-03-01 11:49:23 +01:00
if (Block.n[k1] && !Block.n[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
}
public boolean interact(World world, int i, int j, int k, EntityHuman entityhuman) {
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;
}
}
2011-09-15 02:23:52 +02:00
// CraftBukkit - priv to public
public void dispense(World world, int i, int j, int k, Random random) {
2011-02-01 23:01:37 +01:00
int l = world.getData(i, j, k);
2011-05-26 14:48:22 +02:00
byte b0 = 0;
byte b1 = 0;
2011-02-01 22:55:30 +01:00
if (l == 3) {
2011-05-26 14:48:22 +02:00
b1 = 1;
2011-02-01 22:55:30 +01:00
} else if (l == 2) {
2011-05-26 14:48:22 +02:00
b1 = -1;
2011-02-01 22:55:30 +01:00
} else if (l == 5) {
2011-05-26 14:48:22 +02:00
b0 = 1;
2011-02-01 22:55:30 +01:00
} else {
2011-05-26 14:48:22 +02:00
b0 = -1;
2011-02-01 22:55:30 +01:00
}
2011-02-01 23:01:37 +01:00
TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);
2011-05-14 16:29:42 +02:00
2011-09-15 02:23:52 +02:00
if (tileentitydispenser != null) {
2011-05-08 11:56:40 +02:00
// CraftBukkit start
2011-09-15 02:23:52 +02:00
int dispenseSlot = tileentitydispenser.findDispenseSlot();
ItemStack itemstack = null;
if (dispenseSlot > -1) {
itemstack = tileentitydispenser.getContents()[dispenseSlot];
2011-05-14 16:29:42 +02:00
2011-09-15 02:23:52 +02:00
// Copy item stack, because we want it to have 1 item
itemstack = new ItemStack(itemstack.id, 1, itemstack.getData(), itemstack.getEnchantments());
2011-09-15 02:23:52 +02:00
}
2011-05-08 11:56:40 +02:00
// CraftBukkit end
2011-02-01 23:01:37 +01:00
2011-09-15 02:23:52 +02:00
double d0 = (double) i + (double) b0 * 0.6D + 0.5D;
double d1 = (double) j + 0.5D;
double d2 = (double) k + (double) b1 * 0.6D + 0.5D;
if (itemstack == null) {
2012-02-29 22:31:04 +01:00
world.triggerEffect(1001, i, j, k, 0);
2011-02-01 22:55:30 +01:00
} else {
2011-05-08 11:56:40 +02:00
// CraftBukkit start
2011-09-15 02:23:52 +02:00
double d3 = random.nextDouble() * 0.1D + 0.2D;
double motX = (double) b0 * d3;
double motY = 0.20000000298023224D;
double motZ = (double) b1 * d3;
motX += random.nextGaussian() * 0.007499999832361937D * 6.0D;
motY += random.nextGaussian() * 0.007499999832361937D * 6.0D;
motZ += random.nextGaussian() * 0.007499999832361937D * 6.0D;
org.bukkit.block.Block block = world.getWorld().getBlockAt(i, j, k);
org.bukkit.inventory.ItemStack bukkitItem = new CraftItemStack(itemstack).clone();
BlockDispenseEvent event = new BlockDispenseEvent(block, bukkitItem, new Vector(motX, motY, motZ));
world.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
if (event.getItem().equals(bukkitItem)) {
// Actually remove the item
tileentitydispenser.splitStack(dispenseSlot, 1);
}
2011-09-15 02:23:52 +02:00
motX = event.getVelocity().getX();
motY = event.getVelocity().getY();
motZ = event.getVelocity().getZ();
itemstack = CraftItemStack.createNMSItemStack(event.getItem());
2011-05-08 11:56:40 +02:00
// CraftBukkit end
2011-02-01 23:01:37 +01:00
2011-09-15 02:23:52 +02:00
if (itemstack.id == Item.ARROW.id) {
EntityArrow entityarrow = new EntityArrow(world, d0, d1, d2);
2012-01-12 16:27:39 +01:00
entityarrow.shoot((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
2011-09-15 02:23:52 +02:00
entityarrow.fromPlayer = true;
world.addEntity(entityarrow);
2012-02-29 22:31:04 +01:00
world.triggerEffect(1002, i, j, k, 0);
2011-09-15 02:23:52 +02:00
} else if (itemstack.id == Item.EGG.id) {
EntityEgg entityegg = new EntityEgg(world, d0, d1, d2);
entityegg.a((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
world.addEntity(entityegg);
2012-02-29 22:31:04 +01:00
world.triggerEffect(1002, i, j, k, 0);
2011-09-15 02:23:52 +02:00
} else if (itemstack.id == Item.SNOW_BALL.id) {
EntitySnowball entitysnowball = new EntitySnowball(world, d0, d1, d2);
entitysnowball.a((double) b0, 0.10000000149011612D, (double) b1, 1.1F, 6.0F);
world.addEntity(entitysnowball);
2012-02-29 22:31:04 +01:00
world.triggerEffect(1002, i, j, k, 0);
2011-11-20 09:01:14 +01:00
} else if (itemstack.id == Item.POTION.id && ItemPotion.c(itemstack.getData())) {
EntityPotion entitypotion = new EntityPotion(world, d0, d1, d2, itemstack.getData());
entitypotion.a((double) b0, 0.10000000149011612D, (double) b1, 1.375F, 3.0F);
world.addEntity(entitypotion);
2012-02-29 22:31:04 +01:00
world.triggerEffect(1002, i, j, k, 0);
2012-03-01 11:49:23 +01:00
} else if (itemstack.id == Item.EXP_BOTTLE.id) {
EntityThrownExpBottle entitythrownexpbottle = new EntityThrownExpBottle(world, d0, d1, d2);
entitythrownexpbottle.a((double) b0, 0.10000000149011612D, (double) b1, 1.375F, 3.0F);
world.addEntity(entitythrownexpbottle);
world.triggerEffect(1002, i, j, k, 0);
} else if (itemstack.id == Item.MONSTER_EGG.id) {
ItemMonsterEgg.a(world, itemstack.getData(), d0 + (double) b0 * 0.3D, d1 - 0.3D, d2 + (double) b1 * 0.3D);
world.triggerEffect(1002, i, j, k, 0);
} else if (itemstack.id == Item.FIREBALL.id) {
EntitySmallFireball entitysmallfireball = new EntitySmallFireball(world, d0 + (double) b0 * 0.3D, d1, d2 + (double) b1 * 0.3D, (double) b0 + random.nextGaussian() * 0.05D, random.nextGaussian() * 0.05D, (double) b1 + random.nextGaussian() * 0.05D);
world.addEntity(entitysmallfireball);
world.triggerEffect(1009, i, j, k, 0);
2011-09-15 02:23:52 +02:00
} else {
EntityItem entityitem = new EntityItem(world, d0, d1 - 0.3D, d2, itemstack);
// CraftBukkit start
// double d3 = random.nextDouble() * 0.1D + 0.2D; // Moved up
entityitem.motX = motX;
entityitem.motY = motY;
entityitem.motZ = motZ;
// CraftBukkit end
world.addEntity(entityitem);
2012-02-29 22:31:04 +01:00
world.triggerEffect(1000, i, j, k, 0);
2011-09-15 02:23:52 +02:00
}
2012-02-29 22:31:04 +01:00
world.triggerEffect(2000, i, j, k, b0 + 1 + (b1 + 1) * 3);
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) {
2011-11-20 09:01:14 +01:00
world.c(i, j, k, this.id, this.d());
2011-02-01 22:55:30 +01:00
}
}
}
public void a(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, random);
2011-02-01 22:55:30 +01:00
}
}
2011-09-15 02:23:52 +02:00
public TileEntity a_() {
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
public void remove(World world, int i, int j, int k) {
TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);
2011-09-15 02:23:52 +02:00
if (tileentitydispenser != null) {
for (int l = 0; l < tileentitydispenser.getSize(); ++l) {
ItemStack itemstack = tileentitydispenser.getItem(l);
2011-05-26 14:48:22 +02:00
2011-09-15 02:23:52 +02:00
if (itemstack != null) {
float f = this.a.nextFloat() * 0.8F + 0.1F;
float f1 = this.a.nextFloat() * 0.8F + 0.1F;
float f2 = this.a.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 i1 = this.a.nextInt(21) + 10;
2011-05-26 14:48:22 +02:00
2011-09-15 02:23:52 +02:00
if (i1 > itemstack.count) {
i1 = itemstack.count;
}
2011-05-26 14:48:22 +02:00
2011-09-15 02:23:52 +02:00
itemstack.count -= i1;
EntityItem entityitem = new EntityItem(world, (double) ((float) i + f), (double) ((float) j + f1), (double) ((float) k + f2), new ItemStack(itemstack.id, i1, 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
2011-09-15 02:23:52 +02:00
entityitem.motX = (double) ((float) this.a.nextGaussian() * f3);
entityitem.motY = (double) ((float) this.a.nextGaussian() * f3 + 0.2F);
entityitem.motZ = (double) ((float) this.a.nextGaussian() * f3);
world.addEntity(entityitem);
}
2011-05-26 14:48:22 +02:00
}
}
}
super.remove(world, i, j, k);
}
2011-02-02 00:32:18 +01:00
}