Skip to content

Commit

Permalink
Teleport pets with player
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkerr committed May 4, 2024
1 parent 0eb183d commit a9b1121
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.object.Translation;
import com.palmergames.bukkit.towny.object.economy.Account;
import com.palmergames.bukkit.towny.utils.SpawnUtil;
import com.palmergames.bukkit.util.BukkitTools;

import io.papermc.lib.PaperLib;

import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -64,8 +67,10 @@ public void run() {
// Only teleport & add cooldown if player is valid
if (player == null)
continue;

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

List<Entity> pets = SpawnUtil.getPets(player);
pets.add(player);
SpawnUtil.teleportEntities(request.destinationLocation(), pets, TeleportCause.COMMAND);

if (request.cooldown() > 0)
CooldownTimerTask.addCooldownTimer(resident.getName(), "teleport", request.cooldown());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.palmergames.bukkit.towny.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
Expand All @@ -19,7 +20,11 @@
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sittable;
import org.bukkit.entity.Tameable;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import com.palmergames.bukkit.towny.Towny;
Expand Down Expand Up @@ -669,12 +674,45 @@ private static void initiateSpawn(Player player, Location spawnLoc, int cooldown
// Don't use teleport warmup
if (player.getVehicle() != null)
player.getVehicle().eject();
PaperLib.teleportAsync(player, spawnLoc, TeleportCause.COMMAND);

List<Entity> pets = getPets(player);
pets.add(player);
teleportEntities(spawnLoc, pets, TeleportCause.COMMAND);
if (cooldown > 0 && !hasPerm(player, PermissionNodes.TOWNY_SPAWN_ADMIN_NOCOOLDOWN))
CooldownTimerTask.addCooldownTimer(player.getName(), "teleport", cooldown);
}
}

/**
* Gets any pets owned by the specified player that are nearby them
*
* @param player The player to get the pets of
*/
public static List<Entity> getPets(Player player) {
List<Entity> pets = new ArrayList<>();

for (Entity entity : player.getNearbyEntities(16, 16, 16)) {
if (!(entity instanceof Tameable tameable)) continue;

AnimalTamer tamer = tameable.getOwner();
if (tamer == null) continue;

if (!(entity instanceof Sittable sittable)) continue;
if (sittable.isSitting()) continue;

if (tamer.equals(player))
pets.add(entity);
}

return pets;
}

public static void teleportEntities(Location loc, List<Entity> entities, TeleportCause cause) {
for (Entity entity : entities) {
PaperLib.teleportAsync(entity, loc, cause);
}
}

/**
* Begin a costed teleportation.
*
Expand Down Expand Up @@ -745,8 +783,11 @@ private static void initiatePluginTeleport(Resident resident, Location loc, bool
final Player player = resident.getPlayer();
if (player == null)
return;

List<Entity> pets = getPets(player);
pets.add(player);

plugin.getScheduler().runLater(player, () -> PaperLib.teleportAsync(resident.getPlayer(), loc, TeleportCause.PLUGIN),
plugin.getScheduler().runLater(player, () -> teleportEntities(loc, pets, TeleportCause.PLUGIN),
ignoreWarmup ? 0 : TownySettings.getTeleportWarmupTime() * 20L);
}

Expand Down

0 comments on commit a9b1121

Please sign in to comment.