Noob plugin question

Discussion in 'Spigot Plugin Development' started by X8Reap3rZ, May 11, 2015.

  1. I am creating my first plugin doing some command addons and ive wanted to add a /clear command to erase potion effects, when i add this line of code:

    if(label.equalsIgnoreCase("clear")){
    for(PotionEffect effect : player.getActivePotionEffects())
    player.removePotionEffect(effect.getType());

    It says player is not resolved?

    Sorry for the noob question fairly new at plugin developing, - thanks.
     
  2. full code please?
     
  3. Code (Text):
            if(label.equalsIgnoreCase("skateboards")){
                    sender.sendMessage("SKATEBOARDS!");
                    Player p = (Player)sender;
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,Integer.MAX_VALUE,5));
                 
                   if(label.equalsIgnoreCase("clear")){
                            for(PotionEffect effect : player.getActivePotionEffects())
                                    player.removePotionEffect(effect.getType());
                    }
            }
    your code for 'clear' will never be reached, because you have it inside the 'skateboards' command. If you are wanting to do /skateboards clear, then it needs to be change from



    Code (Text):
    if(label.equalsIgnoreCase("clear")){
    to

    Code (Text):
    if(args[0].equalsIgnoreCase("clear")){
     
    • Optimistic Optimistic x 1
  4. Thanks for the info but, players is still not resolved?
     
  5. Well yeah, at no point have you set 'player' at all. Use 'p' instead. because that is what you declared and defined.. not 'player'
     
    • Like Like x 1
  6. Ahh i understand now, player p = (Player)sender; shortcut defined for player, either way im still kinda learning and ive just learnt more so thankyou for your help.
     
  7. This is the problem:
    Code (Text):
    Player p = (Player)sender;
    You are casting the instance of Player to a CommandSender.
    A CommandSender can sometimes be a player, or console, or rcon.
    You need something like this...
    Code (Text):
    if(sender instanceof Player) {
        Player p = (Player)sender;
        p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,Integer.MAX_VALUE,5));
    } else {
        sender.sendMessage("Must be used in-game.");
    }
     

  8. While he does need to make sure the instance is corrected, it is definitely not what was causing his code to fail. SO.. it is not THE problem.. just something he should practice doing.
     
    • Agree Agree x 1
  9. This is my resulting code:
    Code (Text):
    package me.X8ReaperZ.Skateboards;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class sclass extends JavaPlugin {
    @Override
    public boolean onCommand(CommandSender sender, Command command,
            String label, String[] args) {
        // TODO Auto-generated method stub
        if(label.equalsIgnoreCase("skateboards")){
            sender.sendMessage("SKATEBOARDS!");
            Player p = (Player)sender;
            p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,Integer.MAX_VALUE,5));
           
            if(args[0].equalsIgnoreCase("clear")){
                sender.sendMessage("NO SKATEBOARDS!?");
                for(PotionEffect effect : p.getActivePotionEffects())
                    p.removePotionEffect(effect.getType());
            }
        }
    return false;
    }}
     
    But i have an error:
    Code (Text):
    [15:24:36] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'skateboards' in plugin Skateboards v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-35348a5-ee6d0fa]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at me.X8ReaperZ.Skateboards.sclass.onCommand(sclass.java:20) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-35348a5-ee6d0fa]
        ... 14 more
     
     
  10. Code (Text):
    if(args.length > 0 && args[0].equalsIgnoreCase("clear")){
     
  11. Instead of spoon-feeding explain?
     
    • Agree Agree x 1
  12. Thanks that fixed it, the only final issue occuring is just when i type /skateboards clear, it sends me SKATEBOARDS! and NO SKATEBOARDS!? at the same time. As if both commands are registering from /skateboards.
     
  13. My guess is it checks the length of the argument for none, so the command inherits from.. skateboards?? i really am not completely sure. correct me if im wrong.
     
    • Like Like x 1
  14. An easy fix would be to move the sending of the "SKATEBOARDS" message to an else after you've checked for the clear argument.
     
  15. Code (Text):
    public boolean onCommand(CommandSender sender, Command command,
                                 String label, String[] args) {
            if(label.equalsIgnoreCase("skateboards")) {

                if (sender instanceof Player) {//So you don't get an NPE if Console or RCON executes the command
                 
                 
                    Player p = (Player) sender;//Declaring 'p' for usage
                 
                 
                    if (args.length > 0 && args[0].equalsIgnoreCase("clear")) { //Checking to make sure arguments are set to avoid the out of bounds error

                        //This executes if you type /skateboards clear
                        sender.sendMessage("NO SKATEBOARDS!?");
                     
                        for (PotionEffect effect : p.getActivePotionEffects()) {
                         
                            p.removePotionEffect(effect.getType());
                         
                        }
                     
                    }else {

                        //This executes if you type /skateboards with no arguments
                        sender.sendMessage("SKATEBOARDS!");
                        p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 5));
                    }

                }
             
                return false;
            }
            return false;
        }
    Explanations for changes in comments.
     
  16. If you don't want the command to come back instead of returning false, return true;