Generating defined items chest

Discussion in 'Plugin Development' started by Hex_27, Dec 28, 2014.

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

    Hex_27

    Here's my config:
    Code:
    refill-contents:
    - DIAMOND
    - IRON_ORE
    - IRON_INGOT
    - DIRT
    - APPLE
    Here's the code that fills the chest with stuff, but it simply told the console that the item was invalid. (The exception message that I set)
    Code:
                    Material[] materials = {};
                    for(String s : plugin.getConfig().getStringList("refill-contents")){
                        try{
                          
                            Material m = Material.getMaterial(s);
                            ArrayList<Material> temp = new ArrayList<Material>(Arrays.asList(materials));
                            temp.add(m);
                           
                            materials = (Material[]) temp.toArray();
                          
                        }catch(Exception e){
                            System.out.println("[ChestRefill]: An invalid material was detected!");
                        }
                    }
                  
                    for(String sloc: plugin.chests.getConfigurationSection("").getKeys(false)){
                        sender.sendMessage(sloc);
                        sender.sendMessage(plugin.chests.getString(sloc));
                        Location loc = stringToLocation(plugin.chests.getString(sloc));
                        Block block = loc.getBlock();
                        if(block instanceof Chest){
                            Chest chest = (Chest) block;
                            if(isInvEmpty(chest.getInventory())){
                                Inventory cinv = chest.getInventory();
                                Random random = new Random();
                              
                              
                                  for (int i = 0; i <= materials.length; i++) {
                                    if (random.nextInt(materials.length) == i) {
                                        int maxstack = random.nextInt(plugin.getConfig().getInt("refill-max-amt"));
                                        if (maxstack > plugin.getConfig().getInt("refill-min-amt")) {
                                            cinv.addItem(new ItemStack(materials[i], maxstack));
                                        }else{
                                            cinv.addItem(new ItemStack(materials[i], plugin.getConfig().getInt("refill-min-amt")));
                                        }
                                    }
                                }
                            }
                        }
                    }
                  
                  
               
                
     
  2. I'm on my phone right now so I only quickly glanced at the code but I noticed 2 things
    1) why don't you just make materials a list instead of converting it back and forth.
    2) for(String sloc: plugin.chests.getConfigurationSection("").getKeys(false)){
    Could just be plugin.chests.getKeys(false)
     
  3. Offline

    mine-care

    @Hex_27
    dont use system.out.println(); there is a reason why Logger exists.

    also dont use the nuke exception catch
    prefer to catch specific exceptions and handle them properly
     
  4. Offline

    Hex_27

    @mine-care whats the exception when a string isn't recognized as a material?
    @WD_MUFFINFLUFFER I'll would try that, but I need the
    materials[number] thing for a random generation. Unless its possible to take a value from a list from random.
     
  5. Offline

    mine-care

    @Hex_27 No exception, the code for this method is:
    Code:
    public static Material getMaterial(final String name) {
    return BY_NAME.get(name); // BY_NAME is a map so when its not found returns null.
    }
    But you might want to use the matchMaterial, the code of witch is:
    Code:
    public static Material matchMaterial(final String name) {
    Material result = null;
                 try {
                   result = getMaterial(Integer.parseInt(name));
               } catch (NumberFormatException ex) {}
      
               if (result == null) {
                   String filtered = name.toUpperCase();
                   filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", "");
                  result = BY_NAME.get(filtered);
               }
               return result;
          }
    As you see it is more flexible to inputs :)
    So prefer to use the matchMaterial and check material for null instead of exceptions.
     
  6. Offline

    Hex_27

    @mine-care the BY_NAME is unrecognized by eclipse
     
  7. Offline

    mine-care

    @Hex_27 ... i was not giving you code to use xD i showed you how the getMaterial(name) works, this code is not mine, its bukkit's, as i sugested use Material.matchMaterial(material);
     
  8. Offline

    1Rogue


    And that's why you don't blindly copy/paste code from posts.
     
    mine-care likes this.
  9. list.get(new Random().nextInt(list.size())); ?
     
Thread Status:
Not open for further replies.

Share This Page