Guest User

Untitled

a guest
Sep 8th, 2019
147
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package de.blablubbabc.test2;
  2.  
  3. import java.util.Collection;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Material;
  7. import org.bukkit.block.Dispenser;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.Listener;
  11. import org.bukkit.event.block.Action;
  12. import org.bukkit.event.block.BlockDispenseEvent;
  13. import org.bukkit.event.player.PlayerInteractEvent;
  14. import org.bukkit.inventory.EquipmentSlot;
  15.  
  16. public class DispenserTest implements Listener {
  17.  
  18.     @EventHandler
  19.     public void onInteract(PlayerInteractEvent event) {
  20.         if (event.getItem() != null && event.getHand() == EquipmentSlot.HAND
  21.                 && event.hasBlock() && event.getClickedBlock().getType() == Material.DISPENSER) {
  22.             Player player = event.getPlayer();
  23.             Dispenser dispenser = (Dispenser) event.getClickedBlock().getState();
  24.             if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
  25.                 player.sendMessage("dispensing");
  26.                 dispenser.dispenseItem(event.getItem());
  27.             } else {
  28.                 player.sendMessage("dropping");
  29.                 dispenser.dropItem(event.getItem());
  30.             }
  31.             event.setCancelled(true); // don't access or break block
  32.         }
  33.     }
  34.  
  35.     @EventHandler
  36.     public void onDipense(BlockDispenseEvent event) {
  37.         Collection<? extends Player> online = Bukkit.getOnlinePlayers();
  38.         if (online.isEmpty()) return;
  39.         Player player = online.iterator().next();
  40.  
  41.         if (event.getItem().isSimilar(player.getInventory().getItemInOffHand())) {
  42.             player.sendMessage("Dropping item instead.");
  43.             event.setDropItem(true);
  44.         }
  45.     }
  46. }
RAW Paste Data