I need help with Commands!

Discussion in 'Plugin Development' started by Evolutio, Jan 19, 2012.

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

    Evolutio

    Hello,
    I would make my "own" Plugin for Bukkit.
    And I have made a few Commands, the Commands was before in the Evolutio.class, but I have see in much Source codes, that they have a own Package + Class. I have made a Packeage (evolutio.evolutio.cmd).
    But see my Code:

    Evolutio.java
    Code:
    package me.evolutio.evolutio;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    import org.bukkit.plugin.Plugin;
    import java.util.Properties;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
     
     
    public class Evolutio extends JavaPlugin {
       
        public static PermissionHandler permissionHandler;
        private static String ordner = "plugins/Evolutio";
        public static Properties prop = new Properties();
        public static File r = new File(ordner + File.separator + "Ranks.properties");
       
        private FileManager fileManager = new FileManager();
     
        @Override
        public void onDisable() {
           
            System.out.println("Evolutio's Plugin Test! <Disable> ");
           
        }
     
        @Override
        public void onEnable() {
           
            fileManager.createConfig();
       
            setupPermissions();
            System.out.println("Evolutio's Plugin Test! <Enable> ");
           
            if (!r.exists()) {
                new File(ordner).mkdir();
                try {
                  FileOutputStream out2 = new FileOutputStream(r);
                  r.createNewFile();
                  prop.put("rank1", "Guest");
                  prop.store(out2, null);
                  out2.flush();
                  out2.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
           
        }
     
        private void setupPermissions() {
            if (permissionHandler != null) {
                return;
            }
           
            Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
           
            if (permissionsPlugin == null) {
                System.out.println("Permission system not detected, defaulting to OP");
                return;
            }
           
            permissionHandler = ((Permissions) permissionsPlugin).getHandler();
            System.out.println("Found and will use plugin "+((Permissions)permissionsPlugin).getDescription().getFullName());
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            boolean succeed = false;
           
            Player player = (Player) sender;
           
            if (sender instanceof Player) {
               
                //
                if (cmd.getName().equalsIgnoreCase("setadmin")) {
                   
                      if (!Evolutio.permissionHandler.has(player, "evolutio.setadmin")) {
                          return false;
                      }
                   
                    if (args.length == 0) {
                       
                        player.getInventory().setHelmet(new ItemStack(310, 1));
                        player.getInventory().setChestplate(new ItemStack(311, 1));
                        player.getInventory().setLeggings(new ItemStack(312, 1));
                        player.getInventory().setBoots(new ItemStack(313, 1));
       
                        player.sendMessage(ChatColor.GREEN + "[Server]" + ChatColor.YELLOW + fileManager.readDouble("setadmin"));
                        succeed = true;
                       
                    } else {
                        player.sendMessage(ChatColor.GREEN + "[Server] " + ChatColor.DARK_RED + "Dieser Befehl darf keine Argumente enthalten!");
                        succeed = false;
                    }
                }
               
                // heal
                if (cmd.getName().equalsIgnoreCase("heal")) {
                   
                      if (!Evolutio.permissionHandler.has(player, "evolutio.setheal")) {
                          return false;
                      }
                     
                      if (args.length == 0) {
                          player.setHealth(20);
                          player.setFoodLevel(20);
                          player.sendMessage(ChatColor.GREEN + "[Server]" + ChatColor.YELLOW + "Test");
                          succeed = true;
                      }
                }
               
                // god
               
                if (cmd.getName().equalsIgnoreCase("gott")) {
                   
                      if (!Evolutio.permissionHandler.has(player, "evolutio.gott")) {
                          return false;
                      }
                     
                      if (args.length == 0) {
                          player.setFoodLevel(20);
                          player.setHealth(20);
                          player.setMaximumNoDamageTicks(1);
                          player.sendMessage(ChatColor.GREEN + "[Server]" + ChatColor.YELLOW + "Gottmodus aktiviert.");
                          succeed = true;
                      }
                   
                }
               
                if (cmd.getName().equalsIgnoreCase("ranks")) {
                      try {
                        FileInputStream in2 = new FileInputStream(r);
                        prop.load(in2);
                        String rank1 = prop.getProperty("rank1").toString();
     
                        player.sendMessage(ChatColor.GRAY + rank1);
                        succeed = true;
                      }
                      catch (IOException e) {
                        e.printStackTrace();
                      }
                    }
                else
                {
                  Commands cmds = new Commands();
                  cmds.getCmd(sender, cmd, label, args);
                  return false;
                }
                  }
            return succeed;
        }
    }
    
    FileManager.java

    Code:
    package me.evolutio.evolutio;
    
    import java.io.File;
    import org.bukkit.util.config.Configuration;
    
    @SuppressWarnings("deprecation")
    public class FileManager {
    
    private static String ordner = "plugins/Evolutio";
    private static File configFile = new File(ordner + File.separator + "config.yml");
    
    private static Configuration config;
    
    private Configuration loadConfig() {
    try {
    Configuration config = new Configuration(configFile);
    config.load();
    return config;
    
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    }
    
    public void createConfig() {
    new File(ordner).mkdir();
    
    if(!configFile.exists()) {
    try {
    configFile.createNewFile();
    
    config = loadConfig();
    
    config.setProperty("setadmin", "Dein_Text");
    config.setProperty("heal", "Dein_Text");
    config.save();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    config = loadConfig();
    }
    
    public double readDouble(String key) {
    double value = config.getDouble(key, 1);
    return value;
    }
    }
    
    Health.java

    Code:
    package me.evolutio.evolutio.cmd;
    
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Health
    {
    private Logger log = Logger.getLogger("Minecraft");
    
    public void doCmd(CommandSender sender, Command cmd, String cmdName, String[] args)
    {
    if (args.length == 0) {
    if ((sender instanceof Player))
    {
    ((Player)sender).setHealth(20);
    ((Player)sender).setFoodLevel(20);
    sender.sendMessage(ChatColor.GREEN + "[Server]" + ChatColor.YELLOW + "geheilt!");
    }
    else {
    this.log.info("You can't heal the Console");
    }
    
    }
    else if (args.length == 1)
    for (Player p : Bukkit.getServer().getOnlinePlayers())
    if (p.getName().toLowerCase().contains(args[0].toLowerCase())) {
    p.setHealth(20);
    p.setFoodLevel(20);
    p.sendMessage(ChatColor.GREEN + "[Server]" + ChatColor.YELLOW + "Du wurdest von " + sender.getName() + "geheilt!");
    sender.sendMessage(ChatColor.GREEN + "[Server]" + ChatColor.YELLOW + "Du hast " + p.getName() + "geheilt!");
    }
    }
    }
    Commands.java

    Code:
    package me.evolutio.evolutio;
    
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import me.evolutio.evolutio.cmd.Health;
    
    
    public class Commands
    { 
    public Logger log = Logger.getLogger("Minecraft");
    
    public void getCmd(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
    String cmdName = cmd.getName();
    Player player = (Player) sender;
    
    
    // heal
    if (cmd.getName().equalsIgnoreCase("heal")) {
    
    if (!Evolutio.permissionHandler.has(player, "evolutio.heal")) {
    
    }
    
    Health heal = new Health();
    heal.doCmd(sender, cmd, cmdName, args);
    sender.sendMessage(ChatColor.RED + "You don't have permission. 1");
    }
    else
    sender.sendMessage(ChatColor.RED + "You don't have permission. 2");
    
    
    }
    }
    I know, that I have Commands on the "Evolutio.java" but I have deletet them and the commands don't work.

    Here is my Plugin.yml

    Code:
    name: Evolutio
    version: 0.1
    description: Test
    author: Evolutio
    
    main: me.evolutio.evolutio.Evolutio
    
    commands: 
    setadmin:
    description: Setzt Armor.
    usage: /<command>
    heal:
    description: Heilt dich.
    usage: /<command>
    gott:
    description: macht dich zu Gott!
    usage: /<command>
    ranks:
    description: Zeigt Ranks an!
    usage: /<command>
    
    But why it don't work ?

    Sorry for my veeeeeeery bad english :D
     
  2. Offline

    wwsean08

    does it throw any errors when you try and do a command?
     
  3. Offline

    Evolutio

    when I try the command in Minecraft it comes:

    /heal

    Here is the error from the log :)
     
  4. Offline

    wwsean08

    are you trying to return a value in the getCmd method after if
    Code:
    (!Evolutio.permissionHandler.has(player, "evolutio.heal")) {
    and just didn't post it, because part of the error is that there is a void method trying to return a value
     
  5. Offline

    Evolutio

    and how can I fix it? or make it other ?

    bad English :)
     
  6. try replacing : [JAVA](!Evolutio.permissionHandler.has(player, "evolutio.heal")) {[/JAVA] wiht player.hasPermission("evo.heal")
     
  7. Offline

    Evolutio

    Code:
        if (cmd.getName().equalsIgnoreCase("heal")) {
             
              if (player.hasPermission("evo.heal")) {
                 
              }
             
            Health heal = new Health();
            heal.doCmd(sender, cmd, cmdName, args);
            sender.sendMessage(ChatColor.RED + "You don't have permission. 1");
        }
        else
          sender.sendMessage(ChatColor.RED + "You don't have permission. 2");
    and what must I insert here ?

    Code:
              if (player.hasPermission("evo.heal")) {
     
                //here
     
              }
     
  8. Offline

    theguynextdoor

    What you put 'here' is the code you want to execute if the player has the permission evo.heal
     
  9. Offline

    Evolutio

    and with ! is, when the player DON'T have the permission ?
    Code:
              if (!player.hasPermission("evo.heal")) {
     
     
              }
     
  10. Offline

    theguynextdoor

    Yes
    if (!player.hasPermission("evo.heal")) {
    means if the player does not have permission. But you are better off checking if they have the permission and doing a simple else statement if they dont
     
  11. Offline

    Evolutio

    it doe's not Work :(
    Code:
        if (cmd.getName().equalsIgnoreCase("heal")) {
             
              if (player.hasPermission("evolutio.heal")) {
                  Health heal = new Health();
                  heal.doCmd(sender, cmd, cmdName, args);
              } else {
                  sender.sendMessage(ChatColor.RED + "You don't have permissions!");
              }
        } 
    Minecraft Says, taht I don't have the permission.
    My Config.yml look like this:
    users:
    Evolutio:
    permissions:
    - evolutio.gott
    - evolutio.ranks
    - evolutio.setheal
    - evolutio.setadmin
    - evolutio.heal
    groups: []
    Player:
    permissions:
    - evolutio.gott
    - evolutio.ranks
    - evolutio.setheal
    - evolutio.setadmin
    - evolutio.heal
    groups: []

    why I can't use the command ? :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
Thread Status:
Not open for further replies.

Share This Page