Skip to content

Commit

Permalink
Fixed safe teleportation to other dimensions that are not the default…
Browse files Browse the repository at this point in the history
… one not working (#991)
  • Loading branch information
OmerBenGera committed Mar 11, 2022
1 parent 2969baf commit 8319cde
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
Expand Up @@ -2,6 +2,7 @@

import com.bgsoftware.superiorskyblock.api.island.Island;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.util.concurrent.CompletableFuture;
Expand All @@ -26,4 +27,14 @@ public interface PlayerTeleportAlgorithm {
*/
CompletableFuture<Boolean> teleport(Player player, Island island);

/**
* Teleport a player to an island in a specific environment.
*
* @param player The player to teleport.
* @param island The island to teleport the player to.
* @param environment The environment to teleport the player to.
* @return CompletableFuture with boolean that indicates whether the teleportation was successful.
*/
CompletableFuture<Boolean> teleport(Player player, Island island, World.Environment environment);

}
Expand Up @@ -185,6 +185,14 @@ public interface SuperiorPlayer extends IMissionsHolder {
*/
void teleport(Island island);

/**
* Teleport the player to an island.
*
* @param island The island to teleport the player to.
* @param environment The environment to teleport the player to.
*/
void teleport(Island island, World.Environment environment);

/**
* Teleport the player to an island.
*
Expand All @@ -193,6 +201,15 @@ public interface SuperiorPlayer extends IMissionsHolder {
*/
void teleport(Island island, @Nullable Consumer<Boolean> teleportResult);

/**
* Teleport the player to an island.
*
* @param island The island to teleport the player to.
* @param environment The environment to teleport the player to.
* @param teleportResult Consumer that will be ran when task is finished.
*/
void teleport(Island island, World.Environment environment, @Nullable Consumer<Boolean> teleportResult);

/**
* Check whether or not the player is inside their island.
* When the player is offline or he doesn't have an island, false will be returned.
Expand Down
Expand Up @@ -349,14 +349,24 @@ public void teleport(Location location, @Nullable Consumer<Boolean> teleportResu

@Override
public void teleport(Island island) {
teleport(island, null);
this.teleport(island, (Consumer<Boolean>) null);
}

@Override
public void teleport(Island island, World.Environment environment) {
this.teleport(island, environment, null);
}

@Override
public void teleport(Island island, @Nullable Consumer<Boolean> teleportResult) {
this.teleport(island, plugin.getSettings().getWorlds().getDefaultWorld(), teleportResult);
}

@Override
public void teleport(Island island, World.Environment environment, @Nullable Consumer<Boolean> teleportResult) {
Player player = asPlayer();
if (player != null) {
playerTeleportAlgorithm.teleport(player, island).whenComplete((result, error) -> {
playerTeleportAlgorithm.teleport(player, island, environment).whenComplete((result, error) -> {
if (teleportResult != null)
teleportResult.accept(error == null && result);
});
Expand Down
Expand Up @@ -24,9 +24,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -159,7 +157,12 @@ public void teleport(Location location, @Nullable Consumer<Boolean> teleportResu

@Override
public void teleport(Island island) {
teleport(island, null);
this.teleport(island, (Consumer<Boolean>) null);
}

@Override
public void teleport(Island island, World.Environment environment) {
this.teleport(island, environment, null);
}

@Override
Expand All @@ -168,6 +171,12 @@ public void teleport(Island island, @Nullable Consumer<Boolean> teleportResult)
teleportResult.accept(false);
}

@Override
public void teleport(Island island, World.Environment environment, @Nullable Consumer<Boolean> teleportResult) {
if (teleportResult != null)
teleportResult.accept(false);
}

@Override
public boolean isInsideIsland() {
return false;
Expand Down
Expand Up @@ -52,7 +52,12 @@ public CompletableFuture<Boolean> teleport(Player player, Location location) {

@Override
public CompletableFuture<Boolean> teleport(Player player, Island island) {
Location homeLocation = island.getIslandHome(plugin.getSettings().getWorlds().getDefaultWorld());
return this.teleport(player, island, plugin.getSettings().getWorlds().getDefaultWorld());
}

@Override
public CompletableFuture<Boolean> teleport(Player player, Island island, World.Environment environment) {
Location homeLocation = island.getIslandHome(environment);

Preconditions.checkNotNull(homeLocation, "Cannot find a suitable home location for island " +
island.getUniqueId());
Expand All @@ -72,7 +77,7 @@ public CompletableFuture<Boolean> teleport(Player player, Island island) {
return;
}

Block islandCenterBlock = island.getCenter(plugin.getSettings().getWorlds().getDefaultWorld()).getBlock();
Block islandCenterBlock = island.getCenter(environment).getBlock();
float rotationYaw = homeLocation.getYaw();
float rotationPitch = homeLocation.getPitch();

Expand Down Expand Up @@ -108,8 +113,8 @@ public CompletableFuture<Boolean> teleport(Player player, Island island) {
* Finding a new block to teleport the player to.
*/

List<CompletableFuture<ChunkSnapshot>> chunksToLoad = island.getAllChunksAsync(
plugin.getSettings().getWorlds().getDefaultWorld(), true, true, null)
List<CompletableFuture<ChunkSnapshot>> chunksToLoad = island.getAllChunksAsync(environment,
true, true, null)
.stream().map(future -> future.thenApply(Chunk::getChunkSnapshot)).collect(Collectors.toList());

Executor.createTask().runAsync(v -> {
Expand Down
@@ -1,10 +1,10 @@
package com.bgsoftware.superiorskyblock.utils.logic;

import com.bgsoftware.superiorskyblock.lang.Message;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.schematic.Schematic;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.lang.Message;
import com.bgsoftware.superiorskyblock.lang.PlayerLocales;
import com.bgsoftware.superiorskyblock.player.SuperiorNPCPlayer;
import com.bgsoftware.superiorskyblock.utils.StringUtils;
Expand Down Expand Up @@ -54,11 +54,9 @@ public static void handlePlayerPortal(Player player, Location portalLocation,
}

try {
Location destinationLocation = island.getIslandHome(destinationEnvironment);

// If schematic was already generated, simply teleport player to destination location.
if (island.wasSchematicGenerated(destinationEnvironment)) {
superiorPlayer.teleport(destinationLocation);
superiorPlayer.teleport(island, destinationEnvironment);
return;
}

Expand Down Expand Up @@ -92,6 +90,8 @@ public static void handlePlayerPortal(Player player, Location portalLocation,
island.setBonusLevel(island.getBonusLevel().subtract(schematicLevel));
}

Location destinationLocation = island.getIslandHome(destinationEnvironment);

if (destinationEnvironment == World.Environment.THE_END) {
plugin.getNMSDragonFight().awardTheEndAchievement(player);
if (plugin.getSettings().getWorlds().getEnd().isDragonFight())
Expand Down

0 comments on commit 8319cde

Please sign in to comment.