SPIGOT-5282: Improve bucket event API

This commit is contained in:
md_5 2019-08-23 20:30:16 +10:00
parent 6bbb3b04ed
commit 4914117237
3 changed files with 22 additions and 24 deletions

View File

@ -11,13 +11,12 @@
public class EntityCow extends EntityAnimal { public class EntityCow extends EntityAnimal {
public EntityCow(EntityTypes<? extends EntityCow> entitytypes, World world) { public EntityCow(EntityTypes<? extends EntityCow> entitytypes, World world) {
@@ -55,13 +60,23 @@ @@ -55,13 +60,22 @@
ItemStack itemstack = entityhuman.b(enumhand); ItemStack itemstack = entityhuman.b(enumhand);
if (itemstack.getItem() == Items.BUCKET && !entityhuman.abilities.canInstantlyBuild && !this.isBaby()) { if (itemstack.getItem() == Items.BUCKET && !entityhuman.abilities.canInstantlyBuild && !this.isBaby()) {
+ // CraftBukkit start - Got milk? + // CraftBukkit start - Got milk?
+ org.bukkit.Location loc = this.getBukkitEntity().getLocation(); + org.bukkit.event.player.PlayerBucketFillEvent event = CraftEventFactory.callPlayerBucketFillEvent(entityhuman.world, entityhuman, this.getChunkCoordinates(), this.getChunkCoordinates(), null, itemstack, Items.MILK_BUCKET);
+ org.bukkit.event.player.PlayerBucketFillEvent event = CraftEventFactory.callPlayerBucketFillEvent(entityhuman, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), null, itemstack, Items.MILK_BUCKET);
+ +
+ if (event.isCancelled()) { + if (event.isCancelled()) {
+ return false; + return false;

View File

@ -20,7 +20,7 @@
if (iblockdata.getBlock() instanceof IFluidSource) { if (iblockdata.getBlock() instanceof IFluidSource) {
+ // CraftBukkit start + // CraftBukkit start
+ FluidType dummyFluid = ((IFluidSource) iblockdata.getBlock()).removeFluid(DummyGeneratorAccess.INSTANCE, blockposition, iblockdata); + FluidType dummyFluid = ((IFluidSource) iblockdata.getBlock()).removeFluid(DummyGeneratorAccess.INSTANCE, blockposition, iblockdata);
+ PlayerBucketFillEvent event = CraftEventFactory.callPlayerBucketFillEvent(entityhuman, blockposition.getX(), blockposition.getY(), blockposition.getZ(), null, itemstack, dummyFluid.b()); + PlayerBucketFillEvent event = CraftEventFactory.callPlayerBucketFillEvent(world, entityhuman, blockposition, blockposition, movingobjectpositionblock.getDirection(), itemstack, dummyFluid.b());
+ +
+ if (event.isCancelled()) { + if (event.isCancelled()) {
+ ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutBlockChange(world, blockposition)); // SPIGOT-5163 (see PlayerInteractManager) + ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutBlockChange(world, blockposition)); // SPIGOT-5163 (see PlayerInteractManager)
@ -94,7 +94,7 @@
} else { } else {
+ // CraftBukkit start + // CraftBukkit start
+ if (entityhuman != null) { + if (entityhuman != null) {
+ PlayerBucketEmptyEvent event = CraftEventFactory.callPlayerBucketEmptyEvent(entityhuman, clicked.getX(), clicked.getY(), clicked.getZ(), enumdirection, itemstack); + PlayerBucketEmptyEvent event = CraftEventFactory.callPlayerBucketEmptyEvent(world, entityhuman, blockposition, clicked, enumdirection, itemstack);
+ if (event.isCancelled()) { + if (event.isCancelled()) {
+ ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutBlockChange(world, blockposition)); // SPIGOT-4238: needed when looking through entity + ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutBlockChange(world, blockposition)); // SPIGOT-4238: needed when looking through entity
+ ((EntityPlayer) entityhuman).getBukkitEntity().updateInventory(); // SPIGOT-4541 + ((EntityPlayer) entityhuman).getBukkitEntity().updateInventory(); // SPIGOT-4541

View File

@ -200,16 +200,15 @@ public class CraftEventFactory {
public static Entity entityDamage; // For use in EntityDamageByEntityEvent public static Entity entityDamage; // For use in EntityDamageByEntityEvent
// helper methods // helper methods
private static boolean canBuild(CraftWorld world, Player player, int x, int z) { private static boolean canBuild(World world, Player player, int x, int z) {
WorldServer worldServer = world.getHandle();
int spawnSize = Bukkit.getServer().getSpawnRadius(); int spawnSize = Bukkit.getServer().getSpawnRadius();
if (world.getHandle().getWorldProvider().getDimensionManager() != DimensionManager.OVERWORLD) return true; if (world.getWorldProvider().getDimensionManager() != DimensionManager.OVERWORLD) return true;
if (spawnSize <= 0) return true; if (spawnSize <= 0) return true;
if (((CraftServer) Bukkit.getServer()).getHandle().getOPs().isEmpty()) return true; if (((CraftServer) Bukkit.getServer()).getHandle().getOPs().isEmpty()) return true;
if (player.isOp()) return true; if (player.isOp()) return true;
BlockPosition chunkcoordinates = worldServer.getSpawn(); BlockPosition chunkcoordinates = world.getSpawn();
int distanceFromSpawn = Math.max(Math.abs(x - chunkcoordinates.getX()), Math.abs(z - chunkcoordinates.getZ())); int distanceFromSpawn = Math.max(Math.abs(x - chunkcoordinates.getX()), Math.abs(z - chunkcoordinates.getZ()));
return distanceFromSpawn > spawnSize; return distanceFromSpawn > spawnSize;
@ -276,7 +275,7 @@ public class CraftEventFactory {
boolean canBuild = true; boolean canBuild = true;
for (int i = 0; i < blockStates.size(); i++) { for (int i = 0; i < blockStates.size(); i++) {
if (!canBuild(craftWorld, player, blockStates.get(i).getX(), blockStates.get(i).getZ())) { if (!canBuild(world, player, blockStates.get(i).getX(), blockStates.get(i).getZ())) {
canBuild = false; canBuild = false;
break; break;
} }
@ -304,7 +303,7 @@ public class CraftEventFactory {
Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ); Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ);
Block placedBlock = replacedBlockState.getBlock(); Block placedBlock = replacedBlockState.getBlock();
boolean canBuild = canBuild(craftWorld, player, placedBlock.getX(), placedBlock.getZ()); boolean canBuild = canBuild(world, player, placedBlock.getX(), placedBlock.getZ());
org.bukkit.inventory.ItemStack item; org.bukkit.inventory.ItemStack item;
EquipmentSlot equipmentSlot; EquipmentSlot equipmentSlot;
@ -347,32 +346,32 @@ public class CraftEventFactory {
/** /**
* Bucket methods * Bucket methods
*/ */
public static PlayerBucketEmptyEvent callPlayerBucketEmptyEvent(EntityHuman who, int clickedX, int clickedY, int clickedZ, EnumDirection clickedFace, ItemStack itemInHand) { public static PlayerBucketEmptyEvent callPlayerBucketEmptyEvent(World world, EntityHuman who, BlockPosition changed, BlockPosition clicked, EnumDirection clickedFace, ItemStack itemInHand) {
return (PlayerBucketEmptyEvent) getPlayerBucketEvent(false, who, clickedX, clickedY, clickedZ, clickedFace, itemInHand, Items.BUCKET); return (PlayerBucketEmptyEvent) getPlayerBucketEvent(false, world, who, changed, clicked, clickedFace, itemInHand, Items.BUCKET);
} }
public static PlayerBucketFillEvent callPlayerBucketFillEvent(EntityHuman who, int clickedX, int clickedY, int clickedZ, EnumDirection clickedFace, ItemStack itemInHand, net.minecraft.server.Item bucket) { public static PlayerBucketFillEvent callPlayerBucketFillEvent(World world, EntityHuman who, BlockPosition changed, BlockPosition clicked, EnumDirection clickedFace, ItemStack itemInHand, net.minecraft.server.Item bucket) {
return (PlayerBucketFillEvent) getPlayerBucketEvent(true, who, clickedX, clickedY, clickedZ, clickedFace, itemInHand, bucket); return (PlayerBucketFillEvent) getPlayerBucketEvent(true, world, who, clicked, changed, clickedFace, itemInHand, bucket);
} }
private static PlayerEvent getPlayerBucketEvent(boolean isFilling, EntityHuman who, int clickedX, int clickedY, int clickedZ, EnumDirection clickedFace, ItemStack itemstack, net.minecraft.server.Item item) { private static PlayerEvent getPlayerBucketEvent(boolean isFilling, World world, EntityHuman who, BlockPosition changed, BlockPosition clicked, EnumDirection clickedFace, ItemStack itemstack, net.minecraft.server.Item item) {
Player player = (who == null) ? null : (Player) who.getBukkitEntity(); Player player = (Player) who.getBukkitEntity();
CraftItemStack itemInHand = CraftItemStack.asNewCraftStack(item); CraftItemStack itemInHand = CraftItemStack.asNewCraftStack(item);
Material bucket = CraftMagicNumbers.getMaterial(itemstack.getItem()); Material bucket = CraftMagicNumbers.getMaterial(itemstack.getItem());
CraftWorld craftWorld = (CraftWorld) player.getWorld();
CraftServer craftServer = (CraftServer) player.getServer(); CraftServer craftServer = (CraftServer) player.getServer();
Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ); Block block = CraftBlock.at(world, changed);
Block blockClicked = CraftBlock.at(world, clicked);
BlockFace blockFace = CraftBlock.notchToBlockFace(clickedFace); BlockFace blockFace = CraftBlock.notchToBlockFace(clickedFace);
PlayerEvent event = null; PlayerEvent event;
if (isFilling) { if (isFilling) {
event = new PlayerBucketFillEvent(player, blockClicked, blockFace, bucket, itemInHand); event = new PlayerBucketFillEvent(player, block, blockClicked, blockFace, bucket, itemInHand);
((PlayerBucketFillEvent) event).setCancelled(!canBuild(craftWorld, player, clickedX, clickedZ)); ((PlayerBucketFillEvent) event).setCancelled(!canBuild(world, player, changed.getX(), changed.getZ()));
} else { } else {
event = new PlayerBucketEmptyEvent(player, blockClicked, blockFace, bucket, itemInHand); event = new PlayerBucketEmptyEvent(player, block, blockClicked, blockFace, bucket, itemInHand);
((PlayerBucketEmptyEvent) event).setCancelled(!canBuild(craftWorld, player, clickedX, clickedZ)); ((PlayerBucketEmptyEvent) event).setCancelled(!canBuild(world, player, changed.getX(), changed.getZ()));
} }
craftServer.getPluginManager().callEvent(event); craftServer.getPluginManager().callEvent(event);