Solved Kit Command

Discussion in 'Plugin Development' started by Astrophylite, Sep 8, 2015.

Thread Status:
Not open for further replies.
  1. Hey everyone,

    I am trying to make a kit plugin, and I am stuck on attempting to get all the items from config. I have managed to get to the point where I am checking if the kit doesn't exist, but I don't know how to get the itemstacks from the config.

    Any help will be appreicated,
    _Zircon_
     
  2. ItemStack itemstack = config.getItemStack("path");
    config.set("path", itemstack);
     
  3. Offline

    NullChips

    @MaTaMoR_ I think he means from an integer list in the config.
     
  4. He can create a CustomItem class implementing ConfigurationSerializable, then serialize the CustomItem however he want and the load it getting the map, it's really easy also he can just get the Integer and create a new ItemStack
     
  5. Okay, I don't get anything you just said :p. Don't worry, I'm just testing my new one and I'll see if your code works :p

    EDIT: I made it so it doesn't error, but it doesn't give me any items. <- My life story.
    I am also stuck on how to grab the amount thing that I made in the config.
    Config structure: http://prntscr.com/8e2mp1
    Here is the code:
    Da Code (open)

    Code:
    package me.zircon.zirconessentials.commands.inventory;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class Kits implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        ArrayList<Player> cooldown = new ArrayList<Player>();
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Only in-game players can use /" + label + "!");
                return false;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("kit")) {
                if(!sender.hasPermission("zirconessentials.kit")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to give kits!");
                    return false;
                } else {
                    if(args.length == 0 || args.length > 1) {
                        p.sendMessage(ChatColor.RED + "Usage: /" + label + " <kit>");
                        return false;
                    }
                    String kitName = args[0].toString();
                    if(settings.getConfig().getString("kits." + kitName) == null) {
                        p.sendMessage(ChatColor.RED + "That kit cannot be found!");
                        return false;
                    } else {
                        ConfigurationSection configSection = settings.getConfig().getConfigurationSection("kits");
                        List<Integer> intList = configSection.getIntegerList(kitName + ".items");
                        Inventory pi = p.getInventory();
                        for(int i = 0; i < intList.size(); i++) {
                            Material mat = Material.getMaterial(intList.get(i));
                            pi.addItem(new ItemStack(mat));
                        }
                    }
                }
            }
            return false;
        }
    }
    
     
    Last edited: Sep 8, 2015
  6. Offline

    Ruptur

    @_zircon_
    It probably doesnt give you any items because the material is null each time or intList is empty.

    You dont need to add '.toString()' after args[0] because it is completly unnecessary what you are essential doing is string.toString() (if you are using intellij ide, it will point this out)

    In case you didnt know, returning false will send the usage message to the command sender and if thier is none it will relay the command back to the sender.

    Material.getMaterial(int) is deprecated so instead you should use Material.byId(int)

    You should change the way you store the items because i dont think you will be able to retrieve the amount of each item (correct me if im wrong)

    I would store the kit items something like this. It doesnt matter what the key for each item is (i normally use 1, 2, 3, ..)
    Code:
    items:
      1:
        id: 5
        amount: 20
      2:
        id: 5
        amount: 20
      3:
        id: 5
        amount: 20
    
    Hope i helped
     
  7. @Ruptur I will attempt this now.

    EDIT: How do I make a cooldown so that the player can use other kits but not the one they are in the cooldown for ? I am using a HashMap<Player, String> by the way.
    EDIT2: I'm just trying to figure out how to grab the item and the amount from that! How would I make it pick up the amount of items you have stored ??
     
    Last edited: Sep 9, 2015
  8. Offline

    Ruptur

    @_zircon_
    You cooldown for a certain item, depending on how what the plugin does there are a bunch of ways to go about this.
    If you are using a Map to store players then ill suggest you store the player to a list of recent kits.
    Code:
    Map<UUID, List<String>> recentKits
    When giving the kits to the player you then schedule a delayed task that will run 20 * delay in secs and will remove the kit from the list
    Code:
    recentKits.get(sender.getUniqueId()).remove(kitName);
    To prevent the players from using kits whilst still on delay
    Just check if the player is in the map and the list has the kit.
    Code:
    if (recentKits.containsKey(sender.getUniqueId())
      && recentKits.get(sender.getUniqueId()).contains(kitname)) {
    they are still in the delay time and are prevented from using kit 'kitname'
    
    return here or use else
    ----
    If you layout out the config like i recommended then getting the items with amount is easy.
    1) Loop through all the keys in items configsection with getKeys(false)
    2) Get the material from 'key.id' and the amount with 'key.amount'
    3) Create an itemstack with the amount and material
    4) Add the item to a list
    **I recommend you get the kits when the plugin enables not when the player wants to equip it

    Sample code
    Code:java
    1.  
    2. ConfigurationSection kitSection = config.getConfigurationSection("xxx.kit");
    3. List<ItemStack> items = new ArrayList<>();
    4.  
    5. for (String key : ketSection.getConfigurationSection("items").getKeys(false)) {
    6. Material material = Material.getById(ketSection.getInt(String.format("items.%s.id", key));
    7. int amount = ketSection.getInt(String.format("items.%s.amount", key));
    8. ItemStack item = new ItemStack(material, amount);
    9. items.add(item);
    10. }
     
    Last edited: Sep 9, 2015
  9. Thanks for your reply @Ruptur! However, I have created the map and all, but how would I add the kitName to it, and how would I make it so it would do it even if the map was empty ?
     
  10. Offline

    Ruptur

    @_zircon_
    Do you mean how to add the kit name of the recent kit of a player to the map?
     
  11. @Ruptur Yes and what I mean is that it keeps throwing an exception when trying to add the first record to the map....
     
  12. Offline

    mythbusterma

  13. Okay, so I changed the Map that @Ruptur told me to use into a HashMap, so that has fixed that part, but it isn't giving me any items, and there are no console errors. Also, where he says in the code to add the item that we create to an ArrayList, he hasn't stated how to then add it to the player's inventory.
     
  14. Offline

    mythbusterma

    @_zircon_

    I'm not sure what your code is at this point, so I don't really follow.
     
  15. Offline

    DoggyCode™

    I see that you're using array list for the cool down, unless it's like a 15 second cool down it is a very bad idea. Why? Because your array list resets every time the server reloads or something (that causes the plugin to reboot), which means that if you want a cool down of a 2 days, then you would either somehow manage to keep the server up for that amount of time, or the player could just use the kit maybe 3 times in one day where the cool down of that op kit was going to be much longer. Find another solution.
     
  16. @DoggyCode™ Thanks for the tip.
    Updated Code (open)

    Code:
    package me.zircon.zirconessentials.commands.inventory;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    import me.zircon.zirconessentials.miscellaneous.Utils;
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class Kits implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        HashMap<UUID, String> recentKits = new HashMap<UUID, String>();
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Only in-game players can use /" + label + "!");
                return false;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("kit")) {
                if(!sender.hasPermission("zirconessentials.kit")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to give kits!");
                    return false;
                } else {
                    if(args.length == 0 || args.length > 1) {
                        p.sendMessage(ChatColor.RED + "Usage: /" + label + " <kit>");
                        return false;
                    }
                    String kitName = args[0];
                    if(settings.getConfig().getString("kits." + kitName) == null) {
                        p.sendMessage(ChatColor.RED + "That kit cannot be found!");
                        return false;
                    } else if(recentKits.containsKey(p.getUniqueId()) && recentKits.get(p.getUniqueId()).contains(kitName)) {
                        p.sendMessage(ChatColor.RED + "You are not allowed to use this kit yet!");
                        return false;
                    } else {
                        ConfigurationSection kitSection = settings.getConfig().getConfigurationSection("kits." + kitName);
                        List<ItemStack> items = new ArrayList<>();
                        Inventory pi = p.getInventory();
                        for(String key : kitSection.getConfigurationSection("items").getKeys(false)) {
                            Material material = Material.getMaterial(kitSection.getInt(String.format("items.%s.id", key)));
                            int amount = kitSection.getInt(String.format("items.%s.amount", key));
                            ItemStack item = new ItemStack(material, amount);
                            items.add(item);
                        }
                        if(!p.hasPermission("zirconessentials.kit.bypass")) {
                            recentKits.put(p.getUniqueId(), kitName);
                            Bukkit.getScheduler().scheduleSyncDelayedTask(Utils.instance(), new Runnable() {
                                public void run() {
                                    recentKits.remove(p.getUniqueId(), kitName);
                                }
                            }, settings.getData().getInt("kits." + kitName + ".delay"));
                        }
                    }
                }
            }
            return false;
        }
    }
    


    This is the last part of the plugin that is holding me up, come on guys :p

    EDIT: Finally! I got it working!!!! Massive thanks to @Ruptur who gave me the suggestion of how to format the config and the code that he gave me!

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

Share This Page