Solved Random Arena

Discussion in 'Spigot Plugin Development' started by OopsyPoopsy, Apr 26, 2017.

Thread Status:
Not open for further replies.
  1. Hello Spigot.
    I need help choosing a random arena for my practice plugin. I want the plugin to chose a random string from the config and teleport the players to the arena specified. I have no clue where to get started with this.

    Code (Java):

    package me.idc.Practice.Queue;



    import org.bukkit.Bukkit;

    import org.bukkit.Material;

    import org.bukkit.entity.Player;

    import org.bukkit.event.EventHandler;

    import org.bukkit.event.Listener;

    import org.bukkit.event.inventory.InventoryClickEvent;

    import org.bukkit.inventory.Inventory;

    import org.bukkit.inventory.ItemStack;

    import org.bukkit.scheduler.BukkitRunnable;



    import me.idc.Practice.Main;

    import me.idc.Practice.UnrankedSelector;

    import me.idc.Practice.Utils;



    public class QueueAdder implements Listener {

    Main plugin;

    public QueueAdder(Main instance) {

    plugin = instance;

    }


    @EventHandler

    public void onInventoryClick(InventoryClickEvent e) {

    Player p = (Player) e.getWhoClicked();

    ItemStack clicked = e.getCurrentItem();

    Inventory inventory = e.getInventory();

    if (inventory.getName().equals(UnrankedSelector.kits.getName())) {


    if (clicked.getType() == Material.GOLD_SWORD) {

    e.setCancelled(true);

    p.closeInventory();

    Utils.queueAdded(p);

    Queues.nodebuff.add(p);

    p.sendMessage("§eAdded to §6NoDebuff §equeue.");

    new BukkitRunnable() {

    @SuppressWarnings("deprecation")

    public void run() {

    for (Player all : Bukkit.getServer().getOnlinePlayers()) {

    Player target = all.getPlayer();

    if (Queues.nodebuff.contains(target)) {

    if (!target.getName().equals(p.getName())) {

    Queues.inmatch.add(p);

    Queues.inmatch.add(target);

    Queues.nodebuff.remove(p);

    Queues.nodebuff.remove(target);

    //pick arena & teleport


    }

    }

    }

    }

    }.runTaskAsynchronously(plugin);

    }

    }

    }

    }
     
     
  2. Assuming, you have a list of arena. You want to get one random arena. Let's see how you can do that.

    Code (Text):
    List<World> arenas = new ArrayList<World>(); // your arenas list. World can be remplaced with anything

    arenas.add(Bukkit.getWorld("wonderful"));
    arenas.add(Bukkit.getWorld("red-carpet"));
    arenas.add(Bukkit.getWorld("yoplait"));

    World randomArena = arena.get(new Random().nextInt(arenas.size())); // we want a random integer between 0 and arenas.size()
     
  3. How would I use this in terms of the config?

    Config:

    Code (Java):

    test1:
      x1: -89.35107494725492
      y1: 63.0
      z1: -0.14359376590873218
      x2: -89.43945570450853
      y2: 63.0
      z2: -6.609562144139142
    test2:
      x2: -82.32616891212588
      y2: 63.0
      z2: -6.7062851276614825
      x1: -82.2353079276049
      y1: 63.0
      z1: -0.02452328878497831
     
     
  4. With the same method, but when you got the arena name you get location.

    Code (Text):
    String arenaName;

    Location location = new Location(arenaName, config.getDouble(arenaName + ".x1"), config.getDouble(arenaName + ".y1"), config.getDouble(arenaName + ".z1"));
     
  5. Code (Java):

    List<World> arenas = new ArrayList<World>();

    String arenaName;



    Location location = new Location(arenaName, plugin.getConfig().getDouble(arenaName + ".x1"), plugin.getConfig().getDouble(arenaName + ".y1"), plugin.getConfig().getDouble(arenaName + ".z1"));

    World randomArena = arenas.get(new Random().nextInt(arenas.size()));
     
     
  6. God please... learn java first guy..

    Code (Text):
    List<String> arenas = yourConfiguration.getStringList("arenas");

    String arenaName = arenas.get(new Random().nextInt(arenas.size()));

    Location location = new Location(arenaName, plugin.getConfig().getDouble(arenaName + ".x1"), plugin.getConfig().getDouble(arenaName + ".y1"), plugin.getConfig().getDouble(arenaName + ".z1"));
    I can't do anything else for you.
     
    • Like Like x 1
  7. What I would do for stuff like this is put the coordinates in a hashmap, then do a random to select those coordinates.
     
  8. It's a solution but for the optimisation it's not good. Now, let's see what @OopsyPoopsy will do.
     
  9. Read all the locations or arena names in, add them into an array.

    String[] arenas = {"arena1","arena2","arena3","arena4"};

    Obviously you would populate this array with your config

    String selArena = arenas[threadLocalRandom.current().nextInt(0,arenas.length) ];

    Would pull a random arena name from the array, this could be done just as easily with locations

    Sent from my iPhone using Tapatalk
     
    • Like Like x 1
  10. That's not changing anything with my method.. I'll give the same answer..
     
  11. Sorry but it's very difficult to learn from the code provided when there's not really an explanation.
     
  12. What exactly do you not understand? That way we can go more in depth with the issues you are having.
     
  13. I don't know what to do with his code...
     
  14. Inkzzz

    Resource Staff

    Strongly advise to use a location serialiser with the simple toString() and fromString() methods. This would be easier and faster for you.

    Also use ThreadLocalRandom#current rather than creating lots of random instances.
     
    • Agree Agree x 1
  15. Code (Java):

    List<String> arenas = plugin.getConfig().getStringList("arenas");



    String arenaName = arenas.get(new Random().nextInt(arenas.size()));



    Location loc1 = new Location(Bukkit.getWorld("world"), plugin.getConfig().getDouble(arenaName + ".x1"), plugin.getConfig().getDouble(arenaName + ".y1"), plugin.getConfig().getDouble(arenaName + ".z1"));

    Location loc2 = new Location(Bukkit.getWorld("world"), plugin.getConfig().getDouble(arenaName + ".x2"), plugin.getConfig().getDouble(arenaName + ".y2"), plugin.getConfig().getDouble(arenaName + ".z2"));
     
    is this what you mean?
     
  16. Yes ! You do it like a boss
     
  17. Ay! It worked! Thank you so much!
     
  18. I'm not sure but I think you may want to -1 from where you put arenas.getSize() as lists start at 0. For example say you have 20 areas in the list, get size will be 20, But the first arena is at .get(0).

    I may not be remembering that correctly though.
     
Thread Status:
Not open for further replies.