Fix BlockPlace

By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
CraftBukkit/Spigot 2011-03-21 00:25:26 +01:00
parent 7b978d1dd4
commit c964c8657e
2 changed files with 108 additions and 0 deletions

View file

@ -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
*

View file

@ -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;
}
}