diff --git a/com/gamingmesh/jobs/api/JobsAreaSelectionEvent.java b/com/gamingmesh/jobs/api/JobsAreaSelectionEvent.java new file mode 100644 index 00000000..54519289 --- /dev/null +++ b/com/gamingmesh/jobs/api/JobsAreaSelectionEvent.java @@ -0,0 +1,47 @@ +package com.gamingmesh.jobs.api; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import com.gamingmesh.jobs.container.CuboidArea; + +public final class JobsAreaSelectionEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private CuboidArea area; + private Player player; + + public JobsAreaSelectionEvent(Player player, CuboidArea area) { + this.player = player; + this.area = area; + } + + public Player getPlayer() { + return this.player; + } + + public CuboidArea getArea() { + return area; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} \ No newline at end of file diff --git a/com/gamingmesh/jobs/commands/list/area.java b/com/gamingmesh/jobs/commands/list/area.java new file mode 100644 index 00000000..41b14a4e --- /dev/null +++ b/com/gamingmesh/jobs/commands/list/area.java @@ -0,0 +1,132 @@ +package com.gamingmesh.jobs.commands.list; + +import java.util.HashMap; +import java.util.List; +import java.util.Map.Entry; + +import org.bukkit.Material; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.commands.Cmd; +import com.gamingmesh.jobs.commands.JobCommand; +import com.gamingmesh.jobs.config.RestrictedAreaManager; +import com.gamingmesh.jobs.container.CuboidArea; +import com.gamingmesh.jobs.container.RestrictedArea; + +public class area implements Cmd { + + @SuppressWarnings("deprecation") + @Override + @JobCommand(300) + public boolean perform(Jobs plugin, final CommandSender sender, final String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage(Jobs.getLanguage().getMessage("general.error.ingame")); + return false; + } + Player player = (Player) sender; + + if (args.length == 0) { + return false; + } + + RestrictedAreaManager ra = Jobs.getRestrictedAreaManager(); + + if (args.length == 3 && args[0].equalsIgnoreCase("add") && player.hasPermission("jobs.area.add")) { + String name = args[1]; + double bonus = 0D; + try { + bonus = Double.parseDouble(args[2]); + } catch (Exception e) { + return false; + } + if (ra.isExist(name)) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.output.exist")); + return true; + } + + if (!Jobs.getSelectionManager().hasPlacedBoth(player)) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.output.select", "%tool%", Material.getMaterial(Jobs.getGCManager().getSelectionTooldID).name().toLowerCase())); + return true; + } + ra.addNew(new RestrictedArea(name, Jobs.getSelectionManager().getSelectionCuboid(player), bonus), true); + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.output.addedNew", "%bonus%", bonus)); + return true; + } + + if (args.length == 2 && args[0].equalsIgnoreCase("remove") && player.hasPermission("jobs.area.remove")) { + String name = args[1]; + + if (!ra.isExist(name)) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.output.dontExist")); + return true; + } + + ra.remove(name, true); + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.output.removed", "%name%", name)); + return true; + } + + if (args.length == 1 && args[0].equalsIgnoreCase("info")) { + + List areas = Jobs.getRestrictedAreaManager().getRestrictedAreasByLoc(player.getLocation()); + + String msg = ""; + + for (RestrictedArea area : areas) { + if (!msg.isEmpty()) + msg += ", "; + msg += area.getName(); + } + + if (msg.isEmpty()) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.output.noAreasByLoc")); + return true; + } + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.output.areaList", "%list%", msg)); + return true; + } + + if (args.length == 1 && args[0].equalsIgnoreCase("list")) { + + HashMap areas = Jobs.getRestrictedAreaManager().getRestrictedAres(); + if (areas.isEmpty()) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.output.noAreas")); + return true; + } + + sender.sendMessage(Jobs.getLanguage().getMessage("general.info.separator")); + int i = 0; + for (Entry area : areas.entrySet()) { + i++; + CuboidArea cuboid = area.getValue().getCuboidArea(); + sender.sendMessage(Jobs.getLanguage().getMessage("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())); + } + sender.sendMessage(Jobs.getLanguage().getMessage("general.info.separator")); + return true; + } + + if (args.length > 1) { + if (args[0].equalsIgnoreCase("add")) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.help.addUsage")); + return true; + } + if (args[0].equalsIgnoreCase("remove")) { + sender.sendMessage(Jobs.getLanguage().getMessage("command.area.help.removeUsage")); + return true; + } + } + + return true; + } + +} diff --git a/com/gamingmesh/jobs/config/GeneralConfigManager.java b/com/gamingmesh/jobs/config/GeneralConfigManager.java index e9f5865e..b40eae6c 100644 --- a/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -344,6 +344,7 @@ public class GeneralConfigManager { * * loads from Jobs/generalConfig.yml */ + @SuppressWarnings("deprecation") private synchronized void loadGeneralSettings() { File f = new File(plugin.getDataFolder(), "generalConfig.yml"); YamlConfiguration conf = YamlConfiguration.loadConfiguration(f); @@ -415,6 +416,8 @@ public class GeneralConfigManager { saveOnDisconnect = c.get("save-on-disconnect", false); getSelectionTooldID = c.get("selectionTool", 294); + if (Material.getMaterial(Jobs.getGCManager().getSelectionTooldID) == null) + getSelectionTooldID = 294; c.getW().addComment("MultiServerCompatability", "Enable if you are using one data base for multiple servers across bungee network", "This will force to load players data every time he is logging in to have most up to date data instead of having preloaded data", diff --git a/com/gamingmesh/jobs/config/LanguageManager.java b/com/gamingmesh/jobs/config/LanguageManager.java index 415da318..fff7a464 100644 --- a/com/gamingmesh/jobs/config/LanguageManager.java +++ b/com/gamingmesh/jobs/config/LanguageManager.java @@ -347,6 +347,23 @@ public class LanguageManager { c.get("command.gtop.output.prev", "&e<<<<< Prev page &2|"); c.get("command.gtop.output.next", "&2|&e Next Page >>>>"); c.get("command.gtop.output.show", "&2Show from &e[from] &2until &e[until] &2global top list"); + + + c.get("command.area.help.info", "Modify restricted areas."); + c.get("command.area.help.args", "add/remove/info/list"); + c.get("command.area.help.addUsage", "&eUsage: &6/Jobs area add [areaName] [bonus]"); + c.get("command.area.help.removeUsage", "&eUsage: &6/Jobs area remove [areaName]"); + c.get("command.area.output.addedNew", "&eAdded new restricted area with &6%bonus% &ebonus"); + c.get("command.area.output.removed", "&eRemoved 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.noAreas", "&eThere is no saved restricted areas"); + c.get("command.area.output.noAreasByLoc", "&eThere is 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 first point: &6%x%:%y%:%z%"); + c.get("command.area.output.selected2", "&eSelected second point: &6%x%:%y%:%z%"); + c.get("command.area.output.select", "&eSelect 2 points with selection tool (%tool%)"); + c.get("command.area.output.exist", "&eRestriction area by this name already exist"); + c.get("command.area.output.dontExist", "&eRestriction area by this name don't exist"); c.get("command.log.help.info", "Shows statistics."); c.get("command.log.help.args", "[playername]"); diff --git a/com/gamingmesh/jobs/config/RestrictedAreaManager.java b/com/gamingmesh/jobs/config/RestrictedAreaManager.java index 664bf963..02101225 100644 --- a/com/gamingmesh/jobs/config/RestrictedAreaManager.java +++ b/com/gamingmesh/jobs/config/RestrictedAreaManager.java @@ -46,6 +46,21 @@ public class RestrictedAreaManager { if (save) save(); } + + public void remove(String name, boolean save) { + for (Entry area : restrictedAreas.entrySet()) { + if (area.getKey().equalsIgnoreCase(name)){ + restrictedAreas.remove(area.getKey()); + break; + } + } + if (save) + save(); + } + + public HashMap getRestrictedAres(){ + return restrictedAreas; + } private void save() { File f = new File(plugin.getDataFolder(), "restrictedAreas.yml"); diff --git a/com/gamingmesh/jobs/container/CuboidArea.java b/com/gamingmesh/jobs/container/CuboidArea.java new file mode 100644 index 00000000..ecc22edd --- /dev/null +++ b/com/gamingmesh/jobs/container/CuboidArea.java @@ -0,0 +1,72 @@ +package com.gamingmesh.jobs.container; + +import org.bukkit.Location; +import org.bukkit.World; + +public class CuboidArea { + protected Location highPoints; + protected Location lowPoints; + protected String worldName; + + 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(); + } + + 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 xsize * ysize * zsize; + } + + public int getXSize() { + return (highPoints.getBlockX() - lowPoints.getBlockX()) + 1; + } + + public int getYSize() { + return (highPoints.getBlockY() - lowPoints.getBlockY()) + 1; + } + + public int getZSize() { + return (highPoints.getBlockZ() - lowPoints.getBlockZ()) + 1; + } + + public Location getHighLoc() { + return highPoints; + } + + public Location getLowLoc() { + return lowPoints; + } + + public World getWorld() { + return highPoints.getWorld(); + } +} diff --git a/com/gamingmesh/jobs/i18n/Language.java b/com/gamingmesh/jobs/i18n/Language.java index c968d403..8e27462a 100644 --- a/com/gamingmesh/jobs/i18n/Language.java +++ b/com/gamingmesh/jobs/i18n/Language.java @@ -49,6 +49,10 @@ public class Language { * @param key - the key of the message * @return the message */ + public String getMessage(String key) { + return getMessage(key, ""); + } + public String getMessage(String key, Object... variables) { String missing = "Missing locale for " + key + " "; String msg = ""; diff --git a/com/gamingmesh/jobs/listeners/JobsListener.java b/com/gamingmesh/jobs/listeners/JobsListener.java index 9124d9b0..11feb5bd 100644 --- a/com/gamingmesh/jobs/listeners/JobsListener.java +++ b/com/gamingmesh/jobs/listeners/JobsListener.java @@ -93,23 +93,23 @@ public class JobsListener implements Listener { int heldItemId = iih.getTypeId(); if (heldItemId != Jobs.getGCManager().getSelectionTooldID()) return; - - if (!player.hasPermission("jobs.selectarea")) + + if (!player.hasPermission("jobs.area.select")) return; if (player.getGameMode() == GameMode.CREATIVE) event.setCancelled(true); - + Block block = event.getClickedBlock(); if (event.getAction() == Action.LEFT_CLICK_BLOCK) { Location loc = block.getLocation(); Jobs.getSelectionManager().placeLoc1(player, loc); - player.sendMessage("Selected: " + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ()); + player.sendMessage(Jobs.getLanguage().getMessage("command.area.output.selected1", "%x%", loc.getBlockX(), "%y%", loc.getBlockY(), "%z%", loc.getBlockZ())); event.setCancelled(true); } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { Location loc = block.getLocation(); Jobs.getSelectionManager().placeLoc2(player, loc); - player.sendMessage("Selected: " + loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ()); + player.sendMessage(Jobs.getLanguage().getMessage("command.area.output.selected2", "%x%", loc.getBlockX(), "%y%", loc.getBlockY(), "%z%", loc.getBlockZ())); event.setCancelled(true); } diff --git a/com/gamingmesh/jobs/selection/.gitignore b/com/gamingmesh/jobs/selection/.gitignore new file mode 100644 index 00000000..4b9edf4f --- /dev/null +++ b/com/gamingmesh/jobs/selection/.gitignore @@ -0,0 +1,6 @@ +/AutoSelection.class +/SchematicsManager.class +/SelectionManager$Direction.class +/SelectionManager.class +/WorldEditSelectionManager.class +/WorldGuardUtil.class diff --git a/com/gamingmesh/jobs/selection/SelectionManager.java b/com/gamingmesh/jobs/selection/SelectionManager.java new file mode 100644 index 00000000..0760fdd6 --- /dev/null +++ b/com/gamingmesh/jobs/selection/SelectionManager.java @@ -0,0 +1,109 @@ +package com.gamingmesh.jobs.selection; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import com.gamingmesh.jobs.container.CuboidArea; + +public class SelectionManager { + protected Map playerLoc1; + protected Map playerLoc2; + + public static final int MIN_HEIGHT = 0; + + public SelectionManager() { + playerLoc1 = Collections.synchronizedMap(new HashMap()); + playerLoc2 = Collections.synchronizedMap(new HashMap()); + } + + public void updateLocations(Player player, Location loc1, Location loc2) { + if (loc1 != null && loc2 != null) { + playerLoc1.put(player.getName(), loc1); + playerLoc2.put(player.getName(), loc2); + } + } + + public void placeLoc1(Player player, Location loc) { + if (loc != null) { + playerLoc1.put(player.getName(), loc); + } + } + + public void placeLoc2(Player player, Location loc) { + if (loc != null) { + playerLoc2.put(player.getName(), loc); + } + } + + public Location getPlayerLoc1(Player player) { + return getPlayerLoc1(player.getName()); + } + + public Location getPlayerLoc1(String player) { + return playerLoc1.get(player); + } + + public Location getPlayerLoc2(Player player) { + return getPlayerLoc2(player.getName()); + } + + public Location getPlayerLoc2(String player) { + return playerLoc2.get(player); + } + + public CuboidArea getSelectionCuboid(Player player) { + return getSelectionCuboid(player.getName()); + } + + public CuboidArea getSelectionCuboid(String player) { + return new CuboidArea(getPlayerLoc1(player), getPlayerLoc2(player)); + } + + public boolean hasPlacedBoth(Player player) { + return hasPlacedBoth(player.getName()); + } + + public boolean hasPlacedBoth(String player) { + return playerLoc1.containsKey(player) && playerLoc2.containsKey(player); + } + + public void clearSelection(Player player) { + playerLoc1.remove(player.getName()); + playerLoc2.remove(player.getName()); + } + + public void selectChunk(Player player) { + Chunk chunk = player.getWorld().getChunkAt(player.getLocation()); + int xcoord = chunk.getX() * 16; + int zcoord = chunk.getZ() * 16; + int ycoord = MIN_HEIGHT; + int xmax = xcoord + 15; + int zmax = zcoord + 15; + int ymax = player.getLocation().getWorld().getMaxHeight() - 1; + playerLoc1.put(player.getName(), new Location(player.getWorld(), xcoord, ycoord, zcoord)); + playerLoc2.put(player.getName(), new Location(player.getWorld(), xmax, ymax, zmax)); + player.sendMessage("msg"); + } + + public boolean worldEdit(Player player) { + player.sendMessage("msg"); + return false; + } + + public boolean worldEditUpdate(Player player) { + player.sendMessage("msg"); + return false; + } + + public void selectBySize(Player player, int xsize, int ysize, int zsize) { + Location myloc = player.getLocation(); + Location loc1 = new Location(myloc.getWorld(), myloc.getBlockX() + xsize, myloc.getBlockY() + ysize, myloc.getBlockZ() + zsize); + Location loc2 = new Location(myloc.getWorld(), myloc.getBlockX() - xsize, myloc.getBlockY() - ysize, myloc.getBlockZ() - zsize); + placeLoc1(player, loc1); + placeLoc2(player, loc2); + player.sendMessage("msg"); + } +}