From a3744955d9c04871ebd93f22911a7ae32ee9e6f3 Mon Sep 17 00:00:00 2001 From: ewof Date: Fri, 26 Apr 2024 17:11:15 -0400 Subject: [PATCH] give travel cost to town/nation --- .../commands/TownyWaypointsCommand.java | 67 +++++++++++-------- .../townywaypoints/settings/ConfigNodes.java | 31 ++++++--- .../settings/TownyWaypointsSettings.java | 13 ++-- src/main/resources/lang/en-US.yml | 1 + 4 files changed, 67 insertions(+), 45 deletions(-) diff --git a/src/main/java/net/mvndicraft/townywaypoints/commands/TownyWaypointsCommand.java b/src/main/java/net/mvndicraft/townywaypoints/commands/TownyWaypointsCommand.java index 08f679f..c60fc9c 100644 --- a/src/main/java/net/mvndicraft/townywaypoints/commands/TownyWaypointsCommand.java +++ b/src/main/java/net/mvndicraft/townywaypoints/commands/TownyWaypointsCommand.java @@ -1,14 +1,13 @@ package net.mvndicraft.townywaypoints.commands; import javax.annotation.Nonnull; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -import com.palmergames.bukkit.towny.Towny; import com.palmergames.bukkit.towny.TownyAPI; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.TownBlock; @@ -32,19 +31,17 @@ import net.mvndicraft.townywaypoints.util.TownBlockMetaDataController; @CommandAlias("townywaypoints|twaypoints|twp") -public class TownyWaypointsCommand extends BaseCommand -{ +public class TownyWaypointsCommand extends BaseCommand { @Default @Description("Lists the version of the plugin") - public static void onTownyWaypoints(CommandSender player) - { + public static void onTownyWaypoints(CommandSender player) { player.sendMessage(Component.text(TownyWaypoints.getInstance().toString())); } - @Subcommand("reload") @CommandPermission(TownyWaypoints.ADMIN_PERMISSION) + @Subcommand("reload") + @CommandPermission(TownyWaypoints.ADMIN_PERMISSION) @Description("Reloads the plugin config and locales.") - public static void onReload(CommandSender player) - { + public static void onReload(CommandSender player) { Settings.loadConfigAndLang(); Messaging.sendMsg(player, Translatable.of("townywaypoints_msg_reload", TownyWaypoints.getInstance().getName())); } @@ -53,30 +50,30 @@ public static void onReload(CommandSender player) @Syntax("set []") @CommandCompletion("@open_statuses @nothing") @Description("Change which people the plot is open to teleports from.") - public static void onSetOpen(Player player, String status) - { - if (!player.hasPermission(TownyWaypoints.ADMIN_PERMISSION) && !player.hasPermission("towny.command.town.toggle.public")) { + public static void onSetOpen(Player player, String status) { + if (!player.hasPermission(TownyWaypoints.ADMIN_PERMISSION) + && !player.hasPermission("towny.command.town.toggle.public")) { Messaging.sendErrorMsg(player, Translatable.of("msg_err_waypoint_set_open_insufficient_permission")); return; } TownBlock townBlock = TownyAPI.getInstance().getTownBlock(player); if (townBlock == null) { - Messaging.sendErrorMsg(player,Translatable.of("msg_err_not_in_townblock")); + Messaging.sendErrorMsg(player, Translatable.of("msg_err_not_in_townblock")); return; } TownBlockMetaDataController.setSdf(townBlock, TownBlockMetaDataController.statusKey, status); - Messaging.sendMsg(player,Translatable.of("msg_status_set",status)); + Messaging.sendMsg(player, Translatable.of("msg_status_set", status)); } @Subcommand("set spawn") @Syntax("set ") @Description("Set the block a player gets teleported to on arival for a waypoint plot.") - public static void onSetSpawn(Player player) - { - if (!player.hasPermission(TownyWaypoints.ADMIN_PERMISSION) && !player.hasPermission("towny.command.town.set.spawn")) { + public static void onSetSpawn(Player player) { + if (!player.hasPermission(TownyWaypoints.ADMIN_PERMISSION) + && !player.hasPermission("towny.command.town.set.spawn")) { Messaging.sendErrorMsg(player, Translatable.of("msg_err_waypoint_set_spawn_insufficient_permission")); return; } @@ -89,7 +86,7 @@ public static void onSetSpawn(Player player) Location loc = player.getLocation(); - Messaging.sendMsg(player, Translatable.of("msg_spawn_set",loc.toString())); + Messaging.sendMsg(player, Translatable.of("msg_spawn_set", loc.toString())); TownBlockMetaDataController.setSpawn(townBlock, loc); } @@ -97,16 +94,14 @@ public static void onSetSpawn(Player player) @Syntax(" ") @CommandCompletion("@waypointed_towns @town_waypoints @waypoint_plot_names @nothing") @Description("Travel between different waypoints.") - public static void onTravel(Player player, String townName, String waypointName, String waypointPlotName) - { + public static void onTravel(Player player, String townName, String waypointName, String waypointPlotName) { Town town = TownyAPI.getInstance().getTown(townName); if (town == null) return; TownBlock townBlock = null; - for (TownBlock _townBlock:town.getTownBlocks()) - { + for (TownBlock _townBlock : town.getTownBlocks()) { String plotName = _townBlock.getName(); if (plotName.equals("")) plotName = Translatable.of("townywaypoints_plot_unnamed").defaultLocale(); @@ -130,7 +125,8 @@ public static void onTravel(Player player, String townName, String waypointName, plotName = Translatable.of("townywaypoints_plot_unnamed").defaultLocale(); if (!admin && TownyWaypoints.getEconomy().getBalance(player) - travelcost < 0) { - Messaging.sendErrorMsg(player, Translatable.of("msg_err_waypoint_travel_insufficient_funds", plotName, travelcost)); + Messaging.sendErrorMsg(player, + Translatable.of("msg_err_waypoint_travel_insufficient_funds", plotName, travelcost)); return; } @@ -141,8 +137,10 @@ public static void onTravel(Player player, String townName, String waypointName, return; } - if (!admin && (TownyWaypointsSettings.getMaxDistance() != -1 && player.getLocation().distance(loc) > TownyWaypointsSettings.getMaxDistance())) { - Messaging.sendErrorMsg(player, Translatable.of("msg_err_waypoint_travel_too_far", townBlock.getName(), TownyWaypointsSettings.getMaxDistance())); + if (!admin && (TownyWaypointsSettings.getMaxDistance() != -1 + && player.getLocation().distance(loc) > TownyWaypointsSettings.getMaxDistance())) { + Messaging.sendErrorMsg(player, Translatable.of("msg_err_waypoint_travel_too_far", townBlock.getName(), + TownyWaypointsSettings.getMaxDistance())); return; } @@ -150,7 +148,8 @@ public static void onTravel(Player player, String townName, String waypointName, TownBlock playerTownBlock = townyAPI.getTownBlock(player); - if (!admin && (playerTownBlock == null || TownyWaypointsSettings.getPeerToPeer() && !playerTownBlock.getType().getName().equals(waypointName))) { + if (!admin && (playerTownBlock == null || TownyWaypointsSettings.getPeerToPeer() + && !playerTownBlock.getType().getName().equals(waypointName))) { Messaging.sendErrorMsg(player, Translatable.of("msg_err_waypoint_p2p", waypointName, waypointName)); return; } @@ -168,10 +167,20 @@ public static void onTravel(Player player, String townName, String waypointName, Messaging.sendMsg(player, Translatable.of("msg_waypoint_travel_warmup_cost", travelcost)); teleport(player, loc, waypoint.travelWithVehicle()); + double splitCost = travelcost * TownyWaypointsSettings.getSplit(); + + town.getAccount().deposit(town.hasNation() ? splitCost : travelcost, + Translatable.of("msg_deposit_reason").toString()); + + if (town.hasNation()) + town.getNationOrNull().getAccount().deposit(splitCost, + Translatable.of("msg_deposit_reason").toString()); + if (!CooldownTimerTask.hasCooldown(player.getName(), "waypoint")) CooldownTimerTask.addCooldownTimer(player.getName(), "waypoint", TownyWaypointsSettings.getCooldown()); } else { - Messaging.sendErrorMsg(player, Translatable.of("msg_err_waypoint_travel_cooldown", cooldown, townBlock.getName())); + Messaging.sendErrorMsg(player, + Translatable.of("msg_err_waypoint_travel_cooldown", cooldown, townBlock.getName())); } } @@ -179,12 +188,12 @@ private static void teleport(@Nonnull final Player player, @Nonnull Location loc Entity vehicle = player.getVehicle(); boolean needToTpVehicle = travelWithVehicle && player.isInsideVehicle() && vehicle != null; - if(needToTpVehicle) { + if (needToTpVehicle) { vehicle.eject(); PaperLib.teleportAsync(vehicle, loc, TeleportCause.COMMAND); } PaperLib.teleportAsync(player, loc, TeleportCause.COMMAND); - if(needToTpVehicle) + if (needToTpVehicle) TownyWaypoints.getScheduler().runTask(loc, () -> vehicle.addPassenger(player)); } } diff --git a/src/main/java/net/mvndicraft/townywaypoints/settings/ConfigNodes.java b/src/main/java/net/mvndicraft/townywaypoints/settings/ConfigNodes.java index dbb7bb5..79ec13f 100644 --- a/src/main/java/net/mvndicraft/townywaypoints/settings/ConfigNodes.java +++ b/src/main/java/net/mvndicraft/townywaypoints/settings/ConfigNodes.java @@ -8,7 +8,22 @@ public enum ConfigNodes { LANGUAGE("language", "en_US.yml", "# The language file you wish to use."), - WAYPOINTS("waypoints","",""), + WAYPOINTS("waypoints", "", ""), + WAYPOINTS_ECONOMY( + "waypoints.economy", + "", + "", + "############################################################", + "# +------------------------------------------------------+ #", + "# | Economy | #", + "# +------------------------------------------------------+ #", + "############################################################", + ""), + WAYPOINTS_ECONOMY_SPLIT( + "waypoints.economy.split", + "0.5", + "", + "# The percentage of the travel cost that gets added to the waypoints town bank, the rest goes to the nation (if it has no nation then 100% goes to the town)."), WAYPOINTS_RESTRICTIONS( "waypoints.restrictions", "", @@ -18,7 +33,7 @@ public enum ConfigNodes { "# | Restrictions | #", "# +------------------------------------------------------+ #", "############################################################", - ""), + ""), WAYPOINTS_RESTRICTIONS_MAX_DISTANCE( "waypoints.restrictions.max_distance", "2700", @@ -40,8 +55,7 @@ public enum ConfigNodes { private final String Default; private final String[] comments; - ConfigNodes(String root, String def, String... comments) - { + ConfigNodes(String root, String def, String... comments) { this.Root = root; this.Default = def; @@ -53,8 +67,7 @@ public enum ConfigNodes { * * @return The root for a config option */ - public String getRoot() - { + public String getRoot() { return Root; } @@ -64,8 +77,7 @@ public String getRoot() * * @return The default value for a config path */ - public String getDefault() - { + public String getDefault() { return Default; } @@ -75,8 +87,7 @@ public String getDefault() * * @return The comments for a config path */ - public String[] getComments() - { + public String[] getComments() { if (comments != null) { return comments; } diff --git a/src/main/java/net/mvndicraft/townywaypoints/settings/TownyWaypointsSettings.java b/src/main/java/net/mvndicraft/townywaypoints/settings/TownyWaypointsSettings.java index f729462..0c83c20 100644 --- a/src/main/java/net/mvndicraft/townywaypoints/settings/TownyWaypointsSettings.java +++ b/src/main/java/net/mvndicraft/townywaypoints/settings/TownyWaypointsSettings.java @@ -1,18 +1,19 @@ package net.mvndicraft.townywaypoints.settings; public class TownyWaypointsSettings { - public static int getMaxDistance() - { + public static int getMaxDistance() { return Settings.getInt(ConfigNodes.WAYPOINTS_RESTRICTIONS_MAX_DISTANCE); } - public static int getCooldown() - { + public static int getCooldown() { return Settings.getInt(ConfigNodes.WAYPOINTS_RESTRICTIONS_COOLDOWN); } - public static boolean getPeerToPeer() - { + public static boolean getPeerToPeer() { return Settings.getBoolean(ConfigNodes.WAYPOINTS_RESTRICTIONS_PEER_TO_PEER); } + + public static double getSplit() { + return Settings.getDouble(ConfigNodes.WAYPOINTS_ECONOMY_SPLIT); + } } diff --git a/src/main/resources/lang/en-US.yml b/src/main/resources/lang/en-US.yml index edf7b3d..d2d791b 100644 --- a/src/main/resources/lang/en-US.yml +++ b/src/main/resources/lang/en-US.yml @@ -40,3 +40,4 @@ msg_err_waypoint_travel_cooldown: 'You must wait %ds before traveling to %s!' msg_err_waypoint_p2p: 'You must be standing in a %s waypoint plot to travel to another %s waypoint!' msg_err_waypoint_set_spawn_insufficient_permission: 'You do not have permission to set the spawn of this waypoint!' msg_err_waypoint_set_open_insufficient_permission: 'You do not have permission to set the open status of this waypoint!' +msg_deposit_reason: 'Waypoint travel'