Skip to content

Commit

Permalink
Cleanup util classes (#5348)
Browse files Browse the repository at this point in the history
* Translate messages and cleanup code that was in TownyAPI

* Remove optimized imports from JailUtil

* Unify ifs

* Fix incorrect method being called

* Translate messages and cleanup code that was in TownyAPI

* Remove optimized imports from JailUtil

* Unify ifs

* Fix incorrect method being called

* Add missing dot in lang file.

Co-authored-by: LlmDl <LlmDlio@gmail.com>

* Fixes to jail book, found in testing.

All of the new lines were not working for whatever reason.

* Resolve unneeded diffs

* Resolve more.

Doesn't use PaperLib's getBedSpawnLocationAsync (yet.)

* More unneeded import diff.

* Readd warmups

* Add ignoreWarmup functionality

* Fix TownyAPI.

* Fix early translation.

* Fix Utils.

* Fix things with warmup times.

* Improve onPlayerRespawn.

Co-authored-by: LlmDl <LlmDlio@gmail.com>
  • Loading branch information
ggvbo and LlmDl authored Oct 24, 2021
1 parent 5ea381d commit edda66b
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 63 deletions.
36 changes: 35 additions & 1 deletion resources/lang/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1604,4 +1604,38 @@ towny_prices_nation_upkeep_based_on: '&7Nation upkeep is based on &athe %s'
#Shown in /towny prices, describing what controls nation upkeep when it is per town.
towny_prices_upkeep_num_towns: 'number of towns.'
#Shown in /towny prices, describing that controls nation upkeep when it is per resident.
towny_prices_upkeep_nation_level: 'nation level (num residents.)'
towny_prices_upkeep_nation_level: 'nation level (num residents.)'

#Jail causes
msg_jailed_mayor: 'Jailed by mayor.'
#Jail causes
msg_jailed_outlaw: 'Killed as an outlaw.'
#Jail causes
msg_jailed_war: 'Prisoner of War.'
#Jail handbook messages
msg_jailed_handbook_1: 'Looks like you''ve been jailed, for the reason %s'
#Jail handbook messages
msg_jailed_handbook_2: 'That''s too bad huh. Well what can you do about it? Here''s some helpful tips for you while you serve your sentence.'
#Jail handbook messages
msg_jailed_handbook_3: 'You have been jailed for %s hours. By serving your sentence you will become free.'
#Jail handbook messages
msg_jailed_handbook_4_can: 'You can escape from jail by leaving your town using ''/town leave''.'
#Jail handbook messages
msg_jailed_handbook_4_cant: 'While you''re jailed you won''t be able to leave your town to escape jail.'
#Jail handbook messages
msg_jailed_handbook_bail_1: 'You can be freed from jail by paying your bail using ''/res jail paybail''.'
#Jail handbook messages
msg_jailed_handbook_bail_2: 'Bail will cost %s.'
#Jail handbook messages
msg_jailed_handbook_5: 'If you must persist and make efforts to escape, if you make it to the wilderness you will also earn your freedom.'
#Jail handbook messages
msg_jailed_handbook_6: 'But don''t die before you reach the wilderness or you''ll end up right back in jail.'
#Jail handbook messages
msg_jailed_teleport: 'Luckily, enderpearls and chorus fruit are allowed to be used to help you escape, now you''ve just got to get a hold of some.'
#Jail handbook messages
msg_jailed_war_prisoner: 'As a prisoner of war you will be freed if your countrymen can reduce the jail plot''s HP to 0, or if the town you are jailed in is removed from the war.'
#Jail handbook title
msg_jailed_title: 'So you''ve been jailed :('
#Jail handbook author
msg_jailed_author: 'Towny Jailco.'

2 changes: 2 additions & 0 deletions src/com/palmergames/bukkit/towny/TownyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,9 @@ public List<Resident> getOnlineResidents(ResidentList owner) {
*
* @param player {@link Player} to be teleported to jail.
* @param location {@link Location} of the jail to be teleported to.
* @deprecated Since 0.97.3.0 use {@link com.palmergames.bukkit.towny.utils.SpawnUtil#jailTeleport(Resident)} or {@link com.palmergames.bukkit.towny.utils.SpawnUtil#jailAwayTeleport(Resident)} instead.
*/
@Deprecated
public void jailTeleport(final Player player, final Location location) {
Bukkit.getScheduler().scheduleSyncDelayedTask(towny, () -> PaperLib.teleportAsync(player, location, PlayerTeleportEvent.TeleportCause.PLUGIN),
(long) TownySettings.getTeleportWarmupTime() * 20);
Expand Down
25 changes: 14 additions & 11 deletions src/com/palmergames/bukkit/towny/listeners/TownyPlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,27 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {
if (Towny.is116Plus() && event.isAnchorSpawn() && TownySettings.isRespawnAnchorHigherPrecedence()) {
return;
}

Location respawn;
respawn = TownyAPI.getInstance().getTownSpawnLocation(player);
if (respawn == null) {
// Town has not set respawn location. Using default.
return;

// Towny might be prioritizing bed spawns over town spawns.
if (TownySettings.getBedUse()) {
Location bed = player.getBedSpawnLocation();
if (bed != null)
respawn = bed;
}

// Town spawn could be null and no bed was available.
if (respawn == null)
return;

// Check if only respawning in the same world as the town's spawn.
if (TownySettings.isTownRespawningInOtherWorlds() && !player.getWorld().equals(respawn.getWorld()))
return;

// Bed spawn or town.
if (TownySettings.getBedUse() && (player.getBedSpawnLocation() != null)) {
event.setRespawnLocation(player.getBedSpawnLocation());
} else {
event.setRespawnLocation(respawn);
}

event.setRespawnLocation(respawn);

// Handle Spawn protection
long protectionTime = TownySettings.getSpawnProtection();
if (protectionTime > 0l) {
Expand Down
6 changes: 3 additions & 3 deletions src/com/palmergames/bukkit/towny/object/jail/JailReason.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public enum JailReason {

MAYOR("Jailed by mayor.", 1), // hours is unused, always specified by the mayor when jailing.
OUTLAW_DEATH("Killed as an outlaw.", TownySettings.getJailedOutlawJailHours()),
PRISONER_OF_WAR("Prisoner of War.", 99);
MAYOR("msg_jailed_mayor", 1), // hours is unused, always specified by the mayor when jailing.
OUTLAW_DEATH("msg_jailed_outlaw", TownySettings.getJailedOutlawJailHours()),
PRISONER_OF_WAR("msg_jailed_war", 99);

private final String cause;
private final int hours;
Expand Down
11 changes: 2 additions & 9 deletions src/com/palmergames/bukkit/towny/utils/CombatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,7 @@ private static boolean isPvP(@NotNull TownBlock townBlock) {
* @return true if the world is PvP
*/
public static boolean isWorldPvP(TownyWorld world) {

// Universe is only PvP
if (world.isForcePVP() || world.isPVP())
return true;

return false;
return (world.isForcePVP() || world.isPVP());
}

/**
Expand Down Expand Up @@ -619,9 +614,7 @@ public static boolean isEnemy(Town a, Town b) {

if (!a.hasNation() || !b.hasNation())
return false;
if (isSameTown(a, b))
return false;
if (isSameNation(a, b))
if (isSameTown(a, b) || isSameNation(a, b))
return false;
if (a.getNationOrNull().hasEnemy(b.getNationOrNull()))
return true;
Expand Down
57 changes: 21 additions & 36 deletions src/com/palmergames/bukkit/towny/utils/JailUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import java.util.List;
import java.util.UUID;

import com.palmergames.bukkit.towny.object.Translatable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.TownyEconomyHandler;
import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownySettings;
Expand All @@ -24,6 +22,9 @@
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.object.Translation;
import com.palmergames.bukkit.towny.object.Translator;
import com.palmergames.bukkit.towny.object.jail.Jail;
import com.palmergames.bukkit.towny.object.jail.JailReason;
import com.palmergames.bukkit.towny.object.jail.UnJailReason;
Expand All @@ -46,9 +47,7 @@ public class JailUtil {
public static void jailResident(Resident resident, Jail jail, int cell, int hours, JailReason reason, CommandSender jailer) {

// Set senderName
String senderName = "Admin";
if (jailer instanceof Player)
senderName = ((Player) jailer).getName();
String senderName = jailer instanceof Player ? (jailer).getName() : "Admin";

// Stop mayors from setting incredibly high hours.
if (hours > 10000)
Expand Down Expand Up @@ -168,39 +167,37 @@ public static void unJailResident(Resident resident, UnJailReason reason) {
* @param reason JailReason the player is in jail for.
*/
private static void sendJailedBookToResident(Player player, JailReason reason) {
final Translator translator = Translator.locale(Translation.getLocale(player));

/*
* A nice little book for the not so nice person in jail.
*/
String pages = "Looks like you've been jailed, for the reason " + reason.getCause();
pages += "That's too bad huh. Well what can you do about it? Here's some helpful tips for you while you serve your sentence.\n\n";
pages += "You have been jailed for " + reason.getHours() + " hours. By serving your sentence you will become free.\n\n";
if (TownySettings.JailDeniesTownLeave())
pages += "While you're jailed you won't be able to leave your town to escape jail.\n";
else
pages += "You can escape from jail by leaving your town using '/town leave'.\n";
String pages = translator.of("msg_jailed_handbook_1", translator.of(reason.getCause()));
pages += translator.of("msg_jailed_handbook_2") + "\n\n";
pages += translator.of("msg_jailed_handbook_3", reason.getHours()) + "\n\n";
pages += TownySettings.JailDeniesTownLeave() ? translator.of("msg_jailed_handbook_4_cant") : translator.of("msg_jailed_handbook_4_can") + "\n";
if (TownySettings.isAllowingBail() && TownyEconomyHandler.isActive()) {
pages += "You can be freed from jail by paying your bail using '/res jail paybail'.";
pages += translator.of("msg_jailed_handbook_bail_1");
double cost = TownySettings.getBailAmount();
Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
if (resident.isMayor())
cost = TownySettings.getBailAmountMayor();
if (resident.isKing())
cost = TownySettings.getBailAmountKing();
pages += "Bail will cost: " + TownyEconomyHandler.getFormattedBalance(cost) + "\n\n";
pages += translator.of("msg_jailed_handbook_bail_2", TownyEconomyHandler.getFormattedBalance(cost)) + "\n\n";
}
pages += "If you must persist and make efforts to escape, if you make it to the wilderness you will also earn your freedom.";
pages += "But don't die before you reach the wilderness or you'll end up right back in jail.";
pages += translator.of("msg_jailed_handbook_5");
pages += translator.of("msg_jailed_handbook_6");
if (TownySettings.JailAllowsTeleportItems())
pages += "Luckily, enderpearls and chorus fruit are allowed to be used to help you escape, now you've just got to get a hold of some.";
pages +="\n\n";
pages += translator.of("msg_jailed_teleport");
pages += "\n\n";
if (reason.equals(JailReason.PRISONER_OF_WAR))
pages += "As a prisoner of war you will be freed if your countrymen can reduce the jail plot's HP to 0, or if the town you are jailed in is removed from the war.";
pages += translator.of("msg_jailed_war_prisoner");

/*
* Send the book off to the BookFactory to be made.
*/
player.getInventory().addItem(new ItemStack(BookFactory.makeBook("So you've been jailed :(", "Towny Jailco.", pages)));
player.getInventory().addItem(new ItemStack(BookFactory.makeBook(translator.of("msg_jailed_title"), translator.of("msg_jailed_author"), pages)));
}

public static void createJailPlot(TownBlock townBlock, Town town, Location location) throws TownyException {
Expand All @@ -216,32 +213,20 @@ public static void createJailPlot(TownBlock townBlock, Town town, Location locat

private static void teleportAwayFromJail(Resident resident) {
// Don't teleport a player who isn't online.
if (resident.getPlayer() == null)
return;

// Send a pardoned player to the world spawn, or their town's spawn if they have a town with a spawn.
Location loc = Bukkit.getWorld(resident.getPlayer().getWorld().getName()).getSpawnLocation();
try {
loc = resident.getTown().getSpawn();
} catch (TownyException e) {}

// Use teleport warmup
TownyMessaging.sendMsg(resident, Translatable.of("msg_town_spawn_warmup", TownySettings.getTeleportWarmupTime()));
TownyAPI.getInstance().jailTeleport(resident.getPlayer(), loc);

if (!resident.isOnline()) return;
SpawnUtil.jailAwayTeleport(resident);
}

private static void teleportToJail(Resident resident) {
// Send a player to their jail cell.
TownyMessaging.sendMsg(resident, Translatable.of("msg_you_are_being_sent_to_jail"));
TownyMessaging.sendMsg(resident, Translatable.of("msg_town_spawn_warmup", TownySettings.getTeleportWarmupTime()));
TownyAPI.getInstance().jailTeleport(resident.getPlayer(), resident.getJailSpawn());
SpawnUtil.jailTeleport(resident);
}

private static void addJailedPlayerToLogOutMap(Resident resident) {
queuedJailedResidents.add(resident);
TownyMessaging.sendMsg(resident, Translatable.of("msg_do_not_log_out_while_waiting_to_be_teleported"));
Bukkit.getScheduler().scheduleSyncDelayedTask(Towny.getPlugin(), () -> queuedJailedResidents.remove(resident), TownySettings.getTeleportWarmupTime() + 20);
Bukkit.getScheduler().scheduleSyncDelayedTask(Towny.getPlugin(), () -> queuedJailedResidents.remove(resident), TownySettings.getTeleportWarmupTime() * 20);

}

Expand Down
38 changes: 35 additions & 3 deletions src/com/palmergames/bukkit/towny/utils/SpawnUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ public static boolean sendSpawnEvent(Player player, SpawnType type, Location spa
*
* @param player - Player being spawned.
* @param spawnLoc - Location being spawned to.
* @param admin - True if player has admin spawn nodes.
*/
private static void initiateSpawn(Player player, Location spawnLoc) {

Expand Down Expand Up @@ -414,11 +413,44 @@ public static void outlawTeleport(Town town, Resident outlaw) {
spawnLocation = Objects.requireNonNull(Bukkit.getWorld(TownySettings.getOutlawTeleportWorld())).getSpawnLocation();
}
// sets tp location to their bedspawn only if it isn't in the town they're being teleported from.
if ((outlawedPlayer.getBedSpawnLocation() != null) && (TownyAPI.getInstance().getTown(outlawedPlayer.getBedSpawnLocation()) != town))
spawnLocation = outlawedPlayer.getBedSpawnLocation();
Location bed = outlawedPlayer.getBedSpawnLocation();
if (bed != null && TownyAPI.getInstance().getTown(bed) != town)
spawnLocation = bed;
if (outlaw.hasTown() && TownyAPI.getInstance().getTownSpawnLocation(outlawedPlayer) != null)
spawnLocation = TownyAPI.getInstance().getTownSpawnLocation(outlawedPlayer);
TownyMessaging.sendMsg(outlaw, Translatable.of("msg_outlaw_kicked", town));
PaperLib.teleportAsync(outlaw.getPlayer(), spawnLocation, TeleportCause.PLUGIN);
}

public static void jailAwayTeleport(Resident jailed) {
initiatePluginTeleport(jailed, getIdealLocation(jailed), false);
}

public static void jailTeleport(Resident jailed) {
initiatePluginTeleport(jailed, jailed.getJailSpawn(), false);
}

/**
* Get the best location an resident can be teleported to
* @param resident
* @return bed spawn OR town spawn OR last world spawn
*/
private static Location getIdealLocation(Resident resident) {
Town town = resident.getTownOrNull();
Location loc = resident.getPlayer().getWorld().getSpawnLocation();

if (town != null && town.hasSpawn())
loc = town.getSpawnOrNull();

Location bed = resident.getPlayer().getBedSpawnLocation();
if (bed != null)
loc = bed;

return loc;
}

private static void initiatePluginTeleport(Resident resident, Location loc, boolean ignoreWarmup) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> PaperLib.teleportAsync(resident.getPlayer(), loc, TeleportCause.PLUGIN),
ignoreWarmup ? 0 : TownySettings.getTeleportWarmupTime() * 20);
}
}

0 comments on commit edda66b

Please sign in to comment.