Please write me a PaperMC RTP plugin with the following parameters:
This is not a traditional /rtp plugin. Instead, it should track when you are standing within a certain area, and teleport all players that are in the area to a position less than 15 blocks away but more than three blocks away, on a configurable timer.
Appropriate RTP positions are within a region (minx, minz, maxx, maxz) set in the config, and at surface level, while also being not above a configurable height.
All players in the RTP pool should get teleported to the same position.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
There should be placeholderapi integration for time until next teleport, and which players are within the teleport area currently.
To ensure you do not teleport into any claimed land, fire both a block break and place event at the target location for ALL players. Ensure this event is not cancelled for ANY players. If it is cancelled for at least one player, find a different position. You can try a maximum of three positions per tick.
There should be a command to reload the configuration and one to trigger the teleport now. All permissions should be clearly defined in the plugin.yml.
When retrying teleports, if one player fails, it could be because that player happens to be within a claim while the others are not. In that case, consider retrying that player only same tick, while not retrying others. The entire teleport must be retried if more than one player fails the land claim test described above.
All files needed to create and build the plugin should be shared in this chat, with instructions on where to place them.
You should use the modern PaperMC 1.20.4 API, including using kyori adventure for text formatting, ideally using MiniMessage message formatting (Plugin Reloaded) and similar for everything (please be sure to serialize placeholderapi placeholder outputs into legacy message formatting), to make the plugin easily configurable.
Please format your code with Lombok, and finalize variables where possible.
Assistant
Absolutely! Below is a complete PaperMC 1.20.4 plugin as you described, with all required files, instructions, and code.
This plugin is called AreaRTP.
name: AreaRTP
main: com.example.areartp.AreaRTP
version: 1.0.0
api-version: "1.20"
author: YourName
description: Area-based RTP plugin for PaperMC 1.20.4
commands:
areartp-reload:
description: Reload AreaRTP config
usage: /areartp-reload
permission: areartp.reload
areartp-now:
description: Force AreaRTP teleport now
usage: /areartp-now
permission: areartp.now
permissions:
areartp.reload:
description: Allows reloading the AreaRTP config
default: op
areartp.now:
description: Allows forcing an RTP teleport
default: op
4. config.yml
yaml
# AreaRTP Config
# The world name to use for RTP
world: "world"
# The area in which players are eligible for RTP
area:
minx: -100
minz: -100
maxx: 100
maxz: 100
# The region in which RTP can place players
rtp-region:
minx: -500
minz: -500
maxx: 500
maxz: 500
# Maximum Y height for RTP
max-y: 120
# Minimum distance from original position
min-distance: 3
# Maximum distance from original position
max-distance: 15
# How often (in seconds) to RTP all players in the area
rtp-interval-seconds: 60
5. Java Source Files
5.1 AreaRTP.java (Main Plugin Class)
java
package com.example.areartp;
import com.example.areartp.commands.ReloadCommand;
import com.example.areartp.commands.RTPNowCommand;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
@Getter
@Slf4j
public final class AreaRTP extends JavaPlugin {
private static AreaRTP instance;
private BukkitAudiences adventure;
private AreaRTPConfig configData;
private AreaRTPManager manager;
@Override
public void onEnable() {
instance = this;
saveDefaultConfig();
reloadConfigData();
adventure = BukkitAudiences.create(this);
manager = new AreaRTPManager(this);
manager.start();
// Register commands
getCommand("areartp-reload").setExecutor(new ReloadCommand(this));
getCommand("areartp-now").setExecutor(new RTPNowCommand(this));
// Register PlaceholderAPI expansion
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new AreaRTPPlaceholderExpansion(this).register();
log.info("PlaceholderAPI expansion registered.");
}
log.info("AreaRTP enabled.");
}
@Override
public void onDisable() {
if (adventure != null) {
adventure.close();
}
if (manager != null) {
manager.stop();
}
log.info("AreaRTP disabled.");
}
public void reloadConfigData() {
reloadConfig();
configData = new AreaRTPConfig(getConfig());
if (manager != null) {
manager.reload();
}
}
public static AreaRTP getInstance() {
return instance;
}
}
5.2 AreaRTPConfig.java (Config Data Holder)
java
package com.example.areartp;
import lombok.Getter;
import org.bukkit.configuration.file.FileConfiguration;
@Getter
public final class AreaRTPConfig {
private final String world;
private final int areaMinX, areaMinZ, areaMaxX, areaMaxZ;
private final int rtpMinX, rtpMinZ, rtpMaxX, rtpMaxZ;
private final int maxY;
private final int minDistance, maxDistance;
private final int rtpIntervalSeconds;
public AreaRTPConfig(final FileConfiguration config) {
this.world = config.getString("world", "world");
this.areaMinX = config.getInt("area.minx");
this.areaMinZ = config.getInt("area.minz");
this.areaMaxX = config.getInt("area.maxx");
this.areaMaxZ = config.getInt("area.maxz");
this.rtpMinX = config.getInt("rtp-region.minx");
this.rtpMinZ = config.getInt("rtp-region.minz");
this.rtpMaxX = config.getInt("rtp-region.maxx");
this.rtpMaxZ = config.getInt("rtp-region.maxz");
this.maxY = config.getInt("max-y", 120);
this.minDistance = config.getInt("min-distance", 3);
this.maxDistance = config.getInt("max-distance", 15);
this.rtpIntervalSeconds = config.getInt("rtp-interval-seconds", 60);
}
}
5.3 AreaRTPManager.java (Core Logic)
java
package com.example.areartp;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
@Slf4j
@RequiredArgsConstructor
public class AreaRTPManager {
private final AreaRTP plugin;
@Getter
private final Set<UUID> areaPlayers = new CopyOnWriteArraySet<>();
private BukkitRunnable task;
private long nextTeleportTime = 0L;
public void start() {
stop();
task = new BukkitRunnable() {
@Override
public void run() {
try {
tick();
} catch (Exception e) {
log.error("Error in AreaRTPManager tick", e);
}
}
};
task.runTaskTimer(plugin, 0L, 20L); // every second
nextTeleportTime = System.currentTimeMillis() + plugin.getConfigData().getRtpIntervalSeconds() * 1000L;
}
public void stop() {
if (task != null) {
task.cancel();
task = null;
}
}
public void reload() {
stop();
start();
}
public long getTimeUntilNextTeleportMillis() {
return Math.max(0, nextTeleportTime - System.currentTimeMillis());
}
public List<Player> getPlayersInArea() {
final World world = Bukkit.getWorld(plugin.getConfigData().getWorld());
if (world == null) return Collections.emptyList();
return Bukkit.getOnlinePlayers().stream()
.filter(p -> p.getWorld().equals(world))
.filter(this::isInArea)
.collect(Collectors.toList());
}
public boolean isInArea(@NotNull Player player) {
final AreaRTPConfig cfg = plugin.getConfigData();
final Location loc = player.getLocation();
return loc.getWorld().getName().equals(cfg.getWorld())
&& loc.getBlockX() >= cfg.getAreaMinX()
&& loc.getBlockX() <= cfg.getAreaMaxX()
&& loc.getBlockZ() >= cfg.getAreaMinZ()
&& loc.getBlockZ() <= cfg.getAreaMaxZ();
}
private void tick() {
// Update areaPlayers
areaPlayers.clear();
getPlayersInArea().forEach(p -> areaPlayers.add(p.getUniqueId()));
if (System.currentTimeMillis() >= nextTeleportTime) {
if (!areaPlayers.isEmpty()) {
teleportAreaPlayers();
}
nextTeleportTime = System.currentTimeMillis() + plugin.getConfigData().getRtpIntervalSeconds() * 1000L;
}
}
public void forceTeleportNow() {
if (!areaPlayers.isEmpty()) {
teleportAreaPlayers();
}
nextTeleportTime = System.currentTimeMillis() + plugin.getConfigData().getRtpIntervalSeconds() * 1000L;
}
private void teleportAreaPlayers() {
final List<Player> players = getPlayersInArea();
if (players.isEmpty()) return;
final AreaRTPConfig cfg = plugin.getConfigData();
final World world = Bukkit.getWorld(cfg.getWorld());
if (world == null) return;
// Try up to 3 positions per tick
for (int attempt = 0; attempt < 3; attempt++) {
final Location target = findValidRTPPosition(world, cfg, players);
if (target == null) continue;
// Fire block break/place events for all players
boolean allAllowed = true;
final List<Player> failedPlayers = new ArrayList<>();
for (final Player player : players) {
if (!canTeleportHere(player, target)) {
allAllowed = false;
failedPlayers.add(player);
}
}
if (allAllowed) {
// Teleport all
for (final Player player : players) {
player.teleportAsync(target).thenAccept(success -> {
if (success) {
plugin.getAdventure().player(player).sendMessage(MiniMessage.miniMessage().deserialize("<green>Teleported!</green>"));
} else {
plugin.getAdventure().player(player).sendMessage(MiniMessage.miniMessage().deserialize("<red>Teleport failed!</red>"));
}
});
}
return;
} else if (failedPlayers.size() == 1) {
// Retry for that player only, others teleport
final Player failed = failedPlayers.get(0);
final List<Player> allowed = new ArrayList<>(players);
allowed.remove(failed);
for (final Player player : allowed) {
player.teleportAsync(target).thenAccept(success -> {
if (success) {
plugin.getAdventure().player(player).sendMessage(MiniMessage.miniMessage().deserialize("<green>Teleported!</green>"));
} else {
plugin.getAdventure().player(player).sendMessage(MiniMessage.miniMessage().deserialize("<red>Teleport failed!</red>"));
}
});
}
// Try to find a valid position for the failed player only
for (int retry = 0; retry < 3; retry++) {
final Location retryLoc = findValidRTPPosition(world, cfg, Collections.singletonList(failed));
if (retryLoc != null && canTeleportHere(failed, retryLoc)) {
failed.teleportAsync(retryLoc).thenAccept(success -> {
if (success) {
plugin.getAdventure().player(failed).sendMessage(MiniMessage.miniMessage().deserialize("<green>Teleported!</green>"));
} else {
plugin.getAdventure().player(failed).sendMessage(MiniMessage.miniMessage().deserialize("<red>Teleport failed!</red>"));
}
});
break;
}
}
return;
}
// else, try another position for all
}
// If we get here, failed to find a valid position
for (final Player player : players) {
plugin.getAdventure().player(player).sendMessage(MiniMessage.miniMessage().deserialize("<red>Failed to find a safe RTP location!</red>"));
}
}
private Location findValidRTPPosition(@NotNull World world, @NotNull AreaRTPConfig cfg, @NotNull List<Player> players) {
for (int tries = 0; tries < 10; tries++) {
final int x = random(cfg.getRtpMinX(), cfg.getRtpMaxX());
final int z = random(cfg.getRtpMinZ(), cfg.getRtpMaxZ());
final int y = world.getHighestBlockYAt(x, z);
if (y > cfg.getMaxY()) continue;
final Block block = world.getBlockAt(x, y - 1, z);
if (!block.getType().isSolid()) continue;
final Location loc = new Location(world, x + 0.5, y, z + 0.5);
// Check min/max distance for all players
boolean valid = true;
for (final Player player : players) {
final Location ploc = player.getLocation();
final double dist = ploc.distance(loc);
if (dist < cfg.getMinDistance() || dist > cfg.getMaxDistance()) {
valid = false;
break;
}
}
if (valid) return loc;
}
return null;
}
private int random(int min, int max) {
return min + (int) (Math.random() * (max - min + 1));
}
private boolean canTeleportHere(@NotNull Player player, @NotNull Location loc) {
// Fire block break and place events at the target location
final Block block = loc.getBlock();
final BlockBreakEvent breakEvent = new BlockBreakEvent(block, player);
final BlockPlaceEvent placeEvent = new BlockPlaceEvent(block, block.getState(), block, null, player, true, null);
Bukkit.getPluginManager().callEvent(breakEvent);
Bukkit.getPluginManager().callEvent(placeEvent);
return !breakEvent.isCancelled() && !placeEvent.isCancelled();
}
}