From 1e4b7aa00260d564e370b757f4ae9358077a38b6 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Wed, 24 Jun 2020 09:27:17 +0200 Subject: [PATCH] Some fixes - Fixed when crafting tipped arrows, it didn't work - Fix when PayForEachCraft option not works - Some null checks to prevent exceptions - Trying to fix issue with editpoints --- .../com/gamingmesh/jobs/PlayerManager.java | 20 +++++----- .../com/gamingmesh/jobs/Signs/SignInfo.java | 5 ++- .../com/gamingmesh/jobs/Signs/jobsSign.java | 8 +--- .../jobs/commands/list/editpoints.java | 6 +-- .../gamingmesh/jobs/config/ShopManager.java | 2 +- .../jobs/container/ItemBonusCache.java | 29 -------------- .../com/gamingmesh/jobs/container/Job.java | 30 ++++++++------ .../gamingmesh/jobs/container/JobItems.java | 5 ++- .../gamingmesh/jobs/container/JobsPlayer.java | 16 +++----- .../com/gamingmesh/jobs/container/Quest.java | 17 ++++---- .../jobs/container/QuestProgression.java | 7 +--- .../gamingmesh/jobs/container/ShopItem.java | 21 +++++----- .../jobs/listeners/JobsPaymentListener.java | 39 ++++++++----------- 13 files changed, 84 insertions(+), 121 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index 072c7fe8..3ede1954 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -205,7 +205,6 @@ public class PlayerManager { jPlayer.onConnect(); jPlayer.reloadHonorific(); Jobs.getPermissionHandler().recalculatePermissions(jPlayer); - return; } @@ -776,10 +775,11 @@ public class PlayerManager { */ public void performCommandOnLevelUp(JobsPlayer jPlayer, Job job, int oldLevel) { int newLevel = oldLevel + 1; - Player player = Bukkit.getServer().getPlayer(jPlayer.getUniqueId()); + Player player = Bukkit.getPlayer(jPlayer.getUniqueId()); JobProgression prog = jPlayer.getJobProgression(job); if (prog == null) return; + for (JobCommands command : job.getCommands()) { if (newLevel >= command.getLevelFrom() && newLevel <= command.getLevelUntil()) { for (String commandString : new ArrayList(command.getCommands())) { @@ -860,7 +860,7 @@ public class PlayerManager { } } - private HashMap> cache = new HashMap<>(); + private final HashMap> cache = new HashMap<>(); public void resetiItemBonusCache(UUID uuid) { cache.remove(uuid); @@ -875,20 +875,20 @@ public class PlayerManager { ItemBonusCache c = cj.get(prog); if (c == null) { - c = new ItemBonusCache(player, prog); - c.recheck(); + c = new ItemBonusCache(); + c.setBoostMultiplier(getInventoryBoost(player, prog)); cj.put(prog, c); return c.getBoostMultiplier(); } + return c.getBoostMultiplier(); } public BoostMultiplier getInventoryBoost(Player player, Job prog) { BoostMultiplier data = new BoostMultiplier(); - if (player == null) - return data; - if (prog == null) + if (player == null || prog == null) return data; + ItemStack iih = Jobs.getNms().getItemInMainHand(player); JobItems jitem = getJobsItemByNbt(iih); if (jitem != null && jitem.getJobs().contains(prog)) @@ -919,9 +919,7 @@ public class PlayerManager { } public boolean containsItemBoostByNBT(ItemStack item) { - if (item == null) - return false; - return Jobs.getReflections().hasNbtString(item, JobsItemBoost); + return item == null ? false : Jobs.getReflections().hasNbtString(item, JobsItemBoost); } private final String JobsItemBoost = "JobsItemBoost"; diff --git a/src/main/java/com/gamingmesh/jobs/Signs/SignInfo.java b/src/main/java/com/gamingmesh/jobs/Signs/SignInfo.java index a06e2bce..660c87eb 100644 --- a/src/main/java/com/gamingmesh/jobs/Signs/SignInfo.java +++ b/src/main/java/com/gamingmesh/jobs/Signs/SignInfo.java @@ -5,10 +5,11 @@ import java.util.List; public class SignInfo { - private List AllSigns = new ArrayList<>(); + private final List AllSigns = new ArrayList<>(); public void setAllSigns(List AllSigns) { - this.AllSigns = AllSigns; + this.AllSigns.clear(); + this.AllSigns.addAll(AllSigns == null ? new ArrayList<>() : AllSigns); } public List GetAllSigns() { diff --git a/src/main/java/com/gamingmesh/jobs/Signs/jobsSign.java b/src/main/java/com/gamingmesh/jobs/Signs/jobsSign.java index e084e2ca..d834f60e 100644 --- a/src/main/java/com/gamingmesh/jobs/Signs/jobsSign.java +++ b/src/main/java/com/gamingmesh/jobs/Signs/jobsSign.java @@ -11,9 +11,7 @@ public class jobsSign { private String worldName; - private Integer x; - private Integer y; - private Integer z; + private Integer x, y, z; private World world; private Location loc; @@ -105,9 +103,7 @@ public class jobsSign { String[] split = string.replace(",", ".").split(";"); - Integer x = 0; - Integer y = 0; - Integer z = 0; + int x = 0, y = 0, z = 0; if (split.length > 0) try { diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/editpoints.java b/src/main/java/com/gamingmesh/jobs/commands/list/editpoints.java index c87ae533..f838a5a7 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/editpoints.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/editpoints.java @@ -32,11 +32,6 @@ public class editpoints implements Cmd { } PlayerPoints pointInfo = jPlayer.getPointsData(); - if (pointInfo == null) { - sender.sendMessage(Jobs.getLanguage().getMessage("general.error.noinfoByPlayer", "%playername%", jPlayer.getName())); - return true; - } - switch (args[0].toLowerCase()) { case "take": pointInfo.takePoints(amount); @@ -63,6 +58,7 @@ public class editpoints implements Cmd { } Jobs.getJobsDAO().savePoints(jPlayer); + Jobs.getJobsDAO().loadPoints(jPlayer); return true; } } diff --git a/src/main/java/com/gamingmesh/jobs/config/ShopManager.java b/src/main/java/com/gamingmesh/jobs/config/ShopManager.java index 7aaffb1d..292d19ff 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ShopManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ShopManager.java @@ -363,7 +363,7 @@ public class ShopManager { if (NameSection.isBoolean("Icon.HideWithoutPermission")) Sitem.setHideWithoutPerm(NameSection.getBoolean("Icon.HideWithoutPermission")); - if (NameSection.isList("RequiredPermission") && !NameSection.getStringList("RequiredPermission").isEmpty()) { + if (NameSection.isList("RequiredPermission")) { Sitem.setRequiredPerm(NameSection.getStringList("RequiredPermission")); } diff --git a/src/main/java/com/gamingmesh/jobs/container/ItemBonusCache.java b/src/main/java/com/gamingmesh/jobs/container/ItemBonusCache.java index 2c219e60..0e28ea49 100644 --- a/src/main/java/com/gamingmesh/jobs/container/ItemBonusCache.java +++ b/src/main/java/com/gamingmesh/jobs/container/ItemBonusCache.java @@ -1,43 +1,14 @@ package com.gamingmesh.jobs.container; -import org.bukkit.entity.Player; - -import com.gamingmesh.jobs.Jobs; - public class ItemBonusCache { - private Player player; - private Long lastCheck = null; private BoostMultiplier bm = new BoostMultiplier(); - private Job job; - - public ItemBonusCache(Player player, Job job) { - this.player = player; - this.job = job; - } - - public Long getLastCheck() { - return lastCheck; - } - - public void setLastCheck(Long lastCheck) { - this.lastCheck = lastCheck; - } public BoostMultiplier getBoostMultiplier() { - if (lastCheck == null || System.currentTimeMillis() - lastCheck > 1000 * 60) - recheck(); return bm; } public void setBoostMultiplier(BoostMultiplier bm) { this.bm = bm; } - - public ItemBonusCache recheck() { - bm = Jobs.getPlayerManager().getInventoryBoost(player, job); - lastCheck = System.currentTimeMillis(); - return this; - } - } diff --git a/src/main/java/com/gamingmesh/jobs/container/Job.java b/src/main/java/com/gamingmesh/jobs/container/Job.java index b309ae3c..becc5806 100644 --- a/src/main/java/com/gamingmesh/jobs/container/Job.java +++ b/src/main/java/com/gamingmesh/jobs/container/Job.java @@ -20,6 +20,7 @@ package com.gamingmesh.jobs.container; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.CMILib.CMIMaterial; +import com.gamingmesh.jobs.actions.PotionItemActionInfo; import com.gamingmesh.jobs.resources.jfep.Parser; import com.gamingmesh.jobs.stuff.ChatColor; @@ -145,9 +146,7 @@ public class Job { } public boolean isSame(Job job) { - if (job == null) - return false; - return this.getName().equalsIgnoreCase(job.getName()); + return job == null ? false : getName().equalsIgnoreCase(job.getName()); } public int getTotalPlayers() { @@ -166,22 +165,26 @@ public class Job { public void updateBonus() { if (!Jobs.getGCManager().useDynamicPayment) return; + Parser eq = Jobs.getGCManager().DynamicPaymentEquation; eq.setVariable("totalworkers", Jobs.getJobsDAO().getTotalPlayers()); eq.setVariable("totaljobs", Jobs.getJobs().size()); eq.setVariable("jobstotalplayers", getTotalPlayers()); + double now = eq.getValue(); if (now > Jobs.getGCManager().DynamicPaymentMaxBonus) now = Jobs.getGCManager().DynamicPaymentMaxBonus; if (now < Jobs.getGCManager().DynamicPaymentMaxPenalty * -1) now = Jobs.getGCManager().DynamicPaymentMaxPenalty * -1; + this.bonus = (now / 100D); } public double getBonus() { - if (this.bonus == null) + if (bonus == null) updateBonus(); - return this.bonus == null ? 0D : this.bonus; + + return bonus == null ? 0D : bonus; } public List getCmdOnJoin() { @@ -226,6 +229,11 @@ public class Job { public JobInfo getJobInfo(ActionInfo action, int level) { BiPredicate condition = (jobInfo, actionInfo) -> { + if (actionInfo instanceof PotionItemActionInfo) { + return jobInfo.getName().equalsIgnoreCase(((PotionItemActionInfo) action).getNameWithSub()) || + (jobInfo.getName() + ":" + jobInfo.getMeta()).equalsIgnoreCase(((PotionItemActionInfo) action).getNameWithSub()); + } + return jobInfo.getName().equalsIgnoreCase(action.getNameWithSub()) || (jobInfo.getName() + ":" + jobInfo.getMeta()).equalsIgnoreCase(action.getNameWithSub()) || jobInfo.getName().equalsIgnoreCase(action.getName()); @@ -327,19 +335,19 @@ public class Job { } public int getMaxLevel(JobsPlayer player) { - if (player == null) - return getMaxLevel(); - return player.getMaxJobLevelAllowed(this); + return player == null ? getMaxLevel() : player.getMaxJobLevelAllowed(this); } public int getMaxLevel(CommandSender sender) { if (sender == null) return getMaxLevel(); + if (sender instanceof Player) { JobsPlayer player = Jobs.getPlayerManager().getJobsPlayer((Player) sender); if (player != null) return player.getMaxJobLevelAllowed(this); } + return getMaxLevel() > getVipMaxLevel() ? getMaxLevel() : getVipMaxLevel(); } @@ -458,7 +466,8 @@ public class Job { } public void setFullDescription(List fDescription) { - this.fDescription = fDescription; + this.fDescription.clear(); + this.fDescription.addAll(fDescription == null ? new ArrayList<>() : fDescription); } public List getQuests() { @@ -480,7 +489,7 @@ public class Job { public void setQuests(List quests) { this.quests.clear(); - this.quests = quests; + this.quests.addAll(quests == null ? new ArrayList<>() : quests); } // public Quest getNextQuest() { @@ -499,7 +508,6 @@ public class Job { for (Quest one : ls) { if (one.getChance() >= target) if (excludeQuests == null || !excludeQuests.contains(one.getConfigName().toLowerCase())) { - if (!one.isInLevelRange(level)) continue; diff --git a/src/main/java/com/gamingmesh/jobs/container/JobItems.java b/src/main/java/com/gamingmesh/jobs/container/JobItems.java index 27c750b1..e505c9de 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobItems.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobItems.java @@ -77,7 +77,7 @@ public class JobItems { this.node = node; this.boostMultiplier = boostMultiplier; - this.jobs = jobs; + setJobs(jobs); } public String getNode() { @@ -135,7 +135,8 @@ public class JobItems { } public void setJobs(List jobs) { - this.jobs = jobs; + this.jobs.clear(); + this.jobs.addAll(jobs == null ? new ArrayList<>() : jobs); } public HashMap getEnchants() { diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index c34e9be7..eee728e3 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -539,14 +539,12 @@ public class JobsPlayer { public void promoteJob(Job job, int levels) { // synchronized (saveLock) { JobProgression prog = getJobProgression(job); - if (prog == null) + if (prog == null || levels <= 0) return; - if (levels <= 0) - return; - int oldLevel = prog.getLevel(); - int newLevel = oldLevel + levels; - int maxLevel = job.getMaxLevel(this); + int oldLevel = prog.getLevel(), + newLevel = oldLevel + levels, + maxLevel = job.getMaxLevel(this); if (maxLevel > 0 && newLevel > maxLevel) newLevel = maxLevel; @@ -563,10 +561,9 @@ public class JobsPlayer { public void demoteJob(Job job, int levels) { // synchronized (saveLock) { JobProgression prog = getJobProgression(job); - if (prog == null) - return; - if (levels <= 0) + if (prog == null || levels <= 0) return; + int newLevel = prog.getLevel() - levels; if (newLevel < 1) newLevel = 1; @@ -618,7 +615,6 @@ public class JobsPlayer { reloadLimits(); reloadHonorific(); Jobs.getPermissionHandler().recalculatePermissions(this); - return true; } } diff --git a/src/main/java/com/gamingmesh/jobs/container/Quest.java b/src/main/java/com/gamingmesh/jobs/container/Quest.java index f34d4d49..cc857e80 100644 --- a/src/main/java/com/gamingmesh/jobs/container/Quest.java +++ b/src/main/java/com/gamingmesh/jobs/container/Quest.java @@ -48,7 +48,8 @@ public class Quest { } public void setRewardCmds(List rewardCmds) { - this.rewardCmds = rewardCmds; + this.rewardCmds.clear(); + this.rewardCmds.addAll(rewardCmds == null ? new ArrayList<>() : rewardCmds); } public List getDescription() { @@ -56,7 +57,8 @@ public class Quest { } public void setDescription(List rewards) { - this.rewards = rewards; + this.rewards.clear(); + this.rewards.addAll(rewards == null ? new ArrayList<>() : rewards); } public List getRestrictedAreas() { @@ -64,7 +66,8 @@ public class Quest { } public void setRestrictedArea(List area) { - this.area = area; + this.area.clear(); + this.area.addAll(area == null ? new ArrayList<>() : area); } public Long getValidUntil() { @@ -95,7 +98,7 @@ public class Quest { } public Job getJob() { - return Jobs.getJob(this.job.getName()); + return Jobs.getJob(job.getName()); } public void setJob(Job job) { @@ -146,10 +149,10 @@ public class Quest { if (level == null) return true; - if (this.getMinLvl() != null && level < this.getMinLvl()) + if (getMinLvl() != null && level < getMinLvl()) return false; - if (this.getMaxLvl() != null && level > this.getMaxLvl()) + if (getMaxLvl() != null && level > getMaxLvl()) return false; return true; @@ -184,7 +187,7 @@ public class Quest { public void addObjective(QuestObjective objective) { HashMap old = objectives.get(objective.getAction()); if (old == null) { - old = new HashMap(); + old = new HashMap<>(); old.put(objective.getTargetName(), objective); objectives.put(objective.getAction(), old); } diff --git a/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java b/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java index f58da328..1222a400 100644 --- a/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java +++ b/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java @@ -61,7 +61,7 @@ public class QuestProgression { } public Long getValidUntil() { - return this.validUntil; + return validUntil; } public void setValidUntil(Long validUntil) { @@ -79,7 +79,7 @@ public class QuestProgression { public boolean isCompleted() { for (Entry> oneA : quest.getObjectives().entrySet()) { for (Entry one : oneA.getValue().entrySet()) { - Integer amountDone = this.done.get(one.getValue()); + Integer amountDone = done.get(one.getValue()); if (amountDone == null || amountDone < one.getValue().getAmount()) return false; } @@ -154,9 +154,6 @@ public class QuestProgression { Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ev.getCommand().startsWith("/") ? ev.getCommand().substring(1) : ev.getCommand()); } } - - return; - } public boolean isGivenReward() { diff --git a/src/main/java/com/gamingmesh/jobs/container/ShopItem.java b/src/main/java/com/gamingmesh/jobs/container/ShopItem.java index 2dd8e051..c48a0468 100644 --- a/src/main/java/com/gamingmesh/jobs/container/ShopItem.java +++ b/src/main/java/com/gamingmesh/jobs/container/ShopItem.java @@ -16,19 +16,18 @@ public class ShopItem { private String IconMaterial = null; private int IconAmount = 1; private String IconName = null; - private List IconLore = new ArrayList<>(); + private final List IconLore = new ArrayList<>(); private boolean HideWithoutPerm = false; private boolean hideNoEnoughPoint = false; private int RequiredTotalLevels = -1; - private List RequiredPerm = new ArrayList<>(); private HashMap RequiredJobs = new HashMap<>(); - private List Commands = new ArrayList<>(); - - private List items = new ArrayList<>(); + private final List RequiredPerm = new ArrayList<>(); + private final List Commands = new ArrayList<>(); + private final List items = new ArrayList<>(); private String PlayerName; private boolean useCurrentPlayer = false; @@ -55,7 +54,8 @@ public class ShopItem { } public void setitems(List items) { - this.items = items; + this.items.clear(); + this.items.addAll(items == null ? new ArrayList<>() : items); } public List getitems() { @@ -63,7 +63,8 @@ public class ShopItem { } public void setCommands(List Commands) { - this.Commands = Commands; + this.Commands.clear(); + this.Commands.addAll(Commands == null ? new ArrayList<>() : Commands); } public List getCommands() { @@ -79,7 +80,8 @@ public class ShopItem { } public void setRequiredPerm(List RequiredPerm) { - this.RequiredPerm = RequiredPerm; + this.RequiredPerm.clear(); + this.RequiredPerm.addAll(RequiredPerm == null ? new ArrayList<>() : RequiredPerm); } public List getRequiredPerm() { @@ -103,7 +105,8 @@ public class ShopItem { } public void setIconLore(List IconLore) { - this.IconLore = IconLore; + this.IconLore.clear(); + this.IconLore.addAll(IconLore == null ? new ArrayList<>() : IconLore); } public List getIconLore() { diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index 362b08a0..73405319 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -89,13 +89,13 @@ import java.util.*; import java.util.Map.Entry; public class JobsPaymentListener implements Listener { + private Jobs plugin; - public static final String furnaceOwnerMetadata = "jobsFurnaceOwner"; - public static final String brewingOwnerMetadata = "jobsBrewingOwner"; - private final String BlockMetadata = "BlockOwner"; - public static final String VegyMetadata = "VegyTimer"; - private final String CowMetadata = "CowTimer"; - private final String entityDamageByPlayer = "JobsEntityDamagePlayer"; + + public static final String furnaceOwnerMetadata = "jobsFurnaceOwner", + brewingOwnerMetadata = "jobsBrewingOwner", VegyMetadata = "VegyTimer"; + + private final String BlockMetadata = "BlockOwner", CowMetadata = "CowTimer", entityDamageByPlayer = "JobsEntityDamagePlayer"; public JobsPaymentListener(Jobs plugin) { this.plugin = plugin; @@ -618,9 +618,7 @@ public class JobsPaymentListener implements Listener { int y = -1; - CMIMaterial first = null; - CMIMaterial second = null; - CMIMaterial third = null; + CMIMaterial first = null, second = null, third = null; boolean leather = false; boolean shulker = false; @@ -700,24 +698,19 @@ public class JobsPaymentListener implements Listener { } } - // when we trying to craft tipped arrow effects - ItemStack currentItem = event.getCurrentItem(); - if (currentItem != null) { - if (currentItem.hasItemMeta() && currentItem.getItemMeta() instanceof PotionMeta) { - PotionMeta potion = (PotionMeta) currentItem.getItemMeta(); - Jobs.action(jPlayer, new PotionItemActionInfo(currentItem, ActionType.CRAFT, potion.getBasePotionData().getType())); - } else { - Jobs.action(jPlayer, new ItemActionInfo(currentItem, ActionType.CRAFT)); - } - - return; - } - // If we need to pay only by each craft action we will skip calculation how much was crafted if (!Jobs.getGCManager().PayForEachCraft) { - if (resultStack.hasItemMeta() && resultStack.getItemMeta().hasDisplayName()) { + ItemStack currentItem = event.getCurrentItem(); + + // when we trying to craft tipped arrow effects + if (currentItem != null && currentItem.hasItemMeta() && currentItem.getItemMeta() instanceof PotionMeta) { + PotionMeta potion = (PotionMeta) currentItem.getItemMeta(); + Jobs.action(jPlayer, new PotionItemActionInfo(currentItem, ActionType.CRAFT, potion.getBasePotionData().getType())); + } else if (resultStack.hasItemMeta() && resultStack.getItemMeta().hasDisplayName()) { Jobs.action(jPlayer, new ItemNameActionInfo(ChatColor.stripColor(resultStack.getItemMeta() .getDisplayName()), ActionType.CRAFT)); + } else if (currentItem != null) { + Jobs.action(jPlayer, new ItemActionInfo(currentItem, ActionType.CRAFT)); } return;