Skip to content

Commit

Permalink
- API: Add SuccessfulTownyTeleportEvent
Browse files Browse the repository at this point in the history
    - Thrown when a player is spawned by Towny after they use /res,
/town, or /nation spawn.
  • Loading branch information
LlmDl committed May 31, 2024
1 parent 20744ca commit 73f3921
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.palmergames.bukkit.towny.event.teleport;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import com.palmergames.bukkit.towny.object.Resident;

/**
* Thrown when Towny teleports a player after they have used /res, /town, or
* /nation spawn.
*
* @author LlmDl
* @since 0.100.2.12
*/
public class SuccessfulTownyTeleportEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private Resident resident;
private Location teleportLocation;

public SuccessfulTownyTeleportEvent(Resident resident, Location loc) {
super(!Bukkit.isPrimaryThread());
this.resident = resident;
this.teleportLocation = loc;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

public Resident getResident() {
return resident;
}

public Location getTeleportLocation() {
return teleportLocation;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.palmergames.bukkit.towny.TownyTimerHandler;
import com.palmergames.bukkit.towny.event.teleport.CancelledTownyTeleportEvent;
import com.palmergames.bukkit.towny.event.teleport.CancelledTownyTeleportEvent.CancelledTeleportReason;
import com.palmergames.bukkit.towny.event.teleport.SuccessfulTownyTeleportEvent;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.TeleportWarmupParticle;
import com.palmergames.bukkit.towny.object.TeleportRequest;
Expand Down Expand Up @@ -67,6 +68,8 @@ public void run() {

PaperLib.teleportAsync(player, request.destinationLocation(), TeleportCause.COMMAND);

BukkitTools.fireEvent(new SuccessfulTownyTeleportEvent(resident, request.destinationLocation()));

if (request.cooldown() > 0)
CooldownTimerTask.addCooldownTimer(resident.getName(), "teleport", request.cooldown());
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.palmergames.bukkit.towny.event.SpawnEvent;
import com.palmergames.bukkit.towny.event.TownSpawnEvent;
import com.palmergames.bukkit.towny.event.teleport.ResidentSpawnEvent;
import com.palmergames.bukkit.towny.event.teleport.SuccessfulTownyTeleportEvent;
import com.palmergames.bukkit.towny.event.teleport.UnjailedResidentTeleportEvent;
import com.palmergames.bukkit.towny.object.SpawnInformation;
import com.palmergames.bukkit.towny.object.Translatable;
Expand Down Expand Up @@ -658,18 +659,20 @@ private static SpawnEvent getSpawnEvent(Player player, SpawnType spawnType, Loca
* @param refundAccount The account that the player paid the cost to, used for refunds if the player aborts the teleport.
*/
private static void initiateSpawn(Player player, Location spawnLoc, int cooldown, double cost, @Nullable Account refundAccount) {
Resident resident = TownyAPI.getInstance().getResident(player);
if (resident == null)
return;

if (TownyTimerHandler.isTeleportWarmupRunning() && !hasPerm(player, PermissionNodes.TOWNY_SPAWN_ADMIN_NOWARMUP)) {
// Use teleport warmup
TownyMessaging.sendMsg(player, Translatable.of("msg_town_spawn_warmup", TownySettings.getTeleportWarmupTime()));

Resident resident = TownyAPI.getInstance().getResident(player);
if (resident != null)
TeleportWarmupTimerTask.requestTeleport(resident, spawnLoc, cooldown, refundAccount, cost);
TeleportWarmupTimerTask.requestTeleport(resident, spawnLoc, cooldown, refundAccount, cost);
} else {
// Don't use teleport warmup
if (player.getVehicle() != null)
player.getVehicle().eject();
PaperLib.teleportAsync(player, spawnLoc, TeleportCause.COMMAND);
BukkitTools.fireEvent(new SuccessfulTownyTeleportEvent(resident, spawnLoc));
if (cooldown > 0 && !hasPerm(player, PermissionNodes.TOWNY_SPAWN_ADMIN_NOCOOLDOWN))
CooldownTimerTask.addCooldownTimer(player.getName(), "teleport", cooldown);
}
Expand Down
4 changes: 3 additions & 1 deletion Towny/src/main/resources/ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9789,4 +9789,6 @@ v0.92.0.11:
- New Config Option: nation.default_map_color
- Default: ""
- When set, all new Nations will have their map color set to this color. You must use a colour listed in the global_nation_settings.allowed_map_colors setting below, ie aqua, azure, etc.
- Fix players who log into an area where they are outlawed not using the correct town.
- Fix players who log into an area where they are outlawed not using the correct town.
- API: Add SuccessfulTownyTeleportEvent
- Thrown when a player is spawned by Towny after they use /res, /town, or /nation spawn.

0 comments on commit 73f3921

Please sign in to comment.