Files
src
build.gradle
settings.gradle
Code
5 209
src/main/java/com/kodari/randomteleport/RandomTeleportPlugin.java
package com.kodari.randomteleport;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public final class RandomTeleportPlugin extends JavaPlugin {
private static final Set<String> DANGEROUS_BLOCKS = Set.of(
"LAVA", "WATER", "FIRE", "SOUL_FIRE", "MAGMA_BLOCK", "CACTUS",
"CAMPFIRE", "SOUL_CAMPFIRE", "SWEET_BERRY_BUSH", "WITHER_ROSE",
"POWDER_SNOW", "POINTED_DRIPSTONE", "NETHER_PORTAL", "END_PORTAL",
"END_GATEWAY", "COBWEB"
);
@Override
public void onEnable() {
saveDefaultConfig();
boolean changed = false;
for (String mode : new String[]{"default", "near", "far"}) {
String path = "ranges." + mode + ".world";
if (!getConfig().contains(path, true)) {
getConfig().set(path, "");
changed = true;
}
}
if (changed) {
saveConfig();
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sendMessage(sender, "players-only");
return true;
}
Player player = (Player) sender;
String mode;
switch (command.getName().toLowerCase(java.util.Locale.ROOT)) {
case "rtpdefault": mode = "default"; break;
case "rtpnear": mode = "near"; break;
case "rtpfar": mode = "far"; break;
default: return false;
}
String worldName = getConfig().getString("ranges." + mode + ".world", "").trim();
World world = worldName.isEmpty() ? player.getWorld() : Bukkit.getWorld(worldName);
if (world == null) {
player.sendMessage(message("invalid-world", "&cМир «%world%» не найден или не загружен.")
.replace("%world%", worldName));
return true;
}
List<Player> nearbyPlayers = new ArrayList<>();
if (mode.equals("near")) {
for (Player other : world.getPlayers()) {
if (!other.getUniqueId().equals(player.getUniqueId())) {
nearbyPlayers.add(other);
}
}
if (nearbyPlayers.isEmpty()) {
player.sendMessage(message("no-players-target", "&cВ целевом мире «%world%» нет других игроков для телепортации поблизости.")
.replace("%world%", world.getName()));
return true;
}
}
int defaultMin = mode.equals("far") ? 1000 : mode.equals("near") ? 30 : 100;
int defaultMax = mode.equals("far") ? 3000 : mode.equals("near") ? 150 : 500;
int min = Math.max(0, getConfig().getInt("ranges." + mode + ".min", defaultMin));
int max = Math.max(min, getConfig().getInt("ranges." + mode + ".max", defaultMax));
int attempts = Math.max(1, Math.min(1000, getConfig().getInt("attempts", 20)));
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < attempts; i++) {
Location center = mode.equals("near")
? nearbyPlayers.get(random.nextInt(nearbyPlayers.size())).getLocation()
: player.getWorld().equals(world) ? player.getLocation() : world.getSpawnLocation();
double angle = random.nextDouble() * Math.PI * 2;
double distance = Math.sqrt((double) min * min + random.nextDouble() * ((double) max * max - (double) min * min));
double candidateX = center.getX() + Math.cos(angle) * distance;
double candidateZ = center.getZ() + Math.sin(angle) * distance;
if (candidateX < -29999984 || candidateX >= 29999984 || candidateZ < -29999984 || candidateZ >= 29999984) {
continue;
}
int x = (int) Math.floor(candidateX);
int z = (int) Math.floor(candidateZ);
Location horizontal = new Location(world, x + 0.5, center.getY(), z + 0.5);
if (!world.getWorldBorder().isInside(horizontal)) {
continue;
}
int groundY = world.getHighestBlockYAt(x, z);
if (groundY < world.getMinHeight() || groundY + 2 >= world.getMaxHeight()) {
continue;
}
Block ground = world.getBlockAt(x, groundY, z);
Block feet = world.getBlockAt(x, groundY + 1, z);
Block head = world.getBlockAt(x, groundY + 2, z);
if (!ground.getType().isSolid() || isDangerous(ground)
|| !feet.isPassable() || isDangerous(feet)
|| !head.isPassable() || isDangerous(head)) {
continue;
}
Location destination = new Location(world, x + 0.5, groundY + 1, z + 0.5,
player.getLocation().getYaw(), player.getLocation().getPitch());
if (player.teleport(destination)) {
String message = message("success").replace("%x%", Integer.toString(x))
.replace("%y%", Integer.toString(groundY + 1))
.replace("%z%", Integer.toString(z));
player.sendMessage(message);
} else {
player.sendMessage(message("teleport-cancelled", "&cТелепортация не состоялась: она была отменена или запрещена."));
}
return true;
}
sendMessage(player, "failure");
return true;
}
private boolean isDangerous(Block block) {
return block.isLiquid() || DANGEROUS_BLOCKS.contains(block.getType().name());
}
private String message(String key) {
return ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages." + key, ""));
}
private String message(String key, String fallback) {
return ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages." + key, fallback));
}
private void sendMessage(CommandSender sender, String key) {
sender.sendMessage(message(key));
}
}