[Help] Error with a simple plugin

Discussion in 'Spigot Plugin Development' started by SwagiWagi, May 30, 2016.

  1. Hello developers!

    I'm starting to develop spigot plugins, and just learning the basics.
    What is wrong with this code?
    Code (Text):
    package me.rom.com;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements CommandExecutor {

        public void onEnable() {
            getLogger().info("Hey");
            getCommand("Nope").setExecutor(this);
        }

        public void onDisable() {
            getLogger().info("Sup");
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (label.equalsIgnoreCase("Nope")) {

                if (sender instanceof Player)
                    return false;

                Player player = (Player) sender;
                player.setCanPickupItems(false);

                return false;
            }
            return false;
        }
    }
    Thanks.
     
  2. Next time actually tell us whats wrong with your code. But I'll take a guess and say that this is not what you want:
    Code (Text):
    if (sender instanceof Player)
        return false;
    Player player = (Player) sender;
    You never reach the third line if the sender is an instance of Player because you return false then. Guess you forgot to add a "!";)
     
  3. Yea, I should have told what the problem is, it is just not doing anything.
    I tried your code, still not working.
     
  4. The following code is what this well, then you see that he did wrong :)

    Code (Text):
    package me.rom.com;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements CommandExecutor {

        public void onEnable() {
            getLogger().info("Hey");
            getCommand("Nope").setExecutor(this);
        }

        public void onDisable() {
            getLogger().info("Sup");
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (label.equalsIgnoreCase("Nope")) {

                if (sender instanceof Player) {
                Player player = (Player) sender;
                player.setCanPickupItems(false);

                return true;
                }
                return true;
            }
            return false;
        }
     
  5. I didn't give you any code. I just copied the part that is causing your error and explained to you, why it's not working;)

    Edit: Alright you got spoonfed. Well. Good luck then^^
     
  6. I mean the "!".
     
  7. How did you add it? I am pretty confident that it will work^^
     
  8. Sorry I didn't got you.
    I may have not the best english...
     
  9. Code (Text):
    package me.rom.com;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements CommandExecutor {

        public void onEnable() {
            getLogger().info("Hey");
            getCommand("Nope").setExecutor(this);
        }

        public void onDisable() {
            getLogger().info("Sup");
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (label.equalsIgnoreCase("Nope")) {

                if (!(sender instanceof Player))
                    return false;

                Player player = (Player) sender;
                player.setCanPickupItems(false);

                return false;
            }
            return false;
        }
    }
     
  10. The code I gave you before, is that you have to use to avoid errors
     
  11. Well that should work if I understood your plugin correctly. If a player types "/nope" you call setCanPickupItems(false) on that player. And then you return false...you might want to return true there. Actually try it like this:
    Code (Text):
    [...]
       if (!(sender instanceof Player))
          return false;

       Player player = (Player) sender;
       player.setCanPickupItems(false);
       player.sendMessage("Oh look, it works");
        return true;
    [...]
     
  12. PD: Do you speak Spanish?
     
  13. The code part has no logic, saying that if the sender is NOT a player runs that code
     
  14. It has, you just didn't get it^^ It is returing false if the sender is not an instanceof Player. So if you run the nope command in console you'll get an error.
     
  15. It is runs the code since it is sending the message, but I can still pick up items.
    Nope.
     
  16. What I mean is that the code such that it is, what it does is, if the command that is not a player will run the code has no logic because the console does not you can apply:
    Player player = (Player) sender;
    player.setCanPickupItems(false);
     
  17. At least you know that your command is executed now^^
    I don't know why setCanPickupItems is not working, I've never used it before. Are there any errors in the console?

    Edit: @DeiAlexTVT I am not sure if I understood you correctly but the code you posted earlier will throw an error if you run the command from console. You have to check if the sender is an instanceof Player before casting it. The OP is trying to prevent that error.
     
  18. No.
     
  19. Alright so I just tried it myself, the method doesn't work for Players. So here's an alternative:
    Save the players that are not supposed to pick up items somewhere. For example in a list or a map. Then when a player picks up an item this event is fired. Check if the player of that event is also contained in your map/list. If he is, cancel the event.
    And when I say save the player, I mean save their UUIDs (better performance).
     
    • Informative Informative x 1