Solved Needing help with making a 3 word command and then some...

Discussion in 'Plugin Help/Development/Requests' started by nentendomitchell, Jun 18, 2015.

Thread Status:
Not open for further replies.
  1. Hi, I'm having some trouble trying to make a three word command. I know how to make a two word command just fine, but I'm having some difficulty making a three word command.... Also, I'm trying to make it so, when you want to, you can add an item in a player's inventory (custom Item that I made). Here's how the command I'm trying to make should look
    Code:
    /give (Player name typed here) MasterSword
     
  2. Offline

    Drkmaster83

    Any code to begin with, or have you just not even attempted it?
     
  3. Well... It might look stupid. I mean here's how I make two word commands.
    Code:
    if (cmd.getName().equals("ZeldaWorld")) {
                    if (args.length == 0) {
                    } else {
                        if (args[0].equalsIgnoreCase("staff")) {
    //do stuff
    }
    }
    }
    As for getting a player that was typed in chat. I'm really confused on that. I've been looking, but to no luck.
     
  4. Offline

    Drkmaster83

    Well, since you're going to be adding the item into the player's inventory, you need to verify that they're online. Bukkit has deprecated the method that allows you to get a player by their name, but you can still use it regardless.
    Code:
    Player targetPlayer = Bukkit.getPlayer(args[0]); //If command was "/zeldaworld Drkmaster83 MasterSword", args[0] is "Drkmaster83", and args[1] is "MasterSword".
    if(targetPlayer == null || (!targetPlayer.isOnline())) {
        sender.sendMessage(args[0] + " is not online!");
        return false;
    }
    targetPlayer.getInventory().addItem(masterSwordItemStackThatYouCreated);
     
  5. Nice, thanks.
     
  6. Offline

    Drkmaster83

    No problem! Good luck on your plugin. I'm a Zelda fan. :)
     
  7. Thanks!
    So, just for safety, should it look like this do far?
    Code:
     
                if (cmd.getName().equalsIgnoreCase("give")) {
                    if (args.length == 0) {
                    } else {
                        Player targetPlayer = Bukkit.getPlayer(args[0]);
                        {
                            if (targetPlayer == null || (!targetPlayer.isOnline())) {
                                sender.sendMessage(args[0] + " is not online!");
                                return false;
                            } else if (args[1].equalsIgnoreCase("MasterSword")) {
                                if (player.hasPermission("zeldaworld.give")) {
                                    String MasterSword = ChatColor.GOLD + ""
                                            + ChatColor.BOLD + "Master Sword";
                                    lores.add(ChatColor.RED
                                            + "No evil shall touch this blade!");
                                    ItemStack MS = new ItemStack(
                                            Material.DIAMOND_SWORD, 1);
                                    MS.addEnchantment(Enchantment.DAMAGE_ALL, 4);
                                    MS.addEnchantment(Enchantment.FIRE_ASPECT, 2);
                                    MS.addEnchantment(Enchantment.KNOCKBACK, 2);
                                    MS.addEnchantment(Enchantment.DAMAGE_UNDEAD, 5);
                                    MS.addEnchantment(Enchantment.LOOT_BONUS_MOBS,
                                            3);
                                    MS.addEnchantment(
                                            Enchantment.DAMAGE_ARTHROPODS, 5);
                                    MS.addUnsafeEnchantment(Enchantment.DURABILITY,
                                            1000);
                                    ItemMeta im = MS.getItemMeta();
                                    im.setDisplayName(MasterSword);
                                    im.setLore(lores);
                                    MS.setItemMeta(im);
                                    inventory.addItem(MS);
                                    player.getPlayer().updateInventory();
                                    player.sendMessage(ChatColor.GOLD
                                            + "You've been given the Master Sword by the gods!");
                                } else {
                                    if (!player.hasPermission("zeldaworld.give")) {
                                        player.sendMessage(ChatColor.RED
                                                + "You don't have permission for this command!");
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
     
  8. Offline

    Drkmaster83

    Not in particular. You have to account for if the players don't enter an args[0] or args[1]. If you have an args[1], args.length == 2, if you have an args[0], args.length == 1. If you don't check for this, you'll get a NullPointerException.

    Otherwise, looking good.
     
  9. Thank you
     
    Drkmaster83 likes this.
  10. Offline

    Drkmaster83

    No problem. Just remember, if your issue is resolved, set the thread status as so. ^_^
     
    nentendomitchell likes this.
  11. Getting an error....
    Code:
    //ERROR
    [18:10:15] [Server thread/ERROR]: Could not load 'plugins\DisguiseCraft.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: net/minecraft/server/v1_8_R2/Packet
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugins(CraftServer.java:291) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.reload(CraftServer.java:744) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.Bukkit.reload(Bukkit.java:534) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchServerCommand(CraftServer.java:632) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at net.minecraft.server.v1_8_R1.DedicatedServer.aM(DedicatedServer.java:353) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:317) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NoClassDefFoundError: net/minecraft/server/v1_8_R2/Packet
        at java.lang.Class.forName0(Native Method) ~[?:1.8.0_45]
        at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:64) ~[craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        ... 14 more
    Caused by: java.lang.ClassNotFoundException: net.minecraft.server.v1_8_R2.Packet
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:91) ~[craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
        at java.lang.Class.forName0(Native Method) ~[?:1.8.0_45]
        at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:64) ~[craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[craftbukkit.jar:git-Spigot-8a983f9-e81edfc]
        ... 14 more
    Here's my command I have for it
    Code:
    //My Command
    if (cmd.getName().equalsIgnoreCase("zwgive")) {
                    if (args.length == 0) {
                    } else {
                        Player targetPlayer = Bukkit.getPlayer(args[0]);
                        {
                            if (targetPlayer == null || (!targetPlayer.isOnline())) {
                                sender.sendMessage(args[0] + " is not online!");
                                return false;
                            } else if (args[1].equalsIgnoreCase("MasterSword")) {
                                if (player.hasPermission("zeldaworld.give")) {
                                    String MasterSword = ChatColor.GOLD + ""
                                            + ChatColor.BOLD + "Master Sword";
                                    lores.add(ChatColor.RED
                                            + "No evil shall touch this blade!");
                                    ItemStack MS = new ItemStack(
                                            Material.DIAMOND_SWORD, 1);
                                    MS.addEnchantment(Enchantment.DAMAGE_ALL, 4);
                                    MS.addEnchantment(Enchantment.FIRE_ASPECT, 2);
                                    MS.addEnchantment(Enchantment.KNOCKBACK, 2);
                                    MS.addEnchantment(Enchantment.DAMAGE_UNDEAD, 5);
                                    MS.addEnchantment(Enchantment.LOOT_BONUS_MOBS,
                                            3);
                                    MS.addEnchantment(
                                            Enchantment.DAMAGE_ARTHROPODS, 5);
                                    MS.addUnsafeEnchantment(Enchantment.DURABILITY,
                                            1000);
                                    ItemMeta im = MS.getItemMeta();
                                    im.setDisplayName(MasterSword);
                                    im.setLore(lores);
                                    MS.setItemMeta(im);
                                    inventory.addItem(MS);
                                    player.getPlayer().updateInventory();
                                    player.sendMessage(ChatColor.GOLD
                                            + "You've been given the Master Sword by the gods!");
                                } else {
                                    if (!player.hasPermission("zeldaworld.give")) {
                                        player.sendMessage(ChatColor.RED
                                                + "You don't have permission for this command!");
                                    }
                                }
                            }
                        }
                    }
                }
            }
    I'm sorry, I am still in the learning stages of Java Coding.
     
  12. Offline

    Drkmaster83

    Code:
    Caused by: java.lang.ClassNotFoundException: net.minecraft.server.v1_8_R2.Packet
    Check your imports. Check your .jar that you're running the server on, Make sure the two match.
    IF you have imported net.minecraft.server.v1_8_R2, try changing the R2 to R3 or R1... if that doesn't work, let me know.
     
  13. It happens when I do /zwgive
    I must have messed up in that code somewhere...
     
  14. Offline

    Tecno_Wizard

    @nentendomitchell. It's probably a failed compile. Eclipse i'm Guessing? Eclipse has a pretty bad track record of failing to correctly compile code with dependent libraries.
     
  15. Just for debugging proposes, I commented out the part where do put in the MasterSword part. It seems to have an error with the part where you insert the name. So I put in getName() in there and it worked fin.
     
  16. Offline

    Tecno_Wizard

  17. @Tecno_Wizard I updated my post to tell you that I did
     
  18. Offline

    Tecno_Wizard

    @nentendomitchell, personally I don;t think the name is the problem. I don't see a declaration for the variable "lore" anywhere.

    Also, variable names start with lowercase letters. "mS" or "ms" is better.


    EDIT: nvm, found it. You have an invalid import in one of your classes that you instantiated in the onEnable method.
     
  19. Here's the error I get in real time when I type /zwgive
    Screenshot_5.png

    Just to make sure here's the code again. I think it has something to do with the args and the length
    Code:
                if (cmd.getName().equalsIgnoreCase("zwgive")) {
                    if (args.length == 1) {
                    } else {
                        Player targetPlayer = Bukkit.getPlayer(args[0]);
                        {
    
                            if (targetPlayer == null || (!targetPlayer.isOnline())) {
                                sender.sendMessage(args[0] + ChatColor.RED
                                        + " is not online!");
                                return false;
                            } else {
                                if (args[1].equalsIgnoreCase("MasterSword")) {
                                    if (player.hasPermission("zeldaworld.give")) {
                                        String MasterSword = ChatColor.GOLD + ""
                                                + ChatColor.BOLD + "Master Sword";
                                        lores.add(ChatColor.RED
                                                + "No evil shall touch this blade!");
                                        ItemStack MS = new ItemStack(
                                                Material.DIAMOND_SWORD, 1);
                                        MS.addEnchantment(Enchantment.DAMAGE_ALL, 4);
                                        MS.addEnchantment(Enchantment.FIRE_ASPECT,
                                                2);
                                        MS.addEnchantment(Enchantment.KNOCKBACK, 2);
                                        MS.addEnchantment(
                                                Enchantment.DAMAGE_UNDEAD, 5);
                                        MS.addEnchantment(
                                                Enchantment.LOOT_BONUS_MOBS, 3);
                                        MS.addEnchantment(
                                                Enchantment.DAMAGE_ARTHROPODS, 5);
                                        MS.addUnsafeEnchantment(
                                                Enchantment.DURABILITY, 1000);
                                        ItemMeta im = MS.getItemMeta();
                                        im.setDisplayName(MasterSword);
                                        im.setLore(lores);
                                        MS.setItemMeta(im);
                                        inventory.addItem(MS);
                                        player.getPlayer().updateInventory();
                                        player.sendMessage(ChatColor.GOLD
                                                + "You've been given the Master Sword by the gods!");
                                    } else {
                                        if (!player
                                                .hasPermission("zeldaworld.give")) {
                                            player.sendMessage(ChatColor.RED
                                                    + "You don't have permission for this command!");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  20. Offline

    Drkmaster83

    @nentendomitchell
    Code:
    if (cmd.getName().equalsIgnoreCase("zwgive")) {
        if (args.length == 0) {
           player.sendMessage(ChatColor.RED + "Please provide a player name and the name of an item to give to the player!");
            return false;
        } else if (args.length == 1) {
            player.sendMessage(ChatColor.RED + "Please provide the name of an item to give to the player!");
            return false;
        } else if(args.length == 2) {
           Player targetPlayer = Bukkit.getPlayer(args[0]);
            if (targetPlayer == null || (!targetPlayer.isOnline())) {
                sender.sendMessage(args[0] + ChatColor.RED
                        + " is not online!");
                return false;
            }
           if (args[1].equalsIgnoreCase("MasterSword")) {
                if (player.hasPermission("zeldaworld.give")) {
                    String MasterSword = ChatColor.GOLD + ""
                            + ChatColor.BOLD + "Master Sword";
                    lores.add(ChatColor.RED
                            + "No evil shall touch this blade!");
                    ItemStack MS = new ItemStack(
                            Material.DIAMOND_SWORD, 1);
                    MS.addEnchantment(Enchantment.DAMAGE_ALL, 4);
                    MS.addEnchantment(Enchantment.FIRE_ASPECT,
                            2);
                    MS.addEnchantment(Enchantment.KNOCKBACK, 2);
                    MS.addEnchantment(
                            Enchantment.DAMAGE_UNDEAD, 5);
                    MS.addEnchantment(
                            Enchantment.LOOT_BONUS_MOBS, 3);
                    MS.addEnchantment(
                            Enchantment.DAMAGE_ARTHROPODS, 5);
                    MS.addUnsafeEnchantment(
                            Enchantment.DURABILITY, 1000);
                    ItemMeta im = MS.getItemMeta();
                    im.setDisplayName(MasterSword);
                    im.setLore(lores);
                    MS.setItemMeta(im);
                    targetPlayer.getInventory().addItem(MS); //Make sure it's giving it to the targetPlayer!
                    targetPlayer.updateInventory(); //Make sure to update targetPlayer's inventory!
                    targetPlayer.sendMessage(ChatColor.GOLD
                            + "You've been given the Master Sword by the gods!");
                } else {
                    if (!player.hasPermission("zeldaworld.give")) {
                        player.sendMessage(ChatColor.RED
                                + "You don't have permission for this command!");
                        return false;
                    }
                }
            }
        }
        else player.sendMessage(ChatColor.RED + "Too many arguments!");
    }
    Edit: Sorry for any weird formatting, I just kinda copy-pasted and fixed as quick as I could.
     
Thread Status:
Not open for further replies.

Share This Page