Teleport help

Discussion in 'Plugin Development' started by RebzO1, Jun 6, 2014.

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

    RebzO1

    I have made a plugin that resets my 'mine_world' but first it needs to teleport all players in 'mine_world' to 'world'

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("mw-delete")) {
    2. for(Player online : Bukkit.getOnlinePlayers()) {
    3. online.teleport(online.getServer().getWorld("world").getSpawnLocation());
    4. }


    this teleports everyone and that's going to get annoying fast for my players, how would i achieve the intended output?
     
  2. Offline

    xTigerRebornx

    RebzO1 Add a check to see if the Player is in 'mine_world', and teleport them if that check is true
     
  3. Offline

    MoseMister

    Try this (in on phone so don't get the code box)


    Code:java
    1.  
    2. Player[] players = Bukkit.getOnlinePlayers()
    3. for(int A = 0; A >= players.length;){
    4. Player player = players[A];
    5. if (player.getWorld().equals(worldToBeRemoved){
    6. player.teleport(NewLocation);
    7. }
    8. A++;
    9. }
     
  4. Offline

    RebzO1

    MoseMister
    ya that didn't work for me ty though
     
  5. Offline

    Pixelatorx2

    Code:
    if(cmd.getName().equalsIgnoreCase("mw-delete")) {
    for(Player p : Bukkit.getOnlinePlayers()) {
    if(p.getWorld().getName().equalsIgnoreCase("mine-world")) {
    p.teleport(loc);
    } else {
    return false;
    }
    }
    }
    
     
  6. Offline

    RebzO1

    Pixelatorx2
    this is what it says now
    Code:java
    1. Player p = (Player) sender;
    2. Location loc = (Bukkit.getServer().getWorld("world").getSpawnLocation());
    3. if (cmd.getName().equalsIgnoreCase("mw-delete")) {
    4. // for(Player online : Bukkit.getOnlinePlayers()) {
    5. // online.teleport(online.getServer().getWorld("world").getSpawnLocation());
    6. // }
    7. for(Player o : Bukkit.getOnlinePlayers()) {
    8. if(o.getWorld().getName().equals("mine-world")) {
    9. o.teleport(loc);
    10. } else {
    11. return false;
    12. }
    13. }


    ignore the comments its just incase i need to go back to it

    This code just sends me /mw-delete in my game window but does nothing else
     
  7. Offline

    Pixelatorx2

    RebzO1
    Can I see your plugin.yml?
     
  8. Offline

    Kilovice


    Try adding a delay after they are teleported?
     
  9. Offline

    RebzO1

    Kilovice

    problem is, it is not actually teleporting the players at all

    The delete process works if i use the commented out lines and teleports everyone successfully.
     
  10. Offline

    Pixelatorx2

    RebzO1
    Can I see your plugin.yml please? I know the problem if I can see it
     
  11. Offline

    RebzO1

    Pixelatorx2
    name: CC-Essentials
    main: me.rebz.essentials.Main
    version: 1.0.1
    commands:
    mw-delete:
    usage: /mw-delete
    permission: ccessentials.reset
    permission-message: You can't do that!
    mw-create:
    usage: /mw-create
    permission: ccessentials.reset
    permission-message: You can't do that!
    mn-delete:
    usage: /mn-delete
    permission: ccessentials.reset
    permission-message: You can't do that!
    mn-create:
    usage: /mn-create
    permission: ccessentials.reset
    permission-message: You can't do that!
    permissions:
    ccessentials.reset:
    default: op
     
  12. Offline

    Pixelatorx2

    RebzO1
    Remove "usage: mw-delete" then it should work :p
    Just that line, everything else is needed
     
  13. Offline

    RebzO1

    Pixelatorx2
    Done that and it didn't fix anything
    Like i said if i change back to the original code it was teleporting everyone i only want those in the world 'mine_world' to be teleported so i don't see how removing usage helps
     
  14. Offline

    teej107

    RebzO1 World has a method to get the players in it. getPlayers() or something similar.
     
  15. Pixelatorx2 Lol. How was that meant to actually fix the problem? All that would do is ensure that there isn't a message to send.
    RebzO1 It doesn't help, you should put back in. For your information,the command's usage message is displayed whenever it hits a "return false" in your command - you should return true if the command was successful, which would prevent you being shown the usage message. In order to solve your problem, you should put in some debug statements to see what parts is executed. I'll give you a hint though, this is what your method currently does when somebody performs that command:

    • Get the list of online players
    • Check if the first person is in the mine_world world
    • If they are, teleport them to the main spawn and repeat with the next online player
    • If they're not in that world, command ended.
    Which I doubt is what you want. As soon as it finds someone who isn't in the mine_world world, it will stop looking, regardless of where everyone else is.
     
  16. Offline

    teej107

    AdamQpzm
    theWorld.getPlayers() would be a slightly better option.
     
  17. Offline

    Pixelatorx2

    AdamQpzm
    He asked why there was a message, so I told him how to remove it.

    As to this, he is trying to get everyone in the world, ending the command there for one person wouldnt help at all frankly.

    and what do you think this is doing?
     
  18. Pixelatorx2 But by removing the message, you're just hiding the real issue, not solving it. And that solution creates another problem - what if he wants a usage message? And that would be like solving an exception by catching it and making sure that it doesn't print the stacktrace... you've hidden the problem, not solved it.

    And I didn't say that's what he should do, I said that's what he's currently doing.

    teej107 As above, I was simply telling him what he was doing, not what he should be doing.
     
  19. Offline

    Pixelatorx2

    AdamQpzm
    I never have the usage parameter, yet I still get Stacktraces fine.. So I dont see how I was hiding the problem.
     
  20. Pixelatorx2 I'm not saying not having a usage message will prevent stacktraces, I'm saying that these two situations essentially work on the same logic that you're advising, and both aren't really solutions:

    Problem: "My command's usage message is being displayed"
    Solution: "Remove the usage message, then it has nothing to display"

    Problem: "I'm getting an exception"
    Solution: "Catch the exception, and then don't print it out. The error will stop displaying."

    The usage isn't a "show this every time someone performs the command" it's a "show this when the command returns false". The real solution is to return true if the command is successful, not to remove the message.
     
  21. Offline

    Pixelatorx2

    I never said it would fix it >.<
     

  22.  
  23. Offline

    Plo124

    RebzO1
    The reason this code:
    doesnt work is because you are returning (stopping) the code from being executed at the wrong time.

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("yourCommandHere"){
    2. for (Player p : Bukkit.getWorld("mine-world").getPlayers()){
    3. p.teleport(loc);
    4. }
    5. sender.sendMessage("Teleported all players in mine-world to world");
    6. return true;
    7. }


    Edit:
    For future reference, returning true or false or whatever stops executing that method. It is important to complete all relevant code before a return (if you need to return). Putting a if statement with a return in it will only return if the if was true.

    With the bukkit commandHandler you made, it was saying the usage of the command because you were returning false, which basically says "I didnt do anything useful with this command", which is useful if the arg you were coding was not one of the ones you coded (by returning false at the bottom of the commandHandler). Returning true means "Yes, I needed to do something with this command", and it wont send the usage (or error if there is no usage) to the player.
     
  24. Offline

    RebzO1

    Plo124
    I will try this in a few i need my coffeez just wanted to thank you for the reply!

    Plo124
    with a lil modification it works now i can finally delete worlds without stopping the server

    ty for your help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
Thread Status:
Not open for further replies.

Share This Page