From c3ccf30b6d393632c0d6c48b208a1bd3da7d2aba Mon Sep 17 00:00:00 2001 From: Zrips Date: Fri, 1 Feb 2019 15:38:25 +0200 Subject: [PATCH] Lets update signs and heads in hard way and lets make it more optimized --- .../com/gamingmesh/jobs/Signs/SignUtil.java | 303 ++++++++++-------- .../jobs/config/LanguageManager.java | 2 +- .../gamingmesh/jobs/stuff/TabComplete.java | 109 ++++--- 3 files changed, 231 insertions(+), 183 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Signs/SignUtil.java b/src/main/java/com/gamingmesh/jobs/Signs/SignUtil.java index 2dcf091c..46f6e337 100644 --- a/src/main/java/com/gamingmesh/jobs/Signs/SignUtil.java +++ b/src/main/java/com/gamingmesh/jobs/Signs/SignUtil.java @@ -3,19 +3,24 @@ package com.gamingmesh.jobs.Signs; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Skull; +import org.bukkit.block.data.BlockData; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.CMILib.CMIChatColor; import com.gamingmesh.jobs.config.CommentedYamlConfiguration; import com.gamingmesh.jobs.container.TopList; +import com.gamingmesh.jobs.stuff.Debug; public class SignUtil { @@ -32,9 +37,9 @@ public class SignUtil { // Sign file public void LoadSigns() { - // Boolean false does not create a file - if (!Jobs.getGCManager().SignsEnabled) - return; + // Boolean false does not create a file + if (!Jobs.getGCManager().SignsEnabled) + return; Signs.GetAllSigns().clear(); File file = new File(plugin.getDataFolder(), "Signs.yml"); @@ -65,8 +70,8 @@ public class SignUtil { // Signs save file public void saveSigns() { - if (!Jobs.getGCManager().SignsEnabled) - return; + if (!Jobs.getGCManager().SignsEnabled) + return; File f = new File(plugin.getDataFolder(), "Signs.yml"); YamlConfiguration conf = YamlConfiguration.loadConfiguration(f); @@ -99,148 +104,184 @@ public class SignUtil { } public boolean SignUpdate(String JobName) { - if (!Jobs.getGCManager().SignsEnabled) - return true; + if (!Jobs.getGCManager().SignsEnabled) + return true; - List Copy = new ArrayList<>(Signs.GetAllSigns().size()); - for (com.gamingmesh.jobs.Signs.Sign foo : Signs.GetAllSigns()) { - Copy.add(foo); - } int timelapse = 1; - for (com.gamingmesh.jobs.Signs.Sign one : Copy) { + + List PlayerList = new ArrayList<>(); + + if (JobName.equalsIgnoreCase("gtoplist")) + PlayerList = Jobs.getJobsDAO().getGlobalTopList(0); + + for (com.gamingmesh.jobs.Signs.Sign one : new ArrayList<>(Signs.GetAllSigns())) { String SignJobName = one.GetJobName(); - if (JobName.equalsIgnoreCase(SignJobName)) { - String SignsWorld = one.GetWorld(); - double SignsX = one.GetX(); - double SignsY = one.GetY(); - double SignsZ = one.GetZ(); - int number = one.GetNumber() - 1; + if (!JobName.equalsIgnoreCase(SignJobName)) + continue; - List PlayerList = new ArrayList<>(); - if (!JobName.equalsIgnoreCase("gtoplist")) { - PlayerList = Jobs.getJobsDAO().toplist(SignJobName, number); - } else { - PlayerList = Jobs.getJobsDAO().getGlobalTopList(number); - } - if (PlayerList.size() != 0) { - World world = Bukkit.getWorld(SignsWorld); - if (world == null) - continue; - Location nloc = new Location(world, SignsX, SignsY, SignsZ); - Block block = nloc.getBlock(); - if (!(block.getState() instanceof org.bukkit.block.Sign)) { - Signs.GetAllSigns().remove(one); - saveSigns(); - } else { - org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState(); - if (!one.isSpecial()) { - for (int i = 0; i < 4; i++) { - if (i >= PlayerList.size()) - break; - String PlayerName = PlayerList.get(i).getPlayerName(); + World world = Bukkit.getWorld(one.GetWorld()); + if (world == null) + continue; - if (PlayerName != null && PlayerName.length() > 8) { - String PlayerNameStrip = PlayerName.split("(?<=\\G.{7})")[0]; - PlayerName = PlayerNameStrip + "~"; - } + double SignsX = one.GetX(); + double SignsY = one.GetY(); + double SignsZ = one.GetZ(); + int number = one.GetNumber() - 1; - if (PlayerName == null) - PlayerName = "Unknown"; - - String line = Jobs.getLanguage().getMessage("signs.List"); - line = line.replace("[number]", String.valueOf(i + number + 1)); - line = line.replace("[player]", PlayerName); - line = line.replace("[level]", String.valueOf(PlayerList.get(i).getLevel())); - - sign.setLine(i, line); - } - sign.update(); - UpdateHead(sign, PlayerList.get(0).getPlayerName(), timelapse); - } else { - String PlayerName = PlayerList.get(0).getPlayerName(); - if (PlayerName.length() > 8) { - String PlayerNameStrip = PlayerName.split("(?<=\\G.{7})")[0]; - PlayerName = PlayerNameStrip + "~"; - } - String line1 = Jobs.getLanguage().getMessage("signs.SpecialList.p" + one.GetNumber(), - "[number]", one.GetNumber() + number + 1, - "[player]", PlayerName, - "[level]", PlayerList.get(0).getLevel(), - "[job]", JobName); - - sign.setLine(0, line1); - - line1 = Jobs.getLanguage().getMessage("signs.SpecialList.name", - "[number]", one.GetNumber() + number + 1, - "[player]", PlayerName, - "[level]", PlayerList.get(0).getLevel(), - "[job]", JobName); - - sign.setLine(1, line1); - - line1 = Jobs.getLanguage().getMessage("signs.SpecialList.level", - "[number]", one.GetNumber() + number + 1, - "[player]", PlayerName, - "[level]", PlayerList.get(0).getLevel(), - "[job]", JobName); - - sign.setLine(2, line1); - - line1 = Jobs.getLanguage().getMessage("signs.SpecialList.bottom", - "[number]", one.GetNumber() + number + 1, - "[player]", PlayerName, - "[level]", PlayerList.get(0).getLevel(), - "[job]", JobName); - - sign.setLine(3, line1); - sign.update(); - UpdateHead(sign, PlayerList.get(0).getPlayerName(), timelapse); - } - - timelapse++; - } - } + if (!JobName.equalsIgnoreCase("gtoplist")) { + PlayerList = Jobs.getJobsDAO().toplist(SignJobName, number); } + + if (PlayerList.isEmpty()) + continue; + + Location nloc = new Location(world, SignsX, SignsY, SignsZ); + Block block = nloc.getBlock(); + if (!(block.getState() instanceof org.bukkit.block.Sign)) { + Signs.GetAllSigns().remove(one); + saveSigns(); + continue; + } + + org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState(); + if (!one.isSpecial()) { + for (int i = 0; i < 4; i++) { + if (i >= PlayerList.size()) + break; + String PlayerName = PlayerList.get(i).getPlayerName(); + + if (PlayerName != null && PlayerName.length() > 8) { + String PlayerNameStrip = PlayerName.split("(?<=\\G.{7})")[0]; + PlayerName = PlayerNameStrip + "~"; + } + + if (PlayerName == null) + PlayerName = "Unknown"; + + String line = Jobs.getLanguage().getMessage("signs.List"); + line = line.replace("[number]", String.valueOf(i + number + 1)); + line = line.replace("[player]", PlayerName); + line = line.replace("[level]", String.valueOf(PlayerList.get(i).getLevel())); + + sign.setLine(i, line); + } + sign.update(); + if (!UpdateHead(sign, PlayerList.get(0).getPlayerName(), timelapse)) + timelapse--; + } else { + + TopList pl = PlayerList.get(one.GetNumber() - 1); + + String PlayerName = pl.getPlayerName(); + + if (PlayerName.length() > 8) { + String PlayerNameStrip = PlayerName.split("(?<=\\G.{7})")[0]; + PlayerName = PlayerNameStrip + "~"; + } + String line1 = Jobs.getLanguage().getMessage("signs.SpecialList.p" + one.GetNumber(), + "[number]", one.GetNumber() + number + 1, + "[player]", PlayerName, + "[level]", pl.getLevel(), + "[job]", JobName); + + sign.setLine(0, line1); + + line1 = Jobs.getLanguage().getMessage("signs.SpecialList.name", + "[number]", one.GetNumber() + number + 1, + "[player]", PlayerName, + "[level]", pl.getLevel(), + "[job]", JobName); + + sign.setLine(1, line1); + + line1 = Jobs.getLanguage().getMessage("signs.SpecialList.level", + "[number]", one.GetNumber() + number + 1, + "[player]", PlayerName, + "[level]", pl.getLevel(), + "[job]", JobName); + + sign.setLine(2, line1); + + line1 = Jobs.getLanguage().getMessage("signs.SpecialList.bottom", + "[number]", one.GetNumber() + number + 1, + "[player]", PlayerName, + "[level]", pl.getLevel(), + "[job]", JobName); + + sign.setLine(3, line1); + sign.update(); + + if (!UpdateHead(sign, PlayerName, timelapse)) + timelapse--; + } + + timelapse++; + } return true; } - public void UpdateHead(final org.bukkit.block.Sign sign, final String Playername, final int timelapse) { + public boolean UpdateHead(final org.bukkit.block.Sign sign, final String Playername, int timelapse) { + try { + timelapse = timelapse < 1 ? 1 : timelapse; + org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) sign.getData(); + BlockFace directionFacing = signMat.getFacing(); - Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - @Override - public void run() { + final Location loc = sign.getLocation().clone(); + loc.add(0, 1, 0); - org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) sign.getData(); - BlockFace directionFacing = signMat.getFacing(); + if (Playername == null) + return false; - Location loc = sign.getLocation().clone(); - loc.add(0, 1, 0); + Block block = loc.getBlock(); - if (Playername == null) + if (block == null || !(block.getState() instanceof Skull)) + loc.add(directionFacing.getOppositeFace().getModX(), 0, directionFacing.getOppositeFace().getModZ()); + + block = loc.getBlock(); + + if (block == null || !(block.getState() instanceof Skull)) + return false; + + Skull skull = (Skull) block.getState(); + + if (skull == null) + return false; + if (skull.getOwner() != null && skull.getOwner().equalsIgnoreCase(Playername)) + return false; + + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + @Override + public void run() { + + Block b = loc.getBlock(); + final Material type = b.getType(); + final BlockData data = b.getBlockData(); + + b.setType(Material.AIR); + + Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + @Override + public void run() { + Block b = loc.getBlock(); + + b.setType(type); + b.setBlockData(data); + Skull skull = (Skull) b.getState(); + if (skull == null) + return; + skull.setOwner(Playername); + skull.update(); + return; + } + }, 1L); return; - - Block block = loc.getBlock(); - - if (block == null || !(block.getState() instanceof Skull)) - loc.add(directionFacing.getOppositeFace().getModX(), 0, directionFacing.getOppositeFace().getModZ()); - - block = loc.getBlock(); - - if (block == null || !(block.getState() instanceof Skull)) - return; - - Skull skull = (Skull) block.getState(); - - if (skull == null) - return; - - skull.setOwner(Playername); - skull.update(); - - return; - } - }, timelapse * Jobs.getGCManager().InfoUpdateInterval * 20L); + } + }, timelapse * Jobs.getGCManager().InfoUpdateInterval * 20L); + } catch (Exception | Error e) { + e.printStackTrace(); + } + return true; } } diff --git a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java index 12dc504d..f34028eb 100644 --- a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java @@ -342,7 +342,7 @@ public class LanguageManager { c.get("command.give.help.info", "Gives item by jobs name and item category name. Player name is optional"); c.get("command.give.help.args", "[playername] [jobname] [items/limiteditems] [jobitemname]"); - Jobs.getGCManager().commandArgs.put("give", Arrays.asList("[playername]", "[jobname]", "[jobitemname]")); + Jobs.getGCManager().commandArgs.put("give", Arrays.asList("[playername]", "[jobname]%%[jobitemname]", "[jobitemname]")); c.get("command.give.output.notonline", "&4Player [%playername%] is not online!"); c.get("command.give.output.noitem", "&4Can't find any item by given name!"); diff --git a/src/main/java/com/gamingmesh/jobs/stuff/TabComplete.java b/src/main/java/com/gamingmesh/jobs/stuff/TabComplete.java index 1e9a8005..fafc8329 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/TabComplete.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/TabComplete.java @@ -51,73 +51,80 @@ public class TabComplete implements TabCompleter { continue; String arg = ArgsList.get(i - 1); - List temp = new ArrayList<>(); + List t2 = new ArrayList<>(); if (arg.contains("%%")) for (String one : arg.split("%%")) { - temp.add(one); + t2.add(one); } + else + t2.add(arg); - switch (arg) { - case "[jobname]": - List Jobsai = Jobs.getJobs(); - for (Job one : Jobsai) { - temp.add(one.getName()); - } - break; - case "[playername]": - for (Player player : Bukkit.getOnlinePlayers()) { - temp.add(player.getName()); - } - break; - case "[action]": - for (ActionType action : ActionType.values()) { - temp.add(action.getName()); - } - break; - case "[jobitemname]": - if (args[3].equals("items")) { + List temp = new ArrayList<>(); + for (String ar : t2) { + switch (ar) { + case "[jobname]": + List Jobsai = Jobs.getJobs(); + for (Job one : Jobsai) { + temp.add(one.getName()); + } + break; + case "[playername]": + for (Player player : Bukkit.getOnlinePlayers()) { + temp.add(player.getName()); + } + break; + case "[action]": + for (ActionType action : ActionType.values()) { + temp.add(action.getName()); + } + break; + case "[jobitemname]": for (Entry one : ItemBoostManager.getItems().entrySet()) { temp.add(one.getValue().getNode()); } - } else if (args[3].equals("limiteditems")) { - Job oneJob = Jobs.getJob(args[i - 1]); - if (oneJob != null) - if (args[3].equals("limiteditems")) { - for (Entry limitedItem : oneJob.getLimitedItems().entrySet()) { - temp.add(limitedItem.getValue().getNode()); + if (args.length > 3 && args[3].equals("limiteditems")) { + Job oneJob = Jobs.getJob(args[i - 1]); + if (oneJob != null) + if (args[3].equals("limiteditems")) { + for (Entry limitedItem : oneJob.getLimitedItems().entrySet()) { + temp.add(limitedItem.getValue().getNode()); + } } - } - } - break; - case "[boosteditems]": - for (Entry one : ItemBoostManager.getItems().entrySet()) { - temp.add(one.getValue().getNode()); - } - break; - case "[oldjob]": - JobsPlayer onePlayerJob = Jobs.getPlayerManager().getJobsPlayer(args[i - 1]); - if (onePlayerJob != null) - for (JobProgression oneOldJob : onePlayerJob.getJobProgression()) { - temp.add(oneOldJob.getJob().getName()); } - break; - case "[oldplayerjob]": - if (sender instanceof Player) { - onePlayerJob = Jobs.getPlayerManager().getJobsPlayer((Player) sender); + break; + case "[boosteditems]": + for (Entry one : ItemBoostManager.getItems().entrySet()) { + temp.add(one.getValue().getNode()); + } + break; + case "[oldjob]": + JobsPlayer onePlayerJob = Jobs.getPlayerManager().getJobsPlayer(args[i - 1]); if (onePlayerJob != null) for (JobProgression oneOldJob : onePlayerJob.getJobProgression()) { temp.add(oneOldJob.getJob().getName()); } - } - break; - case "[newjob]": - if (sender instanceof Player) { - for (Job job : Jobs.getJobs()) { - temp.add(job.getName()); + break; + case "[oldplayerjob]": + if (sender instanceof Player) { + onePlayerJob = Jobs.getPlayerManager().getJobsPlayer((Player) sender); + if (onePlayerJob != null) + for (JobProgression oneOldJob : onePlayerJob.getJobProgression()) { + temp.add(oneOldJob.getJob().getName()); + } } + break; + case "[newjob]": + if (sender instanceof Player) { + for (Job job : Jobs.getJobs()) { + temp.add(job.getName()); + } + } + break; + default: + temp.add(ar); + break; } - break; } StringUtil.copyPartialMatches(PartOfCommand, temp, completionList);