Teleporting

Discussion in 'Spigot Plugin Development' started by GamemodeSurvival, Sep 12, 2019 at 5:52 AM.

  1. Code (Text):
            if (player.isOp() && args[0].equalsIgnoreCase("set") || Core.getFileManager().isAdmin(player) && args[0].equalsIgnoreCase("set")) {
                Common.tell(player, msg.getString("spawn-set"));
                cfg.set("spawnpoint", getStringFromLocation(player.getLocation()));
                cfg.saveConfig();
            } else {
                if(cfg.isConfigurationSection("spawnpoint")) {
                    Location spawn = getLocationFromString(cfg.getString("spawnspoint"));
                    Location destination = new Location(spawn.getWorld(), spawn.getX(), spawn.getY(), spawn.getZ(), spawn.getYaw(), spawn.getPitch());
                    Common.tell(player, msg.getString("teleporting-to-spawn"));
                    player.teleport(destination);
                } else {
                    Common.tell(player, msg.getString("spawn-not-set"));
                }
            }
    Nothing after the else statements works. I even tried just sending a message right after and it still doesn't work.
     
  2. I see you're (most likely) using a command.
    Are you handling the command in your main class (the one that extends JavaPlugin)?
    If not, did you set the executor as the executor for the command?
    Have you confirmed the onCommand method is being called?

    In general:
    What are you trying to do?
    What are you expecting to happen?
    What is happening instead?
     
  3. Please read what I wrote under the code.
     
  4. I did read what you wrote. That doesn't answer any of my questions.

    Please answer my questions.
     
  5. If it's not running code as expected ensure that block of code is being called, this is usually failing to register listeners or in this case properly registering the command in your program's entry point. If you are sure that is being ran (put a debug statement outside of all the conditions to ensure..) then debug your conditional statements to ensure everything is doing what it should.
     
    #5 Lubdan, Sep 12, 2019 at 7:59 AM
    Last edited: Sep 12, 2019 at 12:24 PM
  6. @GamemodeSurvival

    Aside from what Lubdan suggested (very sound suggestion), your code can be made more readable and less redundant:
    Code (Java):
    if (player.isOp() && args[0].equalsIgnoreCase("set") || Core.getFileManager().isAdmin(player) && args[0].equalsIgnoreCase("set")) {

    //BECOMES:

    if (args[0].equalsIgnoreCase("set") && (player.isOp() || Core.getFileManager().isAdmin(player))) { //argument is set AND player has op OR perms

    //////////////////////NEXT FIX//////////////////////

    if (args[0].equalsIgnoreCase("set") && (player.isOp() || Core.getFileManager().isAdmin(player))) {
        Common.tell(player, msg.getString("spawn-set"));
        cfg.set("spawnpoint", getStringFromLocation(player.getLocation()));
        cfg.saveConfig();
    } else {
        if(cfg.isConfigurationSection("spawnpoint")) {
            Location spawn = getLocationFromString(cfg.getString("spawnspoint"));
            Location destination = new Location(spawn.getWorld(), spawn.getX(), spawn.getY(), spawn.getZ(), spawn.getYaw(), spawn.getPitch());
            Common.tell(player, msg.getString("teleporting-to-spawn"));
            player.teleport(destination);
        } else {
            Common.tell(player, msg.getString("spawn-not-set"));
        }
    }

    //BECOMES:

    if (args[0].equalsIgnoreCase("set") && (player.isOp() || Core.getFileManager().isAdmin(player))) {
        Common.tell(player, msg.getString("spawn-set"));
        cfg.set("spawnpoint", getStringFromLocation(player.getLocation()));
        cfg.saveConfig();
    } else if (cfg.isConfigurationSection("spawnpoint")) {
            Location spawn = getLocationFromString(cfg.getString("spawnspoint"));
            Location destination = new Location(spawn.getWorld(), spawn.getX(), spawn.getY(), spawn.getZ(), spawn.getYaw(), spawn.getPitch());
            Common.tell(player, msg.getString("teleporting-to-spawn"));
            player.teleport(destination);
    } else {
        Common.tell(player, msg.getString("spawn-not-set"));
    }
    //The above is more indicative of the actual logic occurring, and may lead you to fixing a logic error that might be the cause of your problem.
    //Hint, hint, you're missing an args.length check, which can be pretty fatal to any command with multiple arguments.
    Please, make sure to check the comments in my code (and hopefully take my suggestions into account).
     
  7. I answered all of your questions if you read what I wrote under the code. Everything AFTER the else statement doesn't work. So the command is working just everything after the ELSE statement isn't.
     
  8. First of all, there's two else statements. Which one is "the" else statement?

    Secondly, is it really this difficult to answer the questions I posted? Why do I need to keep guessing?
    Are you handling the command in your main class (the one that extends JavaPlugin)? - No answer
    If not, did you set the executor as the executor for the command? - No answer
    Have you confirmed the onCommand method is being called? - I guess you're trying to say it's getting called, but beating around the bush

    What are you trying to do? - still no answer
    What are you expecting to happen? - still no answer
    What is happening instead? - still no answer


    You need to help me understand what you're trying to do. I can't read your mind.

    For instance, tell me what the command is supposed to do. What's supposed to happen in different situations? What's happening instead?

    If you're not sure whether or not some piece of code is run, you can simply debug it by, for example Bukkit.getLogger().info("I am here");. You can also add variables in the debug output to very they are what you think they should be.
     
  9. The code in the second else statement doesn't work. Its the code idk whats wrong with it. It doesn't matter if its a command its suppose to teleport the player to the coords.
     
  10. @drives_a_ford I'm beginning to feel your pain.

    @GamemodeSurvival You're either new to this, ignorant, or both. You've just contradicted yourself. The code in the second else doesn't call teleport at all, just Common.tell.

    If you want this to get resolved, it's best you answer the questions, and also consider my suggestions in my previous post. If you don't know how to answer the questions asked of you, then you probably should learn a bit more before attempting this.
     
  11. I meant first else under the if and new??? Check profile.
     
  12. You've once again contradicted yourself. You've posted 179 times on these forums. You're not listening and/or are arguing with 2 people who have 1,746 and 2,310 posts respectively on these forums. How are you not new, comparatively?

    Anyways, I found your issue now that you've pointed me to the proper section of code (only took you 4 tries), but you should really change the way your code is structured as I suggested here.

    @GamemodeSurvival Here is your issue:
    Code (Java):
    cfg.set("spawnpoint", getStringFromLocation(player.getLocation()));
    // ...

    if(cfg.isConfigurationSection("spawnpoint")) {
            Location spawn = getLocationFromString(cfg.getString("spawnspoint"));
    If you don't understand what is wrong... you can't be helped.