Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. package blockCache;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import org.bukkit.Material;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.scheduler.BukkitRunnable;
  8. import me.PCPSells.PWE.PWEMain;
  9. import me.PCPSells.PWE.apis.ColorAPI;
  10. import me.PCPSells.PWE.apis.FileAPI;
  11. public class UndoBlockCache {
  12. Player p;
  13. public HashMap<Player, HashMap<Integer, ArrayList<Block>>> undoBlockCache;
  14. public HashMap<Player, HashMap<Integer, ArrayList<Material>>> undoBlockMatCache;
  15. public HashMap<Player, ArrayList<Block>> undoBlocksTask;
  16. public HashMap<Player, Integer> undoID;
  17. public UndoBlockCache(Player player) {
  18. p = player;
  19. undoBlockCache = PWEMain.getCore().undoBlockCache;
  20. undoBlockMatCache = PWEMain.getCore().undoBlockMatCache;
  21. undoBlocksTask = PWEMain.getCore().undoBlocksTask;
  22. undoID = PWEMain.getCore().undoID;
  23. }
  24. public void addBlocks(ArrayList<Block> b) {
  25. HashMap<Integer, ArrayList<Block>> blocks = new HashMap<>();
  26. ArrayList<Material> matList = new ArrayList<Material>();
  27. HashMap<Integer, ArrayList<Material>> mats = new HashMap<>();
  28. if (undoBlockCache.containsKey(p)) {
  29. if (undoBlockCache.get(p).get(getID()) == null || undoBlockCache.get(p).get(getID()).isEmpty()) {
  30. blocks.put(getID(), b);
  31. for (Block block : blocks.get(getID())) {
  32. matList.add(block.getType());
  33. }
  34. mats.put(getID(), matList);
  35. } else {
  36. blocks.put(getNewID(), b);
  37. for (Block block : blocks.get(getID())) {
  38. matList.add(block.getType());
  39. }
  40. mats.put(getID(), matList);
  41. }
  42. undoBlockMatCache.put(p, mats);
  43. undoBlockCache.put(p, blocks);
  44. } else {
  45. blocks.put(getID(), b);
  46. for (Block block : blocks.get(getID())) {
  47. matList.add(block.getType());
  48. }
  49. mats.put(getID(), matList);
  50. undoBlockMatCache.put(p, mats);
  51. undoBlockCache.put(p, blocks);
  52. }
  53. }
  54. public Integer getNewID() {
  55. undoID.put(p, getID() + 1);
  56. return undoID.get(p);
  57. }
  58. public HashMap<Player, HashMap<Integer, ArrayList<Block>>> getBlocks() {
  59. return undoBlockCache;
  60. }
  61. public Integer setOldID() {
  62. undoID.put(p, getID() - 1);
  63. return undoID.get(p);
  64. }
  65. public Integer getID() {
  66. return undoID.get(p);
  67. }
  68. public void undoSet() {
  69. if (!undoBlockCache.containsKey(p)) {
  70. p.sendMessage("key is missing.");
  71. FileAPI.sendPlayerPrefixFilteredMessage(p, "messages.Nothing-To-Undo");
  72. return;
  73. }
  74. if (undoBlocksTask.containsKey(p)) {
  75. p.sendMessage(ColorAPI.color(FileAPI.getString("messages.Undo-Is-Already-Running").replace("%prefix%", ColorAPI.color(FileAPI.getConfigConfiguration().getString("messages.Prefix"))).replace("%amount%", "" + undoBlocksTask.get(p).size())));
  76. return;
  77. }
  78. ArrayList<Block> blocks = undoBlockCache.get(p).get(getID());
  79. if (blocks == null || blocks.size() == 0) {
  80. p.sendMessage("Blocks is null.");
  81. FileAPI.sendPlayerPrefixFilteredMessage(p, "messages.Nothing-To-Undo");
  82. return;
  83. }
  84. if (PWEMain.getCore().redoID.get(p) == null) {
  85. PWEMain.getCore().initilizePlayersRedoID(p);
  86. }
  87. new RedoBlockCache(p).addBlocks(blocks);
  88. undoBlocksTask.put(p, blocks);
  89. runTimer();
  90. FileAPI.sendPlayerPrefixFilteredMessage(p, "messages.Undo-Is-Now-Running");
  91. p.sendMessage("ID - " + getID());
  92. }
  93. public void runTimer() {
  94. new BukkitRunnable() {
  95. @Override
  96. public void run() {
  97. try {
  98. if (undoBlocksTask.get(p).isEmpty()) {
  99. undoBlocksTask.remove(p);
  100. setOldID();
  101. p.sendMessage("New ID - " + getID());
  102. FileAPI.sendPlayerPrefixFilteredMessage(p, "messages.Undo-Complete");
  103. cancel();
  104. return;
  105. }
  106. Block b = undoBlocksTask.get(p).get(0);
  107. b.getLocation().getBlock().setType(undoBlockMatCache.get(p).get(getID()).get(0));
  108. undoBlockMatCache.get(p).get(getID()).remove(0);
  109. undoBlocksTask.get(p).remove(0);
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. }
  113. }
  114. }.runTaskTimer(PWEMain.getCore(), 0, 1);
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement