Skip to content

Commit

Permalink
Added Spawnutil (#3505)
Browse files Browse the repository at this point in the history
* Testing out a SpawnUtil to replace redundancies found in /res, /t and /n spawn commands.

* Remove commented-out-code.

* Sort out merge conflict in language files.

* Remove more commented-out code.

* Remove debug code.

* Add isTownyAdmin node to SpawnType enum.

* Add/improve comments.

* Rename Spawn() to sendToTownySpawn(), formatted SpawnUtil, swap Boolean
for boolean.

* Small adjustments to SpawnUtil.
  • Loading branch information
LlmDl authored Nov 21, 2019
1 parent 6cfc3b8 commit f66a98a
Show file tree
Hide file tree
Showing 17 changed files with 2,420 additions and 2,543 deletions.
6 changes: 5 additions & 1 deletion resources/chinese.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,8 @@ msg_err_cannot_toggle_pvp_x_seconds_remaining: 'You cannot toggle PVP, %s second
msg_err_cannot_spawn_x_seconds_remaining: 'You cannot spawn for another %s seconds.'

# Added in 0.63
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_err_x_spawn_disallowed_from_x: '&c%s spawn is not allowed from %s.'
msg_the_wilderness: 'the Wilderness'
msg_enemy_areas: 'Enemy areas'
msg_neutral_towns: 'Neutral towns'
8 changes: 6 additions & 2 deletions resources/english.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Towny
version: 0.63
language: english
author: ElgarL
website: 'http://towny.palmergames.com/'
website: 'http://townyadvanced.github.io/'
description: >
Language file for all game messages. Do not alter this file.
If you wish to change any of the entries, make a copy named something else.
Expand Down Expand Up @@ -1017,4 +1017,8 @@ msg_err_cannot_toggle_pvp_x_seconds_remaining: 'You cannot toggle PVP, %s second
msg_err_cannot_spawn_x_seconds_remaining: 'You cannot spawn for another %s seconds.'

# Added in 0.63
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_err_x_spawn_disallowed_from_x: '&c%s spawn is not allowed from %s.'
msg_the_wilderness: 'the Wilderness'
msg_enemy_areas: 'Enemy areas'
msg_neutral_towns: 'Neutral towns'
2,020 changes: 1,012 additions & 1,008 deletions resources/es-mx.yml

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion resources/french.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,8 @@ msg_err_cannot_toggle_pvp_x_seconds_remaining: 'You cannot toggle PVP, %s second
msg_err_cannot_spawn_x_seconds_remaining: 'You cannot spawn for another %s seconds.'

# Added in 0.63
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_err_x_spawn_disallowed_from_x: '&c%s spawn is not allowed from %s.'
msg_the_wilderness: 'the Wilderness'
msg_enemy_areas: 'Enemy areas'
msg_neutral_towns: 'Neutral towns'
6 changes: 5 additions & 1 deletion resources/german.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1020,4 +1020,8 @@ msg_err_cannot_toggle_pvp_x_seconds_remaining: 'You cannot toggle PVP, %s second
msg_err_cannot_spawn_x_seconds_remaining: 'You cannot spawn for another %s seconds.'

# Added in 0.63
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_err_x_spawn_disallowed_from_x: '&c%s spawn is not allowed from %s.'
msg_the_wilderness: 'the Wilderness'
msg_enemy_areas: 'Enemy areas'
msg_neutral_towns: 'Neutral towns'
6 changes: 5 additions & 1 deletion resources/italian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,8 @@ msg_err_cannot_toggle_pvp_x_seconds_remaining: 'You cannot toggle PVP, %s second
msg_err_cannot_spawn_x_seconds_remaining: 'You cannot spawn for another %s seconds.'

# Added in 0.63
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_err_x_spawn_disallowed_from_x: '&c%s spawn is not allowed from %s.'
msg_the_wilderness: 'the Wilderness'
msg_enemy_areas: 'Enemy areas'
msg_neutral_towns: 'Neutral towns'
1,870 changes: 937 additions & 933 deletions resources/korean.yml

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion resources/norwegian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,8 @@ msg_err_cannot_toggle_pvp_x_seconds_remaining: 'You cannot toggle PVP, %s second
msg_err_cannot_spawn_x_seconds_remaining: 'You cannot spawn for another %s seconds.'

# Added in 0.63
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_err_x_spawn_disallowed_from_x: '&c%s spawn is not allowed from %s.'
msg_the_wilderness: 'the Wilderness'
msg_enemy_areas: 'Enemy areas'
msg_neutral_towns: 'Neutral towns'
6 changes: 5 additions & 1 deletion resources/russian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,8 @@ msg_err_cannot_toggle_pvp_x_seconds_remaining: 'You cannot toggle PVP, %s second
msg_err_cannot_spawn_x_seconds_remaining: 'You cannot spawn for another %s seconds.'

# Added in 0.63
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_err_x_spawn_disallowed_from_x: '&c%s spawn is not allowed from %s.'
msg_the_wilderness: 'the Wilderness'
msg_enemy_areas: 'Enemy areas'
msg_neutral_towns: 'Neutral towns'
6 changes: 5 additions & 1 deletion resources/spanish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,8 @@ msg_err_cannot_toggle_pvp_x_seconds_remaining: 'You cannot toggle PVP, %s second
msg_err_cannot_spawn_x_seconds_remaining: 'You cannot spawn for another %s seconds.'

# Added in 0.63
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_plot_set_to_nfs: 'Plot is no longer for sale.'
msg_err_x_spawn_disallowed_from_x: '&c%s spawn is not allowed from %s.'
msg_the_wilderness: 'the Wilderness'
msg_enemy_areas: 'Enemy areas'
msg_neutral_towns: 'Neutral towns'
2 changes: 2 additions & 0 deletions src/com/palmergames/bukkit/towny/Towny.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.palmergames.bukkit.towny.permissions.VaultPermSource;
import com.palmergames.bukkit.towny.regen.TownyRegenAPI;
import com.palmergames.bukkit.towny.utils.PlayerCacheUtil;
import com.palmergames.bukkit.towny.utils.SpawnUtil;
import com.palmergames.bukkit.towny.war.flagwar.TownyWar;
import com.palmergames.bukkit.towny.war.flagwar.listeners.TownyWarBlockListener;
import com.palmergames.bukkit.towny.war.flagwar.listeners.TownyWarCustomListener;
Expand Down Expand Up @@ -136,6 +137,7 @@ public void onEnable() {
TownyFormatter.initialize(this);
TownyRegenAPI.initialize(this);
PlayerCacheUtil.initialize(this);
SpawnUtil.initialize(this);
TownyPerms.initialize(this);
InviteHandler.initialize(this);
ConfirmationHandler.initialize(this);
Expand Down
188 changes: 4 additions & 184 deletions src/com/palmergames/bukkit/towny/command/NationCommand.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.palmergames.bukkit.towny.command;

import com.earth2me.essentials.Teleport;
import com.earth2me.essentials.User;
import com.google.common.collect.ListMultimap;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.TownyEconomyHandler;
import com.palmergames.bukkit.towny.TownyFormatter;
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownySettings;
import com.palmergames.bukkit.towny.TownyTimerHandler;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.confirmations.ConfirmationHandler;
import com.palmergames.bukkit.towny.confirmations.ConfirmationType;
Expand All @@ -26,34 +23,27 @@
import com.palmergames.bukkit.towny.invites.exceptions.TooManyInvitesException;
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.NationSpawnLevel;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.SpawnType;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
import com.palmergames.bukkit.towny.object.TownBlockType;
import com.palmergames.bukkit.towny.object.TownyEconomyObject;
import com.palmergames.bukkit.towny.object.inviteobjects.NationAllyNationInvite;
import com.palmergames.bukkit.towny.object.inviteobjects.TownJoinNationInvite;
import com.palmergames.bukkit.towny.permissions.PermissionNodes;
import com.palmergames.bukkit.towny.permissions.TownyPerms;
import com.palmergames.bukkit.towny.tasks.CooldownTimerTask;
import com.palmergames.bukkit.towny.tasks.CooldownTimerTask.CooldownType;
import com.palmergames.bukkit.towny.utils.SpawnUtil;
import com.palmergames.bukkit.towny.war.flagwar.TownyWar;
import com.palmergames.bukkit.util.BukkitTools;
import com.palmergames.bukkit.util.ChatTools;
import com.palmergames.bukkit.util.Colors;
import com.palmergames.bukkit.util.NameValidation;
import com.palmergames.util.StringMgmt;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
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 org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import javax.naming.InvalidNameException;
import java.io.InvalidObjectException;
import java.util.ArrayList;
Expand Down Expand Up @@ -2281,10 +2271,6 @@ public static void nationSpawn(Player player, String[] split) throws TownyExcept
try {

Resident resident = townyUniverse.getDataSource().getResident(player.getName());

if (TownySettings.getSpawnCooldownTime() > 0 && CooldownTimerTask.hasCooldown(resident.getName(), CooldownType.TELEPORT))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_cannot_spawn_x_seconds_remaining"), CooldownTimerTask.getCooldownRemaining(resident.getName(), CooldownType.TELEPORT)));

Nation nation;
String notAffordMSG;

Expand All @@ -2304,14 +2290,14 @@ public static void nationSpawn(Player player, String[] split) throws TownyExcept
nation = resident.getTown().getNation();
notAffordMSG = TownySettings.getLangString("msg_err_cant_afford_tp");

nationSpawn(player, split, nation, notAffordMSG);
SpawnUtil.sendToTownySpawn(player, split, nation, notAffordMSG, false, SpawnType.NATION);

} else {
// split.length > 1
nation = townyUniverse.getDataSource().getNation(split[0]);
notAffordMSG = String.format(TownySettings.getLangString("msg_err_cant_afford_tp_nation"), nation.getName());

nationSpawn(player, split, nation, notAffordMSG);
SpawnUtil.sendToTownySpawn(player, split, nation, notAffordMSG, false, SpawnType.NATION);

}
} catch (NotRegisteredException e) {
Expand All @@ -2321,170 +2307,4 @@ public static void nationSpawn(Player player, String[] split) throws TownyExcept
}

}

/**
* Core nation spawn function to allow admin use.
*
* @param player - Player.
* @param split - Current command arguments.
* @param nation - Nation.
* @param notAffordMSG - Message for lack of funds.
*/
public static void nationSpawn(Player player, String[] split, Nation nation, String notAffordMSG) {
TownyUniverse townyUniverse = TownyUniverse.getInstance();

try {
boolean isTownyAdmin = townyUniverse.getPermissionSource().has(player, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_NATION_SPAWN_OTHER.getNode());
Resident resident = townyUniverse.getDataSource().getResident(player.getName());
Location spawnLoc;
NationSpawnLevel nationSpawnPermission;

spawnLoc = nation.getNationSpawn();

// Determine conditions
if (isTownyAdmin) {
nationSpawnPermission = NationSpawnLevel.ADMIN;
} else if ((split.length == 0)) {
nationSpawnPermission = NationSpawnLevel.PART_OF_NATION;
} else {
// split.length > 1
if (!resident.hasTown()) {
nationSpawnPermission = NationSpawnLevel.UNAFFILIATED;
}
else if (resident.hasNation()) {
Nation playerNation = resident.getTown().getNation();
Nation targetNation = nation;

if (playerNation == targetNation) {
nationSpawnPermission = NationSpawnLevel.PART_OF_NATION;
} else if (targetNation.hasEnemy(playerNation)) {
// Prevent enemies from using spawn travel.
throw new TownyException(TownySettings.getLangString("msg_err_public_spawn_enemy"));
} else if (targetNation.hasAlly(playerNation)) {
nationSpawnPermission = NationSpawnLevel.NATION_ALLY;
} else {
nationSpawnPermission = NationSpawnLevel.UNAFFILIATED;
}
} else {
nationSpawnPermission = NationSpawnLevel.UNAFFILIATED;
}
}

// Check the permissions
if (!(isTownyAdmin || ((nationSpawnPermission == NationSpawnLevel.UNAFFILIATED) ? nation.isPublic() : nationSpawnPermission.hasPermissionNode(plugin, player, nation)))) {

throw new TownyException(TownySettings.getLangString("msg_err_nation_not_public"));
}

if (!isTownyAdmin) {
// Prevent spawn travel while in disallowed zones (if
// configured)
List<String> disallowedZones = TownySettings.getDisallowedTownSpawnZones();

if (!disallowedZones.isEmpty()) {
String inTown;
try {
Location loc = plugin.getCache(player).getLastLocation();
inTown = TownyAPI.getInstance().getTownName(loc);
} catch (NullPointerException e) {
inTown = TownyAPI.getInstance().getTownName(player.getLocation());
}

if (inTown == null && disallowedZones.contains("unclaimed"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_nation_spawn_disallowed_from"), "the Wilderness"));
if (inTown != null && resident.hasNation() && townyUniverse.getDataSource().getTown(inTown).hasNation()) {
Nation inNation = townyUniverse.getDataSource().getTown(inTown).getNation();
Nation playerNation = resident.getTown().getNation();
if (inNation.hasEnemy(playerNation) && disallowedZones.contains("enemy"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_nation_spawn_disallowed_from"), "Enemy areas"));
if (!inNation.hasAlly(playerNation) && !inNation.hasEnemy(playerNation) && disallowedZones.contains("neutral"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_nation_spawn_disallowed_from"), "Neutral towns"));
}
}
}

double travelCost = 0;

// Taking whichever is smaller, the cost of the spawn price set by the nation, or the cost set in the config (which is the maximum a nation can set their spawncost to.)
travelCost = Math.min(nationSpawnPermission.getCost(nation),nationSpawnPermission.getCost());

// Check if need/can pay
if ( (!townyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_TOWN_SPAWN_FREECHARGE.getNode())) &&
(travelCost > 0 && TownySettings.isUsingEconomy() && (resident.getHoldingBalance() < travelCost)) )
throw new TownyException(notAffordMSG);

// Used later to make sure the chunk we teleport to is loaded.
Chunk chunk = spawnLoc.getChunk();

// isJailed test
if (resident.isJailed()) {
TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_cannot_spawn_while_jailed"));
return;
}

// Essentials tests
boolean UsingESS = plugin.isEssentials();

if (UsingESS && !isTownyAdmin) {
try {
User user = plugin.getEssentials().getUser(player);

if (!user.isJailed() && !resident.isJailed()) {

Teleport teleport = user.getTeleport();
if (!chunk.isLoaded())
chunk.load();
// Cause an essentials exception if in cooldown.
teleport.cooldown(true);
teleport.teleport(spawnLoc, null, TeleportCause.COMMAND);
}
} catch (Exception e) {
TownyMessaging.sendErrorMsg(player, "Error: " + e.getMessage());
// cooldown?
return;
}
}


// Show message if we are using Vault and are charging for spawn travel.
if ( !townyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_TOWN_SPAWN_FREECHARGE.getNode()) ) {
TownyEconomyObject payee = nation;
if (!TownySettings.isTownSpawnPaidToTown())
payee = TownyEconomyObject.SERVER_ACCOUNT;
if (travelCost > 0 && TownySettings.isUsingEconomy() && resident.payTo(travelCost, payee, String.format("Nation Spawn (%s)", nationSpawnPermission))) {
TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_cost_spawn"), TownyEconomyHandler.getFormattedBalance(travelCost)));
}
}

// If an Admin or Essentials teleport isn't being used, use our own.
if (isTownyAdmin) {
if (player.getVehicle() != null)
player.getVehicle().eject();
if (!chunk.isLoaded())
chunk.load();
player.teleport(spawnLoc, PlayerTeleportEvent.TeleportCause.COMMAND);
return;
}

if (!UsingESS) {
if (TownyTimerHandler.isTeleportWarmupRunning()) {
// Use teleport warmup
player.sendMessage(String.format(TownySettings.getLangString("msg_nation_spawn_warmup"), TownySettings.getTeleportWarmupTime()));
TownyAPI.getInstance().requestTeleport(player, spawnLoc);
} else {
// Don't use teleport warmup
if (player.getVehicle() != null)
player.getVehicle().eject();
if (!chunk.isLoaded())
chunk.load();
player.teleport(spawnLoc, PlayerTeleportEvent.TeleportCause.COMMAND);
if (TownySettings.getSpawnCooldownTime() > 0)
CooldownTimerTask.addCooldownTimer(resident.getName(), CooldownType.TELEPORT);
}
}
} catch (TownyException | EconomyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage());
}
}

}
Loading

0 comments on commit f66a98a

Please sign in to comment.