Fixed bug with teleporting where home name would not be used.

This commit is contained in:
tastybento 2024-01-11 13:58:41 +09:00
parent 26e5d750a0
commit 0f655213bd
4 changed files with 45 additions and 23 deletions

View File

@ -15,7 +15,6 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
/**
* @author tastybento
@ -76,23 +75,14 @@ public class IslandGoCommand extends DelayedTeleportCommand {
} else {
IslandInfo info = names.get(name);
getIslands().setPrimaryIsland(user.getUniqueId(), info.island);
if (info.islandName) {
this.delayCommand(user, () -> new SafeSpotTeleport.Builder(getPlugin())
.entity(user.getPlayer())
.location(getIslands().getHomeLocation(info.island))
.thenRun(() -> user.sendMessage("general.success"))
.build());
} else {
this.delayCommand(user, () -> new SafeSpotTeleport.Builder(getPlugin())
.entity(user.getPlayer())
.location(getIslands().getHomeLocation(info.island, name))
.thenRun(() -> user.sendMessage("general.success"))
.build());
if (!info.islandName) {
this.delayCommand(user, () -> getIslands().homeTeleportAsync(getWorld(), user.getPlayer(), name)
.thenAccept((r) -> getIslands().setPrimaryIsland(user.getUniqueId(), info.island)));
return true;
}
}
} else {
this.delayCommand(user, () -> getIslands().homeTeleportAsync(getWorld(), user.getPlayer()));
}
this.delayCommand(user, () -> getIslands().homeTeleportAsync(getWorld(), user.getPlayer()));
return true;
}

View File

@ -1721,9 +1721,20 @@ public class Island implements DataObject, MetaDataAble {
* @since 1.16.0
*/
@NonNull
public Location getHome(String name) {
Location l = getHomes().get(name.toLowerCase());
return l == null ? getProtectionCenter() : l;
public Location getHome(final String nameToLookFor) {
BentoBox.getInstance().logDebug("name:" + nameToLookFor);
final String name2 = "hole";
getHomes().forEach((n, location) -> {
BentoBox.getInstance()
.logDebug("Home '" + n + "' > " + location + " same as '" + name2 + "'? "
+ name2.equals(n) + " " + name2.equalsIgnoreCase(n) + " "
+ n.equals(name2) + " " + n.equalsIgnoreCase(name2));
});
BentoBox.getInstance().logDebug("name:" + nameToLookFor);
return getHomes().entrySet().stream().filter(en -> en.getKey().equalsIgnoreCase(name2))
.map(Entry::getValue)
.findFirst().orElse(getProtectionCenter());
//return Objects.requireNonNullElse(l, getProtectionCenter());
}
/**

View File

@ -335,6 +335,7 @@ public class IslandsManager {
*/
@NonNull
public Set<Island> getIslands(@NonNull World world, UUID uniqueId) {
BentoBox.getInstance().logDebug("Asking the cache");
return islandCache.getIslands(world, uniqueId);
}
@ -609,21 +610,28 @@ public class IslandsManager {
this.setPrimaryIsland(user.getUniqueId(), island);
return "";
}).orElse(homeName);
BentoBox.getInstance().logDebug("The name is now = " + name);
// Try the home location first
Location defaultHome = getHomeLocation(world, user);
Location namedHome = homeName.isBlank() ? null : getHomeLocation(world, user, name);
Location l = namedHome != null ? namedHome : defaultHome;
//Location defaultHome = getHomeLocation(world, user);
//BentoBox.getInstance().logDebug("defaulthome has been got = " + name);
Location namedHome = homeName.isBlank() ? null : getHomeLocation(world, user, name); // TODO This is where the bug is.
BentoBox.getInstance().logDebug("The namedHome location is = " + namedHome);
//Location l = namedHome != null ? namedHome : defaultHome;
Location l = namedHome;
if (l != null) {
Util.getChunkAtAsync(l).thenRun(() -> {
// Check if it is safe
if (isSafeLocation(l)) {
BentoBox.getInstance().logDebug("Safe location found! " + l);
result.complete(l);
return;
}
BentoBox.getInstance().logDebug(l + " is not a safe location");
// To cover slabs, stairs and other half blocks, try one block above
Location lPlusOne = l.clone().add(new Vector(0, 1, 0));
BentoBox.getInstance().logDebug("Trying " + lPlusOne);
if (isSafeLocation(lPlusOne)) {
BentoBox.getInstance().logDebug("This is safer " + lPlusOne);
// Adjust the home location accordingly
setHomeLocation(user, lPlusOne, name);
result.complete(lPlusOne);
@ -640,6 +648,7 @@ public class IslandsManager {
}
private void tryIsland(CompletableFuture<Location> result, Location islandLoc, @NonNull User user, String name) {
BentoBox.getInstance().logDebug("We are trying to find another safe location on island at = " + islandLoc);
Util.getChunkAtAsync(islandLoc).thenRun(() -> {
World w = islandLoc.getWorld();
if (isSafeLocation(islandLoc)) {
@ -672,6 +681,7 @@ public class IslandsManager {
}
}
}
BentoBox.getInstance().logDebug("Nothing found!");
result.complete(null);
});
@ -885,6 +895,7 @@ public class IslandsManager {
*/
@Nullable
public Location getHomeLocation(@NonNull World world, @NonNull User user, String name) {
BentoBox.getInstance().logDebug("getHomeLocation user name = " + name);
return getHomeLocation(world, user.getUniqueId(), name);
}
@ -899,7 +910,8 @@ public class IslandsManager {
*/
@Nullable
public Location getHomeLocation(@NonNull World world, @NonNull UUID uuid, String name) {
return getIslands(world, uuid).stream().map(is -> is.getHome(name)).filter(Objects::nonNull).findFirst()
return getIslands(world, uuid).stream().filter(is -> is.getHomes().containsKey(name))
.map(is -> is.getHome(name)).findFirst()
.orElse(null);
}
@ -1072,6 +1084,7 @@ public class IslandsManager {
* @since 1.16.0
*/
public CompletableFuture<Boolean> homeTeleportAsync(@NonNull World world, @NonNull Player player, String name) {
BentoBox.getInstance().logDebug("homeTeleportAsync name = " + name);
return homeTeleportAsync(world, player, name, false);
}
@ -1107,7 +1120,9 @@ public class IslandsManager {
user.sendMessage("commands.island.go.teleport");
goingHome.add(user.getUniqueId());
readyPlayer(player);
BentoBox.getInstance().logDebug("Home name = " + name);
this.getAsyncSafeHomeLocation(world, user, name).thenAccept(home -> {
BentoBox.getInstance().logDebug("Got a result from getAsyncSafeHomeLocation: " + home);
Island island = getIsland(world, user);
if (home == null) {
// Try to fix this teleport location and teleport the player if possible

View File

@ -194,8 +194,14 @@ public class IslandCache {
public Set<Island> getIslands(@NonNull World world, @NonNull UUID uuid) {
World w = Util.getWorld(world);
if (w == null) {
BentoBox.getInstance().logDebug("The world was " + world.getName() + " but now is null");
return new HashSet<>();
}
BentoBox.getInstance().logDebug("The world was " + world.getName() + " and is now " + w.getName());
BentoBox.getInstance().logDebug("This player's islands in this world are:");
islandsByUUID.get(uuid).stream().filter(i -> w.equals(i.getWorld()))
.forEach(i -> BentoBox.getInstance().logDebug(i));
return islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).stream().filter(i -> w.equals(i.getWorld()))
.collect(Collectors.toSet());
}