From f19138db4d621de1c64ce1cec720e79b157979c3 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Tue, 3 Sep 2019 17:01:49 +0200 Subject: [PATCH] Fixed signs updating - Level, exp adding and taking amounts now correctly calculating. --- .../jobs/CMILib/VersionChecker.java | 2 +- src/main/java/com/gamingmesh/jobs/Jobs.java | 19 ++--- .../jobs/Placeholders/Placeholder.java | 31 +++----- .../java/com/gamingmesh/jobs/Reflections.java | 12 ++-- .../com/gamingmesh/jobs/Signs/SignUtil.java | 72 +++++++++---------- .../jobs/actions/PotionDrinkInfo.java | 24 ------- .../gamingmesh/jobs/commands/list/area.java | 3 +- .../gamingmesh/jobs/commands/list/exp.java | 4 ++ .../gamingmesh/jobs/commands/list/level.java | 6 +- .../gamingmesh/jobs/container/JobsPlayer.java | 9 +-- .../jobs/stuff/FurnaceBrewingHandling.java | 14 +++- .../com/gamingmesh/jobs/stuff/GiveItem.java | 2 +- 12 files changed, 88 insertions(+), 110 deletions(-) delete mode 100644 src/main/java/com/gamingmesh/jobs/actions/PotionDrinkInfo.java diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/VersionChecker.java b/src/main/java/com/gamingmesh/jobs/CMILib/VersionChecker.java index d0947f18..be2cd1b3 100644 --- a/src/main/java/com/gamingmesh/jobs/CMILib/VersionChecker.java +++ b/src/main/java/com/gamingmesh/jobs/CMILib/VersionChecker.java @@ -83,7 +83,7 @@ public class VersionChecker { Version() { try { this.value = Integer.valueOf(this.name().replaceAll("[^\\d.]", "")); - } catch (Throwable e) { + } catch (Exception e) { } shortVersion = this.name().substring(0, this.name().length() - 3); } diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 5dc9a1ca..004de7f9 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -66,7 +66,6 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import java.util.*; import java.util.Map.Entry; import java.util.logging.Logger; @@ -843,28 +842,24 @@ public class Jobs extends JavaPlugin { // itemManager = new ItemManager(this); try { - Class nmsClass; - nmsClass = Class.forName("com.gamingmesh.jobs.nmsUtil." + version); + Class nmsClass = Class.forName("com.gamingmesh.jobs.nmsUtil." + version); if (NMS.class.isAssignableFrom(nmsClass)) { setNms((NMS) nmsClass.getConstructor().newInstance()); } else { System.out.println("Something went wrong, please note down version and contact author, version: " + version); setEnabled(false); + return; } - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (Exception e) { System.out.println("Your server version is not compatible with this plugins version! Plugin will be disabled: " + version); setEnabled(false); e.printStackTrace(); return; } - try { - if (setupPlaceHolderAPI()) { - consoleMsg("&ePlaceholderAPI was found - Enabling capabilities."); - PlaceholderAPIEnabled = true; - } - } catch (Throwable e) { - e.printStackTrace(); + + if (setupPlaceHolderAPI()) { + consoleMsg("&ePlaceholderAPI was found - Enabling capabilities."); + PlaceholderAPIEnabled = true; } try { diff --git a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java index 12a17937..1537ecda 100644 --- a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java +++ b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java @@ -90,7 +90,7 @@ public class Placeholder { try { int id = Integer.parseInt(matcher.group(1).substring(1)); groups.add(id); - } catch (Throwable e) { + } catch (Exception e) { e.printStackTrace(); } } @@ -220,7 +220,7 @@ public class Placeholder { for (Integer oneG : groups) { lsInLs.add(matcher.group(oneG + 1)); } - } catch (Throwable e) { + } catch (Exception e) { } break; } @@ -264,12 +264,9 @@ public class Placeholder { return JobsPlaceholderType.Jobs; } if (plugin.isPlaceholderAPIEnabled()) { - try { - if (placeholder.contains("%")) - if (!placeholder.equals(me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, placeholder))) - return JobsPlaceholderType.PAPI; - } catch (Throwable e) { - + if (placeholder.contains("%")) { + if (!placeholder.equals(me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, placeholder))) + return JobsPlaceholderType.PAPI; } } // For MVdWPlaceholderAPI @@ -282,18 +279,13 @@ public class Placeholder { } public String updatePlaceHolders(Player player, String message) { - if (message == null) return null; if (message.contains("%")) message = translateOwnPlaceHolder(player, message); if (plugin.isPlaceholderAPIEnabled()) { - try { - if (message.contains("%")) - message = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, message); - } catch (Throwable e) { - - } + if (message.contains("%")) + message = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, message); } // For MVdWPlaceholderAPI // if (plugin.isMVdWPlaceholderAPIEnabled()) { @@ -325,8 +317,7 @@ public class Placeholder { if (with.startsWith("$")) with = "\\" + with; message = message.replaceFirst(group, with); - - } catch (Throwable e) { + } catch (Exception e) { e.printStackTrace(); } } @@ -348,7 +339,7 @@ public class Placeholder { int id = Integer.parseInt(value); if (id > 0) j = user.getJobProgression().get(id - 1); - } catch (Throwable e) { + } catch (Exception e) { Job job = Jobs.getJob(value); if (job != null) j = user.getJobProgression(job); @@ -362,7 +353,7 @@ public class Placeholder { int id = Integer.parseInt(value); if (id > 0) j = Jobs.getJobs().get(id - 1); - } catch (Throwable e) { + } catch (Exception e) { j = Jobs.getJob(value); } return j; @@ -376,7 +367,7 @@ public class Placeholder { if (placeHolder == null) return null; - JobsPlayer user = Jobs.getPlayerManager().getJobsPlayer(uuid); + JobsPlayer user = uuid == null ? null : Jobs.getPlayerManager().getJobsPlayer(uuid); // Placeholders by JobsPlayer object if (user != null) { switch (placeHolder) { diff --git a/src/main/java/com/gamingmesh/jobs/Reflections.java b/src/main/java/com/gamingmesh/jobs/Reflections.java index d4da3ed0..bc4b2688 100644 --- a/src/main/java/com/gamingmesh/jobs/Reflections.java +++ b/src/main/java/com/gamingmesh/jobs/Reflections.java @@ -29,22 +29,22 @@ public class Reflections { private void initialize() { try { CraftServerClass = getBukkitClass("CraftServer"); - } catch (ClassNotFoundException | SecurityException | IllegalArgumentException e) { + } catch (ClassNotFoundException e) { e.printStackTrace(); } try { CraftServer = CraftServerClass.cast(Bukkit.getServer()); - } catch (SecurityException | IllegalArgumentException e) { + } catch (ClassCastException e) { e.printStackTrace(); } try { NBTTagCompound = getMinecraftClass("NBTTagCompound"); - } catch (ClassNotFoundException | SecurityException | IllegalArgumentException e) { + } catch (ClassNotFoundException e) { e.printStackTrace(); } try { NBTBase = getMinecraftClass("NBTBase"); - } catch (ClassNotFoundException | SecurityException | IllegalArgumentException e) { + } catch (ClassNotFoundException e) { e.printStackTrace(); } /*try { @@ -55,7 +55,7 @@ public class Reflections { try { CraftItemStack = getBukkitClass("inventory.CraftItemStack"); - } catch (ClassNotFoundException | SecurityException | IllegalArgumentException e) { + } catch (ClassNotFoundException e) { e.printStackTrace(); } /*try { @@ -65,7 +65,7 @@ public class Reflections { }*/ try { IStack = getMinecraftClass("ItemStack"); - } catch (ClassNotFoundException | SecurityException | IllegalArgumentException e) { + } catch (ClassNotFoundException e) { e.printStackTrace(); } } diff --git a/src/main/java/com/gamingmesh/jobs/Signs/SignUtil.java b/src/main/java/com/gamingmesh/jobs/Signs/SignUtil.java index 005b4771..63afd031 100644 --- a/src/main/java/com/gamingmesh/jobs/Signs/SignUtil.java +++ b/src/main/java/com/gamingmesh/jobs/Signs/SignUtil.java @@ -126,8 +126,12 @@ public class SignUtil { old.put(loc, newTemp); SignsByLocation.put(loc, newTemp); } - Jobs.consoleMsg("&e[Jobs] Loaded " + SignsByLocation.size() + " top list signs"); - return; + + if (!SignsByLocation.isEmpty()) { + Jobs.consoleMsg("&e[Jobs] Loaded " + SignsByLocation.size() + " top list signs"); + } + + signUpdate(); } // Signs save file @@ -163,6 +167,21 @@ public class SignUtil { return; } + private int update = -1; + + public void signUpdate() { + if (update == -1) { + update = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { + @Override + public void run() { + for (Entry> one : SignsByType.entrySet()) { + SignUpdate(Jobs.getJob(one.getKey())); + } + } + }, 60 * 20L, Jobs.getGCManager().InfoUpdateInterval * 20L); + } + } + public boolean SignUpdate(Job job) { return SignUpdate(job, SignTopType.toplist); } @@ -171,34 +190,28 @@ public class SignUtil { return SignUpdate(null, type); } - public static Integer questSignUpdateShed = null; - public boolean SignUpdate(Job job, SignTopType type) { if (!Jobs.getGCManager().SignsEnabled) return true; - int timelapse = 1; + if (update == -1) { + signUpdate(); + } - List PlayerList = new ArrayList<>(); - - if (type == null && job == null) + if (job == null) return false; - if (type == null && job != null) - type = SignTopType.toplist; - if (type == null) type = SignTopType.toplist; - HashMap> temp = new HashMap<>(); - String JobNameOrType = jobsSign.getIdentifier(job, type); - HashMap signs = this.SignsByType.get(JobNameOrType.toLowerCase()); if (signs == null) return false; + List PlayerList = new ArrayList<>(); + switch (type) { case toplist: break; @@ -212,6 +225,8 @@ public class SignUtil { break; } + HashMap> temp = new HashMap<>(); + boolean save = false; for (Entry one : (new HashMap(signs)).entrySet()) { jobsSign jSign = one.getValue(); @@ -280,8 +295,7 @@ public class SignUtil { sign.setLine(i, line); } sign.update(); - if (!UpdateHead(sign, PlayerList.get(0).getPlayerName(), timelapse)) - timelapse--; + UpdateHead(sign, PlayerList.get(0).getPlayerName()); } else { if (jSign.getNumber() > PlayerList.size()) return true; @@ -315,12 +329,9 @@ public class SignUtil { sign.setLine(3, translateSignLine("signs.SpecialList.bottom", no, PlayerName, pl.getLevel(), SignJobName)); sign.update(); - if (!UpdateHead(sign, pl.getPlayerName(), timelapse)) - timelapse--; + UpdateHead(sign, pl.getPlayerName()); } - timelapse++; - } if (save) saveSigns(); @@ -337,9 +348,8 @@ public class SignUtil { "[job]", jobname); } - public boolean UpdateHead(final org.bukkit.block.Sign sign, final String Playername, int timelapse) { + public boolean UpdateHead(final org.bukkit.block.Sign sign, final String Playername) { try { - timelapse = timelapse < 1 ? 1 : timelapse; BlockFace directionFacing = null; if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) { org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) sign.getData(); @@ -378,22 +388,8 @@ public class SignUtil { 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(); - if (!(b.getState() instanceof Skull)) - return; - - Skull skull = (Skull) b.getState(); - if (skull == null) - return; - - skull.setOwner(Playername); - skull.update(); - return; - } - }, timelapse * Jobs.getGCManager().InfoUpdateInterval * 20L); + skull.setOwner(Playername); + skull.update(); } catch (Throwable e) { e.printStackTrace(); } diff --git a/src/main/java/com/gamingmesh/jobs/actions/PotionDrinkInfo.java b/src/main/java/com/gamingmesh/jobs/actions/PotionDrinkInfo.java deleted file mode 100644 index c2487f26..00000000 --- a/src/main/java/com/gamingmesh/jobs/actions/PotionDrinkInfo.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.gamingmesh.jobs.actions; - -import com.gamingmesh.jobs.container.ActionInfo; -import com.gamingmesh.jobs.container.ActionType; -import com.gamingmesh.jobs.container.BaseActionInfo; - -public class PotionDrinkInfo extends BaseActionInfo implements ActionInfo { - private String potion; - - public PotionDrinkInfo(String potion, ActionType type) { - super(type); - this.potion = potion; - } - - @Override - public String getName() { - return potion; - } - - @Override - public String getNameWithSub() { - return getName(); - } -} diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/area.java b/src/main/java/com/gamingmesh/jobs/commands/list/area.java index cc4d40ad..ba91cad5 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/area.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/area.java @@ -47,11 +47,12 @@ public class area implements Cmd { if (args[0].equalsIgnoreCase("add")) { if (!Jobs.hasPermission(player, "jobs.area.add", true)) return true; + String name = args[1]; double bonus = 0D; try { bonus = Double.parseDouble(args[2]); - } catch (Throwable e) { + } catch (NumberFormatException e) { return false; } boolean wg = false; diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/exp.java b/src/main/java/com/gamingmesh/jobs/commands/list/exp.java index 0c91e68f..57d08a4e 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/exp.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/exp.java @@ -64,14 +64,18 @@ public class exp implements Cmd { // check if player already has the job if (jPlayer.isInJob(job)) { JobProgression prog = jPlayer.getJobProgression(job); + double total = 0d; + switch (action) { case Add: + total = (prog.getExperience() + amount); prog.addExperience(amount); break; case Set: prog.setExperience(amount); break; case Take: + total = (prog.getExperience() - amount); prog.takeExperience(amount); break; default: diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/level.java b/src/main/java/com/gamingmesh/jobs/commands/list/level.java index dc163f74..8af40dec 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/level.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/level.java @@ -75,14 +75,18 @@ public class level implements Cmd { // check if player already has the job if (jPlayer.isInJob(job)) { JobProgression prog = jPlayer.getJobProgression(job); + int total = 0; + switch (action) { case Set: prog.setLevel(amount); break; case Add: - prog.setLevel(amount); + total = (prog.getLevel() + amount); + prog.setLevel(total); break; case Take: + total = (prog.getLevel() - amount); prog.setLevel(amount); break; default: diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index 1d8ac7bd..ed917807 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -30,7 +30,6 @@ import org.bukkit.entity.Player; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Signs.SignTopType; -import com.gamingmesh.jobs.Signs.SignUtil; import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.economy.PaymentData; import com.gamingmesh.jobs.resources.jfep.Parser; @@ -1061,16 +1060,18 @@ public class JobsPlayer { this.doneQuests = doneQuests; } + private Integer questSignUpdateShed = null; + public void addDoneQuest() { this.doneQuests++; this.setSaved(false); - if (SignUtil.questSignUpdateShed == null) { - SignUtil.questSignUpdateShed = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Jobs.getInstance(), new Runnable() { + if (questSignUpdateShed == null) { + questSignUpdateShed = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Jobs.getInstance(), new Runnable() { @Override public void run() { Jobs.getSignUtil().SignUpdate(SignTopType.questtoplist); - SignUtil.questSignUpdateShed = null; + questSignUpdateShed = null; } }, Jobs.getGCManager().getSavePeriod() * 60 * 20L); } diff --git a/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java b/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java index 18daef56..888d695e 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/FurnaceBrewingHandling.java @@ -195,13 +195,18 @@ public class FurnaceBrewingHandling { } } + if (uuid == null) { + return false; + } + List ls = furnaceMap.get(uuid); if (ls == null) - return true; + return false; for (blockLoc one : ls) { if (!one.getLocation().equals(block.getLocation())) continue; + block.removeMetadata(JobsPaymentListener.furnaceOwnerMetadata, Jobs.getInstance()); ls.remove(one); return true; @@ -221,13 +226,18 @@ public class FurnaceBrewingHandling { } } + if (uuid == null) { + return false; + } + List ls = brewingMap.get(uuid); if (ls == null) - return true; + return false; for (blockLoc one : ls) { if (!one.getLocation().equals(block.getLocation())) continue; + block.removeMetadata(JobsPaymentListener.brewingOwnerMetadata, Jobs.getInstance()); ls.remove(one); return true; diff --git a/src/main/java/com/gamingmesh/jobs/stuff/GiveItem.java b/src/main/java/com/gamingmesh/jobs/stuff/GiveItem.java index 6716d96e..979223a3 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/GiveItem.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/GiveItem.java @@ -34,7 +34,7 @@ public class GiveItem { if (itemStack.getType() == CMIMaterial.ENCHANTED_BOOK.getMaterial()) { EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) ItemMeta; for (Entry oneEnch : enchants.entrySet()) { - bookMeta.addStoredEnchant(oneEnch.getKey(), oneEnch.getValue(), false); + bookMeta.addStoredEnchant(oneEnch.getKey(), oneEnch.getValue(), true); } } else { for (Entry OneEnchant : enchants.entrySet()) {