From 182039647ca56f0705c72c84f5d290f3318a3a88 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Fri, 28 May 2021 20:41:08 +0200 Subject: [PATCH] Fixed array index out of bounds exception --- .../com/gamingmesh/jobs/ItemBoostManager.java | 51 +++++++++++-------- src/main/java/com/gamingmesh/jobs/Jobs.java | 6 +++ .../com/gamingmesh/jobs/PlayerManager.java | 12 ++--- .../gamingmesh/jobs/config/ConfigManager.java | 27 +++++----- .../jobs/config/GeneralConfigManager.java | 4 +- .../jobs/config/NameTranslatorManager.java | 4 +- .../jobs/config/RestrictedAreaManager.java | 6 ++- .../gamingmesh/jobs/config/ShopManager.java | 8 +-- .../gamingmesh/jobs/config/TitleManager.java | 5 +- .../gamingmesh/jobs/container/JobsPlayer.java | 4 +- .../blockOwnerShip/BlockOwnerShip.java | 23 +++++---- .../jobs/listeners/JobsPaymentListener.java | 39 +++++++------- .../com/gamingmesh/jobs/stuff/PageInfo.java | 9 ++-- .../jobs/stuff/PerformCommands.java | 10 ++-- .../com/gamingmesh/jobs/stuff/blockLoc.java | 11 +--- 15 files changed, 121 insertions(+), 98 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/ItemBoostManager.java b/src/main/java/com/gamingmesh/jobs/ItemBoostManager.java index 3d7a2c53..6cc6bdca 100644 --- a/src/main/java/com/gamingmesh/jobs/ItemBoostManager.java +++ b/src/main/java/com/gamingmesh/jobs/ItemBoostManager.java @@ -115,11 +115,14 @@ public class ItemBoostManager { continue; List jobs = new ArrayList<>(); - for (String oneJ : cfg.get(one + ".jobs", Arrays.asList(""))) { - if (oneJ.equalsIgnoreCase("all")) { - jobs.addAll(Jobs.getJobs()); - } else { + List j = cfg.get(one + ".jobs", Arrays.asList("")); + + if (j.contains("all")) { + jobs.addAll(Jobs.getJobs()); + } else { + for (String oneJ : j) { Job job = Jobs.getJob(oneJ); + if (job != null) { jobs.add(job); } else { @@ -133,27 +136,29 @@ public class ItemBoostManager { continue; } - List lore = new ArrayList<>(); - if (cfg.getC().isList(one + ".lore")) { - for (String eachLine : cfg.get(one + ".lore", Arrays.asList(""))) { - lore.add(CMIChatColor.translate(eachLine)); - } + List lore = cfg.get(one + ".lore", Arrays.asList("")); + for (int a = 0; a < lore.size(); a++) { + lore.set(a, CMIChatColor.translate(lore.get(a))); } Map enchants = new HashMap<>(); if (cfg.getC().isList(one + ".enchants")) for (String eachLine : cfg.get(one + ".enchants", Arrays.asList(""))) { - if (!eachLine.contains("=")) + String[] split = eachLine.split("=", 2); + if (split.length == 0) continue; - String[] split = eachLine.split("=", 2); Enchantment ench = CMIEnchantment.getEnchantment(split[0]); - Integer level = -1; - try { - level = Integer.parseInt(split[1]); - } catch (NumberFormatException e) { - continue; + int level = -1; + + if (split.length > 1) { + try { + level = Integer.parseInt(split[1]); + } catch (NumberFormatException e) { + continue; + } } + if (ench != null && level != -1) enchants.put(ench, level); } @@ -161,7 +166,8 @@ public class ItemBoostManager { BoostMultiplier b = new BoostMultiplier(); for (CurrencyType oneC : CurrencyType.values()) { String typeName = oneC.toString().toLowerCase(); - if (cfg.getC().isDouble(one + "." + typeName + "Boost") || cfg.getC().isInt(one + "." + typeName + "Boost")) + + if (cfg.getC().isDouble(one + "." + typeName + "Boost")) b.add(oneC, cfg.get(one + "." + typeName + "Boost", 1D) - 1); } @@ -171,8 +177,10 @@ public class ItemBoostManager { String node = one.toLowerCase(); Color leatherColor = null; - if (cfg.getC().isString(one + ".leather-color")) { - String[] split = cfg.getC().getString(one + ".leather-color").split(",", 3); + String lc = cfg.getC().getString(one + ".leather-color", ""); + if (!lc.isEmpty()) { + String[] split = lc.split(",", 3); + if (split.length != 0) { int red = Integer.parseInt(split[0]); int green = split.length > 0 ? Integer.parseInt(split[1]) : 0; @@ -198,8 +206,9 @@ public class ItemBoostManager { } // Lets add into legacy map - if (one.contains("_")) { - item.setLegacyKey(one.split("_", 2)[1].toLowerCase()); + String[] split = one.split("_", 2); + if (split.length > 1) { + item.setLegacyKey(split[1].toLowerCase()); LEGACY.put(item.getLegacyKey(), item); } diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 7da5ca02..5577b75f 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -180,6 +180,12 @@ public class Jobs extends JavaPlugin { return Optional.empty(); } + public void removeBlockOwnerShip(org.bukkit.block.Block block) { + for (BlockOwnerShip ship : blockOwnerShips) { + ship.remove(block); + } + } + /** * @return a set of block owner ships. */ diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index c7d89530..5123ff06 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -956,18 +956,16 @@ public class PlayerManager { if (player == null || job == null) return data; - ItemStack iih; List jitems = new ArrayList<>(); // Check mainhand slot - if (Jobs.getGCManager().boostedItemsInMainHand && (iih = Jobs.getNms().getItemInMainHand(player)) != null) { - jitems.add(getJobsItemByNbt(iih)); + if (Jobs.getGCManager().boostedItemsInMainHand) { + jitems.add(getJobsItemByNbt(Jobs.getNms().getItemInMainHand(player))); } // Check offhand slot - if (Version.isCurrentEqualOrHigher(Version.v1_9_R1) && Jobs.getGCManager().boostedItemsInOffHand - && (iih = CMIReflections.getItemInOffHand(player)) != null) { - jitems.add(getJobsItemByNbt(iih)); + if (Version.isCurrentEqualOrHigher(Version.v1_9_R1) && Jobs.getGCManager().boostedItemsInOffHand) { + jitems.add(getJobsItemByNbt(player.getInventory().getItemInOffHand())); } // Check armor slots @@ -988,7 +986,7 @@ public class PlayerManager { } return data; -} + } private final String jobsItemBoost = "JobsItemBoost"; diff --git a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java index 34485a14..552c2a01 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java @@ -561,7 +561,7 @@ public class ConfigManager { if (myKey.contains("-")) { String[] split = myKey.split("-", 2); - if (split.length >= 2) { + if (split.length > 1) { subType = ":" + split[1]; meta = split[1]; } @@ -1092,12 +1092,12 @@ public class ConfigManager { String item = guiSection.getString("Item"); String subType = ""; - if (item.contains("-")) { - String[] split = item.split("-", 2); - subType = ":" + split[1]; - item = split[0]; - } else if (item.contains(":")) { // when we uses tipped arrow effect types - item = item.split(":", 2)[0]; + String[] itemSplit = item.split("-", 2); + if (itemSplit.length > 1) { + subType = ":" + itemSplit[1]; + item = itemSplit[0]; + } else if ((itemSplit = item.split(":", 2)).length > 0) { // when we uses tipped arrow effect types + item = itemSplit[0]; } CMIMaterial material = CMIMaterial.get(item + (subType)); @@ -1316,18 +1316,21 @@ public class ConfigManager { Map enchants = new HashMap<>(); for (String eachLine : itemSection.getStringList("enchants")) { - if (!eachLine.contains("=")) + String[] split = eachLine.split("=", 2); + if (split.length == 0) continue; - String[] split = eachLine.split("=", 2); Enchantment ench = CMIEnchantment.getEnchantment(split[0]); if (ench == null) continue; int level = -1; - try { - level = Integer.parseInt(split[1]); - } catch (NumberFormatException e) { + + if (split.length > 1) { + try { + level = Integer.parseInt(split[1]); + } catch (NumberFormatException e) { + } } if (level != -1) diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index f758cf81..657a7b96 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -481,8 +481,8 @@ public class GeneralConfigManager { String mName = one; String ench = null; - if (one.contains("=")) { - String[] split = one.split("=", 2); + String[] split = one.split("=", 2); + if (split.length > 1) { mName = split[0]; ench = split[1]; } diff --git a/src/main/java/com/gamingmesh/jobs/config/NameTranslatorManager.java b/src/main/java/com/gamingmesh/jobs/config/NameTranslatorManager.java index fc413d30..4ab21e8a 100644 --- a/src/main/java/com/gamingmesh/jobs/config/NameTranslatorManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/NameTranslatorManager.java @@ -131,8 +131,8 @@ public class NameTranslatorManager { String mName = materialName; String level = ""; - if (mName.contains(":")) { - String[] split = materialName.split(":", 2); + String[] split = materialName.split(":", 2); + if (split.length > 1) { mName = split[0]; level = ":" + split[1]; } diff --git a/src/main/java/com/gamingmesh/jobs/config/RestrictedAreaManager.java b/src/main/java/com/gamingmesh/jobs/config/RestrictedAreaManager.java index 84443f84..f60577c4 100644 --- a/src/main/java/com/gamingmesh/jobs/config/RestrictedAreaManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/RestrictedAreaManager.java @@ -197,6 +197,7 @@ public class RestrictedAreaManager { */ public void load() { restrictedAreas.clear(); + File f = new File(Jobs.getFolder(), "restrictedAreas.yml"); YamlConfiguration conf = YamlConfiguration.loadConfiguration(f); @@ -228,8 +229,9 @@ public class RestrictedAreaManager { } } - if (restrictedAreas.size() > 0) - Jobs.consoleMsg("&e[Jobs] Loaded " + restrictedAreas.size() + " restricted areas!"); + int size = restrictedAreas.size(); + if (size > 0) + Jobs.consoleMsg("&e[Jobs] Loaded " + size + " restricted areas!"); try { conf.save(f); diff --git a/src/main/java/com/gamingmesh/jobs/config/ShopManager.java b/src/main/java/com/gamingmesh/jobs/config/ShopManager.java index 2f96ce95..1b3e087a 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ShopManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ShopManager.java @@ -362,10 +362,10 @@ public class ShopManager { Map requiredJobs = new HashMap<>(); for (String one : nameSection.getStringList("RequiredJobLevels")) { - if (!one.contains("-")) + String[] split = one.split("-", 2); + if (split.length == 0) continue; - String[] split = one.split("-", 2); int lvl = 1; if (split.length > 1) { try { @@ -413,10 +413,10 @@ public class ShopManager { Map enchants = new HashMap<>(); for (String eachLine : itemSection.getStringList("Enchants")) { - if (!eachLine.contains("=")) + String[] split = eachLine.split("=", 2); + if (split.length == 0) continue; - String[] split = eachLine.split("=", 2); Enchantment ench = CMIEnchantment.getEnchantment(split[0]); if (ench == null) continue; diff --git a/src/main/java/com/gamingmesh/jobs/config/TitleManager.java b/src/main/java/com/gamingmesh/jobs/config/TitleManager.java index 2d93afa6..5b018de6 100644 --- a/src/main/java/com/gamingmesh/jobs/config/TitleManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/TitleManager.java @@ -148,7 +148,8 @@ public class TitleManager { titles.add(new Title(titleName, titleShortName, titleColor, levelReq, titleSection.getString(titleKey + ".JobName"))); } - if (titles.size() > 0) - Jobs.consoleMsg("&e[Jobs] Loaded " + titles.size() + " titles!"); + int size = titles.size(); + if (size > 0) + Jobs.consoleMsg("&e[Jobs] Loaded " + size + " titles!"); } } diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index ed69b1a5..857f67e9 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -1358,7 +1358,7 @@ public class JobsPlayer { if (perm.isEmpty()) return 0; - Double maxV = Jobs.getPermissionManager().getMaxPermission(this, perm); + double maxV = Jobs.getPermissionManager().getMaxPermission(this, perm); if (maxV == 0D && type == BlockTypes.FURNACE) maxV = (double) Jobs.getGCManager().getFurnacesMaxDefault(); @@ -1372,7 +1372,7 @@ public class JobsPlayer { if (maxV == 0D && type == BlockTypes.BREWING_STAND) maxV = (double) Jobs.getGCManager().getBrewingStandsMaxDefault(); - return maxV.intValue(); + return (int) maxV; } public int getSkippedQuests() { diff --git a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java index d6b522bf..fd2471c6 100644 --- a/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java +++ b/src/main/java/com/gamingmesh/jobs/container/blockOwnerShip/BlockOwnerShip.java @@ -90,10 +90,7 @@ public class BlockOwnerShip { boolean owner = false; List data = getBlockMetadatas(block); if (!data.isEmpty()) { - // only care about first - MetadataValue value = data.get(0); - - if (!value.asString().equals(player.getUniqueId().toString())) { + if (!data.get(0).asString().equals(jPlayer.getUniqueId().toString())) { return ownershipFeedback.notOwn; } @@ -110,26 +107,28 @@ public class BlockOwnerShip { if (have >= max && max > 0) return ownershipFeedback.tooMany; - block.setMetadata(metadataName, new FixedMetadataValue(plugin, player.getUniqueId().toString())); + block.setMetadata(metadataName, new FixedMetadataValue(plugin, jPlayer.getUniqueId().toString())); if (!Jobs.getGCManager().isBrewingStandsReassign() && !Jobs.getGCManager().isFurnacesReassign() && !Jobs.getGCManager().BlastFurnacesReassign && !Jobs.getGCManager().SmokerReassign) { return ownershipFeedback.newReg; } - List ls = blockOwnerShips.getOrDefault(player.getUniqueId(), new ArrayList<>()); + List ls = blockOwnerShips.getOrDefault(jPlayer.getUniqueId(), new ArrayList<>()); ls.add(new blockLoc(block.getLocation())); - blockOwnerShips.put(player.getUniqueId(), ls); + blockOwnerShips.put(jPlayer.getUniqueId(), ls); return ownershipFeedback.newReg; } public boolean remove(Block block) { UUID uuid = null; List data = getBlockMetadatas(block); + if (!data.isEmpty()) { - // only care about first - MetadataValue value = data.get(0); - uuid = UUID.fromString(value.asString()); + try { + uuid = UUID.fromString(data.get(0).asString()); + } catch (IllegalArgumentException e) { + } } if (uuid == null) { @@ -137,8 +136,10 @@ public class BlockOwnerShip { } List ls = blockOwnerShips.getOrDefault(uuid, new ArrayList<>()); + org.bukkit.Location blockLoc = block.getLocation(); + for (blockLoc one : ls) { - if (one.getLocation().equals(block.getLocation())) { + if (one.getLocation().equals(blockLoc)) { block.removeMetadata(metadataName, plugin); ls.remove(one); return true; diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index 27e3ecf9..b907adc0 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -380,7 +380,7 @@ public class JobsPaymentListener implements Listener { return; // Remove block owner ships - plugin.getBlockOwnerShips().forEach(os -> os.remove(block)); + plugin.removeBlockOwnerShip(block); if (!Jobs.getPermissionHandler().hasWorldPermission(player, player.getLocation().getWorld().getName())) return; @@ -401,11 +401,14 @@ public class JobsPaymentListener implements Listener { return; // Protection for block break with silktouch - ItemStack item = Jobs.getNms().getItemInMainHand(player); - if (Jobs.getGCManager().useSilkTouchProtection && item.getType() != Material.AIR) { - for (Enchantment one : item.getEnchantments().keySet()) { - if (CMIEnchantment.get(one) == CMIEnchantment.SILK_TOUCH && Jobs.getBpManager().isInBp(block)) { - return; + if (Jobs.getGCManager().useSilkTouchProtection) { + ItemStack item = Jobs.getNms().getItemInMainHand(player); + + if (item.getType() != Material.AIR && Jobs.getBpManager().isInBp(block)) { + for (Enchantment one : item.getEnchantments().keySet()) { + if (CMIEnchantment.get(one) == CMIEnchantment.SILK_TOUCH) { + return; + } } } } @@ -834,6 +837,17 @@ public class JobsPaymentListener implements Listener { if (resultStack == null) return; + // Fix for possible money duplication bugs. + switch (event.getClick()) { + case UNKNOWN: + case WINDOW_BORDER_LEFT: + case WINDOW_BORDER_RIGHT: + case NUMBER_KEY: + return; + default: + break; + } + // Check for world permissions if (!Jobs.getPermissionHandler().hasWorldPermission(player, player.getLocation().getWorld().getName())) return; @@ -846,17 +860,6 @@ public class JobsPaymentListener implements Listener { if (Jobs.getGCManager().disablePaymentIfRiding && player.isInsideVehicle()) return; - // Fix for possible money duplication bugs. - switch (event.getClick()) { - case UNKNOWN: - case WINDOW_BORDER_LEFT: - case WINDOW_BORDER_RIGHT: - case NUMBER_KEY: - return; - default: - break; - } - // Checking if this is only item rename ItemStack firstSlot = null; try { @@ -1534,7 +1537,7 @@ public class JobsPaymentListener implements Listener { if (block == null) continue; - plugin.getBlockOwnerShips().forEach(b -> b.remove(block)); + plugin.removeBlockOwnerShip(block); if (Jobs.getGCManager().useBlockProtection && block.getState().hasMetadata(blockMetadata)) return; diff --git a/src/main/java/com/gamingmesh/jobs/stuff/PageInfo.java b/src/main/java/com/gamingmesh/jobs/stuff/PageInfo.java index 6c66568a..c51932ff 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/PageInfo.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/PageInfo.java @@ -50,7 +50,8 @@ public class PageInfo { } public boolean isContinueNoAdd() { - return currentEntry - 1 >= start && currentEntry - 1 <= end; + int entry = currentEntry - 1; + return entry >= start && entry <= end; } public boolean isBreak() { @@ -86,10 +87,12 @@ public class PageInfo { } public int getNextPageNumber() { - return currentPage + 1 > totalPages ? totalPages : currentPage + 1; + int page = currentPage + 1; + return page > totalPages ? totalPages : page; } public int getPrevPageNumber() { - return currentPage - 1 < 1 ? 1 : currentPage - 1; + int prev = currentPage - 1; + return prev < 1 ? 1 : prev; } } diff --git a/src/main/java/com/gamingmesh/jobs/stuff/PerformCommands.java b/src/main/java/com/gamingmesh/jobs/stuff/PerformCommands.java index abde4243..c459b53a 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/PerformCommands.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/PerformCommands.java @@ -5,17 +5,21 @@ import org.bukkit.Bukkit; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobsPlayer; -public class PerformCommands { +public final class PerformCommands { public static void performCommandsOnLeave(JobsPlayer jPlayer, Job job) { + String pName = jPlayer.getName(); + for (String one : job.getCmdOnLeave()) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), one.replace("[name]", jPlayer.getName()).replace("[jobname]", job.getName())); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), one.replace("[name]", pName).replace("[jobname]", job.getName())); } } public static void performCommandsOnJoin(JobsPlayer jPlayer, Job job) { + String pName = jPlayer.getName(); + for (String one : job.getCmdOnJoin()) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), one.replace("[name]", jPlayer.getName()).replace("[jobname]", job.getName())); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), one.replace("[name]", pName).replace("[jobname]", job.getName())); } } } diff --git a/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java b/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java index c15750b9..8a2feff3 100644 --- a/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java +++ b/src/main/java/com/gamingmesh/jobs/stuff/blockLoc.java @@ -50,9 +50,6 @@ public class blockLoc { } public boolean fromString(String loc) { - if (!loc.contains(":")) - return false; - String[] split = loc.split(":", 4); if (split.length == 0) { return false; @@ -86,15 +83,11 @@ public class blockLoc { if (worldName == null && w == null) return null; - World w = this.w == null ? Bukkit.getWorld(worldName) : this.w; + // Make sure cached world is loaded + World w = this.w == null ? Bukkit.getWorld(worldName) : Bukkit.getWorld(this.w.getName()); if (w == null) return null; - w = Bukkit.getWorld(w.getName()); - if (w == null) { - return null; - } - this.w = w; return new Location(w, x, y, z);