SHARE
TWEET

RenewBlocks

a guest Sep 16th, 2011 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.V10lator.RenewBlocks;
  2.  
  3. import org.bukkit.Chunk;
  4. import org.bukkit.Location;
  5. import org.bukkit.World;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.event.Event;
  8. import org.bukkit.event.Event.Priority;
  9. import org.bukkit.event.block.BlockBreakEvent;
  10. import org.bukkit.event.block.BlockListener;
  11. import org.bukkit.plugin.PluginManager;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13. import org.bukkit.util.config.Configuration;
  14.  
  15. import couk.Adamki11s.Regios.Regions.GlobalRegionManager;
  16. import couk.Adamki11s.Regios.Regions.Region;
  17.  
  18. public class RenewBlocks extends JavaPlugin
  19. {
  20.   private boolean regios = false;
  21.   private final RenewBlocks that = this;
  22.   private Configuration config;
  23.   private long delay;
  24.  
  25.   public void onEnable()
  26.   {
  27.         PluginManager pm = getServer().getPluginManager();
  28.        
  29.         if(pm.getPlugin("Regios") != null)
  30.           regios = true;
  31.        
  32.         config = getConfiguration();
  33.         config.load();
  34.         delay = config.getInt("Delay", 0) + 1;
  35.        
  36.         pm.registerEvent(Event.Type.BLOCK_BREAK, new RBBL(), Priority.Normal, this);
  37.         getServer().getLogger().info("["+getDescription().getName()+"] Enabled!");
  38.   }
  39.  
  40.   public void onDisable()
  41.   {
  42.         config.setProperty("Delay", delay - 1);
  43.         config.save();
  44.         getServer().getLogger().info("["+getDescription().getName()+"] Disabled!");
  45.   }
  46.  
  47.   private class RBBL extends BlockListener
  48.   {
  49.         public void onBlockBreak(BlockBreakEvent event)
  50.         {
  51.           if(event.isCancelled())
  52.                 return;
  53.          
  54.           Block block = event.getBlock();
  55.           Chunk chunk = block.getChunk();
  56.           int cx = chunk.getX();
  57.           int cz = chunk.getZ();
  58.           World world = block.getWorld();
  59.           int old = block.getTypeId();
  60.           byte data = block.getData();
  61.           Location loc = block.getLocation();
  62.  
  63.           if(regios)
  64.           {
  65.             for(Region region: GlobalRegionManager.getRegions())
  66.             {
  67.                   for(Chunk c: region.getChunkGrid().getChunks())
  68.                   {
  69.                     if(!c.getWorld().getName().equals(world) ||
  70.                                     cx != c.getX() ||
  71.                                     cz != c.getZ())
  72.                     {
  73.                           getServer().getScheduler().scheduleSyncDelayedTask(that, new task(old, data, loc), delay);
  74.                     }
  75.                   }
  76.             }
  77.           }
  78.           else
  79.             getServer().getScheduler().scheduleSyncDelayedTask(that, new task(old, data, loc), 1L);
  80.         }
  81.   }
  82.  
  83.   private class task implements Runnable
  84.   {
  85.     private final int old;
  86.     private final byte data;
  87.     private final Location loc;
  88.  
  89.     private task(int old, byte data, Location loc)
  90.     {
  91.           this.old = old;
  92.           this.data = data;
  93.           this.loc = loc;
  94.     }
  95.    
  96.     public void run()
  97.     {
  98.           loc.getWorld().getBlockAt(loc).setTypeIdAndData(old, data, false);
  99.     }
  100.   }
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top