Random Arena Teleporting Help.

Discussion in 'Plugin Development' started by ZomBlade_Shadow, Jan 28, 2015.

Thread Status:
Not open for further replies.
  1. Offline


    Ok,so basically,I did a Matchmaking plugin (with the help of people on the forums) and the plugin is that:

    - Doing /setarena [Name] sets an arena in a config file (exactly like the /sethome command in essentials)

    - Doing /goarena [Name] makes you go to that arena (only for mods though,so this isn't important)

    - When you do /Random and p.getName() isn't in the ArrayList,it adds you to the Arraylist (queue)
    else it removes you.

    - When there are 2 or more players in queue,it takes 2 random players of that queue.

    - What I need is to take those 2 players and tp them to a random arena in my set arenas (Config Files)

    REMOVED >.>

    So how could I get a random arena coordinates and tp those 2 players in it (it doesn't matter where,just inside,so just TP both of them to the same arena,same place)

    Right about here:

    REMOVED >.>
    Ofc all credit will go to who helped me code.
    It's not debugging,it's helping me code bits of the plugin,don't criticize
    [Noob Alert]
    Last edited: Jan 31, 2015
    GrandmaJam likes this.
  2. @ZomBlade_Shadow
    Do you actually define the bounds of the arena? You would need two locations to define the bounds of the arena so the players could get teleported into it.
    But to answer your question, you would need to get a list of the arenas in your config and teleport the players into a random location inside of the randomly chosen arena's bounds.
  3. Offline


    Nice thx! but how do I define bounds?

    I think you misunderstood what I want lol
    I want to tp the players to a random arena I set.Not a random location in 1 arena.

    Just take any arena in the config,and do a new Random() thingy to get 1 out of all arenas saved in the config file.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 13, 2016
  4. Offline


    @ZomBlade_Shadow Could I see how your configuration file looks like when you set an arena?

    If your configuration file looked like this:

             x: 54
             y: 64
             z: - 90
             x: -82
             y: 64
            z: 492
    Then you can make a method like this one below:

    1. public void teleportPlayers(Player one, Player two) {
    3. int size = cfg.getConfigurationSection("arenas").getKeys(false).size();
    4. // This line of code should return '2' since there are two arenas in your configuration file.
    6. int select = new Random().nextInt(size) + 1;
    7. // This line of code will now select a random arena between '1' and '2'. I added the "+1" because numbers start at '0' when calling nextInt() so '+ 1' is needed to increase the range of numbers from '0-1' to '1-2'. Correct me if I'm mistaken because I don't remember well.
    9. int xLoc = cfg.getInt("arenas." + select + ".x");
    10. int yLoc = cfg.getInt("arenas." + select + ".y");
    11. int zLoc = cfg.getInt("arenas." + select + ".z");
    12. // This line of code is grabbing the coordinates from the random arena and assigning it to variables.
    14. Location location = new Location(Bukkit.getWorld("<insert world name>", xLoc, yLoc, zLoc);
    15. // A Location object is being constructed using the three coordinate values of the random arena.
    17. one.teleport(location);
    18. two.teleport(location);
    19. // This final method teleports the players to the constructed location.
    21. }

    Obviously, this method can be shortened but I thought it would be best to show you a step-by-step process so you could understand better. Also, if any developers see a mistake in my code above, please do let me know. Thanks!
    Last edited: Feb 2, 2015
  5. Offline


    You should create an arraylist and save location in when you load your plugin.
    Then you do a method for return a location with a random.
    And you will just have to teleport them

    String chosen = inqueue.get(select);
    Player chosenplayer = Bukkit.getPlayer(chosen);

    You should check if the player is online, or you will get a nice error ;)
    Last edited: Jan 29, 2015
    Synic likes this.
  6. Offline


    @ZomBlade_Shadow Little suggestion: If you want to teleport multiple players you can use:
    public void teleportPlayers(Player... targets){
        for(Player target : targets){
              // Do stuff with location.
              target.teleport(/* Location */);
    Synic likes this.
  7. Offline


    I would convert the Location to a String, save it in a list, retrieve random string from that list -> random location.

    private String toString(Location loc){
          return loc.getWorld().getName()+","+loc.getBlockX()+","... etc
    private Location toLocation(String s){
          String[] parts = thatStringFromTheLocation;
          return new Location(parts[0], parts[1] etc);
    //To get a random string
    List<String> urListWithStringLocations = new ArrayList<String():
    return thatList.get(new Random().nextInt(thatList.size()));
  8. Offline


    @Unica I believe your code causes a error in the whole code of him......
    Because.. You return code in the class XD
  9. Offline


    Your main class implements CommandExecutor because..?
    ChipDev likes this.
  10. Offline


    @DemKazoo Main don't need to extend CommandExecutor... It already implements in JavaPlugin.
  11. Offline


    Thats exactly what I meant. The OP has implemented CommndExecutor.
    ChipDev likes this.
  12. Offline


    @DemKazoo I don't saw that. But it's not a problem.
  13. Offline


    soz about that :c
    Thanks for all the ideas :eek: i'll try em' all out ;)


    Yep, it's like this:

        world: world
        x: -798.7161225116544
        y: 4.0
        z: 270.60570837176306
        yaw: -129.285888671875
        pitch: 5.19191312789917

    I'm trying it out c:
    It was actually cfg.getInt cause getString don't work Thanks :)

    I'm getting a NullPointerException.Hmm:

    [18:35:44 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'rand
    om' in plugin PeeVeePee v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:17
    5) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
            at me.ZomBlade.PeeVeePee.Main.onCommand(Main.java:49) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
            ... 13 more

    My code:

    REMOVED >.>
    <Edit by mrCookieSlime: Merged posts. Please don't triple post. There is an Edit Button right next to the Date.>
    Last edited by a moderator: Jan 31, 2015
  14. Offline


    it's a configurationsection, not a list. You cannot get back a line by a number. Or you have to call you arene (1 2 3 4 etc...)

    Or you have to do a loops.
    int i = 0;
    for (String arene : getconfigurationsection.get(arene).getkeys(false){
    if ( i ==random) //get the string.

    Note: File f = new File(plugin.getdatafolder, namle.yml);
    pluign.getdatafolder will return the folder created by your plugin.
    At least, you should put a saveDefautconfig() in your onEnable and create a config.yml.
    It will be more easy.
    Last edited: Jan 29, 2015
  15. Offline


    In your configuration file, you have "Arenas" not "arenas". Java is case-sensitive.

    int size = cfg.getConfigurationSection("arenas").getKeys(false).size(); - in this line i put "arenas" but for your code you should put "Arenas".
  16. Offline



    It's called pseudo code. No shit sherlock lol.
  17. Offline


    Thx everybody! :D
    It works like a charm!
    Had to put capital "A" in "Arenas" and a "." so it's:
    int size = cfg.getConfigurationSection("Arenas.").getKeys(false).size();
    Thx to
    @BorisTheTerrible <3

    If any of you 5 (Synic,caderape,Unica,Experminator,BorisTheTerrible) want the code,tell meh, it's yours too =D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 13, 2016
    Synic likes this.
  18. Offline


    I'm glad it worked! Thank you for offering your code but I've actually become inspired to write my own arena/dueling plugin based off this thread xD.
    ZomBlade_Shadow likes this.
  19. Offline


    Haha thanks!
    And good luck with that! it was really long for me to do it + I asked alot of help :>
Thread Status:
Not open for further replies.

Share This Page