Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package blockCache;
- import java.util.ArrayList;
- import java.util.HashMap;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.entity.Player;
- import org.bukkit.scheduler.BukkitRunnable;
- import me.PCPSells.PWE.PWEMain;
- import me.PCPSells.PWE.apis.ColorAPI;
- import me.PCPSells.PWE.apis.FileAPI;
- public class UndoBlockCache {
- Player p;
- public HashMap<Player, HashMap<Integer, ArrayList<Block>>> undoBlockCache;
- public HashMap<Player, HashMap<Integer, ArrayList<Material>>> undoBlockMatCache;
- public HashMap<Player, ArrayList<Block>> undoBlocksTask;
- public HashMap<Player, Integer> undoID;
- public UndoBlockCache(Player player) {
- p = player;
- undoBlockCache = PWEMain.getCore().undoBlockCache;
- undoBlockMatCache = PWEMain.getCore().undoBlockMatCache;
- undoBlocksTask = PWEMain.getCore().undoBlocksTask;
- undoID = PWEMain.getCore().undoID;
- }
- public void addBlocks(ArrayList<Block> b) {
- HashMap<Integer, ArrayList<Block>> blocks = new HashMap<>();
- ArrayList<Material> matList = new ArrayList<Material>();
- HashMap<Integer, ArrayList<Material>> mats = new HashMap<>();
- if (undoBlockCache.containsKey(p)) {
- if (undoBlockCache.get(p).get(getID()) == null || undoBlockCache.get(p).get(getID()).isEmpty()) {
- blocks.put(getID(), b);
- for (Block block : blocks.get(getID())) {
- matList.add(block.getType());
- }
- mats.put(getID(), matList);
- } else {
- blocks.put(getNewID(), b);
- for (Block block : blocks.get(getID())) {
- matList.add(block.getType());
- }
- mats.put(getID(), matList);
- }
- undoBlockMatCache.put(p, mats);
- undoBlockCache.put(p, blocks);
- } else {
- blocks.put(getID(), b);
- for (Block block : blocks.get(getID())) {
- matList.add(block.getType());
- }
- mats.put(getID(), matList);
- undoBlockMatCache.put(p, mats);
- undoBlockCache.put(p, blocks);
- }
- }
- public Integer getNewID() {
- undoID.put(p, getID() + 1);
- return undoID.get(p);
- }
- public HashMap<Player, HashMap<Integer, ArrayList<Block>>> getBlocks() {
- return undoBlockCache;
- }
- public Integer setOldID() {
- undoID.put(p, getID() - 1);
- return undoID.get(p);
- }
- public Integer getID() {
- return undoID.get(p);
- }
- public void undoSet() {
- if (!undoBlockCache.containsKey(p)) {
- p.sendMessage("key is missing.");
- FileAPI.sendPlayerPrefixFilteredMessage(p, "messages.Nothing-To-Undo");
- return;
- }
- if (undoBlocksTask.containsKey(p)) {
- 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())));
- return;
- }
- ArrayList<Block> blocks = undoBlockCache.get(p).get(getID());
- if (blocks == null || blocks.size() == 0) {
- p.sendMessage("Blocks is null.");
- FileAPI.sendPlayerPrefixFilteredMessage(p, "messages.Nothing-To-Undo");
- return;
- }
- if (PWEMain.getCore().redoID.get(p) == null) {
- PWEMain.getCore().initilizePlayersRedoID(p);
- }
- new RedoBlockCache(p).addBlocks(blocks);
- undoBlocksTask.put(p, blocks);
- runTimer();
- FileAPI.sendPlayerPrefixFilteredMessage(p, "messages.Undo-Is-Now-Running");
- p.sendMessage("ID - " + getID());
- }
- public void runTimer() {
- new BukkitRunnable() {
- @Override
- public void run() {
- try {
- if (undoBlocksTask.get(p).isEmpty()) {
- undoBlocksTask.remove(p);
- setOldID();
- p.sendMessage("New ID - " + getID());
- FileAPI.sendPlayerPrefixFilteredMessage(p, "messages.Undo-Complete");
- cancel();
- return;
- }
- Block b = undoBlocksTask.get(p).get(0);
- b.getLocation().getBlock().setType(undoBlockMatCache.get(p).get(getID()).get(0));
- undoBlockMatCache.get(p).get(getID()).remove(0);
- undoBlocksTask.get(p).remove(0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }.runTaskTimer(PWEMain.getCore(), 0, 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement