Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- public class PlayerSwapPlugin extends JavaPlugin {
- private List<Player> players;
- private Timer timer;
- @Override
- public void onEnable() {
- players = new ArrayList<>();
- timer = new Timer();
- timer.scheduleAtFixedRate(new SwapTask(), 0, 5 * 60 * 1000); // Swap players every 5 minutes
- }
- @Override
- public void onDisable() {
- timer.cancel();
- }
- private class SwapTask extends TimerTask {
- @Override
- public void run() {
- // Get all online players
- players.clear();
- players.addAll(Bukkit.getOnlinePlayers());
- // Ensure there are at least two players
- if (players.size() < 2) {
- return;
- }
- // Randomly select two players to swap
- Random random = new Random();
- int index1 = random.nextInt(players.size());
- int index2 = random.nextInt(players.size());
- while (index2 == index1) {
- index2 = random.nextInt(players.size());
- }
- Player player1 = players.get(index1);
- Player player2 = players.get(index2);
- // Teleport the players to each other's locations
- player1.teleport(player2.getLocation());
- player2.teleport(player1.getLocation());
- // Notify the players
- player1.sendMessage("You have been swapped with " + player2.getName() + "!");
- player2.sendMessage("You have been swapped with " + player1.getName() + "!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement