Skip to content

Commit

Permalink
give travel cost to town/nation
Browse files Browse the repository at this point in the history
  • Loading branch information
ewof committed Apr 26, 2024
1 parent 6ea361b commit a374495
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()));
}
Expand All @@ -53,30 +50,30 @@ public static void onReload(CommandSender player)
@Syntax("set <property> [<value>]")
@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 <property> <value>")
@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;
}
Expand All @@ -89,24 +86,22 @@ 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);
}

@Subcommand("travel")
@Syntax("<town> <waypoint> <plot name>")
@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();
Expand All @@ -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;
}

Expand All @@ -141,16 +137,19 @@ 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;
}

TownyAPI townyAPI = TownyAPI.getInstance();

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;
}
Expand All @@ -168,23 +167,33 @@ 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()));
}
}

private static void teleport(@Nonnull final Player player, @Nonnull Location loc, boolean travelWithVehicle) {
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"",
Expand All @@ -18,7 +33,7 @@ public enum ConfigNodes {
"# | Restrictions | #",
"# +------------------------------------------------------+ #",
"############################################################",
""),
""),
WAYPOINTS_RESTRICTIONS_MAX_DISTANCE(
"waypoints.restrictions.max_distance",
"2700",
Expand All @@ -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;
Expand All @@ -53,8 +67,7 @@ public enum ConfigNodes {
*
* @return The root for a config option
*/
public String getRoot()
{
public String getRoot() {

return Root;
}
Expand All @@ -64,8 +77,7 @@ public String getRoot()
*
* @return The default value for a config path
*/
public String getDefault()
{
public String getDefault() {

return Default;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions src/main/resources/lang/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit a374495

Please sign in to comment.