Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

CreationListener.java

By: a guest on Jan 30th, 2012  |  syntax: Java  |  size: 5.02 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package net.kitecraft.tyrotoxism.propertycontrol.listeners;
  2.  
  3. import net.kitecraft.tyrotoxism.propertycontrol.PropertyControl;
  4. import net.kitecraft.tyrotoxism.propertycontrol.Setting;
  5. import net.kitecraft.tyrotoxism.propertycontrol.property.Creation;
  6.  
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.entity.Entity;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.EventPriority;
  13. import org.bukkit.event.Listener;
  14. import org.bukkit.event.block.Action;
  15. import org.bukkit.event.block.BlockBreakEvent;
  16. import org.bukkit.event.block.BlockIgniteEvent;
  17. import org.bukkit.event.block.BlockPlaceEvent;
  18. import org.bukkit.event.painting.PaintingBreakByEntityEvent;
  19. import org.bukkit.event.painting.PaintingBreakEvent;
  20. import org.bukkit.event.painting.PaintingPlaceEvent;
  21. import org.bukkit.event.player.PlayerBucketEmptyEvent;
  22. import org.bukkit.event.player.PlayerBucketFillEvent;
  23. import org.bukkit.event.player.PlayerInteractEvent;
  24.  
  25. public final class CreationListener implements Listener {
  26.        
  27.         private PropertyControl plugin;
  28.         private boolean setTop;
  29.         private boolean setFirst;
  30.        
  31.         public CreationListener(final PropertyControl plugin) {
  32.                 this.plugin = plugin;
  33.                 this.setTop = true;
  34.                 this.setFirst = true;
  35.         }
  36.        
  37.         @EventHandler(priority = EventPriority.HIGH)
  38.         public final void onBlockPlace(final BlockPlaceEvent event) {
  39.                 if (event.isCancelled()) { return; } else if (check(event.getPlayer())) { event.setCancelled(true); }
  40.         }
  41.        
  42.         @EventHandler(priority = EventPriority.HIGH)
  43.         public final void onBlockBreak(final BlockBreakEvent event) {
  44.                 if (event.isCancelled()) { return; } else if (check(event.getPlayer())) { event.setCancelled(true); }
  45.         }
  46.  
  47.         @EventHandler(priority = EventPriority.HIGH)
  48.         public final void onBlockIgnite(final BlockIgniteEvent event) {
  49.                 if (event.isCancelled()) { return; } else if (check(event.getPlayer())) { event.setCancelled(true); }
  50.         }
  51.  
  52.         @EventHandler(priority = EventPriority.HIGH)
  53.         public final void onPaintingBreak(final PaintingBreakEvent event) {
  54.                 if (event.isCancelled() || !(event instanceof PaintingBreakByEntityEvent)) { return; }
  55.                 Entity entity = ((PaintingBreakByEntityEvent) event).getRemover();
  56.                 if (entity instanceof Player && check((Player) entity)) { event.setCancelled(true); }
  57.         }
  58.  
  59.         @EventHandler(priority = EventPriority.HIGH)
  60.         public final void onPaintingPlace(final PaintingPlaceEvent event) {
  61.                 if (event.isCancelled()) { return; } else if (check(event.getPlayer())) { event.setCancelled(true); }
  62.         }
  63.        
  64.         @EventHandler(priority = EventPriority.HIGH)
  65.         public final void onPlayerBucketEmpty(final PlayerBucketEmptyEvent event) {
  66.                 if (event.isCancelled()) { return; } else if (check(event.getPlayer())) { event.setCancelled(true); }
  67.         }
  68.        
  69.         @EventHandler(priority = EventPriority.HIGH)
  70.         public final void onPlayerBucketFill(final PlayerBucketFillEvent event) {
  71.                 if (event.isCancelled()) { return; } else if (check(event.getPlayer())) { event.setCancelled(true); }
  72.         }
  73.        
  74.         @EventHandler(priority = EventPriority.HIGH)
  75.         public final void onPlayerInteract(final PlayerInteractEvent event) {
  76.                 if (!event.getPlayer().hasPermission("propertycontrol.create") || !check(event.getPlayer())) { return; }
  77.                 if (!(this.plugin.getSetting(null, event.getPlayer().getWorld().getName()) == null || this.plugin.getSetting(null, event.getPlayer().getWorld().getName()).getValue().equalsIgnoreCase("enable"))) { return; }
  78.                 if (this.plugin.getCreation(event.getPlayer()) == null) { this.plugin.getCreations().add(new Creation(this.plugin, this.plugin.getProperties().get(this.plugin.getProperties().size()-1).getID()+1, event.getPlayer())); }
  79.                 final Creation creation = this.plugin.getCreation(event.getPlayer());
  80.                 final Action action = event.getAction();
  81.                 final Location click = event.getClickedBlock().getLocation();
  82.                 if (action.equals(Action.RIGHT_CLICK_AIR)) {
  83.                         creation.remove();
  84.                         this.setTop = true;
  85.                         this.setFirst = true;
  86.                         event.getPlayer().sendMessage("§eProperty creation has been reset.");
  87.                 } else if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
  88.                         final boolean first = this.setFirst || creation.getPoint(true) == null;
  89.                         creation.setPoint(click, first);
  90.                         event.getPlayer().sendMessage("§ePoint "+((first)?1:2)+" set to §3"+click.getBlockX()+"§e, §3"+click.getBlockZ()+"§e.");
  91.                         this.setFirst = !this.setFirst;
  92.                 } else if (action.equals(Action.LEFT_CLICK_BLOCK)) {
  93.                         final boolean top = (this.setTop && click.getBlockY() > creation.getHeight(false)) || click.getBlockY() > creation.getHeight(true);
  94.                         creation.setHeight(click.getBlockY(), top);
  95.                         event.getPlayer().sendMessage("§e"+((top)?"Top":"Bottom")+" set to §3"+click.getBlockY()+"§e.");
  96.                         this.setTop = !top;
  97.                 }
  98.         }
  99.  
  100.         private final boolean check(final Player player) {
  101.                 final Setting tool = this.plugin.getSetting(player.getName(), "tool");
  102.                 if (player.hasPermission("propertycontrol.create") && player.getItemInHand().getType().equals((tool != null)?Material.getMaterial(tool.getValue()):Material.WOOD_SPADE)) { return true; }
  103.                 return false;
  104.         }
  105. }