Guest User

Untitled

a guest
Apr 19th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1.     /**
  2.      * Called when a player left or right clicks.
  3.      *
  4.      * @param event The event for said left or right clicks.
  5.      */
  6.     @EventHandler(priority = EventPriority.LOWEST)
  7.     public final void playerInteract(final PlayerInteractEvent event) {
  8.  
  9.         // Check if this is an event the plugin should be interested in, a right click with a
  10.         // spawner egg, if it isn't stop here.
  11.         if (!event.hasItem()
  12.                 || event.getItem().getTypeId() != SPAWN_EGG
  13.                 || !event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
  14.             return;
  15.         }
  16.  
  17.         Player player = event.getPlayer();
  18.         ItemStack itemInHand = event.getItem();
  19.         EntityType entityType = EntityType.fromId(itemInHand.getDurability());
  20.  
  21.         // Check if the Metadata on the egg being placed is valid for a spawner, if it
  22.         // isn't stop here.
  23.         if (entityType == null || !entityType.isAlive() || !entityType.isSpawnable()) {
  24.             return;
  25.         }
  26.  
  27.         // Check if the user has the permission to place the egg they're attempting to
  28.         // place based on sss.place.*, if not stop here.
  29.         if (!player.hasPermission(PLACE_SPECIFIC.get(entityType))) {
  30.             return;
  31.         }
  32.  
  33.         // Prevent the normal spawener egg behaviours
  34.         event.setCancelled(true);
  35.         event.setUseItemInHand(Event.Result.DENY);
  36.  
  37.         // Get the block that the player is attempting to place on and ensure
  38.         // that the block is valid
  39.         Block targetBlock = event.getClickedBlock();
  40.         if (targetBlock.getType().equals(Material.AIR)) {
  41.             return;
  42.         }
  43.  
  44.         // Select the block we should be changing into a spawner and ensure it's air,
  45.         // water, or lava which are blocks you can normally put items in.
  46.         Block placedBlock = event.getClickedBlock().getRelative(event.getBlockFace());
  47.         if (placedBlock.getType() != Material.AIR &&
  48.                 placedBlock.getType() != Material.WATER &&
  49.                 placedBlock.getType() != Material.LAVA) {
  50.             return;
  51.         }
  52.  
  53.         // Save the previous state of the block being manipulated, just in case.
  54.         BlockState previousState = placedBlock.getState();
  55.  
  56.         // Replace the placed block with a spawner cage.
  57.         placedBlock.setType(Material.MOB_SPAWNER);
  58.         CreatureSpawner spawner = (CreatureSpawner) placedBlock.getState();
  59.  
  60.         // Create a block place event for compatibility, then call it.
  61.         BlockPlaceEvent bpEvent = new BlockPlaceEvent(placedBlock, previousState, targetBlock,
  62.                 new ItemStack(Material.MOB_SPAWNER, 1, entityType.getTypeId()),
  63.                 player, canBuild(player, placedBlock.getX(), placedBlock.getZ()));
  64.         Bukkit.getPluginManager().callEvent(bpEvent);
  65.  
  66.         bpEvent.canBuild();
  67.  
  68.         // Ensure that there were no errors when attempting to call the event.
  69.         ItemStack itemInHandEvent = bpEvent.getItemInHand();
  70.         if (bpEvent.isCancelled() || itemInHandEvent == null ||
  71.                 itemInHandEvent.getType() != Material.MOB_SPAWNER) {
  72.             previousState.update(true);
  73.             return;
  74.         }
  75.  
  76.         entityType = EntityType.fromId(bpEvent.getItemInHand().getDurability());
  77.         if (entityType == null) {
  78.             previousState.update(true);
  79.             return;
  80.         }
  81.  
  82.         // Update the spawner to spawn the entity type of the egg held in hand.
  83.         spawner.setSpawnedType(entityType);
  84.         spawner.update(true);
  85.  
  86.         // Remove one monster spawner from the players hand if they're not in creative mode.
  87.         if (placedBlock.getState() instanceof CreatureSpawner
  88.                 && player.getGameMode().equals(GameMode.SURVIVAL)) {
  89.             if (itemInHand.getAmount() > 1) {
  90.                 itemInHand.setAmount(itemInHand.getAmount() - 1);
  91.             } else {
  92.                 player.getInventory().setItemInHand(null);
  93.             }
  94.         }
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment