2011-01-10 02:36:19 +01:00
|
|
|
package net.minecraft.server;
|
|
|
|
|
2011-02-01 18:06:17 +01:00
|
|
|
// CraftBukkit start
|
|
|
|
import org.bukkit.event.block.BlockBreakEvent;
|
2011-03-23 16:38:42 +01:00
|
|
|
import org.bukkit.event.block.BlockDamageEvent;
|
2011-03-23 12:22:13 +01:00
|
|
|
import org.bukkit.craftbukkit.event.CraftEventFactory;
|
|
|
|
import org.bukkit.event.Event;
|
|
|
|
import org.bukkit.event.block.Action;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
2011-02-01 18:06:17 +01:00
|
|
|
// CraftBukkit end
|
|
|
|
|
2011-01-10 02:36:19 +01:00
|
|
|
public class ItemInWorldManager {
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
public World world;
|
2011-04-20 19:05:14 +02:00
|
|
|
public EntityHuman player;
|
2011-09-15 02:23:52 +02:00
|
|
|
private int c = -1;
|
|
|
|
private float d = 0.0F;
|
2011-06-27 00:25:01 +02:00
|
|
|
private int lastDigTick;
|
2011-03-31 22:40:00 +02:00
|
|
|
private int f;
|
2011-01-10 09:30:34 +01:00
|
|
|
private int g;
|
2011-09-15 02:23:52 +02:00
|
|
|
private int h;
|
2011-04-20 19:05:14 +02:00
|
|
|
private int currentTick;
|
2011-09-15 02:23:52 +02:00
|
|
|
private boolean j;
|
2011-03-31 22:40:00 +02:00
|
|
|
private int k;
|
2011-02-23 03:37:56 +01:00
|
|
|
private int l;
|
|
|
|
private int m;
|
2011-09-15 02:23:52 +02:00
|
|
|
private int n;
|
2011-01-10 02:36:19 +01:00
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
public ItemInWorldManager(World world) {
|
|
|
|
this.world = world;
|
|
|
|
}
|
|
|
|
|
2011-09-26 09:07:06 +02:00
|
|
|
// CraftBukkit start - keep this for backwards compatibility
|
|
|
|
public ItemInWorldManager(WorldServer world) {
|
|
|
|
this.world = world;
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
|
|
|
|
2012-01-12 16:27:39 +01:00
|
|
|
public void setGameMode(int i) {
|
2011-09-15 02:23:52 +02:00
|
|
|
this.c = i;
|
|
|
|
if (i == 0) {
|
2011-09-15 18:36:27 +02:00
|
|
|
this.player.abilities.canFly = false;
|
|
|
|
this.player.abilities.isFlying = false;
|
|
|
|
this.player.abilities.canInstantlyBuild = false;
|
|
|
|
this.player.abilities.isInvulnerable = false;
|
2011-09-15 02:23:52 +02:00
|
|
|
} else {
|
2011-09-15 18:36:27 +02:00
|
|
|
this.player.abilities.canFly = true;
|
|
|
|
this.player.abilities.canInstantlyBuild = true;
|
|
|
|
this.player.abilities.isInvulnerable = true;
|
2011-09-15 02:23:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-12 16:27:39 +01:00
|
|
|
public int getGameMode() {
|
2011-09-15 02:23:52 +02:00
|
|
|
return this.c;
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:03:48 +01:00
|
|
|
public boolean isCreative() {
|
2011-09-15 02:23:52 +02:00
|
|
|
return this.c == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void b(int i) {
|
|
|
|
if (this.c == -1) {
|
|
|
|
this.c = i;
|
|
|
|
}
|
|
|
|
|
2012-01-12 16:27:39 +01:00
|
|
|
this.setGameMode(this.c);
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
public void c() {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
|
2011-09-15 02:23:52 +02:00
|
|
|
if (this.j) {
|
|
|
|
int i = this.currentTick - this.n;
|
|
|
|
int j = this.world.getTypeId(this.k, this.l, this.m);
|
2011-02-23 03:37:56 +01:00
|
|
|
|
|
|
|
if (j != 0) {
|
|
|
|
Block block = Block.byId[j];
|
2011-04-20 19:05:14 +02:00
|
|
|
float f = block.getDamage(this.player) * (float) (i + 1);
|
2011-02-23 03:37:56 +01:00
|
|
|
|
|
|
|
if (f >= 1.0F) {
|
2011-09-15 02:23:52 +02:00
|
|
|
this.j = false;
|
2012-01-14 21:03:48 +01:00
|
|
|
this.breakBlock(this.k, this.l, this.m);
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
} else {
|
2011-09-15 02:23:52 +02:00
|
|
|
this.j = false;
|
2011-02-23 03:37:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
public void dig(int i, int j, int k, int l) {
|
2011-06-27 00:25:01 +02:00
|
|
|
// this.world.douseFire((EntityHuman) null, i, j, k, l); // CraftBukkit - moved down
|
2012-01-14 21:03:48 +01:00
|
|
|
// CraftBukkit
|
2011-09-15 20:07:40 +02:00
|
|
|
PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(this.player, Action.LEFT_CLICK_BLOCK, i, j, k, l, this.player.inventory.getItemInHand());
|
|
|
|
|
2012-01-14 21:03:48 +01:00
|
|
|
if (this.isCreative()) {
|
|
|
|
// CraftBukkit start
|
2011-09-15 20:07:40 +02:00
|
|
|
if (event.isCancelled()) {
|
2012-01-08 02:26:23 +01:00
|
|
|
// Let the client know the block still exists
|
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j, k, this.world));
|
2011-09-15 20:07:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-01-14 21:03:48 +01:00
|
|
|
// CraftBukkit end
|
2012-03-02 20:55:41 +01:00
|
|
|
if (!this.world.douseFire((EntityHuman) null, i, j, k, l)) {
|
|
|
|
this.breakBlock(i, j, k);
|
|
|
|
}
|
2011-09-15 02:23:52 +02:00
|
|
|
} else {
|
|
|
|
this.lastDigTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
|
|
|
|
int i1 = this.world.getTypeId(i, j, k);
|
2011-01-10 02:36:19 +01:00
|
|
|
|
2012-01-14 21:03:48 +01:00
|
|
|
// CraftBukkit start - Swings at air do *NOT* exist.
|
2011-09-15 02:23:52 +02:00
|
|
|
if (i1 <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2011-03-23 12:22:13 +01:00
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
if (event.useInteractedBlock() == Event.Result.DENY) {
|
|
|
|
// If we denied a door from opening, we need to send a correcting update to the client, as it already opened the door.
|
|
|
|
if (i1 == Block.WOODEN_DOOR.id) {
|
|
|
|
// For some reason *BOTH* the bottom/top part have to be marked updated.
|
|
|
|
boolean bottom = (this.world.getData(i, j, k) & 8) == 0;
|
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j, k, this.world));
|
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j + (bottom ? 1 : -1), k, this.world));
|
|
|
|
} else if (i1 == Block.TRAP_DOOR.id) {
|
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j, k, this.world));
|
|
|
|
}
|
|
|
|
} else {
|
2012-01-14 21:03:48 +01:00
|
|
|
Block.byId[i1].attack(this.world, i, j, k, this.player);
|
2011-09-15 02:23:52 +02:00
|
|
|
// Allow fire punching to be blocked
|
|
|
|
this.world.douseFire((EntityHuman) null, i, j, k, l);
|
2011-03-23 18:29:43 +01:00
|
|
|
}
|
2011-01-29 22:50:29 +01:00
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
// Handle hitting a block
|
|
|
|
float toolDamage = Block.byId[i1].getDamage(this.player);
|
|
|
|
if (event.useItemInHand() == Event.Result.DENY) {
|
|
|
|
// If we 'insta destroyed' then the client needs to be informed.
|
|
|
|
if (toolDamage > 1.0f) {
|
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j, k, this.world));
|
|
|
|
}
|
|
|
|
return;
|
2011-03-24 22:42:23 +01:00
|
|
|
}
|
2011-09-15 02:23:52 +02:00
|
|
|
BlockDamageEvent blockEvent = CraftEventFactory.callBlockDamageEvent(this.player, i, j, k, this.player.inventory.getItemInHand(), toolDamage >= 1.0f);
|
2011-03-23 16:38:42 +01:00
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
if (blockEvent.isCancelled()) {
|
2012-01-08 02:26:23 +01:00
|
|
|
// Let the client know the block still exists
|
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j, k, this.world));
|
2011-09-15 02:23:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-03-23 16:38:42 +01:00
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
if (blockEvent.getInstaBreak()) {
|
|
|
|
toolDamage = 2.0f;
|
|
|
|
}
|
2011-03-23 16:38:42 +01:00
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
if (toolDamage >= 1.0F) {
|
|
|
|
// CraftBukkit end
|
2012-01-14 21:03:48 +01:00
|
|
|
this.breakBlock(i, j, k);
|
2011-09-15 02:23:52 +02:00
|
|
|
} else {
|
|
|
|
this.f = i;
|
|
|
|
this.g = j;
|
|
|
|
this.h = k;
|
|
|
|
}
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
public void a(int i, int j, int k) {
|
2011-09-15 02:23:52 +02:00
|
|
|
if (i == this.f && j == this.g && k == this.h) {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
|
2011-06-27 00:25:01 +02:00
|
|
|
int l = this.currentTick - this.lastDigTick;
|
2011-04-20 19:05:14 +02:00
|
|
|
int i1 = this.world.getTypeId(i, j, k);
|
2011-01-29 22:50:29 +01:00
|
|
|
|
2011-02-23 03:37:56 +01:00
|
|
|
if (i1 != 0) {
|
2011-01-29 22:50:29 +01:00
|
|
|
Block block = Block.byId[i1];
|
2011-04-20 19:05:14 +02:00
|
|
|
float f = block.getDamage(this.player) * (float) (l + 1);
|
2011-02-23 03:37:56 +01:00
|
|
|
|
2011-04-20 22:47:26 +02:00
|
|
|
if (f >= 0.7F) {
|
2012-01-14 21:03:48 +01:00
|
|
|
this.breakBlock(i, j, k);
|
2011-09-15 02:23:52 +02:00
|
|
|
} else if (!this.j) {
|
|
|
|
this.j = true;
|
|
|
|
this.k = i;
|
|
|
|
this.l = j;
|
|
|
|
this.m = k;
|
|
|
|
this.n = this.lastDigTick;
|
2011-01-29 22:50:29 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-14 16:29:42 +02:00
|
|
|
// CraftBukkit start - force blockreset to client
|
2011-03-24 22:42:23 +01:00
|
|
|
} else {
|
2011-04-20 19:05:14 +02:00
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j, k, this.world));
|
2011-03-24 22:42:23 +01:00
|
|
|
// CraftBukkit end
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
2011-02-23 03:37:56 +01:00
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
this.d = 0.0F;
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
public boolean b(int i, int j, int k) {
|
2011-04-20 19:05:14 +02:00
|
|
|
Block block = Block.byId[this.world.getTypeId(i, j, k)];
|
|
|
|
int l = this.world.getData(i, j, k);
|
|
|
|
boolean flag = this.world.setTypeId(i, j, k, 0);
|
2011-01-10 02:36:19 +01:00
|
|
|
|
|
|
|
if (block != null && flag) {
|
2011-04-20 19:05:14 +02:00
|
|
|
block.postBreak(this.world, i, j, k, l);
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
2011-01-29 22:50:29 +01:00
|
|
|
|
2011-01-10 02:36:19 +01:00
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
2012-01-14 21:03:48 +01:00
|
|
|
public boolean breakBlock(int i, int j, int k) {
|
2011-02-23 03:37:56 +01:00
|
|
|
// CraftBukkit start
|
2011-04-20 19:05:14 +02:00
|
|
|
if (this.player instanceof EntityPlayer) {
|
2011-06-27 00:25:01 +02:00
|
|
|
org.bukkit.block.Block block = this.world.getWorld().getBlockAt(i, j, k);
|
2011-02-01 18:06:17 +01:00
|
|
|
|
2012-01-17 22:52:02 +01:00
|
|
|
// Tell client the block is gone immediately then process events
|
|
|
|
if (world.getTileEntity(i, j, k) == null) {
|
2012-01-26 23:01:36 +01:00
|
|
|
Packet53BlockChange packet = new Packet53BlockChange(i, j, k, this.world);
|
2012-01-17 22:52:02 +01:00
|
|
|
|
2012-01-27 02:34:23 +01:00
|
|
|
packet.material = 0;
|
|
|
|
packet.data = 0;
|
2012-01-26 23:01:36 +01:00
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(packet);
|
2012-01-17 22:52:02 +01:00
|
|
|
}
|
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
BlockBreakEvent event = new BlockBreakEvent(block, (org.bukkit.entity.Player) this.player.getBukkitEntity());
|
|
|
|
this.world.getServer().getPluginManager().callEvent(event);
|
2011-02-01 18:06:17 +01:00
|
|
|
|
2011-02-23 13:56:36 +01:00
|
|
|
if (event.isCancelled()) {
|
2012-01-08 02:26:23 +01:00
|
|
|
// Let the client know the block still exists
|
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j, k, this.world));
|
2011-02-25 04:37:47 +01:00
|
|
|
return false;
|
2011-02-01 18:06:17 +01:00
|
|
|
}
|
|
|
|
}
|
2011-02-23 03:37:56 +01:00
|
|
|
// CraftBukkit end
|
2011-02-01 18:06:17 +01:00
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
int l = this.world.getTypeId(i, j, k);
|
|
|
|
int i1 = this.world.getData(i, j, k);
|
2011-05-26 14:48:22 +02:00
|
|
|
|
2012-03-01 11:49:23 +01:00
|
|
|
this.world.a(this.player, 2001, i, j, k, l + (this.world.getData(i, j, k) << 12));
|
2011-05-26 14:48:22 +02:00
|
|
|
boolean flag = this.b(i, j, k);
|
2011-01-10 02:36:19 +01:00
|
|
|
|
2012-01-14 21:03:48 +01:00
|
|
|
if (this.isCreative()) {
|
2011-09-15 02:23:52 +02:00
|
|
|
((EntityPlayer) this.player).netServerHandler.sendPacket(new Packet53BlockChange(i, j, k, this.world));
|
|
|
|
} else {
|
2012-03-01 11:49:23 +01:00
|
|
|
ItemStack itemstack = this.player.T();
|
2011-11-20 09:01:14 +01:00
|
|
|
boolean flag1 = this.player.b(Block.byId[l]);
|
2011-09-15 02:23:52 +02:00
|
|
|
|
|
|
|
if (itemstack != null) {
|
|
|
|
itemstack.a(l, i, j, k, this.player);
|
|
|
|
if (itemstack.count == 0) {
|
|
|
|
itemstack.a(this.player);
|
2012-03-01 11:49:23 +01:00
|
|
|
this.player.U();
|
2011-09-15 02:23:52 +02:00
|
|
|
}
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
2011-01-29 22:50:29 +01:00
|
|
|
|
2011-11-20 09:01:14 +01:00
|
|
|
if (flag && flag1) {
|
2011-09-15 02:23:52 +02:00
|
|
|
Block.byId[l].a(this.world, this.player, i, j, k, i1);
|
|
|
|
}
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
2011-01-29 22:50:29 +01:00
|
|
|
|
2011-01-10 02:36:19 +01:00
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public boolean useItem(EntityHuman entityhuman, World world, ItemStack itemstack) {
|
2011-01-29 22:50:29 +01:00
|
|
|
int i = itemstack.count;
|
2011-09-15 02:23:52 +02:00
|
|
|
int j = itemstack.getData();
|
2011-01-29 22:50:29 +01:00
|
|
|
ItemStack itemstack1 = itemstack.a(world, entityhuman);
|
2011-01-10 02:36:19 +01:00
|
|
|
|
2011-09-15 18:36:27 +02:00
|
|
|
if (itemstack1 == itemstack && (itemstack1 == null || itemstack1.count == i) && (itemstack1 == null || itemstack1.l() <= 0)) {
|
2011-01-29 22:50:29 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2011-04-20 19:05:14 +02:00
|
|
|
entityhuman.inventory.items[entityhuman.inventory.itemInHandIndex] = itemstack1;
|
2012-01-14 21:03:48 +01:00
|
|
|
if (this.isCreative()) {
|
2011-09-15 02:23:52 +02:00
|
|
|
itemstack1.count = i;
|
2011-11-30 00:17:43 +01:00
|
|
|
itemstack1.setData(j);
|
2011-09-15 02:23:52 +02:00
|
|
|
}
|
|
|
|
|
2011-01-29 22:50:29 +01:00
|
|
|
if (itemstack1.count == 0) {
|
2011-04-20 19:05:14 +02:00
|
|
|
entityhuman.inventory.items[entityhuman.inventory.itemInHandIndex] = null;
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
2011-01-29 22:50:29 +01:00
|
|
|
|
2011-01-10 02:36:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
// TODO: Review this code, it changed in 1.8 but I'm not sure if we need to update or not
|
2011-04-20 19:05:14 +02:00
|
|
|
public boolean interact(EntityHuman entityhuman, World world, ItemStack itemstack, int i, int j, int k, int l) {
|
2011-01-29 22:50:29 +01:00
|
|
|
int i1 = world.getTypeId(i, j, k);
|
2011-01-10 02:36:19 +01:00
|
|
|
|
2011-03-23 12:22:13 +01:00
|
|
|
// CraftBukkit start - Interact
|
|
|
|
boolean result = false;
|
|
|
|
if (i1 > 0) {
|
|
|
|
PlayerInteractEvent event = CraftEventFactory.callPlayerInteractEvent(entityhuman, Action.RIGHT_CLICK_BLOCK, i, j, k, l, itemstack);
|
2011-03-24 12:11:28 +01:00
|
|
|
if (event.useInteractedBlock() == Event.Result.DENY) {
|
2011-03-23 18:29:43 +01:00
|
|
|
// If we denied a door from opening, we need to send a correcting update to the client, as it already opened the door.
|
|
|
|
if (i1 == Block.WOODEN_DOOR.id) {
|
|
|
|
boolean bottom = (world.getData(i, j, k) & 8) == 0;
|
2011-04-20 19:05:14 +02:00
|
|
|
((EntityPlayer) entityhuman).netServerHandler.sendPacket(new Packet53BlockChange(i, j + (bottom ? 1 : -1), k, world));
|
2011-03-23 18:29:43 +01:00
|
|
|
}
|
2011-06-08 23:52:10 +02:00
|
|
|
result = (event.useItemInHand() != Event.Result.ALLOW);
|
2011-03-23 18:29:43 +01:00
|
|
|
} else {
|
2011-04-20 19:05:14 +02:00
|
|
|
result = Block.byId[i1].interact(world, i, j, k, entityhuman);
|
2011-06-08 23:52:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (itemstack != null && !result) {
|
2011-09-15 09:30:52 +02:00
|
|
|
int j1 = itemstack.getData();
|
|
|
|
int k1 = itemstack.count;
|
|
|
|
|
2011-06-08 23:52:10 +02:00
|
|
|
result = itemstack.placeItem(entityhuman, world, i, j, k, l);
|
2011-09-15 09:30:52 +02:00
|
|
|
|
|
|
|
// The item count should not decrement in Creative mode.
|
2012-01-14 21:03:48 +01:00
|
|
|
if (this.isCreative()) {
|
2011-11-30 00:17:43 +01:00
|
|
|
itemstack.setData(j1);
|
2011-09-15 09:30:52 +02:00
|
|
|
itemstack.count = k1;
|
|
|
|
}
|
2011-03-23 12:22:13 +01:00
|
|
|
}
|
2011-03-25 07:35:47 +01:00
|
|
|
|
2011-03-24 12:11:28 +01:00
|
|
|
// If we have 'true' and no explicit deny *or* an explicit allow -- run the item part of the hook
|
|
|
|
if (itemstack != null && ((!result && event.useItemInHand() != Event.Result.DENY) || event.useItemInHand() == Event.Result.ALLOW)) {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.useItem(entityhuman, world, itemstack);
|
2011-03-23 12:22:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2011-05-14 16:29:42 +02:00
|
|
|
// CraftBukkit end
|
2011-01-10 02:36:19 +01:00
|
|
|
}
|
2011-09-15 02:23:52 +02:00
|
|
|
|
|
|
|
public void a(WorldServer worldserver) {
|
|
|
|
this.world = worldserver;
|
|
|
|
}
|
2011-01-10 09:30:34 +01:00
|
|
|
}
|