Skip to content

Commit

Permalink
GH-543 Add option to set specific world to random teleport feature (#543
Browse files Browse the repository at this point in the history
)

* add option to set specific world to random teleport feature

* fix check style

* Update eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportService.java

Co-authored-by: Norbert Dejlich <[email protected]>

* Update eternalcore-core/src/main/java/com/eternalcode/core/feature/randomteleport/RandomTeleportService.java

Co-authored-by: Norbert Dejlich <[email protected]>

---------

Co-authored-by: Norbert Dejlich <[email protected]>
Co-authored-by: Martin Sulikowski <[email protected]>
  • Loading branch information
3 people committed Oct 2, 2023
1 parent 60279da commit 2d602bd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public static class RandomTeleport implements RandomTeleportSettings {
@Description("# Radius of random teleportation")
public int randomTeleportRadius = 1000;

@Description("# Teleport to a specific world, if left empty it will teleport to the player's current world")
public String randomTeleportWorld = "world";

@Description("# Number of attempts to teleport to a random location")
public int randomTeleportAttempts = 10;

Expand All @@ -116,6 +119,11 @@ public int randomTeleportRadius() {
return this.randomTeleportRadius;
}

@Override
public String randomTeleportWorld() {
return this.randomTeleportWorld;
}

@Override
public int randomTeleportAttempts() {
return this.randomTeleportAttempts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -43,16 +44,28 @@ class RandomTeleportService {
private static final int NETHER_MAX_HEIGHT = 127;

private final RandomTeleportSettings randomTeleportSettings;

private final Server server;
private final Random random = new Random();

@Inject
RandomTeleportService(RandomTeleportSettings randomTeleportSettings) {
RandomTeleportService(RandomTeleportSettings randomTeleportSettings, Server server) {
this.randomTeleportSettings = randomTeleportSettings;
this.server = server;
}


CompletableFuture<TeleportResult> teleport(Player player) {
return this.getSafeRandomLocation(player.getWorld(), this.randomTeleportSettings.randomTeleportAttempts())
World world = player.getWorld();

if (!this.randomTeleportSettings.randomTeleportWorld().isBlank()) {
world = this.server.getWorld(this.randomTeleportSettings.randomTeleportWorld());

if (world == null) {
throw new IllegalStateException("World " + this.randomTeleportSettings.randomTeleportWorld() + " is not exists!");
}
}

return this.getSafeRandomLocation(world, this.randomTeleportSettings.randomTeleportAttempts())
.thenCompose(location -> PaperLib.teleportAsync(player, location).thenApply(success -> new TeleportResult(success, location)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ public interface RandomTeleportSettings {

int randomTeleportRadius();

String randomTeleportWorld();

int randomTeleportAttempts();
}

0 comments on commit 2d602bd

Please sign in to comment.