Using config for kits.

Discussion in 'Plugin Development' started by NullChips, Aug 31, 2015.

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

    NullChips

    So, I've been wondering... How might I use the config.yml file to set kits, using item ids? What method would I need to use to cast those item ids to an ItemStack?

    That's really all my question is. I'm pretty sure there's a simple answer, but I can't seem to find it...
     
  2. Offline

    caderape

    #new ItemStack(int id, int amount, short data);
    #new ItemStack(Material.getbyName(int id))
     
  3. Offline

    NullChips

    @caderape

    How might I do this from a list of ID's in the config?
     
  4. Offline

    CrystallFTW

  5. Offline

    NullChips

    @CrystallFTW Don't have one set up, I guess I might as well ask how I might lay it out?
     
  6. Offline

    CrystallFTW

  7. Offline

    NullChips

    @CrystallFTW Here it is - just demo, I'm guessing it would be layed out like this, from the reference you sent me.

    Code:
    Kits:
       kit1:
           -1
           -2
           -3
           -4
        kit2:
            -1
            -2
            -3
            -4
     
  8. Offline

    stefvanschie

    Get the list from the kit and give them the correct item.
     
  9. Offline

    CrystallFTW

    @NullChips loop through the list and give them the items
    Something like this:
    Code:
            for(Integer id : Language.getIntegerList("Kits.kit1")){
                player.getInventory().addItem(new ItemStack(id));
            }
     
  10. Offline

    xMrPoi

    I would store the Material names since getting Materials from Integer IDs is deprecated. Also, from your config, you would get the StringList and loop through those and create new ItemStacks from the given Material names.
     
  11. Offline

    NullChips

    @CrystallFTW Thanks for your soloution, however, what method would I use to set an integer list, as setIntegerList() doesn't seem to exist?

    Bump... I'm sure there's a simple answer out there somewhere?...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  12. Offline

    CrystallFTW

    @NullChips add the ids as a string list and to use them as integers use
    Code:
    for(String id : Language.getStringList("Kits.kit1")){
    int id = Integer.parseInt(id);
    // create itemstack
    }
     
  13. Offline

    NullChips

    @CrystallFTW I'm asking how I might create a kit, and then store it in the config, not how to access the items via a string list... Or do I have to enter them manually?
     
  14. Offline

    CrystallFTW

    @NullChips How do you want to create a kit? with a command in game or to make like a 'default' in configs ?
     
  15. Offline

    NullChips

    @CrystallFTW Ideally with a command in game - it would just create a kit with that name, specified by args[0], and then put a default kit of, for example, a stone sword and a bow in there.
     
  16. Offline

    CrystallFTW

    @NullChips well, for example if the command is /kit args[0], set the line
    Code:
    "Kit."+args[0] +".Armor" 
    to
    Code:
    p.getInventory().getArmorContents()
    and for other stuff in the inventory
    Code:
    "Kit."+args[0] +".Items"
    to
    Code:
    p.getInventory().getContents()
    this might work, but it will look kinda messy in the config.
     
  17. Offline

    NullChips

    @CrystallFTW Thanks, that works fine. But how would I go about giving the players these kits? Using this doesn't work, giving me an unhandled exception.

    Code:
    p.getInventory().setContents((ItemStack[]) Main.getPlugin().getConfig().get("Kits." + k.getId() + ".Items"));
    p.getInventory().setArmorContents((ItemStack[]) Main.getPlugin().getConfig().get("Kits." + k.getId() + ".Armour"));
     
  18. Offline

    CrystallFTW

    @NullChips something like this:
    Code:
    ItemStack[] content = ((List<ItemStack>) getConfig().get("Kit." + args[0] +".Armor")).toArray(new ItemStack[0]);
    p.getInventory().setArmorContents(content);
    content = ((List<ItemStack>) getConfig().get("Kit." + args[0] +".Items").toArray(new ItemStack[0]);
     p.getInventory().setContents(content);
    I would recommand you to create a separate method for this.
     
  19. Offline

    NullChips

    @CrystallFTW Still having issues. I'm getting a NullPointerException. I know that I'm referencing the config correctly, however. The exception is pointing towards line 84. Here's the class in which I have placed the method:

    Code:
    package me.nullchips.ffa.handlers;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    
    import me.nullchips.ffa.FFA;
    
    public class Kit {
    
        public static ArrayList<Kit> allKits = new ArrayList<Kit>();
    
        private int displayItem;
        private String displayName;
        private String id;
        private int price;
    
        public Kit(String id) {
            this.id = id;
            this.displayName = (String) FFA.getPlugin().getConfig().get("Kits." + id + ".DisplayName");
            this.displayItem = (int) FFA.getPlugin().getConfig().get("Kits." + id + ".DisplayItem");
            this.price = (int) FFA.getPlugin().getConfig().get("Kits." + id + ".Price");
        }
    
        public String getDisplayName() {
            return displayName;
        }
    
        public static Kit getKit(Player p, Kit k) {
            for(Kit kit : allKits) {
                if(kit.getDisplayName().equalsIgnoreCase(kit.getDisplayName())) {
                    return k;
                }
            }
            return null;
        }
    
        public String getId() {
            return id;
        }
    
        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }
    
        public int getDisplayItem() {
            return displayItem;
        }
    
        public void setDisplayItem(int displayItem) {
            this.displayItem = displayItem;
        }
    
        public int getPrice() {
            return price;
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
    
        public ArrayList<Kit> getKits() {
            return allKits;
        }
    
        public void addKit(Kit kit) {
            allKits.add(kit);
        }
    
        public void removeKit(Kit kit) {
            allKits.remove(kit);
        }
    
        public boolean isKit(Kit kit) {
            return allKits.contains(kit);
        }
    
        @SuppressWarnings("unchecked")
        public void giveKit(Player p, String id) {
    
            ItemStack[] content;
            content = ((List<ItemStack>) FFA.getPlugin().getConfig().get("Kits." + id +".Armor")).toArray(new ItemStack[0]);
            p.getInventory().setArmorContents(content);
            content = ((List<ItemStack>) FFA.getPlugin().getConfig().get("Kits." + id +".Items")).toArray(new ItemStack[0]);
            p.getInventory().setContents(content);
    
        }
    }
    
    
    
    Bumpity bump.

    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