Solved Tping sender to randomized specific coords

Discussion in 'Plugin Development' started by ProSl3nderMan, Jul 13, 2015.

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

    ProSl3nderMan

    So recently I've gotten into coding plugins and I've gotten use to getting my problems fixed from google. Today that changes, I can't find the solution to my problem on google so I came here. I want to tp a player who sends /cr to randomized specific coords.

    Lost ya there? Lol, this should show you guys what I mean:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
        if (cmd.getName().equalsIgnoreCase("cr") && sender instanceof Player) {
         
            Player player = (Player) sender;
         
            Random random = new Random();
          //LOOK HERE
            int x = 59 || 47 || 35 || 23
            int y = 134;
            int z = -68;
         
            player.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
    FYI, "||" means "or" and x, y, and z are coords in minecraft. I know it doesn't work like that, just showing for an example. I know a little about randomize ints, but can't figure out how to use it for my situation. So please help.
     
    Last edited: Jul 13, 2015
  2. Offline

    1Rogue

    Code:java
    1. Random r = ThreadLocalRandom.current();
    2. int randomX = r.nextInt(10000); //random number from 0-9999


    from there, just get the highest block of the coordinates and teleport them.
     
  3. Offline

    ProSl3nderMan

    No, this is not what I meant. I meant having specific coords. For another example, survival games when people are tped to deathmatch, they are tped to a random pod where they wait for the deathmatch to start.
     
  4. Offline

    SuperOriginal

    Store the locations in a set or something then teleport them to a random one of those.
     
  5. Offline

    ProSl3nderMan

    Can you explain on how to do that?
     
  6. Offline

    _Error

    What I would do is detect how much players there are and tp them, and if I want the pattern to be random, Ill use another method, You can ask for it.
     
  7. Offline

    1Rogue

    Then keep a reference of those coordinates, and then teleport people to them accordingly.
     
  8. Offline

    ProSl3nderMan

    I wish for it to tp only the sender.
    If this means to do the /tp to coords command manually, no. I want this to be automatic. I would like to know how to code it to do exactly that.
     
  9. Offline

    _Error

    You mean something like when a player uses /tp it will teleport him to a random location that you specified?
    For Example we have those 3 coords:
    - 100 226 1078
    10 111 1022
    1 80 612
    When sender does /randomtp it will tp him to one of these 3 coords right?

    EDIT: Do you want to tp him to a location with percentage or n0?


    DOUBLE EDIT:
    Im sure there's a easier way than this, But you can try it, UNTESTED.

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    Player p = (Player) sender;
    if (command.getName().equalsIgnoreCase("tpmetoplace")) {
    int random = (int) (Math.random() * 3 /* <- Put how much locations there are here */ + 1);
    if (random == 1) {
    int x = 59;
    int y = 134;
    int z = -68;
    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
    }else if(random == 2) {
    int x = 69;
    int y = 666;
    int z = -68;
    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
    }else if(random == 3) {
    int x = 19;
    int y = 222;
    int z = 1820;
    p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
    }
    }
    return true;
    }
     
    Last edited: Jul 13, 2015
    ProSl3nderMan likes this.
  10. Offline

    ProSl3nderMan

    Yes, that is exactly what I mean.

    I seem to get an error code on line 23 for you. For me it's line 51:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("tpmetoplace")) {
                int random = (int) (Math.random() * 3 /* <- Put how much locations there are here */ + 1);
                    if (random == 1) {
                        int x = 59;
                        int y = 134;
                        int z = -68;
                        p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
            }        else if(random == 2) {
                        int x = 69;
                        int y = 666;
                        int z = -68;
                        p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
            }        else if(random == 3) {
                        int x = 19;
                        int y = 222;
                        int z = 1820;
                        p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
            }
            }
            p.sendMessage(ChatColor.GREEN + "You have been teleported to Alcatraz.");
           
            return true;
    }
        return false;
    }
    }
    Says "- Syntax error on token "}", { expected after this token".

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

    _Error

    Did the code I post worked?
     
  12. Offline

    SuperOriginal

    That is probably the worst possible way you could achieve this.
     
  13. Offline

    Creeperzombi3

  14. Offline

    ProSl3nderMan

    Then can you show me a different way?
    When I removed that one, the two around "return false" both get errors. The first error is the same error as before and the second error says "Syntax error, insert "}" to complete ClassBody"
     
  15. Offline

    mariosunny

    Is this what you are looking for?
    Code:
    List<Location> locations;
    Random random;
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        
        if (cmd.getName().equalsIgnoreCase("cr") && sender instanceof Player) {
    
            teleportToRandomLocation((Player) sender);
        }
    } 
    
    public void teleportToRandomLocation(Player player) {
    
        player.teleport(locations.get(random.nextInt(locations.size())));
    }
     
  16. Offline

    ProSl3nderMan

    No, I need it to be specific coords. I have 3 coords, but I need people to be randomly sent to each 3 spots. So I do /cr and it sends me to spot 1, someone else may do the command and it sends them to spot 3.
     
  17. Offline

    Creeperzombi3

    @ProSl3nderMan
    Make it, it should fix your error
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. Player p = (Player) sender;
    3. if (cmd.getName().equalsIgnoreCase("tpmetoplace")) {
    4. int random = (int) (Math.random() * 3 /* <- Put how much locations there are here */ + 1);
    5. if (random == 1) {
    6. int x = 59;
    7. int y = 134;
    8. int z = -68;
    9. p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
    10. } else if(random == 2) {
    11. int x = 69;
    12. int y = 666;
    13. int z = -68;
    14. p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
    15. } else if(random == 3) {
    16. int x = 19;
    17. int y = 222;
    18. int z = 1820;
    19. p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
    20. }
    21. }
    22. p.sendMessage(ChatColor.GREEN + "You have been teleported to Alcatraz.");
    23.  
    24. return true;
    25. }
     
  18. Offline

    ProSl3nderMan

    Didn't fix it, I'll post my whole code:


    Code:
    package me.ProSl3nderMan.myfirstplugin;
    
    import java.util.Random;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class myfirstpluginmain extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
       
    public void onEnable(){
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + "Has been enabled!");
    }
    
    public void onDisable(){
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + "Has been disabled!");
    }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        Player p = (Player) sender;
        if (cmd.getName().equalsIgnoreCase("tpmetoplace")) {
            int random = (int) (Math.random() * 3 /* <- Put how much locations there are here */ + 1);
            if (random == 1) {
                int x = 59;
                int y = 134;
                int z = -68;
                p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
            }        else if(random == 2) {
                int x = 69;
                int y = 666;
                int z = -68;
                p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
            }        else if(random == 3) {
                int x = 19;
                int y = 222;
                int z = 1820;
                p.teleport(new Location(Bukkit.getWorld("Alcatraz"), x, y, z));
            }
        }
        p.sendMessage(ChatColor.GREEN + "You have been teleported to Alcatraz.");
    
        return true;
    }
        return false;
    }
    }
     
  19. Offline

    Zombie_Striker

    @ProSl3nderMan
    1. JavaNamingConventions
    2. Remove the } from above p.sendmessage()
    3. You import Random, yet not use it.
    4. You are using the wrong logger. Stop Learning from Youtubers.
    5. You are instantly casting the sender to a player. What happens if the Console (who is not an instance of a player) sends the command.
     
  20. Offline

    ProSl3nderMan

    1: What do you mean by "JavaNamingConventions"?
    2: Thanks, for some reason I thought that was suppose to be there xD
    3: Yeah, I removed that once it popped up that it was unused. Thanks!
    4: Can you send me to a website that teaches it the right way then? That would be awesome.
    5: I don't want the console doing the command, only players should be able to do the command.


    Anyways, thank you guys for helping me out. This is solved ;3
     
    Last edited: Jul 14, 2015
Thread Status:
Not open for further replies.

Share This Page