From c964c8657e4b153a96edba7628d85ee4a3d8a9df Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Mon, 21 Mar 2011 00:25:26 +0100 Subject: [PATCH] Fix BlockPlace By: Erik Broes --- .../craftbukkit/block/CraftBlockState.java | 5 + .../craftbukkit/event/CraftEventFactory.java | 103 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java index 9475b146bd..ad5bc0425d 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlockState.java @@ -10,6 +10,7 @@ import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.CraftChunk; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.material.MaterialData; +import net.minecraft.server.WorldServer; public class CraftBlockState implements BlockState { private final CraftWorld world; @@ -33,6 +34,10 @@ public class CraftBlockState implements BlockState { createData(block.getData()); } + public static BlockState getBlockState( net.minecraft.server.World world, int x, int y, int z) { + return new CraftBlockState( ((WorldServer) world).getWorld().getBlockAt(x, y, z) ); + } + /** * Gets the world which contains this Block * diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java new file mode 100644 index 0000000000..275b13ba3e --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -0,0 +1,103 @@ +package org.bukkit.craftbukkit.event; + +import net.minecraft.server.ChunkCoordinates; +import net.minecraft.server.EntityHuman; +import net.minecraft.server.Item; +import net.minecraft.server.ItemStack; +import net.minecraft.server.World; +import net.minecraft.server.WorldServer; + +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockState; +import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.craftbukkit.CraftWorld; +import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.craftbukkit.inventory.CraftItemStack; +import org.bukkit.entity.Player; +import org.bukkit.event.Event.Type; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.player.PlayerBucketEmptyEvent; +import org.bukkit.event.player.PlayerBucketFillEvent; +import org.bukkit.event.player.PlayerEvent; + +public class CraftEventFactory { + private static boolean canBuild(CraftWorld world, Player player, int x, int z) { + WorldServer worldServer = world.getHandle(); + int spawnSize = worldServer.x.spawnProtection; + + if (spawnSize <= 0) return true; + if (player.isOp()) return true; + + ChunkCoordinates chunkcoordinates = worldServer.l(); + + int distanceFromSpawn = (int) Math.max(Math.abs(x - chunkcoordinates.a), Math.abs(z - chunkcoordinates.c)); + return distanceFromSpawn > spawnSize; + } + + /** + * Block place methods + */ + public static BlockPlaceEvent callBlockPlaceEvent(World world, EntityHuman who, BlockState replacedBlockState, int clickedX, int clickedY, int clickedZ, int type) { + return callBlockPlaceEvent(world, who, replacedBlockState, clickedX, clickedY, clickedZ, net.minecraft.server.Block.byId[type]); + } + + public static BlockPlaceEvent callBlockPlaceEvent(World world, EntityHuman who, BlockState replacedBlockState, int clickedX, int clickedY, int clickedZ, net.minecraft.server.Block block) { + return callBlockPlaceEvent(world, who, replacedBlockState, clickedX, clickedY, clickedZ, new ItemStack(block)); + } + + public static BlockPlaceEvent callBlockPlaceEvent(World world, EntityHuman who, BlockState replacedBlockState, int clickedX, int clickedY, int clickedZ, ItemStack itemstack) { + CraftWorld craftWorld = ((WorldServer) world).getWorld(); + CraftServer craftServer = ((WorldServer) world).getServer(); + + Player player = (who == null) ? null : (Player) who.getBukkitEntity(); + CraftItemStack itemInHand = new CraftItemStack(itemstack); + + Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ); + Block placedBlock = replacedBlockState.getBlock(); + + boolean canBuild = canBuild(craftWorld, player, placedBlock.getX(), placedBlock.getZ()); + + BlockPlaceEvent event = new BlockPlaceEvent(Type.BLOCK_PLACE, placedBlock, replacedBlockState, blockClicked, itemInHand, player, canBuild); + craftServer.getPluginManager().callEvent(event); + + return event; + } + + /** + * Bucket methods + */ + public static PlayerBucketEmptyEvent callPlayerBucketEmptyEvent(EntityHuman who, int clickedX, int clickedY, int clickedZ, int clickedFace, ItemStack itemInHand) { + return (PlayerBucketEmptyEvent) getPlayerBucketEvent(Type.PLAYER_BUCKET_EMPTY, who, clickedX, clickedY, clickedZ, clickedFace, itemInHand, Item.BUCKET); + } + + public static PlayerBucketFillEvent callPlayerBucketFillEvent(EntityHuman who, int clickedX, int clickedY, int clickedZ, int clickedFace, ItemStack itemInHand, net.minecraft.server.Item bucket) { + return (PlayerBucketFillEvent) getPlayerBucketEvent(Type.PLAYER_BUCKET_FILL, who, clickedX, clickedY, clickedZ, clickedFace, itemInHand, bucket); + } + + private static PlayerEvent getPlayerBucketEvent(Type type, EntityHuman who, int clickedX, int clickedY, int clickedZ, int clickedFace, ItemStack itemstack, net.minecraft.server.Item item) { + Player player = (who == null) ? null : (Player) who.getBukkitEntity(); + CraftItemStack itemInHand = new CraftItemStack(new ItemStack(item)); + Material bucket = Material.getMaterial(itemstack.id); + + CraftWorld craftWorld = (CraftWorld) player.getWorld(); + CraftServer craftServer = (CraftServer) player.getServer(); + + Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ); + BlockFace blockFace = CraftBlock.notchToBlockFace(clickedFace); + + PlayerEvent event = null; + if (type == Type.PLAYER_BUCKET_EMPTY) { + event = new PlayerBucketEmptyEvent(player, blockClicked, blockFace, bucket, itemInHand); + ((PlayerBucketEmptyEvent) event).setCancelled(!canBuild(craftWorld, player, clickedX, clickedZ)); + } else if(type == Type.PLAYER_BUCKET_FILL) { + event = new PlayerBucketFillEvent(player, blockClicked, blockFace, bucket, itemInHand); + ((PlayerBucketFillEvent) event).setCancelled(!canBuild(craftWorld, player, clickedX, clickedZ)); + } + + craftServer.getPluginManager().callEvent(event); + + return event; + } +}