Advertisement
Exception_Prototype

Untitled

Aug 10th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. public final class RestoreManager implements Runnable {
  2.  
  3.     private final Set<RestoreNode> nodes = Sets.newHashSet();
  4.  
  5.     public void restore(Block block, Material material) {
  6.         restore(block, material, seconds);
  7.     }
  8.  
  9.     public void restore(Block block, Material material, long delay) {
  10.         RestoreNode node = new RestoreNode(block.getState(), material, delay);
  11.         if (delay > 0L) {
  12.             this.nodes.add(node);
  13.             node.onStart();
  14.         } else {
  15.             node.transfer();
  16.         }
  17.     }
  18.  
  19.     @Override
  20.     public void run() {
  21.         if (!this.nodes.isEmpty()) {
  22.             long time = System.currentTimeMillis();
  23.             this.nodes.removeIf(node -> node.update(time));
  24.         }
  25.     }
  26.  
  27.     public static final class RestoreNode {
  28.  
  29.         private final BlockState state;
  30.         private final Material material;
  31.         private final long transferExpire;
  32.  
  33.         RestoreNode(BlockState state, Material material, long delay) {
  34.             this.state = state;
  35.             this.material = material;
  36.             this.transferExpire = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(delay);
  37.         }
  38.  
  39.         boolean update(long time) {
  40.             if (time >= getTransferExpire()) {
  41.                 transfer();
  42.                 return true;
  43.             }
  44.             return false;
  45.         }
  46.  
  47.         void onStart() {
  48.             this.state.setType(Material.BEDROCK);
  49.             this.state.update(true);
  50.         }
  51.  
  52.         void transfer() {
  53.             this.state.setType(this.material);
  54.             this.state.update(true);
  55.         }
  56.  
  57.         long getTransferExpire() {
  58.             return this.transferExpire;
  59.         }
  60.  
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement