#676: Add PlayerHarvestBlockEvent

This commit is contained in:
DiamondDagger590 2020-07-03 15:07:15 +10:00 committed by md_5
parent 47abffa218
commit 145921e2a6
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
2 changed files with 35 additions and 3 deletions

View File

@ -1,17 +1,20 @@
--- a/net/minecraft/server/BlockSweetBerryBush.java --- a/net/minecraft/server/BlockSweetBerryBush.java
+++ b/net/minecraft/server/BlockSweetBerryBush.java +++ b/net/minecraft/server/BlockSweetBerryBush.java
@@ -1,6 +1,10 @@ @@ -1,6 +1,13 @@
package net.minecraft.server; package net.minecraft.server;
import java.util.Random; import java.util.Random;
+// CraftBukkit start +// CraftBukkit start
+import java.util.Collections;
+import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.craftbukkit.block.CraftBlock;
+import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.event.player.PlayerHarvestBlockEvent;
+// CraftBukkit end +// CraftBukkit end
public class BlockSweetBerryBush extends BlockPlant implements IBlockFragilePlantElement { public class BlockSweetBerryBush extends BlockPlant implements IBlockFragilePlantElement {
@@ -28,7 +32,7 @@ @@ -28,7 +35,7 @@
int i = (Integer) iblockdata.get(BlockSweetBerryBush.a); int i = (Integer) iblockdata.get(BlockSweetBerryBush.a);
if (i < 3 && random.nextInt(5) == 0 && worldserver.getLightLevel(blockposition.up(), 0) >= 9) { if (i < 3 && random.nextInt(5) == 0 && worldserver.getLightLevel(blockposition.up(), 0) >= 9) {
@ -20,7 +23,7 @@
} }
} }
@@ -42,7 +46,9 @@ @@ -42,7 +49,9 @@
double d1 = Math.abs(entity.locZ() - entity.F); double d1 = Math.abs(entity.locZ() - entity.F);
if (d0 >= 0.003000000026077032D || d1 >= 0.003000000026077032D) { if (d0 >= 0.003000000026077032D || d1 >= 0.003000000026077032D) {
@ -30,3 +33,20 @@
} }
} }
@@ -59,7 +68,15 @@
} else if (i > 1) {
int j = 1 + world.random.nextInt(2);
- a(world, blockposition, new ItemStack(Items.SWEET_BERRIES, j + (flag ? 1 : 0)));
+ // CraftBukkit start
+ PlayerHarvestBlockEvent event = CraftEventFactory.callPlayerHarvestBlockEvent(world, blockposition, entityhuman, Collections.singletonList(new ItemStack(Items.SWEET_BERRIES, j + (flag ? 1 : 0))));
+ if (event.isCancelled()) {
+ return EnumInteractionResult.SUCCESS; // We need to return a success either way, because making it PASS or FAIL will result in a bug where cancelling while harvesting w/ block in hand places block
+ }
+ for (org.bukkit.inventory.ItemStack itemStack : event.getItemsHarvested()) {
+ a(world, blockposition, CraftItemStack.asNMSCopy(itemStack));
+ }
+ // CraftBukkit end
world.playSound((EntityHuman) null, blockposition, SoundEffects.ITEM_SWEET_BERRIES_PICK_FROM_BUSH, SoundCategory.BLOCKS, 1.0F, 0.8F + world.random.nextFloat() * 0.4F);
world.setTypeAndData(blockposition, (IBlockData) iblockdata.set(BlockSweetBerryBush.a, 1), 2);
return EnumInteractionResult.a(world.isClientSide);

View File

@ -196,6 +196,7 @@ import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerEditBookEvent; import org.bukkit.event.player.PlayerEditBookEvent;
import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerExpChangeEvent; import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.event.player.PlayerHarvestBlockEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemBreakEvent; import org.bukkit.event.player.PlayerItemBreakEvent;
import org.bukkit.event.player.PlayerItemMendEvent; import org.bukkit.event.player.PlayerItemMendEvent;
@ -287,6 +288,17 @@ public class CraftEventFactory {
return entityEnterLoveModeEvent; return entityEnterLoveModeEvent;
} }
/**
* Player Harvest Block Event
*/
public static PlayerHarvestBlockEvent callPlayerHarvestBlockEvent(World world, BlockPosition blockposition, EntityHuman who, List<ItemStack> itemsToHarvest) {
List<org.bukkit.inventory.ItemStack> bukkitItemsToHarvest = new ArrayList<>(itemsToHarvest.stream().map(CraftItemStack::asBukkitCopy).collect(Collectors.toList()));
Player player = (Player) who.getBukkitEntity();
PlayerHarvestBlockEvent playerHarvestBlockEvent = new PlayerHarvestBlockEvent(player, CraftBlock.at(world, blockposition), bukkitItemsToHarvest);
Bukkit.getPluginManager().callEvent(playerHarvestBlockEvent);
return playerHarvestBlockEvent;
}
/** /**
* Trade Index Change Event * Trade Index Change Event
*/ */