Files
src
build.gradle
settings.gradle
Code
5 277
src/main/java/com/kodari/lsspawn/LSSpawnPlugin.java
package com.kodari.lsspawn;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
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.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public final class LSSpawnPlugin extends JavaPlugin implements Listener {
private static final String RTP_MENU_TITLE = "Choose RTP World";
private static final Set<String> HAZARDOUS_BLOCKS = Set.of(
"CACTUS", "MAGMA_BLOCK", "CAMPFIRE", "SOUL_CAMPFIRE", "FIRE", "SOUL_FIRE",
"POWDER_SNOW", "SWEET_BERRY_BUSH", "WITHER_ROSE", "POINTED_DRIPSTONE"
);
private final Set<UUID> pendingRtp = new HashSet<>();
@Override
public void onEnable() {
saveDefaultConfig();
getServer().getPluginManager().registerEvents(this, this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("setlsspawn")) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only a player can set the LSSpawn destination.");
return true;
}
Location location = player.getLocation();
getConfig().set("spawn.world", location.getWorld().getName());
getConfig().set("spawn.x", location.getX());
getConfig().set("spawn.y", location.getY());
getConfig().set("spawn.z", location.getZ());
getConfig().set("spawn.yaw", location.getYaw());
getConfig().set("spawn.pitch", location.getPitch());
saveConfig();
player.sendMessage("LSSpawn destination set to your current location.");
return true;
}
if (command.getName().equalsIgnoreCase("lsspawn")) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only a player can use /lsspawn.");
return true;
}
Location spawn = getSpawnLocation();
if (spawn == null) {
player.sendMessage("The LSSpawn destination has not been set yet.");
return true;
}
player.teleport(spawn);
player.sendMessage("Teleported to LSSpawn.");
return true;
}
if (command.getName().equalsIgnoreCase("lsrtp")) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only a player can use /lsrtp.");
return true;
}
openRtpMenu(player);
return true;
}
return false;
}
private void openRtpMenu(Player player) {
Inventory inventory = Bukkit.createInventory(null, 9, RTP_MENU_TITLE);
inventory.setItem(2, createWorldButton("GRASS_BLOCK", ChatColor.GREEN + "Overworld"));
inventory.setItem(6, createWorldButton("NETHERRACK", ChatColor.DARK_RED + "Nether"));
player.openInventory(inventory);
}
private ItemStack createWorldButton(String materialName, String displayName) {
ItemStack item = new ItemStack(Material.matchMaterial(materialName));
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(displayName);
item.setItemMeta(meta);
return item;
}
@EventHandler
public void onRtpMenuClick(InventoryClickEvent event) {
if (!RTP_MENU_TITLE.equals(event.getView().getTitle())) {
return;
}
event.setCancelled(true);
if (event.getClickedInventory() != event.getView().getTopInventory()
|| !(event.getWhoClicked() instanceof Player player)) {
return;
}
World.Environment environment;
if (event.getSlot() == 2) {
environment = World.Environment.NORMAL;
} else if (event.getSlot() == 6) {
environment = World.Environment.NETHER;
} else {
return;
}
player.closeInventory();
World world = getWorldByEnvironment(environment);
if (world == null) {
player.sendMessage(environment == World.Environment.NETHER
? "No loaded Nether world was found."
: "No loaded Overworld was found.");
return;
}
if (!pendingRtp.add(player.getUniqueId())) {
player.sendMessage("An RTP is already counting down.");
return;
}
player.sendMessage("Random teleporting in 5 seconds...");
Bukkit.getScheduler().runTaskLater(this, () -> {
pendingRtp.remove(player.getUniqueId());
if (!player.isOnline()) {
return;
}
if (teleportRandomly(player, world)) {
player.sendMessage("You were randomly teleported to the "
+ (environment == World.Environment.NETHER ? "Nether." : "Overworld."));
} else {
player.sendMessage("Could not find a safe random location. Try again.");
}
}, 100L);
}
private World getWorldByEnvironment(World.Environment environment) {
String configPath = environment == World.Environment.NETHER
? "rtp.nether-world"
: "rtp.overworld-world";
String worldName = getConfig().getString(configPath);
if (worldName == null) {
return null;
}
World world = Bukkit.getWorld(worldName);
return world != null && world.getEnvironment() == environment ? world : null;
}
private boolean teleportRandomly(Player player, World world) {
var border = world.getWorldBorder();
double halfSize = border.getSize() / 2.0;
double minX = Math.max(border.getCenter().getX() - halfSize + 2, -29_999_998);
double maxX = Math.min(border.getCenter().getX() + halfSize - 2, 29_999_998);
double minZ = Math.max(border.getCenter().getZ() - halfSize + 2, -29_999_998);
double maxZ = Math.min(border.getCenter().getZ() + halfSize - 2, 29_999_998);
if (minX >= maxX || minZ >= maxZ) {
return false;
}
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int attempt = 0; attempt < 30; attempt++) {
int x = (int) Math.floor(random.nextDouble(minX, maxX));
int z = (int) Math.floor(random.nextDouble(minZ, maxZ));
int highestY = Math.min(world.getHighestBlockYAt(x, z), world.getMaxHeight() - 3);
for (int y = highestY; y >= world.getMinHeight() + 1; y--) {
Block ground = world.getBlockAt(x, y, z);
Block feet = world.getBlockAt(x, y + 1, z);
Block head = world.getBlockAt(x, y + 2, z);
if (!ground.getType().isSolid() || ground.isLiquid()
|| !feet.isPassable() || feet.isLiquid()
|| !head.isPassable() || head.isLiquid()
|| isHazardous(ground) || isHazardous(feet) || isHazardous(head)) {
continue;
}
Location destination = new Location(world, x + 0.5, y + 1, z + 0.5,
player.getLocation().getYaw(), player.getLocation().getPitch());
if (player.teleport(destination)) {
return true;
}