Dan_Ender

Untitled

Oct 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. package ru.Dan_Ender.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5.  
  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.Block;
  11. import org.bukkit.entity.Player;
  12.  
  13. import ru.Dan_Ender.config.LWEConfig;
  14. import ru.Dan_Ender.events.LWEEventListener;
  15.  
  16. public class Buffer {
  17.    
  18.     private final LWEConfig cfg = LWEEventListener.cfg;
  19.     ArrayList<HashMap<Location,String[]>> undo = new ArrayList<>();
  20.     ArrayList<HashMap<Location,String[]>> redo = new ArrayList<>();
  21.     Player p;
  22.     int current = -1;
  23.     HashMap<Location,String[]> last;
  24.    
  25.     public Buffer(Player p) {
  26.         this.p = p;
  27.     }
  28.    
  29.     public void add(HashMap<Location,String[]> rg) {
  30.         if (undo.size()==30) {
  31.             undo.remove(0);
  32.             undo.set(29, rg);
  33.             current = undo.size()-1;
  34.         } else {
  35.             undo.add(rg);
  36.             current = undo.size()-1;
  37.         }
  38.     }
  39.    
  40.     @SuppressWarnings("deprecation")
  41.     public void undo() {
  42.         if (current==-1 || undo.isEmpty()) {
  43.             p.sendMessage(c(cfg.getNothingLeftToUndoMsg()));
  44.             return;
  45.         }
  46.         HashMap<Location,String[]> rg = undo.get(current);
  47.         for (Location loc:rg.keySet()) {
  48.             String[] id = rg.get(loc);
  49.             World w = loc.getWorld();
  50.             Block block = w.getBlockAt(loc);
  51.             block.setType(Material.getMaterial(id[0]));
  52.             block.setData(Byte.parseByte(id[1]));
  53.         }
  54.         p.sendMessage(c(cfg.getUndoMsg()));
  55.         current = current-1;
  56.     }
  57.    
  58.     @Deprecated
  59.     public void redo() {
  60.         if (current<=redo.size()-1 || redo.isEmpty()) {
  61.             p.sendMessage(ChatColor.RED+"Нечего возвращать.");
  62.             return;
  63.         }
  64.         HashMap<Location,String[]> rg = redo.get(current+1);
  65.         for (Location loc:rg.keySet()) {
  66.             String[] id = rg.get(loc);
  67.             World w = loc.getWorld();
  68.             Block block = w.getBlockAt(loc);
  69.             block.setType(Material.getMaterial(id[0]));
  70.             block.setData(Byte.parseByte(id[1]));
  71.         }
  72.         p.sendMessage(ChatColor.LIGHT_PURPLE+"Успешно возвращено.");
  73.     }
  74.    
  75.     public int getBufferSize() {
  76.         return undo.size();
  77.     }
  78.    
  79.     private String c(String s) {
  80.         return ChatColor.translateAlternateColorCodes('&', s);
  81.     }
  82.    
  83. }
Add Comment
Please, Sign In to add comment