Skip to content

Commit

Permalink
add option to set specific world to random teleport feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ChudziudgiToJa committed Sep 25, 2023
1 parent 60279da commit d78a49a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 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 @@ -3,11 +3,7 @@
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import io.papermc.lib.PaperLib;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -43,16 +39,24 @@ 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());
}

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 d78a49a

Please sign in to comment.