Save materials in config?

Discussion in 'Plugin Development' started by Scrapnix, Feb 16, 2017.

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

    Scrapnix

    Hey, I saved a whole hotbar into a config.
    My config looks like this:
    Config.yml (open)

    Code:
    inv:
      slot1:
        material: IRON_SWORD
        name: '&aSword'
        amount: 1
      slot2:
        material: BOW
        name: '&4Bow'
        amount: 1
      slot3:
        material: FISHING_ROD
        name: §8Fishing Rod
        amount: 1
      slot4:
        material: BAKED_POTATOE
        name: '&6Food'
        amount: 1
      slot5:
        material: ARROW
        name: '&7Arrow'
        amount: 1
      slot6:
        material: AIR
        name: ''
        amount: 0
      slot7:
        material: AIR
        name: ''
        amount: 0
      slot8:
        material: AIR
        name: ''
        amount: 0
      slot9:
        material: AIR
        name: ''
        amount: 0
      boots:
        material: IRON_BOOTS
        name: '&6Boots'
      leggins:
        material: IRON_LEGGINS
        name: '&6Leggins'
      chestplate:
        material: IRON_CHESTPLATE
        name: '&6Chestplate'
      helmet:
        material: IRON_HELMET
        name: '&6Helmet'


    And my code like this:
    Methods.java (open)

    Code:
        public static void ItemStack(String material, String name, String amount, int slot, Player p) {
            Main.that.getConfig().getString("inv.slot1.name").replace("&", "§");
            Main.that.getConfig().getString("inv.slot2.name").replace("&", "§");
            Main.that.getConfig().getString("inv.slot3.name").replace("&", "§");
            Main.that.getConfig().getString("inv.slot4.name").replace("&", "§");
            Main.that.getConfig().getString("inv.slot5.name").replace("&", "§");
            Main.that.getConfig().getString("inv.slot6.name").replace("&", "§");
            Main.that.getConfig().getString("inv.slot7.name").replace("&", "§");
            Main.that.getConfig().getString("inv.slot8.name").replace("&", "§");
            Main.that.getConfig().getString("inv.slot9.name").replace("&", "§");
            Main.that.getConfig().getString("inv.boots.name").replace("&", "§");
            Main.that.getConfig().getString("inv.leggins.name").replace("&", "§");
            Main.that.getConfig().getString("inv.chestplate.name").replace("&", "§");
            Main.that.getConfig().getString("inv.helmet.name").replace("&", "§");
            ItemStack slot1 = new ItemStack(Material.getMaterial(Main.that.getConfig().getString(material)));
            ItemMeta imslot1 = slot1.getItemMeta();
            imslot1.setDisplayName(Main.that.getConfig().getString(name));
            slot1.setItemMeta(imslot1);
            slot1.setAmount(Main.that.getConfig().getInt(amount));
            p.getInventory().setItem(slot, slot1);
        }
      
        public static void equip(Player p) {
            ItemStack("inv.slot1.material", "inv.slot1.name", "inv.slot1.amount", 0, p);
            ItemStack("inv.slot2.material", "inv.slot2.name", "inv.slot2.amount", 1, p);
            ItemStack("inv.slot3.material", "inv.slot3.name", "inv.slot3.amount", 2, p);
            ItemStack("inv.slot4.material", "inv.slot4.name", "inv.slot4.amount", 3, p);
            ItemStack("inv.slot5.material", "inv.slot5.name", "inv.slot5.amount", 4, p);
            ItemStack("inv.slot6.material", "inv.slot6.name", "inv.slot6.amount", 5, p);
            ItemStack("inv.slot7.material", "inv.slot7.name", "inv.slot7.amount", 6, p);
            ItemStack("inv.slot8.material", "inv.slot8.name", "inv.slot8.amount", 7, p);
            ItemStack("inv.slot9.material", "inv.slot9.name", "inv.slot9.amount", 8, p);
            ItemStack boots = new ItemStack(Material.getMaterial(Main.that.getConfig().getString("inv.boots.material")));
            ItemMeta imboots = boots.getItemMeta();
            imboots.setDisplayName(Main.that.getConfig().getString("inv.boots.name"));
            boots.setItemMeta(imboots);
            ItemStack leggins = new ItemStack(Material.getMaterial(Main.that.getConfig().getString("inv.leggins.material")));
            ItemMeta imleggins = leggins.getItemMeta();
            imleggins.setDisplayName(Main.that.getConfig().getString("inv.leggins.name"));
            leggins.setItemMeta(imleggins);
            ItemStack chest = new ItemStack(Material.getMaterial(Main.that.getConfig().getString("inv.chestplate.material")));
            ItemMeta imchest = chest.getItemMeta();
            imchest.setDisplayName(Main.that.getConfig().getString("inv.chestplate.name"));
            chest.setItemMeta(imchest);
            ItemStack helmet = new ItemStack(Material.getMaterial(Main.that.getConfig().getString("inv.helmet.material")));
            ItemMeta imhelmet = helmet.getItemMeta();
            imhelmet.setDisplayName(Main.that.getConfig().getString("inv.helmet.name"));
            helmet.setItemMeta(imhelmet);
    
        }


    Now my problem:
    Everytime I get equipped, it adds only three items to my inventory, the sword, the bow and the fishing rod.
    The & in front of the name of the sword and the bow becomes replaced threw the §, but the fishing rod becomes replaced and has the right color. There is no error in my console.

    I hope someone can help me with this crazy mistakes.

    EDIT: I forgot to add the p.getInventory().setBoots(boots, ...) - First mistake - checked XD
     
  2. Offline

    Zombie_Striker

    @Scrapnix
    If your problem has been solved, mark this thread as solved.
     
  3. Offline

    Scrapnix

    It was only the first mistake, the other items don't appear!
     
  4. Offline

    Zombie_Striker

    @Scrapnix
    Okay, so I just now looked at your code. There is a lot of things that can be improved upon:
    1. Don't abuse static. "That" is not only not a descriptive name, but you should almost never have to use static. Remove the static modifier, and instead pass the main's instance through this class's constructor.
    2. Since the only thing that changes between items is the name, create a for int loop, starting at 1 and stopping at 9. This will turn those 9 lines into 2.
    3. ChatColor.translateAlternateColorCodes does the same thing as your replace method, but will also be version independent in case of changes to the chat system.
    4. Your itemstack method is very confusing. Instead of getting all the names for every item in the config every time you want to create a single item, you should instead require the path to be required in the parameter.
    5. Most of your code is duplicated. When it comes to coding, you should never copy and paste code. You are either violating DRY (don't repeat yourself) or you are stealing code from another project. In either case, you will end up with potentially broken and inefficient code.
     
  5. Offline

    Scrapnix

    @Zombie_Striker First to your 5th statement: I never copied any piece of code from other projects and I don't understand what you think in my code is unnessasary (redundant).
    To your 4th statement: Okay, thanks for this tip. I will remove the ItemStack method.
    To your 3rd statement: Very good tip too, bro ;)
    To your 2nd statement: Sry, I'm quite new to Bukkit, so I don't know what you mean with the loops. Maybe it could be because of that I am from Germany or maybe I never learned something about that. Perhaps you could describe me how I could make that :)
    To your 1st statement: I will use constructors for every class, thanks XD
     
  6. Offline

    Drkmaster83

    • Which would you prefer?
    Code:
    public String get50As() {
        return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    }
    ...
    public String get20As() {
    ...
    public String get1A() {
    
    or
    Code:
    public String getA(int x) {
        return ImaginaryStringHelper.createRepetitiveString("a", 50); //String, int timesRepeat
    }
    
    public static void main(String[] args) {
        String fiftyAs = getA(50);
        String twentyAs = getA(20);
    }
    
    It's inefficient to have to create 30 methods that do the same rough idea, but remain different. The second snippet of code is an imaginary scenario that demonstrates you can use one method to get two different results that have the same base, while the first demonstrates a scenario where you'd have to create two different methods (and every method in between to cover your bases).

    • You're not new to Bukkit, then, you're new to Java. Read about loop control here.
     
    ipodtouch0218 likes this.
Thread Status:
Not open for further replies.

Share This Page