Skip to content

Commit

Permalink
fixed leashed entity teleportation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
BLuFeNiX committed Jul 10, 2021
1 parent b09df99 commit bbab6f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
24 changes: 16 additions & 8 deletions src/main/java/net/blufenix/teleportationrunes/TeleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import java.util.ArrayList;
Expand Down Expand Up @@ -96,7 +97,7 @@ public static boolean attemptTeleport(final Player player, Location teleporterLo
if (currentExp >= fee) {
// teleport player
Location playerLoc = player.getLocation();
Location adjustedLoc = getSafeDestination(existingWaypoint.loc.clone());
final Location adjustedLoc = getSafeDestination(existingWaypoint.loc.clone());

if (adjustedLoc == null) {
player.sendMessage(StringResources.WAYPOINT_OBSTRUCTED);
Expand All @@ -107,7 +108,7 @@ public static boolean attemptTeleport(final Player player, Location teleporterLo

// check if the player has any leashed animals to teleport as well
// TODO add to numEntities (can player be riding and leash the same/different entity?)
ArrayList<LivingEntity> leashedEntities = new ArrayList<>();
final ArrayList<LivingEntity> leashedEntities = new ArrayList<>();
for (Entity entity : player.getNearbyEntities(10, 10, 10)) {
if (entity instanceof LivingEntity) {
LivingEntity le = (LivingEntity) entity;
Expand Down Expand Up @@ -146,12 +147,19 @@ public void run() {
}
}

// port leashed animals as well if there are any
for (LivingEntity le : leashedEntities) {
le.teleport(adjustedLoc); // todo check if successful, and what to do if not?
le.setLeashHolder(player);
player.spawnParticle(Particle.HEART, adjustedLoc, 1);
}
// teleport leashed entities 1 tick later, to avoid behavior where leash stretches to infinity
new BukkitRunnable(){
@Override
public void run() {
// port leashed animals as well if there are any
for (LivingEntity le : leashedEntities) {
le.teleport(adjustedLoc); // todo check if successful, and what to do if not?
le.setLeashHolder(player);
player.spawnParticle(Particle.HEART, adjustedLoc, 1);
}
}
}.runTaskLater(TeleportationRunes.getInstance(), 1);


if (Config.enableLightningEffect) {
player.getWorld().strikeLightningEffect(adjustedLoc);
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/net/blufenix/teleportationrunes/TeleportTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
import net.blufenix.common.Log;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class TeleportTask extends BukkitRunnable {

// normally this would need to be thread safe, but minecraft only runs on one thread
private static Set<Player> playersCurrentlyTeleporting = new HashSet<>();
private static Set<Player> playersCurrentlyTeleporting = Collections.synchronizedSet(new HashSet<Player>());

// modify these
private int countdownTicks;
Expand Down Expand Up @@ -154,7 +155,12 @@ private void onSuccessOrFail(boolean success) {
if (callback != null) {
callback.onFinished(success);
}
playersCurrentlyTeleporting.remove(player);
Bukkit.getScheduler().runTaskLater(TeleportationRunes.getInstance(), new Runnable() {
@Override
public void run() {
playersCurrentlyTeleporting.remove(player);
}
}, 20);
}

private boolean playerStillAtTeleporter() {
Expand Down

0 comments on commit bbab6f5

Please sign in to comment.