Advertisement
smbarbour

Bukkit place/break event handlers for ported mods.

Jul 29th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. import net.minecraft.server.EntityHuman;
  2. import net.minecraft.server.EntityPlayer;
  3. import net.minecraft.server.ItemInWorldManager;
  4. import net.minecraft.server.ItemStack;
  5. import net.minecraft.server.ModLoader;
  6. import net.minecraft.server.World;
  7.  
  8. import org.bukkit.Server;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.craftbukkit.block.CraftBlockState;
  11. import org.bukkit.craftbukkit.event.CraftEventFactory;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.block.BlockBreakEvent;
  14. import org.bukkit.event.block.BlockPlaceEvent;
  15.  
  16. public class BukkitHandlers {
  17.  
  18.     public static boolean raiseBlockBreakEvent(Server server, World world, int i, int j, int k, String actorName) {
  19.         Block bukkitBlock = world.getWorld().getBlockAt(i, j, k);
  20.         Player actor = server.getPlayer(actorName);
  21.         if (actor == null) {
  22.             actor = getEntityByName(world, actorName).getBukkitEntity();
  23.         }
  24.         BlockBreakEvent event = new BlockBreakEvent(bukkitBlock, actor);
  25.         server.getPluginManager().callEvent(event);
  26.         if (event.isCancelled()) {
  27.             return false;
  28.         } else {
  29.             return true;
  30.         }
  31.     }
  32.    
  33.     public static boolean raiseBlockPlaceEvent(Server server, World world, ItemStack stack, int i, int j, int k, int clickedX, int clickedY, int clickedZ, EntityHuman actor) {
  34.         CraftBlockState replacedBlockState = CraftBlockState.getBlockState(world, i, j, k);
  35.         world.suppressPhysics = true;
  36.         world.setTypeIdAndData(i, j, k, stack.id, stack.getData());
  37.         BlockPlaceEvent event = CraftEventFactory.callBlockPlaceEvent(world, actor, replacedBlockState, clickedX, clickedY, clickedZ);
  38.         replacedBlockState.update(true);
  39.         world.suppressPhysics = false;
  40.         if (event.isCancelled() || !event.canBuild()) {
  41.             world.setTypeIdAndData(i, j, k, replacedBlockState.getTypeId(), replacedBlockState.getRawData());
  42.             return false;
  43.         }
  44.         return true;
  45.     }
  46.    
  47.     public static EntityPlayer getEntityByName(World world, String actorName) {
  48.         return new EntityPlayer(ModLoader.getMinecraftServerInstance(), world, actorName, new ItemInWorldManager(world));
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement