#Making random players tp

1 messages · Page 1 of 1 (latest)

upper zephyr
#

Trying to make random players tp to certain coords

#

and other things but yea

eternal tapir
#

[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]

upper zephyr
#

atm im making a command to show all online players just so i know that works

maiden scaffold
#

get a random player from Bukkit.getOnlinePlayers() and teleport it to the location?
And use a runnable for that

slate solstice
#

@upper zephyr how goes it

upper zephyr
#

horrible

slate solstice
#

u know java?

upper zephyr
#

i mean i guess

slate solstice
#

if not its fine because this is how you learn

upper zephyr
#

not very good

slate solstice
#

Alr, can u show me what you got so far?

upper zephyr
#
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

slate solstice
#

._.

#

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?

upper zephyr
#

it prints enable

#

yml is right

slate solstice
#

ok

#

cool

#

so there's a few additional things you need to do if you want custom commands

#

first, make a seperate class

upper zephyr
#

thats not necessary tho

#

but fine

slate solstice
#

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

upper zephyr
#

ok

slate solstice
#

Also i thought you were trying to swap 2 players?

upper zephyr
#

no

slate solstice
#

ok

#

are you like

upper zephyr
#

i want to players to tp to certain coords

slate solstice
#

pitting them against each other

#

or smtn

upper zephyr
#

basicly the main goal is to have abattle arena

#

yep

slate solstice
#

alright

upper zephyr
#
    public void onEnable() {
        // Plugin startup logic
        getServer().getPluginManager().registerEvents(new events(), this);
        getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Enabled");
        this.getCommand("start").setExecutor(new commands());
    }```
slate solstice
#

that's a little harder

upper zephyr
#

this look right?

slate solstice
#

yep

#

AUGH

#

please rename commands to Commands

upper zephyr
#

._.

#

fine

slate solstice
#

I'm sorry you'll realize later

upper zephyr
#

lol

#

ok

#

does the code i sent look good or just restart

slate solstice
#

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

upper zephyr
#

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

slate solstice
#

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)

slate solstice
#

wdym by that?

upper zephyr
#

like once 2 people finished dueling winner gets tped away and they pick a new 2 players

slate solstice
#

ok um

#

add them to a set?

#

or add their uuid to a set?

upper zephyr
#

maybe

#

idk best way

#

btw does my commands file need to be in commands package

slate solstice
#

¯_(ツ)_/¯

#

if you want to

upper zephyr
#

ok

upper zephyr
#

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);
slate solstice
#

sorry cant help rn :/

slate solstice
#

wait is ur ign catnip???

upper zephyr
#

no

#

datnip

#

why lol

maiden scaffold
#

you cant cast a player to a component

upper zephyr
#

oh

#

well how can i transfer it to a string

eternal tapir
#

.toString()

#

probably

slate solstice
#

Why do u need it to be q componènt

upper zephyr
#

my ide just reccomended that /shrug