#Making random players tp
1 messages · Page 1 of 1 (latest)
[2:23 PM] Coll1234567: getOnlinePlayers
[2:23 PM] Coll1234567: Stick that in a list and then just grab 2 random indexes
[moving it here for later]
atm im making a command to show all online players just so i know that works
get a random player from Bukkit.getOnlinePlayers() and teleport it to the location?
And use a runnable for that
@upper zephyr how goes it
horrible
u know java?
i mean i guess
if not its fine because this is how you learn
not very good
Alr, can u show me what you got so far?
package srnyxbattlearena.srnyxbattlearena;
import org.bukkit.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import srnyxbattlearena.srnyxbattlearena.events.events;
import java.util.Collection;
import java.util.Random;
public final class Srnyxbattlearena extends JavaPlugin {
public static Player[] randomPlayer = null;
public static int ranNum = 0;
@Override
public void onEnable() {
// Plugin startup logic
getServer().getPluginManager().registerEvents(new events(), this);
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Enabled");
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (label.equalsIgnoreCase("start")) {
if (sender instanceof Player) {
Collection<? extends Player> allPlayers = Bukkit.getOnlinePlayers();
Random random = new Random();
boolean ready = false;
int ranNum = 0;
Player[] randomPlayer = null;
while (ready == false) {
ranNum = random.nextInt(allPlayers.size() - 0);
randomPlayer = allPlayers.toArray(new Player[allPlayers.size()]);
if (randomPlayer[ranNum].getLocation().subtract(0, 1, 0).getBlock().equals(Material.SMOOTH_STONE)) {
ready = true;
}
}
Location locationOfArena = new Location(randomPlayer[ranNum].getWorld(), 123, 123, 123);
randomPlayer[ranNum].teleport(locationOfArena); }
Bukkit.broadcastMessage("Debug: RandomPlayer = "+randomPlayer[ranNum].toString());
}
return true;
}
}
this just brodcasts to see if it works
it dont work
thats the goal
._.
uhhhh
first of all,
You should make a seperate class for commands
Did you build with plugin.yml and stuff?
It it not printing "enable" to the console?
ok
cool
so there's a few additional things you need to do if you want custom commands
first, make a seperate class
yeah, but you don't want to clutter your main class
trust me, you'll hate yourself for it later.
i know this.
after that, implement CommandExecutor
In the other class
After that you can override onCommand
And whatever you put in the overridden method will be in any new instances and also get called when a command runs
(after you do this.getCommand("<command>").setExecutor(new <your command class>()))
^^ in your main class onEnable btw
ok
Also i thought you were trying to swap 2 players?
no
i want to players to tp to certain coords
alright
public void onEnable() {
// Plugin startup logic
getServer().getPluginManager().registerEvents(new events(), this);
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Enabled");
this.getCommand("start").setExecutor(new commands());
}```
that's a little harder
this look right?
I'm sorry you'll realize later
bc when people name variables in java they do Player player or (in this case) commands commands
and you cant really do that
ok lemme see
i only want to tp them if there standing on smooth stone
atleast thats best way i can think of
to make so i dont repeat players
Clone the players location before subtracting
I think it might modify the players loc if you dont but im not completely sure
Your code wont work btw
You can't use while loops in the server
it'll prevent everything else from running
(unless its on like a seperate thread or smtn but dw about that)
...to make sure you don't repeat players?
wdym by that?
like once 2 people finished dueling winner gets tped away and they pick a new 2 players
ok
this is somthing i found on the forms
ArrayList<Player> allPlayers = new ArrayList<>();
for (Player players : Bukkit.getOnlinePlayers()) {
allPlayers.add(players);
}
int random = new Random().nextInt(allPlayers.size());
Object picked = allPlayers.get(random);
Bukkit.broadcast((Component) picked);
sorry cant help rn :/
wait is ur ign catnip???
you cant cast a player to a component
Why do u need it to be q componènt
my ide just reccomended that /shrug