SPIGOT-4376: Add draft BlockDropItemEvent

This commit is contained in:
AndaBlock 2018-10-31 19:55:04 +11:00 committed by md_5
parent f3f2fe5795
commit 1057710a78
2 changed files with 40 additions and 27 deletions

View file

@ -1,10 +1,11 @@
--- a/net/minecraft/server/PlayerInteractManager.java --- a/net/minecraft/server/PlayerInteractManager.java
+++ b/net/minecraft/server/PlayerInteractManager.java +++ b/net/minecraft/server/PlayerInteractManager.java
@@ -1,5 +1,14 @@ @@ -1,5 +1,15 @@
package net.minecraft.server; package net.minecraft.server;
+// CraftBukkit start +// CraftBukkit start
+import java.util.ArrayList; +import java.util.ArrayList;
+import org.bukkit.craftbukkit.block.CraftBlock;
+import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockBreakEvent;
+import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.event.Event; +import org.bukkit.event.Event;
@ -15,7 +16,7 @@
public class PlayerInteractManager { public class PlayerInteractManager {
public World world; public World world;
@@ -26,7 +35,7 @@ @@ -26,7 +36,7 @@
this.gamemode = enumgamemode; this.gamemode = enumgamemode;
enumgamemode.a(this.player.abilities); enumgamemode.a(this.player.abilities);
this.player.updateAbilities(); this.player.updateAbilities();
@ -24,7 +25,7 @@
this.world.everyoneSleeping(); this.world.everyoneSleeping();
} }
@@ -51,7 +60,7 @@ @@ -51,7 +61,7 @@
} }
public void a() { public void a() {
@ -33,7 +34,7 @@
float f; float f;
int i; int i;
@@ -96,6 +105,19 @@ @@ -96,6 +106,19 @@
} }
public void a(BlockPosition blockposition, EnumDirection enumdirection) { public void a(BlockPosition blockposition, EnumDirection enumdirection) {
@ -53,7 +54,7 @@
if (this.isCreative()) { if (this.isCreative()) {
if (!this.world.douseFire((EntityHuman) null, blockposition, enumdirection)) { if (!this.world.douseFire((EntityHuman) null, blockposition, enumdirection)) {
this.breakBlock(blockposition); this.breakBlock(blockposition);
@@ -122,16 +144,50 @@ @@ -122,16 +145,50 @@
} }
} }
@ -106,7 +107,7 @@
if (!iblockdata.isAir() && f >= 1.0F) { if (!iblockdata.isAir() && f >= 1.0F) {
this.breakBlock(blockposition); this.breakBlock(blockposition);
} else { } else {
@@ -149,6 +205,7 @@ @@ -149,6 +206,7 @@
public void a(BlockPosition blockposition) { public void a(BlockPosition blockposition) {
if (blockposition.equals(this.f)) { if (blockposition.equals(this.f)) {
@ -114,7 +115,7 @@
int i = this.currentTick - this.lastDigTick; int i = this.currentTick - this.lastDigTick;
IBlockData iblockdata = this.world.getType(blockposition); IBlockData iblockdata = this.world.getType(blockposition);
@@ -166,6 +223,10 @@ @@ -166,6 +224,10 @@
this.j = this.lastDigTick; this.j = this.lastDigTick;
} }
} }
@ -125,16 +126,15 @@
} }
} }
@@ -190,13 +251,89 @@ @@ -190,13 +252,88 @@
public boolean breakBlock(BlockPosition blockposition) { public boolean breakBlock(BlockPosition blockposition) {
IBlockData iblockdata = this.world.getType(blockposition); IBlockData iblockdata = this.world.getType(blockposition);
+ // CraftBukkit start - fire BlockBreakEvent + // CraftBukkit start - fire BlockBreakEvent
+ org.bukkit.block.Block bblock = CraftBlock.at(world, blockposition);
+ BlockBreakEvent event = null; + BlockBreakEvent event = null;
+ +
+ if (this.player instanceof EntityPlayer) { + if (this.player instanceof EntityPlayer) {
+ org.bukkit.block.Block block = this.world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+
+ // Sword + Creative mode pre-cancel + // Sword + Creative mode pre-cancel
+ boolean isSwordNoBreak = !this.player.getItemInMainHand().getItem().a(iblockdata, this.world, blockposition, (EntityHuman) this.player); + boolean isSwordNoBreak = !this.player.getItemInMainHand().getItem().a(iblockdata, this.world, blockposition, (EntityHuman) this.player);
+ +
@ -146,7 +146,7 @@
+ ((EntityPlayer) this.player).playerConnection.sendPacket(packet); + ((EntityPlayer) this.player).playerConnection.sendPacket(packet);
+ } + }
+ +
+ event = new BlockBreakEvent(block, this.player.getBukkitEntity()); + event = new BlockBreakEvent(bblock, this.player.getBukkitEntity());
+ +
+ // Sword + Creative mode pre-cancel + // Sword + Creative mode pre-cancel
+ event.setCancelled(isSwordNoBreak); + event.setCancelled(isSwordNoBreak);
@ -168,8 +168,7 @@
+ } + }
+ +
+ this.world.getServer().getPluginManager().callEvent(event); + this.world.getServer().getPluginManager().callEvent(event);
+
- if (!this.player.getItemInMainHand().getItem().a(iblockdata, this.world, blockposition, (EntityHuman) this.player)) {
+ if (event.isCancelled()) { + if (event.isCancelled()) {
+ if (isSwordNoBreak) { + if (isSwordNoBreak) {
+ return false; + return false;
@ -191,7 +190,8 @@
+ } + }
+ } + }
+ // CraftBukkit end + // CraftBukkit end
+
- if (!this.player.getItemInMainHand().getItem().a(iblockdata, this.world, blockposition, (EntityHuman) this.player)) {
+ if (false && !this.player.getItemInMainHand().getItem().a(iblockdata, this.world, blockposition, (EntityHuman) this.player)) { // CraftBukkit - false + if (false && !this.player.getItemInMainHand().getItem().a(iblockdata, this.world, blockposition, (EntityHuman) this.player)) { // CraftBukkit - false
return false; return false;
} else { } else {
@ -216,23 +216,18 @@
if ((block instanceof BlockCommand || block instanceof BlockStructure) && !this.player.isCreativeAndOp()) { if ((block instanceof BlockCommand || block instanceof BlockStructure) && !this.player.isCreativeAndOp()) {
this.world.notify(blockposition, iblockdata, iblockdata, 3); this.world.notify(blockposition, iblockdata, iblockdata, 3);
return false; return false;
@@ -221,19 +358,36 @@ @@ -221,6 +358,10 @@
} }
} }
+ // CraftBukkit start + // CraftBukkit start
+ org.bukkit.block.BlockState state = bblock.getState();
+ world.captureDrops = new ArrayList<>(); + world.captureDrops = new ArrayList<>();
boolean flag = this.c(blockposition);
+ if (event.isDropItems()) {
+ for (EntityItem item : world.captureDrops) {
+ world.addEntity(item);
+ }
+ }
+ world.captureDrops = null;
+ // CraftBukkit end + // CraftBukkit end
boolean flag = this.c(blockposition);
if (!this.isCreative()) { if (!this.isCreative()) {
ItemStack itemstack1 = this.player.getItemInMainHand(); @@ -228,13 +369,31 @@
boolean flag1 = this.player.hasBlock(iblockdata); boolean flag1 = this.player.hasBlock(iblockdata);
itemstack1.a(this.world, iblockdata, blockposition, this.player); itemstack1.a(this.world, iblockdata, blockposition, this.player);
@ -244,17 +239,28 @@
iblockdata.getBlock().a(this.world, this.player, blockposition, iblockdata, tileentity, itemstack2); iblockdata.getBlock().a(this.world, this.player, blockposition, iblockdata, tileentity, itemstack2);
} }
+ // CraftBukkit end + // CraftBukkit end
}
+ if (event.isDropItems()) {
+ for (EntityItem item : world.captureDrops) {
+ if (!org.bukkit.craftbukkit.event.CraftEventFactory.callBlockDropItemEvent(bblock, state, this.player, item).isCancelled()) {
+ world.addEntity(item);
+ } + }
+ }
+ }
+ world.captureDrops = null;
+ // CraftBukkit end
+ +
+ // CraftBukkit start - Drop event experience + // CraftBukkit start - Drop event experience
+ if (flag && event != null) { + if (flag && event != null) {
+ iblockdata.getBlock().dropExperience(this.world, blockposition, event.getExpToDrop()); + iblockdata.getBlock().dropExperience(this.world, blockposition, event.getExpToDrop());
} + }
+ // CraftBukkit end + // CraftBukkit end
+
return flag; return flag;
} }
@@ -277,11 +431,40 @@ }
@@ -277,11 +436,40 @@
} }
} }
@ -295,7 +301,7 @@
if (tileentity instanceof ITileInventory) { if (tileentity instanceof ITileInventory) {
Block block = iblockdata.getBlock(); Block block = iblockdata.getBlock();
@@ -305,24 +488,26 @@ @@ -305,24 +493,26 @@
boolean flag = !entityhuman.getItemInMainHand().isEmpty() || !entityhuman.getItemInOffHand().isEmpty(); boolean flag = !entityhuman.getItemInMainHand().isEmpty() || !entityhuman.getItemInOffHand().isEmpty();
boolean flag1 = entityhuman.isSneaking() && flag; boolean flag1 = entityhuman.isSneaking() && flag;

View file

@ -157,6 +157,13 @@ public class CraftEventFactory {
return event; return event;
} }
public static BlockDropItemEvent callBlockDropItemEvent(Block block, BlockState state, EntityPlayer player, EntityItem item) {
BlockDropItemEvent event = new BlockDropItemEvent(block, state, player.getBukkitEntity(), (org.bukkit.entity.Item) item.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);
return event;
}
public static EntityPlaceEvent callEntityPlaceEvent(ItemActionContext itemactioncontext, Entity entity) { public static EntityPlaceEvent callEntityPlaceEvent(ItemActionContext itemactioncontext, Entity entity) {
Player who = (itemactioncontext.getEntity() == null) ? null : (Player) itemactioncontext.getEntity().getBukkitEntity(); Player who = (itemactioncontext.getEntity() == null) ? null : (Player) itemactioncontext.getEntity().getBukkitEntity();
org.bukkit.block.Block blockClicked = CraftBlock.at(itemactioncontext.getWorld(), itemactioncontext.getClickPosition()); org.bukkit.block.Block blockClicked = CraftBlock.at(itemactioncontext.getWorld(), itemactioncontext.getClickPosition());