MyUtility

Discussion in 'Plugin Development' started by XFarwar, Jul 22, 2014.

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

    XFarwar

    Hi, i'm going to create an Utility plugin where will much useful commands. But, I0ve a problem.

    Code:
    package SecondPlugin;
     
    import org.bukkit.ChatColor;
    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 Eroe extends JavaPlugin {
        public static Eroe plugin;
       
        @Override
        public void onEnable(){
       
           
        }
       
        @Override
        public void onDisable(){
           
        }
     
        @Override
       
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
           
            if(cmd.getName().equalsIgnoreCase("eroe")) {
               
                Player player = (Player) sender;
               
                player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 3000, 10));
                player.sendMessage(ChatColor.RED + "Modalità eroe " + ChatColor.ITALIC + "attivata!");
             
            }
           
            if(cmd.getName().equalsIgnoreCase("vitamina")) {
               
                Player player = (Player) sender;
               
                player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 3000, 5));
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 3000, 2));
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 3000, 5));
                player.sendMessage(ChatColor.GOLD + "Hai bevuto la vitamina e...");
               
            }
           
            return false;
        }
     
    }
    I don't know why it not works. Infact, if there is only /eroe or only /vitamina it works, but, if there are both, it not works. Help me please :D
     
  2. Offline

    jthort

    XFarwar You should use else if rather then a whole new if statement. Along with that, you shouldn't blindly cast the sender to a Player instance. I would actually suggest a switch statement to check the commands

    What do you mean it doesn't work, does it throw any errors? Does the command execute?
     
  3. Offline

    Zettelkasten

    XFarwar jthort Actually, a switch would be even better then an else:
    Code:java
    1. switch (cmd.getName().toLowerCase()) {
    2. case "eroe":
    3. // Do stuff for that command
    4. return true;
    5. case "vitamina":
    6. // Do stuff for that command
    7. return true;
    8. default:
    9. // Should never happen
    10. return true;
    11. }


    But back to topic: It should work just with just two ifs, just as in XFarwar's code ...
    Have you registered the commands in both plugin.yml and using getCommand(..)?
     
  4. Offline

    jthort

    Zettelkasten
    As I stated :), but back to the topic

    Edit: I actually switch the arguments rather then the commands, that way you don't have to make multiple switch statements. Then if, else if, inside the cases
     
  5. Offline

    PandazNWafflez

    XFarwar

    In onEnable() add
    Code:java
    1. getCommand("eroe").setExecutor(this);
    2. getCommand("vitamina").setExecutor(this);


    And if you haven't already, you need to add them to your plugin.yml file.

    Code:yaml
    1. commands:
    2. eroe:
    3. vitamina:
     
  6. Offline

    jthort

    Rather then setting the executors for the main class individually, why not just set them all to the main class using "this, this" (Of course unless you are using multiple classes for the commands)

    XFarwar What's up with the static instance of the main class, there is no need for that. If you ever need the instance of the main class just use "this" unless you are outside of that main class, then you would use the instance of the main class that you received through the constructor.
     
  7. Offline

    XFarwar

Thread Status:
Not open for further replies.

Share This Page