From 077f560ddc64f4881ca4b8b267ee0c6eec47befe Mon Sep 17 00:00:00 2001 From: zavdav <157126752+zavdav@users.noreply.github.com> Date: Mon, 21 Oct 2024 17:34:04 +0200 Subject: [PATCH 1/3] Add ability to specify direction of village spawn, add /v warp functionality + fix some other stuff --- .../johnymuffin/jvillage/beta/JVUtility.java | 18 +++ .../johnymuffin/jvillage/beta/JVillage.java | 15 +-- .../jvillage/beta/commands/JVillageCMD.java | 3 + .../beta/commands/village/JCreateCommand.java | 10 +- .../commands/village/JDelWarpCommand.java | 68 +++++++++++ .../beta/commands/village/JKickCommand.java | 4 +- .../beta/commands/village/JRenameCommand.java | 6 +- .../commands/village/JSetSpawnCommand.java | 15 ++- .../commands/village/JSetWarpCommand.java | 109 ++++++++++++++++++ .../beta/commands/village/JSpawnCommand.java | 2 +- .../beta/commands/village/JWarpCommand.java | 73 ++++++++++++ .../beta/config/JVillageLanguage.java | 32 ++++- .../beta/config/JVillageSettings.java | 6 + .../jvillage/beta/models/VSpawnCords.java | 52 +++++++++ .../jvillage/beta/models/Village.java | 40 ++++++- src/main/resources/plugin.yml | 13 +++ 16 files changed, 439 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/johnymuffin/jvillage/beta/commands/village/JDelWarpCommand.java create mode 100644 src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java create mode 100644 src/main/java/com/johnymuffin/jvillage/beta/commands/village/JWarpCommand.java create mode 100644 src/main/java/com/johnymuffin/jvillage/beta/models/VSpawnCords.java diff --git a/src/main/java/com/johnymuffin/jvillage/beta/JVUtility.java b/src/main/java/com/johnymuffin/jvillage/beta/JVUtility.java index 979124e..0ae11e9 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/JVUtility.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/JVUtility.java @@ -14,6 +14,7 @@ import java.math.BigDecimal; import java.math.RoundingMode; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.UUID; @@ -72,6 +73,23 @@ public static double round(double value, int places) { return bd.doubleValue(); } + public static int closestYaw(double yaw){ + if (yaw < 0) yaw = 360 + yaw; + Set yaws = new HashSet<>(Arrays.asList(0, 90, 180, 270, 360)); + int closest = -1; + double lowestDiff = Integer.MAX_VALUE; + + for (Integer entry : yaws) { + double diff = Math.abs(yaw - entry); + if (diff < lowestDiff) { + closest = entry; + lowestDiff = diff; + } + } + if (closest == 360) return 0; + return closest; + } + public static String formatVillageList(Village[] villages) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < villages.length; i++) { diff --git a/src/main/java/com/johnymuffin/jvillage/beta/JVillage.java b/src/main/java/com/johnymuffin/jvillage/beta/JVillage.java index 01a16e8..434e829 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/JVillage.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/JVillage.java @@ -16,6 +16,7 @@ import com.johnymuffin.jvillage.beta.maps.JPlayerMap; import com.johnymuffin.jvillage.beta.maps.JVillageMap; import com.johnymuffin.jvillage.beta.models.VCords; +import com.johnymuffin.jvillage.beta.models.VSpawnCords; import com.johnymuffin.jvillage.beta.models.Village; import com.johnymuffin.jvillage.beta.models.chunk.VChunk; import com.johnymuffin.jvillage.beta.models.chunk.VClaim; @@ -584,13 +585,13 @@ public boolean townyImport() { } //Misc - VCords townSpawn = null; + VSpawnCords townSpawn = null; try { - townSpawn = new VCords(town.getSpawn().getBlockX(), town.getSpawn().getBlockY(), town.getSpawn().getBlockZ(), town.getSpawn().getWorld().getName()); + townSpawn = new VSpawnCords(town.getSpawn().getBlockX(), town.getSpawn().getBlockY(), town.getSpawn().getBlockZ(), 0, town.getSpawn().getWorld().getName()); } catch (Exception exception) { log.warning("[" + pluginName + "] Could not find town spawn for town " + newTownName + ". World spawn will be used instead."); logger(Level.WARNING, "Could not find town spawn for town " + newTownName + ". The town spawn will be set to the world spawn."); - townSpawn = new VCords(Bukkit.getWorlds().get(0).getSpawnLocation().getBlockX(), Bukkit.getWorlds().get(0).getSpawnLocation().getBlockY(), Bukkit.getWorlds().get(0).getSpawnLocation().getBlockZ(), Bukkit.getWorlds().get(0).getName()); + townSpawn = new VSpawnCords(Bukkit.getWorlds().get(0).getSpawnLocation().getBlockX(), Bukkit.getWorlds().get(0).getSpawnLocation().getBlockY(), Bukkit.getWorlds().get(0).getSpawnLocation().getBlockZ(), 0, Bukkit.getWorlds().get(0).getName()); } //First chunk from townSpawn @@ -801,15 +802,15 @@ public boolean factionsImport() { //Faction Spawn Location factionSpawn = faction.getHome(); - VCords villageSpawn = null; + VSpawnCords villageSpawn = null; VChunk spawnChunk = null; if(factionSpawn == null) { log.warning("[" + pluginName + "] Faction " + newVillageName + " has no spawn. The world spawn will be used instead."); spawnChunk = new VChunk(Bukkit.getWorlds().get(0).getSpawnLocation()); - villageSpawn = new VCords(Bukkit.getWorlds().get(0).getSpawnLocation()); + villageSpawn = new VSpawnCords(Bukkit.getWorlds().get(0).getSpawnLocation()); } else { spawnChunk = new VChunk(factionSpawn); - villageSpawn = new VCords(factionSpawn); + villageSpawn = new VSpawnCords(factionSpawn); } @@ -902,7 +903,7 @@ public JPlayerData getPlayerData() { return this.playerData; } - public Village generateNewVillage(String townName, UUID owner, VChunk vChunk, VCords townSpawn) { + public Village generateNewVillage(String townName, UUID owner, VChunk vChunk, VSpawnCords townSpawn) { if (!villageNameAvailable(townName)) { return null; } diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/JVillageCMD.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/JVillageCMD.java index 06171e7..12e6374 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/JVillageCMD.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/JVillageCMD.java @@ -38,6 +38,9 @@ public JVillageCMD(JVillage plugin) { registerCommand(new JDemoteCommand(plugin), "demote"); registerCommand(new JPromoteCommand(plugin), "promote"); registerCommand(new JSetSpawnCommand(plugin), "setspawn"); + registerCommand(new JWarpCommand(plugin), "warp"); + registerCommand(new JSetWarpCommand(plugin), "setwarp"); + registerCommand(new JDelWarpCommand(plugin), "delwarp"); registerCommand(new JRenameCommand(plugin), "rename"); registerCommand(new JListCommand(plugin), "list"); registerCommand(new JFlagCommand(plugin), "flag", "flags", "f"); diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JCreateCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JCreateCommand.java index 2d44437..b568747 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JCreateCommand.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JCreateCommand.java @@ -4,7 +4,7 @@ import com.johnymuffin.beta.fundamentals.api.FundamentalsAPI; import com.johnymuffin.jvillage.beta.JVillage; import com.johnymuffin.jvillage.beta.commands.JVBaseCommand; -import com.johnymuffin.jvillage.beta.models.VCords; +import com.johnymuffin.jvillage.beta.models.VSpawnCords; import com.johnymuffin.jvillage.beta.models.Village; import com.johnymuffin.jvillage.beta.models.chunk.ChunkClaimSettings; import com.johnymuffin.jvillage.beta.models.chunk.VChunk; @@ -60,13 +60,15 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, //If string doesn't only contain numbers and letters if (!villageName.matches("[a-zA-Z0-9]+")) { - commandSender.sendMessage(language.getMessage("command_village_create_invalid_name")); + commandSender.sendMessage(language.getMessage("command_village_create_invalid_name") + .replace("%max%", settings.getConfigInteger("settings.town.max-name-length.value").toString())); return true; } //If string is too long if (villageName.length() > settings.getConfigInteger("settings.town.max-name-length.value")) { - commandSender.sendMessage(language.getMessage("command_village_create_invalid_name")); + commandSender.sendMessage(language.getMessage("command_village_create_invalid_name") + .replace("%max%", settings.getConfigInteger("settings.town.max-name-length.value").toString())); return true; } @@ -127,7 +129,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, } } - Village newVillage = new Village(plugin, villageName, UUID.randomUUID(), player.getUniqueId(), vChunk, new VCords(player.getLocation())); + Village newVillage = new Village(plugin, villageName, UUID.randomUUID(), player.getUniqueId(), vChunk, new VSpawnCords(player.getLocation())); plugin.getVillageMap().addVillageToMap(newVillage); //Metadata for first chunk diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JDelWarpCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JDelWarpCommand.java new file mode 100644 index 0000000..7c623bf --- /dev/null +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JDelWarpCommand.java @@ -0,0 +1,68 @@ +package com.johnymuffin.jvillage.beta.commands.village; + +import com.johnymuffin.jvillage.beta.JVillage; +import com.johnymuffin.jvillage.beta.player.VPlayer; +import com.johnymuffin.jvillage.beta.models.Village; +import com.johnymuffin.jvillage.beta.commands.JVBaseCommand; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class JDelWarpCommand extends JVBaseCommand implements CommandExecutor { + + public JDelWarpCommand(JVillage plugin) { + super(plugin); + } + + @Override + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + if (!isAuthorized(commandSender, "jvillage.player.delwarp")) { + commandSender.sendMessage(language.getMessage("no_permission")); + return true; + } + + if (!(commandSender instanceof Player)) { + commandSender.sendMessage(language.getMessage("unavailable_to_console")); + return true; + } + + if (strings.length < 1) { + commandSender.sendMessage(language.getMessage("command_village_delwarp_use")); + return true; + } + + Player player = (Player) commandSender; + VPlayer vPlayer = plugin.getPlayerMap().getPlayer(player.getUniqueId()); + Village village = vPlayer.getSelectedVillage(); + + if (village == null) { + commandSender.sendMessage(language.getMessage("no_village_selected")); + return true; + } + + if (!village.isOwner(vPlayer.getUUID()) && !village.isAssistant(vPlayer.getUUID())) { + String message = language.getMessage("command_village_delwarp_no_permission"); + message = message.replace("%village%", village.getTownName()); + commandSender.sendMessage(message); + return true; + } + + String warpName = strings[0]; + + if (!village.getWarps().containsKey(warpName)) { + commandSender.sendMessage(language.getMessage("command_village_delwarp_not_found") + .replace("%warp%", warpName) + .replace("%village%", village.getTownName())); + return true; + } + + village.removeWarp(warpName); + village.broadcastToTown(language.getMessage("command_village_delwarp_del_broadcast") + .replace("%player%", player.getName()) + .replace("%warp%", warpName) + ); + + return true; + } +} diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JKickCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JKickCommand.java index 447e0e3..1355a99 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JKickCommand.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JKickCommand.java @@ -74,7 +74,9 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, target.setSelectedVillage(null); } village.removeMember(uuid); - commandSender.sendMessage(language.getMessage("command_village_kick_success")); + commandSender.sendMessage(language.getMessage("command_village_kick_success") + .replace("%player%", target.getUsername()) + .replace("%village%", village.getTownName())); //Message the player if they are online Player targetPlayer = getPlayerFromUUID(uuid); diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JRenameCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JRenameCommand.java index d7aba18..9d899a3 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JRenameCommand.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JRenameCommand.java @@ -52,12 +52,14 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, String villageName = strings[0]; if (!villageName.matches("[a-zA-Z0-9]+")) { - commandSender.sendMessage(language.getMessage("command_village_rename_invalid_name")); + commandSender.sendMessage(language.getMessage("command_village_rename_invalid_name") + .replace("%max%", settings.getConfigInteger("settings.town.max-name-length.value").toString())); return true; } if (villageName.length() > settings.getConfigInteger("settings.town.max-name-length.value")) { - commandSender.sendMessage(language.getMessage("command_village_rename_invalid_name")); + commandSender.sendMessage(language.getMessage("command_village_rename_invalid_name") + .replace("%max%", settings.getConfigInteger("settings.town.max-name-length.value").toString())); return true; } diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetSpawnCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetSpawnCommand.java index 4cf2e0e..a514b91 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetSpawnCommand.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetSpawnCommand.java @@ -2,7 +2,7 @@ import com.johnymuffin.jvillage.beta.JVillage; import com.johnymuffin.jvillage.beta.commands.JVBaseCommand; -import com.johnymuffin.jvillage.beta.models.VCords; +import com.johnymuffin.jvillage.beta.models.VSpawnCords; import com.johnymuffin.jvillage.beta.models.Village; import com.johnymuffin.jvillage.beta.models.chunk.VChunk; import com.johnymuffin.jvillage.beta.player.VPlayer; @@ -25,6 +25,11 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, return true; } + if (!(commandSender instanceof Player)) { + commandSender.sendMessage(language.getMessage("unavailable_to_console")); + return true; + } + Player player = (Player) commandSender; VPlayer vPlayer = plugin.getPlayerMap().getPlayer(player.getUniqueId()); Village village = vPlayer.getSelectedVillage(); @@ -49,9 +54,13 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, return true; } - VCords cords = new VCords(player.getLocation()); + VSpawnCords cords = new VSpawnCords(player.getLocation()); village.setTownSpawn(cords); - village.broadcastToTown(player.getDisplayName() + " has set the spawn point to " + cords.toString()); + village.broadcastToTown(language.getMessage("command_village_setspawn_set_broadcast") + .replace("%player%", player.getName()) + .replace("%cords%", cords.toString()) + ); + return true; } } diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java new file mode 100644 index 0000000..4bad8d1 --- /dev/null +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java @@ -0,0 +1,109 @@ +package com.johnymuffin.jvillage.beta.commands.village; + +import com.johnymuffin.beta.fundamentals.api.EconomyAPI; +import com.johnymuffin.beta.fundamentals.api.FundamentalsAPI; +import com.johnymuffin.jvillage.beta.JVillage; +import com.johnymuffin.jvillage.beta.commands.JVBaseCommand; +import com.johnymuffin.jvillage.beta.models.VSpawnCords; +import com.johnymuffin.jvillage.beta.models.Village; +import com.johnymuffin.jvillage.beta.models.chunk.VChunk; +import com.johnymuffin.jvillage.beta.player.VPlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class JSetWarpCommand extends JVBaseCommand implements CommandExecutor { + + public JSetWarpCommand(JVillage plugin) { + super(plugin); + } + + @Override + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + if (!isAuthorized(commandSender, "jvillage.player.setwarp")) { + commandSender.sendMessage(language.getMessage("no_permission")); + return true; + } + + if (!(commandSender instanceof Player)) { + commandSender.sendMessage(language.getMessage("unavailable_to_console")); + return true; + } + + if (strings.length < 1) { + commandSender.sendMessage(language.getMessage("command_village_setwarp_use")); + return true; + } + + Player player = (Player) commandSender; + VPlayer vPlayer = plugin.getPlayerMap().getPlayer(player.getUniqueId()); + Village village = vPlayer.getSelectedVillage(); + + if (village == null) { + commandSender.sendMessage(language.getMessage("no_village_selected")); + return true; + } + + if (!village.isOwner(vPlayer.getUUID()) && !village.isAssistant(vPlayer.getUUID())) { + String message = language.getMessage("command_village_setwarp_no_permission"); + message = message.replace("%village%", village.getTownName()); + commandSender.sendMessage(message); + return true; + } + + String warpName = strings[0]; + + if (!warpName.matches("[a-zA-Z0-9]+")) { + commandSender.sendMessage(language.getMessage("command_village_setwarp_invalid_name") + .replace("%max%", settings.getConfigInteger("settings.warp.max-name-length.value").toString())); + return true; + } + + if (warpName.length() > settings.getConfigInteger("settings.warp.max-name-length.value")) { + commandSender.sendMessage(language.getMessage("command_village_setwarp_invalid_name") + .replace("%max%", settings.getConfigInteger("settings.warp.max-name-length.value").toString())); + return true; + } + + if (village.getWarps().containsKey(warpName)) { + commandSender.sendMessage(language.getMessage("command_village_setwarp_already_exists")); + return true; + } + + VChunk vChunk = new VChunk(player.getLocation()); + if (!village.getClaims().contains(vChunk)) { + String message = language.getMessage("command_village_setwarp_not_in_village"); + message = message.replace("%village%", village.getTownName()); + commandSender.sendMessage(message); + return true; + } + + double creationCost = settings.getConfigDouble("settings.warp.price.amount"); + if (creationCost > 0 && plugin.isFundamentalsEnabled()) { + EconomyAPI.EconomyResult result = FundamentalsAPI.getEconomy().subtractBalance(player.getUniqueId(), creationCost, player.getWorld().getName()); + String message; + switch (result) { + case successful: + commandSender.sendMessage(language.getMessage("command_village_setwarp_payment") + .replace("%amount%", settings.getConfigDouble("settings.warp.price.amount").toString()) + .replace("%warp%", warpName)); + break; + case notEnoughFunds: + commandSender.sendMessage(language.getMessage("command_village_setwarp_insufficient_funds") + .replace("%cost%", String.valueOf(creationCost))); + return true; + default: + message = language.getMessage("unknown_economy_error"); + commandSender.sendMessage(message); + return true; + } + } + VSpawnCords cords = new VSpawnCords(player.getLocation()); + village.addWarp(warpName, cords); + village.broadcastToTown(language.getMessage("command_village_setwarp_set_broadcast") + .replace("%player%", player.getName()) + .replace("%warp%", warpName)); + return true; + } +} diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSpawnCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSpawnCommand.java index c9f0adc..91f48da 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSpawnCommand.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSpawnCommand.java @@ -67,7 +67,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, commandSender.sendMessage(message); return true; } catch (Exception e) { - String message = language.getMessage("command_village_spawm_unsafe"); + String message = language.getMessage("command_village_spawn_unsafe"); message = message.replace("%village%", village.getTownName()); commandSender.sendMessage(message); return true; diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JWarpCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JWarpCommand.java new file mode 100644 index 0000000..a972254 --- /dev/null +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JWarpCommand.java @@ -0,0 +1,73 @@ +package com.johnymuffin.jvillage.beta.commands.village; + +import com.johnymuffin.jvillage.beta.JVillage; +import com.johnymuffin.jvillage.beta.models.Village; +import com.johnymuffin.jvillage.beta.player.VPlayer; +import com.johnymuffin.jvillage.beta.commands.JVBaseCommand; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import static com.johnymuffin.jvillage.beta.JVUtility.getSafeDestination; + +public class JWarpCommand extends JVBaseCommand implements CommandExecutor { + + public JWarpCommand(JVillage plugin) { + super(plugin); + } + + @Override + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + if (!isAuthorized(commandSender, "jvillage.player.warp")) { + commandSender.sendMessage(language.getMessage("no_permission")); + return true; + } + + if (!(commandSender instanceof Player)) { + commandSender.sendMessage(language.getMessage("unavailable_to_console")); + return true; + } + + Player player = (Player) commandSender; + VPlayer vPlayer = plugin.getPlayerMap().getPlayer(player.getUniqueId()); + Village village = vPlayer.getSelectedVillage(); + + if (village == null) { + commandSender.sendMessage(language.getMessage("no_village_selected")); + return true; + } + + if (strings.length == 0) { + commandSender.sendMessage(language.getMessage("command_village_warp_list") + .replace("%village%", village.getTownName())); + commandSender.sendMessage(ChatColor.GRAY + String.join(", ", village.getWarps().keySet())); + return true; + } + + String warpName = strings[0]; + + if (!village.getWarps().containsKey(warpName)) { + commandSender.sendMessage(language.getMessage("command_village_warp_not_found") + .replace("%warp%", warpName) + .replace("%village%", village.getTownName())); + return true; + } + + Location location = village.getWarps().get(warpName).getLocation(); + try { + Location spawnLocation = getSafeDestination(location); + player.teleport(spawnLocation); + commandSender.sendMessage(language.getMessage("command_village_warp_success") + .replace("%warp%", warpName)); + return true; + } catch (Exception e) { + String message = language.getMessage("command_village_warp_unsafe"); + message = message.replace("%warp%", warpName); + commandSender.sendMessage(message); + return true; + } + } +} diff --git a/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageLanguage.java b/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageLanguage.java index 20ab594..8dfe401 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageLanguage.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageLanguage.java @@ -117,6 +117,7 @@ private void loadDefaults() { "\n&8- &7/village autoswitch [on/off] &8- &7Autoswitch town" + "\n&8- &7/village balance [village] &8- &7Shows village balance" + "\n&8- &7/village deposit [village] [amount] &8- &7Deposit money into village bank" + + "\n&8- &7/village warp [name] &8- &7Teleport to a village warp" + "\n&8- &7/village spawn &8- &7Teleport to village spawn"); map.put("command_village_assistant_help", "&cJVillage Assistant Commands" + @@ -126,7 +127,9 @@ private void loadDefaults() { "\n&8- &7/village claim rectangle [chunk radius] &8- &7Claim a rectangle of chunks" + "\n&8- &7/village claim auto &8- &7Claim chunks automatically as you walk (run again to disable)" + "\n&8- &7/village withdraw [village] [amount] &8- &7Withdraw money from village bank" + - "\n&8- &7/village unclaim &8- &7Unclaim the chunk you are standing in"); + "\n&8- &7/village unclaim &8- &7Unclaim the chunk you are standing in" + + "\n&8- &7/village setwarp [name] &8- &7Set a village warp" + + "\n&8- &7/village delwarp [name] &8- &7Delete a village warp"); map.put("command_village_owner_help", "&cJVillage Owner Commands" + "\n&8- &7/village create [name] &8- &7Create a new village" + @@ -198,7 +201,7 @@ private void loadDefaults() { map.put("command_village_delete_broadcast", "&bThe village &9%village% &bhas failed to maintain pace with the world and has fallen into ruin"); map.put("command_village_create_use", "&cSorry, that is invalid. Try /village create [name]"); - map.put("command_village_create_invalid_name", "&cSorry, that is an invalid name. Please only use letters and numbers and less then 16 characters"); + map.put("command_village_create_invalid_name", "&cSorry, that is an invalid name. Please only use letters and numbers and less than %max% characters"); map.put("command_village_create_already_exists", "&cSorry, that village name already exists"); map.put("command_village_create_already_claimed", "&cSorry, that chunk is already claimed"); map.put("command_village_create_success", "&bYou have created the village &9%village%"); @@ -242,7 +245,7 @@ private void loadDefaults() { map.put("command_village_kick_denied", "&cSorry, you are not the owner of &9%village%&c so you can't kick members"); map.put("command_village_kick_message", "&bYou have been kicked from &9%village%"); - map.put("command_village_spawm_unsafe", "&cSorry, teleportation to %village% has been determined to be unsafe"); + map.put("command_village_spawn_unsafe", "&cSorry, teleportation to %village% has been determined to be unsafe"); map.put("command_village_spawn_success", "&bYou have been teleported to the spawn of &9%village%"); map.put("command_village_spawn_not_member", "&cSorry, you are not a member of &9%village%&c so you can't teleport to the spawn"); @@ -254,7 +257,7 @@ private void loadDefaults() { map.put("command_village_rename_use", "&cSorry, that is invalid. Try /village rename [name]"); map.put("command_village_rename_not_owner", "&cSorry, you are not the owner of &9%village%&c so you can't change the name"); - map.put("command_village_rename_invalid_name", "&cSorry, that is an invalid name. Please only use letters and numbers and less then 22 characters"); + map.put("command_village_rename_invalid_name", "&cSorry, that is an invalid name. Please only use letters and numbers and less than %max% characters"); map.put("command_village_rename_already_exists", "&cSorry, that village name already exists"); map.put("command_village_rename_broadcast", "&bThe village &9%village% &bhas been renamed to &9%new_village%"); map.put("command_village_rename_success", "&bYou have changed the name of &9%village%"); @@ -277,6 +280,27 @@ private void loadDefaults() { map.put("command_village_setspawn_not_owner", "&cSorry, you are not the owner of &9%village%&c so you can't change the spawn"); map.put("command_village_setspawn_not_in_village", "&cSorry, that location is not within an area claimed by &9%village%"); + map.put("command_village_setspawn_set_broadcast", "&9%player% &bhas set the spawn point to &9%cords%"); + + map.put("command_village_setwarp_use", "&cSorry, that is invalid. Try /village setwarp [name]"); + map.put("command_village_setwarp_no_permission", "&cSorry, you don't have permission to set warps in &9%village%"); + map.put("command_village_setwarp_not_in_village", "&cSorry, that location is not within an area claimed by &9%village%"); + map.put("command_village_setwarp_invalid_name", "&cSorry, that is an invalid name. Please only use letters and numbers and less than %max% characters"); + map.put("command_village_setwarp_already_exists", "&cSorry, that warp name already exists"); + map.put("command_village_setwarp_insufficient_funds", "&cSorry, you don't have enough money to set a warp. It costs $%cost%"); + map.put("command_village_setwarp_payment", "&bYou have paid &9$%amount% &bto set the warp &9%warp%"); + map.put("command_village_setwarp_set_broadcast", "&9%player% &bhas set the warp &9%warp%"); + + + map.put("command_village_delwarp_use", "&cSorry, that is invalid. Try /village delwarp [name]"); + map.put("command_village_delwarp_no_permission", "&cSorry, you don't have permission to delete warps in &9%village%"); + map.put("command_village_delwarp_not_found", "&cSorry, the warp &9%warp% &cdoes not exist in &9%village%"); + map.put("command_village_delwarp_del_broadcast", "&9%player% &bhas deleted the warp &9%warp%"); + + map.put("command_village_warp_list", "&bWarp list of &9%village%:"); + map.put("command_village_warp_success", "&bYou have been teleported to &9%warp%"); + map.put("command_village_warp_unsafe", "&cSorry, teleportation to %warp% has been determined to be unsafe"); + map.put("command_village_warp_not_found", "&cSorry, the warp &9%warp% &cdoes not exist in &9%village%"); //Village Economy diff --git a/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageSettings.java b/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageSettings.java index 4c4a716..a2ededa 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageSettings.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageSettings.java @@ -30,6 +30,12 @@ private void write() { generateConfigOption("settings.town-claim-outpost.price.amount", 500); generateConfigOption("settings.town-claim-outpost.price.info", "This is the price to claim an outpost. Set to 0 to disable."); + generateConfigOption("settings.warp.price.amount", 500); + generateConfigOption("settings.warp.price.info", "This is the price to set a village warp. Set to 0 to disable."); + + generateConfigOption("settings.warp.max-name-length.value", 20); + generateConfigOption("settings.warp.max-name-length.info", "This is the maximum length of a town warp name."); + generateConfigOption("settings.resident.maximum-towns-owned.value", 10); generateConfigOption("settings.resident.maximum-towns-owned.info", "This is the maximum number of towns a resident can own. Set to 0 to disable."); diff --git a/src/main/java/com/johnymuffin/jvillage/beta/models/VSpawnCords.java b/src/main/java/com/johnymuffin/jvillage/beta/models/VSpawnCords.java new file mode 100644 index 0000000..b02064b --- /dev/null +++ b/src/main/java/com/johnymuffin/jvillage/beta/models/VSpawnCords.java @@ -0,0 +1,52 @@ +package com.johnymuffin.jvillage.beta.models; + +import com.johnymuffin.jvillage.beta.JVUtility; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.json.simple.JSONObject; + +public class VSpawnCords extends VCords { + + private int yaw; + + public VSpawnCords(int x, int y, int z, int yaw, String worldName) { + super(x, y, z, worldName); + this.yaw = yaw; + } + + public VSpawnCords(Location location) { + this(location.getBlockX(), location.getBlockY(), location.getBlockZ(), JVUtility.closestYaw(location.getYaw()), location.getWorld().getName()); + } + + public VSpawnCords(JSONObject jsonObject) { + super(Long.valueOf(String.valueOf(jsonObject.get("x"))).intValue(), + Long.valueOf(String.valueOf(jsonObject.get("y"))).intValue(), + Long.valueOf(String.valueOf(jsonObject.get("z"))).intValue(), + (String) jsonObject.get("world")); + if (jsonObject.get("yaw") == null) { + yaw = 0; + } else { + yaw = Long.valueOf(String.valueOf(jsonObject.get("yaw"))).intValue(); + } + } + + @Override + public JSONObject getJsonObject() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("x", super.getX()); + jsonObject.put("y", super.getY()); + jsonObject.put("z", super.getZ()); + jsonObject.put("yaw", yaw); + jsonObject.put("world", super.getWorldName()); + return jsonObject; + } + + public int getYaw() { + return yaw; + } + + @Override + public Location getLocation() { + return new Location(Bukkit.getWorld(super.getWorldName()), super.getX(), super.getY(), super.getZ(), yaw, 0); + } +} diff --git a/src/main/java/com/johnymuffin/jvillage/beta/models/Village.java b/src/main/java/com/johnymuffin/jvillage/beta/models/Village.java index 618217c..89a7736 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/models/Village.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/models/Village.java @@ -13,6 +13,8 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.TreeMap; +import java.util.Map; import java.util.UUID; public class Village implements ClaimManager { @@ -22,7 +24,8 @@ public class Village implements ClaimManager { private final ArrayList members = new ArrayList(); private final ArrayList assistants = new ArrayList(); private UUID owner; - private VCords townSpawn; + private VSpawnCords townSpawn; + private TreeMap warps = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); private boolean modified = false; @@ -42,7 +45,7 @@ private void initializeFlags() { } } - public Village(JVillage plugin, String townName, UUID townUUID, UUID owner, VChunk vChunk, VCords townSpawn) { + public Village(JVillage plugin, String townName, UUID townUUID, UUID owner, VChunk vChunk, VSpawnCords townSpawn) { this.plugin = plugin; this.townName = townName; this.townUUID = townUUID; @@ -60,7 +63,14 @@ public Village(JVillage plugin, UUID uuid, JSONObject object) { this.townName = String.valueOf(object.get("name")); this.townUUID = uuid; // Ignore UUID in JSON file and use the one from the file name this.owner = UUID.fromString(String.valueOf(object.get("owner"))); - this.townSpawn = new VCords((JSONObject) object.get("townSpawn")); + this.townSpawn = new VSpawnCords((JSONObject) object.get("townSpawn")); + JSONObject warps = (JSONObject) object.get("warps"); + for (Object warp : warps.keySet()) { + String warpName = warp.toString(); + VSpawnCords cords = new VSpawnCords((JSONObject) warps.get(warpName)); + this.warps.put(warpName, cords); + } + JSONArray members = (JSONArray) object.get("members"); for (Object member : members) { this.members.add(UUID.fromString(String.valueOf(member))); @@ -214,6 +224,12 @@ public JSONObject getJsonObject() { object.put("townSpawn", this.townSpawn.getJsonObject()); object.put("creationTime", this.creationTime); object.put("balance", this.balance); + + JSONObject warps = new JSONObject(); + for (Map.Entry entry : this.warps.entrySet()) { + warps.put(entry.getKey(), entry.getValue().getJsonObject()); + } + object.put("warps", warps); return object; } @@ -372,15 +388,29 @@ public void setOwner(UUID uuid) { // return claims; // } - public VCords getTownSpawn() { + public VSpawnCords getTownSpawn() { return townSpawn; } - public void setTownSpawn(VCords cords) { + public void setTownSpawn(VSpawnCords cords) { modified = true; // Indicate that the village has been modified and needs to be saved townSpawn = cords; } + public TreeMap getWarps() { + return this.warps; + } + + public void addWarp(String name, VSpawnCords cords) { + modified = true; + warps.put(name, cords); + } + + public void removeWarp(String name) { + modified = true; + warps.remove(name); + } + public boolean isMember(UUID uuid) { if (members.contains(uuid)) { return true; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9d936aa..173da34 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -92,6 +92,7 @@ permissions: jvillage.player.promote: true jvillage.player.setowner: true jvillage.player.spawn: true + jvillage.player.warp: true jvillage.player.kick: true jvillage.player.unclaim: true jvillage.player.claim: true @@ -127,6 +128,18 @@ permissions: description: Allows access to JVillage player setspawn commands default: true + jvillage.player.warp: + description: Allows access to JVillage player warp command + default: true + + jvillage.player.setwarp: + description: Allows access to JVillage player setwarp command + default: true + + jvillage.player.delwarp: + description: Allows access to JVillage player delwarp command + default: true + jvillage.player.demote: description: Allows access to JVillage player demote command default: true From 59d3a2642d146d6ab8417f3324c5b71b11077ceb Mon Sep 17 00:00:00 2001 From: zavdav <157126752+zavdav@users.noreply.github.com> Date: Wed, 29 Jan 2025 17:12:01 +0100 Subject: [PATCH 2/3] Take needed balance for /setwarp from village and allow refunding of deleted warps --- .../commands/village/JDelWarpCommand.java | 3 +++ .../commands/village/JSetWarpCommand.java | 25 ++++--------------- .../beta/config/JVillageSettings.java | 3 +++ 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JDelWarpCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JDelWarpCommand.java index 7c623bf..892ef07 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JDelWarpCommand.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JDelWarpCommand.java @@ -57,6 +57,9 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, return true; } + if (settings.getConfigBoolean("settings.warp.refund-deleted.enabled")) { + village.addBalance(settings.getConfigDouble("settings.warp.price.amount")); + } village.removeWarp(warpName); village.broadcastToTown(language.getMessage("command_village_delwarp_del_broadcast") .replace("%player%", player.getName()) diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java index 4bad8d1..bd44a56 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java @@ -1,7 +1,5 @@ package com.johnymuffin.jvillage.beta.commands.village; -import com.johnymuffin.beta.fundamentals.api.EconomyAPI; -import com.johnymuffin.beta.fundamentals.api.FundamentalsAPI; import com.johnymuffin.jvillage.beta.JVillage; import com.johnymuffin.jvillage.beta.commands.JVBaseCommand; import com.johnymuffin.jvillage.beta.models.VSpawnCords; @@ -80,24 +78,11 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, } double creationCost = settings.getConfigDouble("settings.warp.price.amount"); - if (creationCost > 0 && plugin.isFundamentalsEnabled()) { - EconomyAPI.EconomyResult result = FundamentalsAPI.getEconomy().subtractBalance(player.getUniqueId(), creationCost, player.getWorld().getName()); - String message; - switch (result) { - case successful: - commandSender.sendMessage(language.getMessage("command_village_setwarp_payment") - .replace("%amount%", settings.getConfigDouble("settings.warp.price.amount").toString()) - .replace("%warp%", warpName)); - break; - case notEnoughFunds: - commandSender.sendMessage(language.getMessage("command_village_setwarp_insufficient_funds") - .replace("%cost%", String.valueOf(creationCost))); - return true; - default: - message = language.getMessage("unknown_economy_error"); - commandSender.sendMessage(message); - return true; - } + if (creationCost > 0 && !village.hasEnough(creationCost)) { + String message = language.getMessage("command_village_setwarp_insufficient_funds") + .replace("%cost%", String.valueOf(creationCost)); + commandSender.sendMessage(message); + return true; } VSpawnCords cords = new VSpawnCords(player.getLocation()); village.addWarp(warpName, cords); diff --git a/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageSettings.java b/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageSettings.java index a2ededa..3d88083 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageSettings.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/config/JVillageSettings.java @@ -35,6 +35,9 @@ private void write() { generateConfigOption("settings.warp.max-name-length.value", 20); generateConfigOption("settings.warp.max-name-length.info", "This is the maximum length of a town warp name."); + + generateConfigOption("settings.warp.refund-deleted.enabled", true); + generateConfigOption("settings.warp.refund-deleted.info", "If true, the price to set a warp will be refunded when deleting a warp."); generateConfigOption("settings.resident.maximum-towns-owned.value", 10); generateConfigOption("settings.resident.maximum-towns-owned.info", "This is the maximum number of towns a resident can own. Set to 0 to disable."); From 2a45194d799b1b27127760beeb190211ce299c98 Mon Sep 17 00:00:00 2001 From: zavdav <157126752+zavdav@users.noreply.github.com> Date: Wed, 29 Jan 2025 17:31:39 +0100 Subject: [PATCH 3/3] bruh --- .../beta/commands/village/JSetWarpCommand.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java index bd44a56..9133e40 100644 --- a/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java +++ b/src/main/java/com/johnymuffin/jvillage/beta/commands/village/JSetWarpCommand.java @@ -78,11 +78,14 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, } double creationCost = settings.getConfigDouble("settings.warp.price.amount"); - if (creationCost > 0 && !village.hasEnough(creationCost)) { - String message = language.getMessage("command_village_setwarp_insufficient_funds") - .replace("%cost%", String.valueOf(creationCost)); - commandSender.sendMessage(message); - return true; + if (creationCost > 0) { + if (!village.hasEnough(creationCost)) { + String message = language.getMessage("command_village_setwarp_insufficient_funds") + .replace("%cost%", String.valueOf(creationCost)); + commandSender.sendMessage(message); + return true; + } + village.subtractBalance(creationCost); } VSpawnCords cords = new VSpawnCords(player.getLocation()); village.addWarp(warpName, cords);