Advertisement
Gamebuster

Redstone Limiter

Jul 27th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package com.gamebuster19901.test;
  2.  
  3. import java.util.HashSet;
  4.  
  5. import org.bukkit.block.Block;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.event.EventHandler;
  8. import org.bukkit.event.Listener;
  9. import org.bukkit.event.block.BlockRedstoneEvent;
  10. import org.bukkit.plugin.java.JavaPlugin;
  11.  
  12. import net.md_5.bungee.api.ChatColor;
  13.  
  14. public final class TestPlugin extends JavaPlugin implements Listener{
  15.    
  16.     private boolean isOff = false;
  17.     private int pauseTime = 0;
  18.     private HashSet<Block> stoppedRedstones = new HashSet<Block>();
  19.    
  20.     private final short max_redstones = 20;
  21.    
  22.     public final void onEnable(){
  23.         getServer().getPluginManager().registerEvents(this, this);
  24.         getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
  25.             public void run() {
  26.                 if (!isOff){
  27.                     if(pauseTime != 0){
  28.                         pauseTime--;
  29.                         getLogger().info(pauseTime + "");
  30.                     }
  31.                     if (stoppedRedstones.size() != 0 && stoppedRedstones.size() - max_redstones > 0){
  32.                         getLogger().info("Stopped " + stoppedRedstones.size() + "redstone events");
  33.                     }
  34.                     reset();
  35.                 }
  36.             }
  37.         },1,1);
  38.         getLogger().info("Test plugin has been enabled");
  39.     }
  40.    
  41.     public final void onDisable() {
  42.         isOff = true;
  43.     }
  44.    
  45.     @EventHandler
  46.     public void onRedstone(BlockRedstoneEvent event){
  47.         stoppedRedstones.add(event.getBlock()); //hashsets only allow one instance of an object in them, don't need to worry about duplicates!
  48.         getLogger().info(stoppedRedstones.size() + " redstone events fired this tick so far");
  49.         if (stoppedRedstones.size() > max_redstones && pauseTime == 0){
  50.             event.setNewCurrent(0);
  51.             pause(400);
  52.         }
  53.         else if (pauseTime != 0){
  54.             event.setNewCurrent(0);
  55.         }
  56.     }
  57.    
  58.     public void reset(){
  59.         stoppedRedstones.clear();
  60.     }
  61.    
  62.     public void pause(int ticks){
  63.         getLogger().info("Stopping all redstone for " + ticks + " ticks");
  64.         pauseTime = ticks;
  65.         for (Player p : getServer().getOnlinePlayers()){
  66.             //if player can build in the house{
  67.                 p.sendMessage(ChatColor.RED + "Oh no, your house has Overheated! Redstone in your house has been disabled for " + ChatColor.BOLD + ChatColor.RED + ticks / 20 + ChatColor.RESET + ChatColor.RED + " seconds.");
  68.                 p.sendMessage(ChatColor.RED + "To prevent this from happening again, make sure less than " + max_redstones + " redstone blocks change per tick!");
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement