Solved Random Teleportation not working.

Discussion in 'Plugin Development' started by InsertAPI, May 17, 2015.

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

    InsertAPI

    Hello.

    I've just been making a little bit of code for a Random Teleporter.
    I'm not sure why this isn't working. Can I have a little help please! :)

    Code:java
    1. package com.InsertAPI.listeners;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.GameMode;
    6. import org.bukkit.Location;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.event.player.PlayerQuitEvent;
    12.  
    13. public class JoinLeave implements Listener {
    14.  
    15. Location spawn1 = new Location(Bukkit.getServer().getWorld("world"), -52, 69, 242);
    16. Location spawn2 = new Location(Bukkit.getServer().getWorld("world"), -49, 68, 242);
    17. Location spawn3 = new Location(Bukkit.getServer().getWorld("world"), -49, 68, 239);
    18. Location spawn4 = new Location(Bukkit.getServer().getWorld("world"), -52, 69, 239);
    19.  
    20. @EventHandler
    21. public void onPlayerJoin(PlayerJoinEvent e){
    22. Player p = e.getPlayer();
    23. p.teleport(spawn1 || spawn2 || spawn3 || spawn4); // supposed to pick a random one.
    24. }
    25. }


    [​IMG]

    [​IMG]
     
    Last edited: May 17, 2015
  2. Offline

    timtower Administrator Administrator Moderator

    @InsertAPI You need to insert a random there, the || is a character for if statements, you can't put it in regular code.
     
  3. Offline

    InsertAPI

    Hello there!
    Sorry for the late reply.

    Can you give me some links to get started please? I have never used 'random' before. :)
     
  4. Offline

    timtower Administrator Administrator Moderator

    @InsertAPI You call 45 minutes a late reply :confused:
    Random random = new Random()
    random.nextInt
     
  5. Offline

    uksspy

    Also, just a suggestion: I would recommend putting your locations in a collection to make it easier if you ever wanted to add more.
     
  6. Offline

    InsertAPI

    Thank you so much!

    A quick question, is the most efficient way to do it?

    Code:java
    1. Random random = new Random();
    2. int Chance = random.nextInt(4);
    3.  
    4. if(Chance == 0) {
    5. p.teleport(spawn1);
    6. }else if(Chance == 1){
    7. p.teleport(spawn2);
    8. }else if(Chance == 2){
    9. p.teleport(spawn3);
    10. }else if(Chance == 3){
    11. p.teleport(spawn4);
    12. }
     
  7. Offline

    timtower Administrator Administrator Moderator

    @InsertAPI Or you make a list of spawns and then use list.get(chance)
     
  8. Offline

    InsertAPI

    Alright!

    Thanks so much!
     
Thread Status:
Not open for further replies.

Share This Page