From 8cc7a25778d56e7cb0cc82024eedcd7cb5736dd6 Mon Sep 17 00:00:00 2001 From: Zrips Date: Sat, 28 Sep 2024 16:06:54 +0300 Subject: [PATCH] Rework of restricted areas By default properly formatting example areas Option to enable/disable specific areas Change in location definition to be more compact Option to limit area to specific job and specific level ranges Option to define bonus for each payment method area command shoul return list in case no variables got provided --- .../com/gamingmesh/jobs/PlayerManager.java | 30 +- .../gamingmesh/jobs/commands/list/area.java | 51 +-- .../jobs/config/LanguageManager.java | 6 +- .../jobs/config/RestrictedAreaManager.java | 333 ++++++++++++------ .../jobs/container/BoostMultiplier.java | 15 +- .../gamingmesh/jobs/container/CuboidArea.java | 110 ++++-- .../jobs/container/LevelLimits.java | 29 ++ .../jobs/container/RestrictedArea.java | 126 +++++-- 8 files changed, 487 insertions(+), 213 deletions(-) create mode 100644 src/main/java/com/gamingmesh/jobs/container/LevelLimits.java diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index 33984c01..850361e7 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -1148,26 +1148,26 @@ public class PlayerManager { McMMO, PetPay, NearSpawner, Permission, Global, Dynamic, Item, Area } - public Boost getFinalBonus(JobsPlayer player, Job prog, boolean force, boolean getall) { - return getFinalBonus(player, prog, null, null, force, getall); + public Boost getFinalBonus(JobsPlayer player, Job job, boolean force, boolean getall) { + return getFinalBonus(player, job, null, null, force, getall); } - public Boost getFinalBonus(JobsPlayer player, Job prog, boolean force) { - return getFinalBonus(player, prog, null, null, force, false); + public Boost getFinalBonus(JobsPlayer player, Job job, boolean force) { + return getFinalBonus(player, job, null, null, force, false); } - public Boost getFinalBonus(JobsPlayer player, Job prog) { - return getFinalBonus(player, prog, null, null, false, false); + public Boost getFinalBonus(JobsPlayer player, Job job) { + return getFinalBonus(player, job, null, null, false, false); } - public Boost getFinalBonus(JobsPlayer player, Job prog, Entity ent, LivingEntity victim) { - return getFinalBonus(player, prog, ent, victim, false, false); + public Boost getFinalBonus(JobsPlayer player, Job job, Entity ent, LivingEntity victim) { + return getFinalBonus(player, job, ent, victim, false, false); } - public Boost getFinalBonus(JobsPlayer player, Job prog, Entity ent, LivingEntity victim, boolean force, boolean getall) { + public Boost getFinalBonus(JobsPlayer player, Job job, Entity ent, LivingEntity victim, boolean force, boolean getall) { Boost boost = new Boost(); - if (player == null || !player.isOnline() || prog == null) + if (player == null || !player.isOnline() || job == null) return boost; Player pl = player.getPlayer(); @@ -1211,18 +1211,18 @@ public class PlayerManager { boost.add(BoostOf.NearSpawner, new BoostMultiplier().add(amount)); } - boost.add(BoostOf.Permission, getBoost(player, prog, force)); - boost.add(BoostOf.Global, prog.getBoost()); + boost.add(BoostOf.Permission, getBoost(player, job, force)); + boost.add(BoostOf.Global, job.getBoost()); if (Jobs.getGCManager().useDynamicPayment) - boost.add(BoostOf.Dynamic, new BoostMultiplier().add(prog.getBonus())); + boost.add(BoostOf.Dynamic, new BoostMultiplier().add(job.getBonus())); if (pl != null) { - boost.add(BoostOf.Item, getItemBoostNBT(pl, prog)); + boost.add(BoostOf.Item, getItemBoostNBT(pl, job)); } if (!Jobs.getRestrictedAreaManager().getRestrictedAreas().isEmpty()) - boost.add(BoostOf.Area, new BoostMultiplier().add(Jobs.getRestrictedAreaManager().getRestrictedMultiplier(pl))); + boost.add(BoostOf.Area, Jobs.getRestrictedAreaManager().getRestrictedMultipliers(player.getJobProgression(job), pl)); return boost; } 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 06ce46ae..16303ee0 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/area.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/area.java @@ -1,7 +1,8 @@ package com.gamingmesh.jobs.commands.list; -import java.util.List; +import java.util.HashMap; import java.util.Map.Entry; +import java.util.Set; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -10,6 +11,7 @@ import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.config.RestrictedAreaManager; import com.gamingmesh.jobs.container.CuboidArea; +import com.gamingmesh.jobs.container.CurrencyType; import com.gamingmesh.jobs.container.RestrictedArea; import com.gamingmesh.jobs.hooks.HookManager; import com.gamingmesh.jobs.i18n.Language; @@ -101,17 +103,17 @@ public class area implements Cmd { if (args.length == 1 && args[0].equalsIgnoreCase("info")) { - List areas = Jobs.getRestrictedAreaManager().getRestrictedAreasByLoc(player.getLocation()); + Set areas = Jobs.getRestrictedAreaManager().getByLocation(player.getLocation()); - String msg = ""; + StringBuilder msg = new StringBuilder(); for (RestrictedArea area : areas) { - if (!msg.isEmpty()) - msg += ", "; - msg += area.getName(); + if (!msg.toString().isEmpty()) + msg.append(LC.info_ListSpliter.getLocale()); + msg.append(area.getName()); } - if (msg.isEmpty()) { + if (msg.toString().isEmpty()) { Language.sendMessage(sender, "command.area.output.noAreasByLoc"); return true; } @@ -119,7 +121,7 @@ public class area implements Cmd { return true; } - if (args.length == 1 && args[0].equalsIgnoreCase("list")) { + if (args.length == 0 || args.length == 1 && args[0].equalsIgnoreCase("list")) { java.util.Map areas = Jobs.getRestrictedAreaManager().getRestrictedAreas(); if (areas.isEmpty()) { @@ -132,21 +134,28 @@ public class area implements Cmd { for (Entry area : areas.entrySet()) { i++; CuboidArea cuboid = area.getValue().getCuboidArea(); + HashMap multi = area.getValue().getMultipliers(); if (area.getValue().getWgName() == null) { - Language.sendMessage(sender, "command.area.output.list", "%number%", i, - "%areaname%", area.getKey(), - "%worldname%", cuboid.getWorld().getName(), - "%x1%", cuboid.getLowLoc().getBlockX(), - "%y1%", cuboid.getLowLoc().getBlockY(), - "%z1%", cuboid.getLowLoc().getBlockZ(), - "%x2%", cuboid.getHighLoc().getBlockX(), - "%y2%", cuboid.getHighLoc().getBlockY(), - "%z2%", cuboid.getHighLoc().getBlockZ(), - "%bonus%", area.getValue().getMultiplier()); + Language.sendMessage(sender, "command.area.output.lists", + "%number%", i, + "%areaname%", area.getValue().getName(), + "%worldname%", cuboid.getWorldName(), + "%x1%", cuboid.getLowPoint().getBlockX(), + "%y1%", cuboid.getLowPoint().getBlockY(), + "%z1%", cuboid.getLowPoint().getBlockZ(), + "%x2%", cuboid.getHighPoint().getBlockX(), + "%y2%", cuboid.getHighPoint().getBlockY(), + "%z2%", cuboid.getHighPoint().getBlockZ(), + "%money%", multi.get(CurrencyType.MONEY), + "%points%", multi.get(CurrencyType.POINTS), + "%exp%", multi.get(CurrencyType.EXP)); } else { - Language.sendMessage(sender, "command.area.output.wgList", "%number%", i, - "%areaname%", area.getKey(), - "%bonus%", area.getValue().getMultiplier()); + Language.sendMessage(sender, "command.area.output.wgLists", + "%number%", i, + "%areaname%", area.getValue().getName(), + "%money%", multi.get(CurrencyType.MONEY), + "%points%", multi.get(CurrencyType.POINTS), + "%exp%", multi.get(CurrencyType.EXP)); } } Language.sendMessage(sender, "general.info.separator"); diff --git a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java index c6c3d221..3016c1fe 100644 --- a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java @@ -559,9 +559,9 @@ public class LanguageManager { c.get("command.area.help.removeUsage", "&eUsage: &6/Jobs area remove [areaName]"); c.get("command.area.output.addedNew", "&eAdded a new restricted area with &6%bonus% &ebonus"); c.get("command.area.output.removed", "&eRemoved the restricted area &6%name%"); - c.get("command.area.output.list", "&e%number%&a. &e%areaname% &e%worldname% (&a%x1%:%y1%:%z1%/&e%x2%:%y2%:%z2%) &6%bonus%"); - c.get("command.area.output.wgList", "&e%number%&a. WorldGuard: &e%areaname% &6%bonus%"); - c.get("command.area.output.noAreas", "&eThere are no saved restricted areas"); + c.get("command.area.output.lists", "&7%number%&f. &7%areaname% &f%worldname% &7(&a%x1%:%y1%:%z1%&7/&e%x2%:%y2%:%z2%&7) &2%money% &6%points% &e%exp%"); + c.get("command.area.output.wgLists", "&7%number%&f. WorldGuard: &7%areaname% &2%money% &6%points% &e%exp%"); + c.get("command.area.output.noAreas", "&eThere are no saved restricted areas"); c.get("command.area.output.noAreasByLoc", "&eThere are no restricted areas in this location"); c.get("command.area.output.areaList", "&eRestricted areas by your location: &6%list%"); c.get("command.area.output.selected1", "&eSelected the first point: &6%x%:%y%:%z%"); diff --git a/src/main/java/com/gamingmesh/jobs/config/RestrictedAreaManager.java b/src/main/java/com/gamingmesh/jobs/config/RestrictedAreaManager.java index 47fb4e5a..38931f82 100644 --- a/src/main/java/com/gamingmesh/jobs/config/RestrictedAreaManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/RestrictedAreaManager.java @@ -3,37 +3,45 @@ package com.gamingmesh.jobs.config; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.util.Vector; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.container.BoostMultiplier; import com.gamingmesh.jobs.container.CuboidArea; +import com.gamingmesh.jobs.container.CurrencyType; +import com.gamingmesh.jobs.container.JobProgression; +import com.gamingmesh.jobs.container.LevelLimits; import com.gamingmesh.jobs.container.RestrictedArea; import com.gamingmesh.jobs.hooks.HookManager; +import net.Zrips.CMILib.Container.CMINumber; +import net.Zrips.CMILib.Container.CMIText; +import net.Zrips.CMILib.Container.CuboidArea.ChunkRef; +import net.Zrips.CMILib.Logs.CMIDebug; import net.Zrips.CMILib.Messages.CMIMessages; public class RestrictedAreaManager { protected final Map restrictedAreas = new HashMap<>(); - private boolean worldGuardArea = false; + private static final String fileName = "restrictedAreas.yml"; public boolean isExist(String name) { - for (String area : restrictedAreas.keySet()) { - if (area.equalsIgnoreCase(name)) - return true; - } - return false; + return restrictedAreas.containsKey(name.toLowerCase()); } public void addNew(RestrictedArea ra) { @@ -41,31 +49,32 @@ public class RestrictedAreaManager { } public void addNew(RestrictedArea ra, boolean save) { - restrictedAreas.put(ra.getName(), ra); + restrictedAreas.put(ra.getName().toLowerCase(), ra); if (save) save(); + if (ra.isEnabled()) + recalculateChunks(ra); } public void remove(String name) { - for (String area : restrictedAreas.keySet()) { - if (area.equalsIgnoreCase(name)) { - restrictedAreas.remove(area); - break; - } - } - File f = new File(Jobs.getFolder(), "restrictedAreas.yml"); - if (f.exists()) { - YamlConfiguration conf = YamlConfiguration.loadConfiguration(f); - conf.options().indent(2); - conf.options().copyDefaults(true); - addHeader(new StringBuilder()); - conf.set("restrictedareas." + name, null); - try { - conf.save(f); - } catch (IOException e) { - e.printStackTrace(); - } + + restrictedAreas.remove(name.toLowerCase()); + + File f = new File(Jobs.getFolder(), fileName); + if (!f.exists()) + return; + + YamlConfiguration conf = YamlConfiguration.loadConfiguration(f); + conf.options().indent(2); + conf.options().copyDefaults(true); + conf.set("restrictedareas." + name, null); + try { + conf.save(f); + } catch (IOException e) { + e.printStackTrace(); } + + recalculateChunks(); } /** @@ -84,29 +93,35 @@ public class RestrictedAreaManager { } private void save() { - File f = new File(Jobs.getFolder(), "restrictedAreas.yml"); + File f = new File(Jobs.getFolder(), fileName); YamlConfiguration conf = YamlConfiguration.loadConfiguration(f); conf.options().indent(2); conf.options().copyDefaults(true); - addHeader(new StringBuilder()); + conf.options().header(addHeader().toString()); - for (Entry area : restrictedAreas.entrySet()) { - String areaKey = area.getKey(); - CuboidArea cuboid = area.getValue().getCuboidArea(); - conf.set("restrictedareas." + areaKey + ".multiplier", area.getValue().getMultiplier()); + for (Entry rs : restrictedAreas.entrySet()) { - if (area.getValue().getWgName() == null) { + RestrictedArea area = rs.getValue(); + + String areaKey = area.getName(); + CuboidArea cuboid = area.getCuboidArea(); + + conf.set("restrictedareas." + areaKey + ".enabled", area.isEnabled()); + + for (Entry one : area.getMultipliers().entrySet()) { + conf.set("restrictedareas." + areaKey + ".multipliers." + one.getKey(), one.getValue()); + } + + if (area.getWgName() == null) { conf.set("restrictedareas." + areaKey + ".world", cuboid.getWorld().getName()); - conf.set("restrictedareas." + areaKey + ".point1.x", cuboid.getLowLoc().getBlockX()); - conf.set("restrictedareas." + areaKey + ".point1.y", cuboid.getLowLoc().getBlockY()); - conf.set("restrictedareas." + areaKey + ".point1.z", cuboid.getLowLoc().getBlockZ()); - conf.set("restrictedareas." + areaKey + ".point2.x", cuboid.getHighLoc().getBlockX()); - conf.set("restrictedareas." + areaKey + ".point2.y", cuboid.getHighLoc().getBlockY()); - conf.set("restrictedareas." + areaKey + ".point2.z", cuboid.getHighLoc().getBlockZ()); + conf.set("restrictedareas." + areaKey + ".point1", cuboid.getLowLoc().getBlockX() + ";" + cuboid.getLowLoc().getBlockY() + ";" + cuboid.getLowLoc().getBlockZ()); + conf.set("restrictedareas." + areaKey + ".point2", cuboid.getHighLoc().getBlockX() + ";" + cuboid.getHighLoc().getBlockY() + ";" + cuboid.getHighLoc().getBlockZ()); } else conf.set("restrictedareas." + areaKey + ".WG", true); + + conf.set("restrictedareas." + areaKey + ".jobs", new ArrayList()); } try { @@ -116,33 +131,49 @@ public class RestrictedAreaManager { } } + @Deprecated + public double getRestrictedMultiplier(Player player) { + return getRestrictedMultiplier(null, player); + } + /** * Gets the area multiplier for the player * @param player * @return - the multiplier */ - public double getRestrictedMultiplier(Player player) { - if (player == null) - return 0D; - for (RestrictedArea area : getRestrictedAreasByLoc(player.getLocation())) { - if (area.inRestrictedArea(player.getLocation()) || (area.getWgName() != null && HookManager.getWorldGuardManager() != null - && HookManager.getWorldGuardManager().inArea(player.getLocation(), area.getWgName()))) - return area.getMultiplier(); - } - return 0D; + @Deprecated + public double getRestrictedMultiplier(JobProgression prog, Player player) { + return getRestrictedMultipliers(prog, player).get(CurrencyType.MONEY); } - public List getRestrictedAreasByLoc(Location loc) { - List areas = new ArrayList<>(); - for (RestrictedArea area : restrictedAreas.values()) { - if (area.inRestrictedArea(loc)) - areas.add(area); + /** + * Gets the area multiplier for the player + * @param player + * @return - the multiplier + */ + public BoostMultiplier getRestrictedMultipliers(JobProgression prog, Player player) { + if (player == null) + return new BoostMultiplier(); + + for (RestrictedArea area : getByLocation(player.getLocation())) { + if (!area.inRestrictedArea(player.getLocation()) || + (area.getWgName() != null && HookManager.getWorldGuardManager() != null && !HookManager.getWorldGuardManager().inArea(player.getLocation(), area.getWgName()))) + continue; + + if (area.getJobs().isEmpty()) + return new BoostMultiplier(area.getMultipliers()); + + if (!area.validLevelRange(prog)) + continue; + + return new BoostMultiplier(area.getMultipliers()); } + return new BoostMultiplier(); + } - if (worldGuardArea && HookManager.getWorldGuardManager() != null) - areas.addAll(HookManager.getWorldGuardManager().getArea(loc)); - - return areas; + @Deprecated + public List getRestrictedAreasByLoc(Location loc) { + return new ArrayList<>(getByLocation(loc)); } public List getRestrictedAreasByName(String name) { @@ -154,9 +185,10 @@ public class RestrictedAreaManager { return areas; } - private static StringBuilder addHeader(StringBuilder header) { + private static StringBuilder addHeader() { String sep = System.lineSeparator(); + StringBuilder header = new StringBuilder(); header.append("Restricted area configuration"); header.append(sep) .append(sep) @@ -164,31 +196,15 @@ public class RestrictedAreaManager { .append("when performing a job.").append(sep).append(sep) .append("The multiplier changes the experience/money gains in an area.").append(sep) .append("A multiplier of 0.0 means no bonus, while 0.5 means you will get 50% more the normal income").append(sep) - .append("While -0.5 means that you will get 50% less the normal income").append(sep) + .append("While -0.5 means that you will get 50% less the normal income"); + + header.append(sep) .append(sep) - .append("restrictedareas:").append(sep) - .append(" area1:").append(sep) - .append(" world: 'world'").append(sep) - .append(" multiplier: 0.0").append(sep) - .append(" point1:").append(sep) - .append(" x: 125").append(sep) - .append(" y: 0").append(sep) - .append(" z: 125").append(sep) - .append(" point2:").append(sep) - .append(" x: 150").append(sep) - .append(" y: 100").append(sep) - .append(" z: 150").append(sep) - .append(" area2:").append(sep) - .append(" world: 'world_nether'").append(sep) - .append(" multiplier: 0.0").append(sep) - .append(" point1:").append(sep) - .append(" x: -100").append(sep) - .append(" y: 0").append(sep) - .append(" z: -100").append(sep) - .append(" point2:").append(sep) - .append(" x: -150").append(sep) - .append(" y: 100").append(sep) - .append(" z: -150"); + .append("jobs section defines list of jobs this area should apply to").append(sep) + .append("Define it as [jobName/all]-[fromLevel]-[untilLevel] for example miner-5-10 would mean that this area applies for miners between level 5 and 10").append(sep) + .append("Level limits are optional and if not defined we will apply to all levels").append(sep) + .append("You can define for all jobs at same time with defined level limit like all-25-69 which would apply for all jobs between level 25 and 69").append(sep); + return header; } @@ -199,41 +215,116 @@ public class RestrictedAreaManager { */ public void load() { restrictedAreas.clear(); + areas.clear(); - File f = new File(Jobs.getFolder(), "restrictedAreas.yml"); + File f = new File(Jobs.getFolder(), fileName); YamlConfiguration conf = YamlConfiguration.loadConfiguration(f); conf.options().indent(2); conf.options().copyDefaults(true); - StringBuilder header = addHeader(new StringBuilder()); - conf.options().header(header.toString()); + conf.options().header(addHeader().toString()); + + if (!conf.isConfigurationSection("restrictedareas")) { + conf.set("restrictedareas.area1.enabled", false); + for (CurrencyType one : CurrencyType.values()) { + conf.set("restrictedareas.area1.multipliers." + CMIText.firstToUpperCase(one.toString()), CMINumber.random(-10, 10) / 10D); + } + + conf.set("restrictedareas.area1.world", Bukkit.getWorlds().get(0).getName()); + conf.set("restrictedareas.area1.point1", "125;0;125"); + conf.set("restrictedareas.area1.point2", "150;100;150"); + conf.set("restrictedareas.area1.jobs", Arrays.asList("digger-0-100")); + + conf.set("restrictedareas.area2.enabled", false); + for (CurrencyType one : CurrencyType.values()) { + conf.set("restrictedareas.area2.multipliers." + CMIText.firstToUpperCase(one.toString()), CMINumber.random(-10, 10) / 10D); + } + conf.set("restrictedareas.area2.world", Bukkit.getWorlds().get(1).getName()); + conf.set("restrictedareas.area2.point1", "-100;0;-100"); + conf.set("restrictedareas.area2.point2", "-150;100;-150"); + conf.set("restrictedareas.area2.jobs", Arrays.asList("all-5-15")); + } ConfigurationSection areaSection = conf.getConfigurationSection("restrictedareas"); if (areaSection != null) { for (String areaKey : areaSection.getKeys(false)) { - double multiplier = areaSection.getDouble(areaKey + ".multiplier"); - if (areaSection.isBoolean(areaKey + ".WG")) { - addNew(new RestrictedArea(areaKey, areaKey, multiplier)); - worldGuardArea = true; - } else { - World world = Bukkit.getServer().getWorld(areaSection.getString(areaKey + ".world", "")); - if (world == null) - continue; - Location point1 = new Location(world, areaSection.getDouble(areaKey + ".point1.x"), areaSection.getDouble(areaKey - + ".point1.y"), areaSection.getDouble(areaKey + ".point1.z")); + RestrictedArea area = new RestrictedArea(areaKey, areaKey); - Location point2 = new Location(world, areaSection.getDouble(areaKey + ".point2.x"), areaSection.getDouble(areaKey - + ".point2.y"), areaSection.getDouble(areaKey + ".point2.z")); - addNew(new RestrictedArea(areaKey, new CuboidArea(point1, point2), multiplier)); + if (!areaSection.isBoolean(areaKey + ".WG")) { + + Vector point1 = new Vector(); + + if (areaSection.isDouble(areaKey + ".point1.x")) + point1 = new Vector( + areaSection.getDouble(areaKey + ".point1.x"), + areaSection.getDouble(areaKey + ".point1.y"), + areaSection.getDouble(areaKey + ".point1.z")); + else if (areaSection.isString(areaKey + ".point1")) { + try { + String[] p1 = areaSection.getString(areaKey + ".point1").split(";"); + point1 = new Vector(Double.parseDouble(p1[0]), Double.parseDouble(p1[1]), Double.parseDouble(p1[2])); + } catch (Exception e) { + e.printStackTrace(); + } + } + + Vector point2 = new Vector(); + + if (areaSection.isDouble(areaKey + ".point2.x")) + point2 = new Vector( + areaSection.getDouble(areaKey + ".point2.x"), + areaSection.getDouble(areaKey + ".point2.y"), + areaSection.getDouble(areaKey + ".point2.z")); + if (areaSection.isString(areaKey + ".point2")) { + try { + String[] p1 = areaSection.getString(areaKey + ".point2").split(";"); + point2 = new Vector(Double.parseDouble(p1[0]), Double.parseDouble(p1[1]), Double.parseDouble(p1[2])); + } catch (Exception e) { + e.printStackTrace(); + } + } + + area = new RestrictedArea(areaKey, new CuboidArea(areaSection.getString(areaKey + ".world", ""), point1, point2)); } + + area.setEnabled(areaSection.getBoolean(areaKey + ".enabled", false)); + + // Outdated way of defining multiplier + if (areaSection.isDouble(areaKey + ".multiplier")) { + double multiplier = areaSection.getDouble(areaKey + ".multiplier", 0D); + for (CurrencyType one : CurrencyType.values()) { + area.getMultipliers().put(one, multiplier); + areaSection.set(areaKey + ".multipliers." + CMIText.firstToUpperCase(one.toString()), multiplier); + } + areaSection.set(areaKey + ".multiplier", null); + } else if (areaSection.isConfigurationSection(areaKey + ".multipliers")) { + for (CurrencyType one : CurrencyType.values()) { + area.getMultipliers().put(one, areaSection.getDouble(areaKey + ".multipliers." + CMIText.firstToUpperCase(one.name()), 0D)); + } + } + + if (areaSection.isList(areaKey + ".jobs")) { + for (String jobSection : areaSection.getStringList(areaKey + ".jobs")) { + String[] split = jobSection.split("-"); + try { + area.getJobs().put(split[0].toLowerCase(), new LevelLimits(split.length > 1 ? Integer.parseInt(split[1]) : 0, split.length > 2 ? Integer.parseInt(split[2]) + : Integer.MAX_VALUE)); + } catch (Exception e) { + CMIMessages.consoleMessage("Incorrectly defined job in restricted area " + areaKey); + } + } + } else { + conf.set("restrictedareas." + areaKey + ".jobs", Arrays.asList("all")); + } + + addNew(area); } } - int size = restrictedAreas.size(); - if (size > 0) - CMIMessages.consoleMessage("&e[Jobs] Loaded " + size + " restricted areas!"); + if (!restrictedAreas.isEmpty()) + CMIMessages.consoleMessage("&eLoaded &6" + restrictedAreas.size() + " &erestricted areas!"); try { conf.save(f); @@ -241,4 +332,44 @@ public class RestrictedAreaManager { e.printStackTrace(); } } + + protected Map>> areas = new HashMap<>(); + + public void recalculateChunks() { + areas.clear(); + for (RestrictedArea one : restrictedAreas.values()) { + recalculateChunks(one); + } + } + + public void recalculateChunks(RestrictedArea area) { + if (area.getCuboidArea() == null) + return; + + if (area.getCuboidArea().getWorld() == null) + return; + + Map> retAreas = areas.computeIfAbsent(area.getCuboidArea().getWorldName(), k -> new HashMap<>()); + + for (ChunkRef chunk : area.getCuboidArea().getChunks()) { + retAreas.computeIfAbsent(chunk, k -> new HashSet<>()).add(area); + } + } + + public Set getByLocation(Location loc) { + Set area = new HashSet<>(); + if (loc == null || loc.getWorld() == null) { + return area; + } + + String worldName = loc.getWorld().getName(); + ChunkRef chunk = new ChunkRef(loc); + Map> chunkMap = areas.getOrDefault(worldName, Collections.emptyMap()); + + chunkMap.getOrDefault(chunk, Collections.emptySet()).stream() + .filter(entry -> entry != null && entry.inRestrictedArea(loc)) + .forEach(area::add); + + return area; + } } diff --git a/src/main/java/com/gamingmesh/jobs/container/BoostMultiplier.java b/src/main/java/com/gamingmesh/jobs/container/BoostMultiplier.java index cf0edc26..f5b66e7b 100644 --- a/src/main/java/com/gamingmesh/jobs/container/BoostMultiplier.java +++ b/src/main/java/com/gamingmesh/jobs/container/BoostMultiplier.java @@ -1,11 +1,12 @@ package com.gamingmesh.jobs.container; import java.util.HashMap; +import java.util.Map; public class BoostMultiplier implements Cloneable { - private final java.util.Map map = new HashMap<>(); - private final java.util.Map timers = new HashMap<>(); + private final Map map = new HashMap<>(); + private final Map timers = new HashMap<>(); @Override public BoostMultiplier clone() { @@ -16,15 +17,21 @@ public class BoostMultiplier implements Cloneable { return boost; } + public BoostMultiplier(Map map) { + for (CurrencyType one : CurrencyType.values()) { + this.map.put(one, map.getOrDefault(one, 0D)); + } + } + public BoostMultiplier() { for (CurrencyType one : CurrencyType.values()) { - map.put(one, 0D); + this.map.put(one, 0D); } } public BoostMultiplier add(CurrencyType type, double amount) { if (!Double.isNaN(amount)) - map.put(type, amount); + this.map.put(type, amount); timers.remove(type); return this; } diff --git a/src/main/java/com/gamingmesh/jobs/container/CuboidArea.java b/src/main/java/com/gamingmesh/jobs/container/CuboidArea.java index 90fc8660..8cd2aa24 100644 --- a/src/main/java/com/gamingmesh/jobs/container/CuboidArea.java +++ b/src/main/java/com/gamingmesh/jobs/container/CuboidArea.java @@ -1,72 +1,108 @@ package com.gamingmesh.jobs.container; +import java.util.ArrayList; +import java.util.List; + import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.util.Vector; + +import net.Zrips.CMILib.Container.CMIWorld; +import net.Zrips.CMILib.Container.CuboidArea.ChunkRef; public class CuboidArea { - protected Location highPoints; - protected Location lowPoints; + protected Vector highPoints; + protected Vector lowPoints; protected String worldName; + protected World world; protected CuboidArea() { } public CuboidArea(Location startLoc, Location endLoc) { - int highx, highy, highz, lowx, lowy, lowz; - if (startLoc.getBlockX() > endLoc.getBlockX()) { - highx = startLoc.getBlockX(); - lowx = endLoc.getBlockX(); - } else { - highx = endLoc.getBlockX(); - lowx = startLoc.getBlockX(); - } - if (startLoc.getBlockY() > endLoc.getBlockY()) { - highy = startLoc.getBlockY(); - lowy = endLoc.getBlockY(); - } else { - highy = endLoc.getBlockY(); - lowy = startLoc.getBlockY(); - } - if (startLoc.getBlockZ() > endLoc.getBlockZ()) { - highz = startLoc.getBlockZ(); - lowz = endLoc.getBlockZ(); - } else { - highz = endLoc.getBlockZ(); - lowz = startLoc.getBlockZ(); - } - highPoints = new Location(startLoc.getWorld(), highx, highy, highz); - lowPoints = new Location(startLoc.getWorld(), lowx, lowy, lowz); - worldName = startLoc.getWorld().getName(); + modifyVectors(startLoc.toVector(), endLoc.toVector()); + this.worldName = startLoc.getWorld().getName(); + } + + public CuboidArea(String worldName, Vector startLoc, Vector endLoc) { + modifyVectors(startLoc, endLoc); + this.worldName = worldName; + } + + private void modifyVectors(Vector startLoc, Vector endLoc) { + + int highx = Math.max(startLoc.getBlockX(), endLoc.getBlockX()); + int lowx = Math.min(startLoc.getBlockX(), endLoc.getBlockX()); + + int highy = Math.max(startLoc.getBlockY(), endLoc.getBlockY()); + int lowy = Math.min(startLoc.getBlockY(), endLoc.getBlockY()); + + int highz = Math.max(startLoc.getBlockZ(), endLoc.getBlockZ()); + int lowz = Math.min(startLoc.getBlockZ(), endLoc.getBlockZ()); + + this.highPoints = new Vector(highx, highy, highz); + this.lowPoints = new Vector(lowx, lowy, lowz); } public long getSize() { - int xsize = (highPoints.getBlockX() - lowPoints.getBlockX()) + 1; - int zsize = (highPoints.getBlockZ() - lowPoints.getBlockZ()) + 1; - int ysize = (highPoints.getBlockY() - lowPoints.getBlockY()) + 1; - return (long) xsize * ysize * zsize; + int xsize = (getHighPoint().getBlockX() - getLowPoint().getBlockX()) + 1; + int zsize = (getHighPoint().getBlockZ() - getLowPoint().getBlockZ()) + 1; + int ysize = (getHighPoint().getBlockY() - getLowPoint().getBlockY()) + 1; + return (long) xsize * ysize * zsize; } public int getXSize() { - return (highPoints.getBlockX() - lowPoints.getBlockX()) + 1; + return (getHighPoint().getBlockX() - getLowPoint().getBlockX()) + 1; } public int getYSize() { - return (highPoints.getBlockY() - lowPoints.getBlockY()) + 1; + return (getHighPoint().getBlockY() - getLowPoint().getBlockY()) + 1; } public int getZSize() { - return (highPoints.getBlockZ() - lowPoints.getBlockZ()) + 1; + return (getHighPoint().getBlockZ() - getLowPoint().getBlockZ()) + 1; } public Location getHighLoc() { - return highPoints; + return getHighPoint().toLocation(getWorld()); } public Location getLowLoc() { - return lowPoints; + return getLowPoint().toLocation(getWorld()); + } + + public String getWorldName() { + return worldName; } public World getWorld() { - return highPoints.getWorld(); + if (world != null) + return world; + world = CMIWorld.getWorld(worldName); + return world; + } + + public List getChunks() { + List chunks = new ArrayList<>(); + + int lowX = ChunkRef.getChunkCoord(this.getLowPoint().getBlockX()); + int lowZ = ChunkRef.getChunkCoord(this.getLowPoint().getBlockZ()); + int highX = ChunkRef.getChunkCoord(this.getHighPoint().getBlockX()); + int highZ = ChunkRef.getChunkCoord(this.getHighPoint().getBlockZ()); + + for (int x = lowX; x <= highX; x++) { + for (int z = lowZ; z <= highZ; z++) { + chunks.add(new ChunkRef(x, z)); + } + } + return chunks; + } + + public Vector getLowPoint() { + return lowPoints; + } + + public Vector getHighPoint() { + return highPoints; } } diff --git a/src/main/java/com/gamingmesh/jobs/container/LevelLimits.java b/src/main/java/com/gamingmesh/jobs/container/LevelLimits.java new file mode 100644 index 00000000..846cbe1e --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/container/LevelLimits.java @@ -0,0 +1,29 @@ +package com.gamingmesh.jobs.container; + +public class LevelLimits { + + private int fromLevel = 0; + private int untilLevel = Integer.MAX_VALUE; + + public LevelLimits(int fromLevel, int untilLevel) { + this.fromLevel = fromLevel; + this.untilLevel = untilLevel; + } + + public int getFromLevel() { + return fromLevel; + } + + public void setFromLevel(int fromLevel) { + this.fromLevel = fromLevel; + } + + public int getUntilLevel() { + return untilLevel; + } + + public void setUntilLevel(int fromLevel) { + this.untilLevel = fromLevel; + } + +} diff --git a/src/main/java/com/gamingmesh/jobs/container/RestrictedArea.java b/src/main/java/com/gamingmesh/jobs/container/RestrictedArea.java index c6f3b1b6..fdce0f9f 100644 --- a/src/main/java/com/gamingmesh/jobs/container/RestrictedArea.java +++ b/src/main/java/com/gamingmesh/jobs/container/RestrictedArea.java @@ -18,37 +18,69 @@ package com.gamingmesh.jobs.container; +import java.util.HashMap; + import org.bukkit.Location; public class RestrictedArea { + private boolean enabled = false; private CuboidArea area; - private double multiplier; + + private HashMap multipliers = new HashMap<>(); + private String name; private String wgName; + private HashMap jobs = new HashMap<>(); + + @Deprecated public RestrictedArea(String name, CuboidArea area, double multiplier) { - this.name = name; - this.area = area; - this.multiplier = multiplier; + this.name = name; + this.area = area; + for (CurrencyType one : CurrencyType.values()) { + multipliers.put(one, multiplier); + } } - + + @Deprecated public RestrictedArea(String name, String wgName, double multiplier) { - this.name = name; - this.wgName = wgName; - this.multiplier = multiplier; + this.name = name; + this.wgName = wgName; + for (CurrencyType one : CurrencyType.values()) { + multipliers.put(one, multiplier); + } + } + + public RestrictedArea(String name, CuboidArea area) { + this.name = name; + this.area = area; + } + + public RestrictedArea(String name, String wgName) { + this.name = name; + this.wgName = wgName; } public CuboidArea getCuboidArea() { - return area; + return area; } /** - * The multipler for the restricted area - * @return - the multipler for this restricted area + * The multiplier for the restricted area + * @return - the multiplier for this restricted area */ + @Deprecated public double getMultiplier() { - return multiplier; + return multipliers.get(CurrencyType.MONEY); + } + + /** + * The multipliers for the restricted area + * @return - the multipliers for this restricted area + */ + public HashMap getMultipliers() { + return multipliers; } /** @@ -58,35 +90,65 @@ public class RestrictedArea { * @return false - the location is outside the restricted area */ public boolean inRestrictedArea(Location loc) { - if (loc == null || area == null) - return false; + if (loc == null || area == null) + return false; - if (!loc.getWorld().getName().equals(area.getWorld().getName())) - return false; - if (area.getLowLoc().getBlockX() > loc.getBlockX()) - return false; - if (area.getHighLoc().getBlockX() < loc.getBlockX()) - return false; - if (area.getLowLoc().getBlockZ() > loc.getBlockZ()) - return false; - if (area.getHighLoc().getBlockZ() < loc.getBlockZ()) - return false; - if (area.getLowLoc().getBlockY() > loc.getBlockY()) - return false; - if (area.getHighLoc().getBlockY() < loc.getBlockY()) - return false; - return true; + if (!loc.getWorld().getName().equals(area.getWorld().getName())) + return false; + if (area.getLowLoc().getBlockX() > loc.getBlockX()) + return false; + if (area.getHighLoc().getBlockX() < loc.getBlockX()) + return false; + if (area.getLowLoc().getBlockZ() > loc.getBlockZ()) + return false; + if (area.getHighLoc().getBlockZ() < loc.getBlockZ()) + return false; + if (area.getLowLoc().getBlockY() > loc.getBlockY()) + return false; + if (area.getHighLoc().getBlockY() < loc.getBlockY()) + return false; + return true; } public String getName() { - return name; + return name; } public String getWgName() { - return wgName; + return wgName; } public void setWgName(String wgName) { - this.wgName = wgName; + this.wgName = wgName; } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public HashMap getJobs() { + return jobs; + } + + public void setJobs(HashMap jobs) { + this.jobs = jobs; + } + + public boolean validLevelRange(JobProgression prog) { + if (prog == null) + return true; + + LevelLimits levelLimit = jobs.get(prog.getJob().getName().toLowerCase()); + if (levelLimit == null) { + levelLimit = jobs.get("all"); + if (levelLimit == null) + return false; + } + return prog.getLevel() >= levelLimit.getFromLevel() && prog.getLevel() <= levelLimit.getUntilLevel(); + } + }