From ecb189b03e17bef0175c51ac94522c7f59b589a6 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Mon, 14 Jun 2021 15:12:31 +0200 Subject: [PATCH] Improved worldguard protected region retrieving --- src/main/java/com/gamingmesh/jobs/Jobs.java | 13 +--- .../jobs/Placeholders/Placeholder.java | 2 +- .../com/gamingmesh/jobs/PlayerManager.java | 30 ++++---- .../hooks/WorldGuard/WorldGuardManager.java | 76 +++++++++++++------ .../jobs/listeners/JobsListener.java | 4 +- .../jobs/listeners/JobsPaymentListener.java | 72 ++++++++++-------- 6 files changed, 113 insertions(+), 84 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 0e3f5b15..cb683338 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -482,11 +482,10 @@ public class Jobs extends JavaPlugin { */ public static Job getJob(String jobName) { for (Job job : jobs) { - if (job.getName().equalsIgnoreCase(jobName)) - return job; - if (job.getJobFullName().equalsIgnoreCase(jobName)) + if (job.getName().equalsIgnoreCase(jobName) || job.getJobFullName().equalsIgnoreCase(jobName)) return job; } + return null; } @@ -869,10 +868,7 @@ public class Jobs extends JavaPlugin { @Override public void onDisable() { - if (instance == null) - return; - - HandlerList.unregisterAll(instance); + HandlerList.unregisterAll(this); if (dao != null) { dao.saveExplore(); @@ -895,8 +891,6 @@ public class Jobs extends JavaPlugin { if (dao != null) { dao.closeConnections(); } - - instance = null; } private static void checkDailyQuests(JobsPlayer jPlayer, Job job, ActionInfo info) { @@ -976,7 +970,6 @@ public class Jobs extends JavaPlugin { * @param ent {@link Entity} * @param victim {@link LivingEntity} * @param block {@link Block} - * @see #action(JobsPlayer, ActionInfo, Block, Entity, LivingEntity) */ public static void action(JobsPlayer jPlayer, ActionInfo info, Block block, Entity ent, LivingEntity victim) { if (jPlayer == null) diff --git a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java index c3be047d..53685d0d 100644 --- a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java +++ b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java @@ -394,7 +394,7 @@ public class Placeholder { return null; } - private static String simplifyDouble(double value) { + private String simplifyDouble(double value) { return String.valueOf((int) (value * 100) / 100D); } diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index 5123ff06..082c3ba3 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -31,7 +31,6 @@ import java.util.concurrent.ThreadLocalRandom; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.FireworkEffect; -import org.bukkit.Sound; import org.bukkit.FireworkEffect.Type; import org.bukkit.entity.Entity; import org.bukkit.entity.Firework; @@ -648,7 +647,7 @@ public class PlayerManager { message = message.replace("%joblevel%", Integer.toString(prog.getLevel())); message = message.replace("%lostLevel%", Integer.toString(oldLevel)); - if (player != null) { + if (player != null && (Jobs.getGCManager().LevelChangeActionBar || Jobs.getGCManager().LevelChangeChat)) { for (String line : message.split("\n")) { if (Jobs.getGCManager().LevelChangeActionBar) ActionBarManager.send(player, line); @@ -685,14 +684,12 @@ public class PlayerManager { if (levelUpEvent.isCancelled()) return; - // If it fails, we can ignore it - try { - if (Jobs.getGCManager().SoundLevelupUse) { - Sound sound = levelUpEvent.getSound(); - if (player != null) - player.getWorld().playSound(player.getLocation(), sound, levelUpEvent.getSoundVolume(), levelUpEvent.getSoundPitch()); + if (player != null && Jobs.getGCManager().SoundLevelupUse) { + try { + player.getWorld().playSound(player.getLocation(), levelUpEvent.getSound(), + levelUpEvent.getSoundVolume(), levelUpEvent.getSoundPitch()); + } catch (Exception e) { // If it fails, we can ignore it } - } catch (Exception e) { } if (Jobs.getGCManager().FireworkLevelupUse && player != null) { @@ -764,23 +761,21 @@ public class PlayerManager { } else if (player != null) { if (Jobs.getGCManager().LevelChangeActionBar) ActionBarManager.send(player, line); + if (Jobs.getGCManager().LevelChangeChat) player.sendMessage(line); } } if (levelUpEvent.getNewTitle() != null && !levelUpEvent.getNewTitle().equals(levelUpEvent.getOldTitle())) { - - // If it fails, we can ignore it - try { - if (Jobs.getGCManager().SoundTitleChangeUse) { - Sound sound = levelUpEvent.getTitleChangeSound(); - if (player != null) - player.getWorld().playSound(player.getLocation(), sound, levelUpEvent.getTitleChangeVolume(), + if (player != null && Jobs.getGCManager().SoundTitleChangeUse) { + try { + player.getWorld().playSound(player.getLocation(), levelUpEvent.getTitleChangeSound(), levelUpEvent.getTitleChangeVolume(), levelUpEvent.getTitleChangePitch()); + } catch (Exception e) { // If it fails, we can ignore it } - } catch (Exception e) { } + // user would skill up message = Jobs.getLanguage().getMessage("message.skillup." + (Jobs.getGCManager().isBroadcastingSkillups() ? "broadcast" : "nobroadcast")); @@ -796,6 +791,7 @@ public class PlayerManager { } else if (player != null) { if (Jobs.getGCManager().TitleChangeActionBar) ActionBarManager.send(player, line); + if (Jobs.getGCManager().TitleChangeChat) player.sendMessage(line); } diff --git a/src/main/java/com/gamingmesh/jobs/hooks/WorldGuard/WorldGuardManager.java b/src/main/java/com/gamingmesh/jobs/hooks/WorldGuard/WorldGuardManager.java index a6049716..762e49fc 100644 --- a/src/main/java/com/gamingmesh/jobs/hooks/WorldGuard/WorldGuardManager.java +++ b/src/main/java/com/gamingmesh/jobs/hooks/WorldGuard/WorldGuardManager.java @@ -26,7 +26,7 @@ public class WorldGuardManager { private boolean useOld = false; public WorldGuardManager() { - Plugin pl = Bukkit.getPluginManager().getPlugin("WorldGuard"); + Plugin pl = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard"); if (pl instanceof WorldGuardPlugin) { wg = (WorldGuardPlugin) pl; @@ -43,17 +43,29 @@ public class WorldGuardManager { public List getArea(Location loc) { try { if (useOld) { - ApplicableRegionSet regions = wg.getRegionContainer().get(loc.getWorld()).getApplicableRegions(loc); - for (ProtectedRegion one : regions.getRegions()) { - if (Jobs.getRestrictedAreaManager().isExist(one.getId())) - return Jobs.getRestrictedAreaManager().getRestrictedAreasByName(one.getId()); + RegionManager manager = wg.getRegionContainer().get(loc.getWorld()); + + if (manager != null) { + ApplicableRegionSet regions = manager.getApplicableRegions(loc); + + for (ProtectedRegion one : regions.getRegions()) { + List rest = Jobs.getRestrictedAreaManager().getRestrictedAreasByName(one.getId()); + + if (!rest.isEmpty()) + return rest; + } } } else { RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); RegionManager regions = container.get(BukkitAdapter.adapt(loc.getWorld())); - for (ProtectedRegion one : regions.getRegions().values()) { - if (Jobs.getRestrictedAreaManager().isExist(one.getId())) - return Jobs.getRestrictedAreaManager().getRestrictedAreasByName(one.getId()); + + if (regions != null) { + for (ProtectedRegion one : regions.getRegions().values()) { + List rest = Jobs.getRestrictedAreaManager().getRestrictedAreasByName(one.getId()); + + if (!rest.isEmpty()) + return rest; + } } } } catch (Throwable e) { @@ -64,36 +76,56 @@ public class WorldGuardManager { public boolean inArea(Location loc, String name) { if (useOld) { - ApplicableRegionSet regions = wg.getRegionContainer().get(loc.getWorld()).getApplicableRegions(loc); - for (ProtectedRegion one : regions.getRegions()) { - if (one.getId().equalsIgnoreCase(name) && one.contains(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) - return true; + RegionManager manager = wg.getRegionContainer().get(loc.getWorld()); + + if (manager != null) { + ApplicableRegionSet regions = manager.getApplicableRegions(loc); + + for (ProtectedRegion one : regions.getRegions()) { + if (one.getId().equalsIgnoreCase(name) && one.contains(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) + return true; + } } } else { RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); RegionManager regions = container.get(BukkitAdapter.adapt(loc.getWorld())); - for (ProtectedRegion one : regions.getRegions().values()) { - if (one.getId().equalsIgnoreCase(name) && one.contains(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) - return true; + + if (regions != null) { + for (ProtectedRegion one : regions.getRegions().values()) { + if (one.getId().equalsIgnoreCase(name) && one.contains(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())) + return true; + } } } + return false; } public ProtectedRegion getProtectedRegionByName(String name) { - for (World one : Bukkit.getWorlds()) { - Map regions; + for (World one : Bukkit.getServer().getWorlds()) { + Map regions = null; + if (useOld) { - regions = wg.getRegionContainer().get(one).getRegions(); + RegionManager manager = wg.getRegionContainer().get(one); + + if (manager != null) + regions = manager.getRegions(); } else { RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); - regions = container.get(BukkitAdapter.adapt(one)).getRegions(); + RegionManager manager = container.get(BukkitAdapter.adapt(one)); + + if (manager != null) + regions = manager.getRegions(); } - for (Entry map : regions.entrySet()) { - if (map.getKey().equalsIgnoreCase(name)) - return map.getValue(); + + if (regions != null) { + for (Entry map : regions.entrySet()) { + if (map.getKey().equalsIgnoreCase(name)) + return map.getValue(); + } } } + return null; } } diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java index f9d77324..c605814f 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java @@ -396,11 +396,13 @@ public class JobsListener implements Listener { } String meinOk = null; + CMIMaterial mat = CMIMaterial.get(iih); mein: for (JobProgression one : jPlayer.getJobProgression()) { for (JobLimitedItems oneItem : one.getJob().getLimitedItems().values()) { - if (one.getLevel() >= oneItem.getLevel() || !isThisItem(oneItem, CMIMaterial.get(iih), name, lore, enchants)) + if (one.getLevel() >= oneItem.getLevel() || !isThisItem(oneItem, mat, name, lore, enchants)) continue; + meinOk = one.getJob().getName(); break mein; } diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index 0e2b9266..92169563 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -324,7 +324,7 @@ public final class JobsPaymentListener implements Listener { return; BlockOwnerShip ownerShip = plugin.getBlockOwnerShip(CMIMaterial.get(block), false).orElse(null); - if (ownerShip == null || !block.hasMetadata(ownerShip.getMetadataName())) + if (ownerShip == null) return; List data = block.getMetadata(ownerShip.getMetadataName()); @@ -346,7 +346,7 @@ public final class JobsPaymentListener implements Listener { Player player = jPlayer.getPlayer(); - if (!player.isOnline() || !Jobs.getPermissionHandler().hasWorldPermission(player)) + if (player == null || !Jobs.getPermissionHandler().hasWorldPermission(player)) return; // check if player is riding @@ -705,20 +705,24 @@ public final class JobsPaymentListener implements Listener { // Checking how much player crafted ItemStack toCraft = event.getCurrentItem(); - ItemStack toStore = event.getCursor(); + // Make sure we are actually crafting anything if (hasItems(toCraft)) if (event.isShiftClick()) schedulePostDetection(player, toCraft.clone(), jPlayer, resultStack.clone(), ActionType.CRAFT); else { // The items are stored in the cursor. Make sure there's enough space. - if (isStackSumLegal(toCraft, toStore)) { + if (isStackSumLegal(toCraft, event.getCursor())) { int newItemsCount = toCraft.getAmount(); + while (newItemsCount >= 1) { newItemsCount--; - if (resultStack.hasItemMeta() && resultStack.getItemMeta().hasDisplayName()) + + org.bukkit.inventory.meta.ItemMeta resultItemMeta = resultStack.getItemMeta(); + + if (resultItemMeta != null && resultItemMeta.hasDisplayName()) Jobs.action(jPlayer, new ItemNameActionInfo(CMIChatColor.stripColor(plugin - .getComplement().getDisplayName(resultStack.getItemMeta())), ActionType.CRAFT)); + .getComplement().getDisplayName(resultItemMeta)), ActionType.CRAFT)); else Jobs.action(jPlayer, new ItemActionInfo(resultStack, ActionType.CRAFT)); } @@ -752,21 +756,20 @@ public final class JobsPaymentListener implements Listener { } } - if (newItemsCount > 0) { - while (newItemsCount >= 1) { - newItemsCount--; - Jobs.action(jPlayer, new ItemActionInfo(resultStack, type)); - } + while (newItemsCount > 0) { + newItemsCount--; + + Jobs.action(jPlayer, new ItemActionInfo(resultStack, type)); } } }, 1); } - private static boolean hasItems(ItemStack stack) { + private boolean hasItems(ItemStack stack) { return stack != null && stack.getAmount() > 0; } - private static boolean hasSameItem(ItemStack a, ItemStack b) { + private boolean hasSameItem(ItemStack a, ItemStack b) { if (a == null) return b == null; else if (b == null) @@ -946,7 +949,7 @@ public final class JobsPaymentListener implements Listener { if (!Jobs.getGCManager().allowEnchantingBoostedItems) { for (JobProgression prog : jPlayer.getJobProgression()) { for (JobItems jobItem : ItemBoostManager.getItemsByJob(prog.getJob())) { - if (event.getItem().isSimilar(jobItem.getItemStack(jPlayer.getPlayer()))) { + if (event.getItem().isSimilar(jobItem.getItemStack(player))) { event.setCancelled(true); return; } @@ -1028,7 +1031,7 @@ public final class JobsPaymentListener implements Listener { return; BlockOwnerShip bos = plugin.getBlockOwnerShip(CMIMaterial.get(block), false).orElse(null); - if (bos == null || !block.hasMetadata(bos.getMetadataName())) { + if (bos == null) { return; } @@ -1556,35 +1559,36 @@ public final class JobsPaymentListener implements Listener { JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(p); Material hand = Jobs.getNms().getItemInMainHand(p).getType(); - if (Version.isCurrentEqualOrHigher(Version.v1_14_R1) && event.useInteractedBlock() != org.bukkit.event.Event.Result.DENY + if (event.useInteractedBlock() != org.bukkit.event.Event.Result.DENY && event.getAction() == Action.RIGHT_CLICK_BLOCK && jPlayer != null && !p.isSneaking()) { - if (cmat == CMIMaterial.COMPOSTER) { - org.bukkit.block.data.Levelled level = (org.bukkit.block.data.Levelled) block.getBlockData(); - if (level.getLevel() == level.getMaximumLevel()) { - Jobs.action(jPlayer, new BlockActionInfo(block, ActionType.COLLECT), block); + if (Version.isCurrentEqualOrHigher(Version.v1_14_R1)) { + if (cmat == CMIMaterial.COMPOSTER) { + org.bukkit.block.data.Levelled level = (org.bukkit.block.data.Levelled) block.getBlockData(); + + if (level.getLevel() == level.getMaximumLevel()) { + Jobs.action(jPlayer, new BlockActionInfo(block, ActionType.COLLECT), block); + } + } else if (cmat == CMIMaterial.SWEET_BERRY_BUSH && hand != CMIMaterial.BONE_MEAL.getMaterial()) { + Ageable age = (Ageable) block.getBlockData(); + Jobs.action(jPlayer, new BlockCollectInfo(block, ActionType.COLLECT, age.getAge()), block); } } - if (cmat == CMIMaterial.SWEET_BERRY_BUSH && hand != CMIMaterial.BONE_MEAL.getMaterial()) { - Ageable age = (Ageable) block.getBlockData(); - Jobs.action(jPlayer, new BlockCollectInfo(block, ActionType.COLLECT, age.getAge()), block); - } - } + if (Version.isCurrentEqualOrHigher(Version.v1_15_R1) && (cmat == CMIMaterial.BEEHIVE || cmat == CMIMaterial.BEE_NEST)) { + org.bukkit.block.data.type.Beehive beehive = (org.bukkit.block.data.type.Beehive) block.getBlockData(); - if (Version.isCurrentEqualOrHigher(Version.v1_15_R1) && event.useInteractedBlock() != org.bukkit.event.Event.Result.DENY - && event.getAction() == Action.RIGHT_CLICK_BLOCK && !p.isSneaking() && jPlayer != null - && (cmat == CMIMaterial.BEEHIVE || cmat == CMIMaterial.BEE_NEST)) { - org.bukkit.block.data.type.Beehive beehive = (org.bukkit.block.data.type.Beehive) block.getBlockData(); - if (beehive.getHoneyLevel() == beehive.getMaximumHoneyLevel() && (hand == CMIMaterial.SHEARS.getMaterial() - || hand == CMIMaterial.GLASS_BOTTLE.getMaterial())) { - Jobs.action(jPlayer, new BlockCollectInfo(block, ActionType.COLLECT, beehive.getHoneyLevel()), block); + if (beehive.getHoneyLevel() == beehive.getMaximumHoneyLevel() && (hand == CMIMaterial.SHEARS.getMaterial() + || hand == CMIMaterial.GLASS_BOTTLE.getMaterial())) { + Jobs.action(jPlayer, new BlockCollectInfo(block, ActionType.COLLECT, beehive.getHoneyLevel()), block); + } } } boolean isBrewingStand = cmat == CMIMaterial.BREWING_STAND || cmat == CMIMaterial.LEGACY_BREWING_STAND; boolean isFurnace = cmat == CMIMaterial.FURNACE || cmat == CMIMaterial.LEGACY_BURNING_FURNACE; + if (isFurnace || cmat == CMIMaterial.SMOKER || cmat == CMIMaterial.BLAST_FURNACE || isBrewingStand) { - BlockOwnerShip blockOwner = plugin.getBlockOwnerShip(CMIMaterial.get(block)).orElse(null); + BlockOwnerShip blockOwner = plugin.getBlockOwnerShip(cmat).orElse(null); if (blockOwner == null) { return; } @@ -1592,8 +1596,10 @@ public final class JobsPaymentListener implements Listener { String name = Jobs.getLanguage().getMessage("general.info.blocks." + (isBrewingStand ? "brewingstand" : isFurnace ? "furnace" : cmat == CMIMaterial.SMOKER ? "smoker" : cmat == CMIMaterial.BLAST_FURNACE ? "blastfurnace" : "")); ownershipFeedback done = blockOwner.register(p, block); + if (done == ownershipFeedback.tooMany) { boolean report = false; + if (block.hasMetadata(blockOwner.getMetadataName())) { List data = blockOwner.getBlockMetadatas(block); if (data.isEmpty())