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
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?
XFarwar jthort Actually, a switch would be even better then an else: Code:java switch (cmd.getName().toLowerCase()) {case "eroe": // Do stuff for that command return true;case "vitamina": // Do stuff for that command return true;default: // Should never happen return true;} 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(..)?
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
XFarwar In onEnable() add Code:java getCommand("eroe").setExecutor(this);getCommand("vitamina").setExecutor(this); And if you haven't already, you need to add them to your plugin.yml file. Code:yaml commands: eroe: vitamina:
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.