Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package net.dipselmedia.WorldProtect;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.ChatColor;
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.World;
  10. import org.bukkit.block.BlockState;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.event.EventHandler;
  13. import org.bukkit.event.Listener;
  14. import org.bukkit.event.block.BlockBreakEvent;
  15. import org.bukkit.event.block.BlockPlaceEvent;
  16.  
  17. public class BlockListener implements Listener {
  18.  
  19.     /** Main instance */
  20.     private Main plugin;
  21.     /** LinkedList that saves every block placed by the user */
  22.     private LinkedList<BlockState> placedBlocks;
  23.    
  24.     /**
  25.      * Simple constructor
  26.      * @param plugin Instance of main class
  27.      */
  28.     public BlockListener (Main plugin){
  29.         this.plugin = plugin;
  30.         Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
  31.         // Create the list
  32.         placedBlocks = new LinkedList<BlockState>();
  33.     }
  34.    
  35.     /**
  36.      * Called when a player places a block
  37.      * @param e The event
  38.      */
  39.     @EventHandler
  40.     public void onBlockPlace(BlockPlaceEvent e) {
  41.         // Apply the placed block to the list
  42.         placedBlocks.add(e.getBlock().getState());
  43.     }
  44.    
  45.     /**
  46.      * Called when a block is being broken
  47.      * @param e The event
  48.      */
  49.     @EventHandler
  50.     public void onBlockBreak(BlockBreakEvent e) {
  51.         // Check whether protection is on or off
  52.         if(plugin.getState()) {
  53.             Player p = e.getPlayer();
  54.             // Get the broken block
  55.             BlockState  bs = e.getBlock().getState();
  56.            
  57.             // Check if the list contains the block
  58.             // (i.e. the block has been paced by the user)
  59.             if(placedBlocks.contains(bs)){
  60.                 placedBlocks.remove(bs);
  61.             } else {
  62.                 // Get the destroyed block's material
  63.                 Material oldMaterial = bs.getBlock().getType();
  64.                 // Send the user a message
  65.                 p.sendMessage(ChatColor.GOLD
  66.                         + "Sorry, you aren't allowed to destroy this "
  67.                         + ChatColor.YELLOW
  68.                         + oldMaterial.toString()
  69.                         + ChatColor.GOLD
  70.                         + " block!");
  71.                 // Create a new Location where the old block was
  72.                 int x = bs.getX();
  73.                 int y = bs.getY();
  74.                 int z = bs.getZ();
  75.                 World world = bs.getWorld();
  76.                 Location loc = new Location(world, x, y, z);
  77.                
  78.                
  79.                 // Get the new block that is at this Location
  80.                 // (should be air, as this block has just been broken)
  81.                 bs = loc.getBlock().getState();
  82.                
  83.                 // Print some debug info to server console
  84.                 System.out.println("[WorldProtect] Material before: " + bs.getType().toString());
  85.                
  86.                 // Set the block's Material
  87.                 bs.setType(oldMaterial);
  88.                
  89.                 // Print some debug info to server console
  90.                 System.out.println("[WorldProtect] Material afterwards: " + bs.getType().toString());
  91.             }
  92.         }
  93.     }
  94.    
  95. }