[API] Minecraft Items - Turn string into material with custom name supported

Discussion in 'Plugin Development' started by spywhere, Apr 6, 2013.

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

    spywhere


    Here is my API for gathering a material from string (I know there's a method getMaterial()) but this also support a custom name of material. All within .yml file

    Requirements
    - This API

    How to use?
    Well, just create new class and put these code in it!

    Methods
    • items.loadItems(String folder);
      • Load materials from materials.yml in specified folder
    • Material material = items.getMaterial(String material);
      • Get material from string. Return null if not found
    • int material = items.getMaterialID(String material);
      • Get material ID from string. Return -1 if not found
    • List<String> nameList = items.getMaterialName(Material material);
      • Get material names from material
    • List<String> nameList = items.getMaterialName(int material);
      • Get material names from material ID
    Source code:
    Show Spoiler
    MinecraftItem.java
    Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
     
    import org.bukkit.Material;
    import org.bukkit.configuration.InvalidConfigurationException;
     
    public class MinecraftItem {
        private HashMap<Integer, List<String>> materialList;
       
        public MinecraftItem() {
            materialList = new HashMap<Integer, List<String>>();
        }
       
        public void loadItems(String folder) throws FileNotFoundException, IOException, InvalidConfigurationException {
            YMLIO yml = new YMLIO(new File(folder, "materials.yml"));
            for(Material mat:Material.values()){
                String str = mat.getId() + ":" + mat.toString() + ((mat.toString().indexOf("_") != -1) ? ":" + mat.toString().replaceAll("_", " ") : "");
                materialList.put(mat.getId(), yml.getStringList("Materials." + mat.getId(), Arrays.asList(str.toLowerCase().split(":"))));
            }
            yml.save();
        }
       
        public Material getMaterial(String material) {
            int id = getMaterialID(material);
            if(id < 0){ return null; }
            return Material.getMaterial(id);
        }
       
        public int getMaterialID(String material) {
            for(int key:materialList.keySet()){
                List<String> matList = getMaterialName(key);
                if(matList == null){
                    continue;
                }
                for(String str:matList){
                    if(str.equalsIgnoreCase(material)){ return key; }
                }
            }
            return -1;
        }
       
        public List<String> getMaterialName(int id) {
            if(materialList.containsKey(id)){ return materialList.get(id); }
            return null;
        }
       
        public List<String> getMaterialName(Material material) {
            return getMaterialName(material.getId());
        }
    }
    


    Example usage:
    Code:
    MinecraftItem items = new MinecraftItem();
    items.loadItems(this.getDataFolder().toString());
    Material mat = items.getMaterial("Diamond Block");
    if(mat != null){
        // Diamond Block is found!
    }else{
        // Diamond block not found!
    }
    Contents of materials.yml (as default) WARNING! Very long text
    Show Spoiler

    Code:
    Materials:
      '0':
      - '0'
      - air
      '1':
      - '1'
      - stone
      '2':
      - '2'
      - grass
      '3':
      - '3'
      - dirt
      '4':
      - '4'
      - cobblestone
      '5':
      - '5'
      - wood
      '6':
      - '6'
      - sapling
      '7':
      - '7'
      - bedrock
      '8':
      - '8'
      - water
      '9':
      - '9'
      - stationary_water
      - stationary water
      '10':
      - '10'
      - lava
      '11':
      - '11'
      - stationary_lava
      - stationary lava
      '12':
      - '12'
      - sand
      '13':
      - '13'
      - gravel
      '14':
      - '14'
      - gold_ore
      - gold ore
      '15':
      - '15'
      - iron_ore
      - iron ore
      '16':
      - '16'
      - coal_ore
      - coal ore
      '17':
      - '17'
      - log
      '18':
      - '18'
      - leaves
      '19':
      - '19'
      - sponge
      '20':
      - '20'
      - glass
      '21':
      - '21'
      - lapis_ore
      - lapis ore
      '22':
      - '22'
      - lapis_block
      - lapis block
      '23':
      - '23'
      - dispenser
      '24':
      - '24'
      - sandstone
      '25':
      - '25'
      - note_block
      - note block
      '26':
      - '26'
      - bed_block
      - bed block
      '27':
      - '27'
      - powered_rail
      - powered rail
      '28':
      - '28'
      - detector_rail
      - detector rail
      '29':
      - '29'
      - piston_sticky_base
      - piston sticky base
      '30':
      - '30'
      - web
      '31':
      - '31'
      - long_grass
      - long grass
      '32':
      - '32'
      - dead_bush
      - dead bush
      '33':
      - '33'
      - piston_base
      - piston base
      '34':
      - '34'
      - piston_extension
      - piston extension
      '35':
      - '35'
      - wool
      '36':
      - '36'
      - piston_moving_piece
      - piston moving piece
      '37':
      - '37'
      - yellow_flower
      - yellow flower
      '38':
      - '38'
      - red_rose
      - red rose
      '39':
      - '39'
      - brown_mushroom
      - brown mushroom
      '40':
      - '40'
      - red_mushroom
      - red mushroom
      '41':
      - '41'
      - gold_block
      - gold block
      '42':
      - '42'
      - iron_block
      - iron block
      '43':
      - '43'
      - double_step
      - double step
      '44':
      - '44'
      - step
      '45':
      - '45'
      - brick
      '46':
      - '46'
      - tnt
      '47':
      - '47'
      - bookshelf
      '48':
      - '48'
      - mossy_cobblestone
      - mossy cobblestone
      '49':
      - '49'
      - obsidian
      '50':
      - '50'
      - torch
      '51':
      - '51'
      - fire
      '52':
      - '52'
      - mob_spawner
      - mob spawner
      '53':
      - '53'
      - wood_stairs
      - wood stairs
      '54':
      - '54'
      - chest
      '55':
      - '55'
      - redstone_wire
      - redstone wire
      '56':
      - '56'
      - diamond_ore
      - diamond ore
      '57':
      - '57'
      - diamond_block
      - diamond block
      '58':
      - '58'
      - workbench
      '59':
      - '59'
      - crops
      '60':
      - '60'
      - soil
      '61':
      - '61'
      - furnace
      '62':
      - '62'
      - burning_furnace
      - burning furnace
      '63':
      - '63'
      - sign_post
      - sign post
      '64':
      - '64'
      - wooden_door
      - wooden door
      '65':
      - '65'
      - ladder
      '66':
      - '66'
      - rails
      '67':
      - '67'
      - cobblestone_stairs
      - cobblestone stairs
      '68':
      - '68'
      - wall_sign
      - wall sign
      '69':
      - '69'
      - lever
      '70':
      - '70'
      - stone_plate
      - stone plate
      '71':
      - '71'
      - iron_door_block
      - iron door block
      '72':
      - '72'
      - wood_plate
      - wood plate
      '73':
      - '73'
      - redstone_ore
      - redstone ore
      '74':
      - '74'
      - glowing_redstone_ore
      - glowing redstone ore
      '75':
      - '75'
      - redstone_torch_off
      - redstone torch off
      '76':
      - '76'
      - redstone_torch_on
      - redstone torch on
      '77':
      - '77'
      - stone_button
      - stone button
      '78':
      - '78'
      - snow
      '79':
      - '79'
      - ice
      '80':
      - '80'
      - snow_block
      - snow block
      '81':
      - '81'
      - cactus
      '82':
      - '82'
      - clay
      '83':
      - '83'
      - sugar_cane_block
      - sugar cane block
      '84':
      - '84'
      - jukebox
      '85':
      - '85'
      - fence
      '86':
      - '86'
      - pumpkin
      '87':
      - '87'
      - netherrack
      '88':
      - '88'
      - soul_sand
      - soul sand
      '89':
      - '89'
      - glowstone
      '90':
      - '90'
      - portal
      '91':
      - '91'
      - jack_o_lantern
      - jack o lantern
      '92':
      - '92'
      - cake_block
      - cake block
      '93':
      - '93'
      - diode_block_off
      - diode block off
      '94':
      - '94'
      - diode_block_on
      - diode block on
      '95':
      - '95'
      - locked_chest
      - locked chest
      '96':
      - '96'
      - trap_door
      - trap door
      '97':
      - '97'
      - monster_eggs
      - monster eggs
      '98':
      - '98'
      - smooth_brick
      - smooth brick
      '99':
      - '99'
      - huge_mushroom_1
      - huge mushroom 1
      '100':
      - '100'
      - huge_mushroom_2
      - huge mushroom 2
      '101':
      - '101'
      - iron_fence
      - iron fence
      '102':
      - '102'
      - thin_glass
      - thin glass
      '103':
      - '103'
      - melon_block
      - melon block
      '104':
      - '104'
      - pumpkin_stem
      - pumpkin stem
      '105':
      - '105'
      - melon_stem
      - melon stem
      '106':
      - '106'
      - vine
      '107':
      - '107'
      - fence_gate
      - fence gate
      '108':
      - '108'
      - brick_stairs
      - brick stairs
      '109':
      - '109'
      - smooth_stairs
      - smooth stairs
      '110':
      - '110'
      - mycel
      '111':
      - '111'
      - water_lily
      - water lily
      '112':
      - '112'
      - nether_brick
      - nether brick
      '113':
      - '113'
      - nether_fence
      - nether fence
      '114':
      - '114'
      - nether_brick_stairs
      - nether brick stairs
      '115':
      - '115'
      - nether_warts
      - nether warts
      '116':
      - '116'
      - enchantment_table
      - enchantment table
      '117':
      - '117'
      - brewing_stand
      - brewing stand
      '118':
      - '118'
      - cauldron
      '119':
      - '119'
      - ender_portal
      - ender portal
      '120':
      - '120'
      - ender_portal_frame
      - ender portal frame
      '121':
      - '121'
      - ender_stone
      - ender stone
      '122':
      - '122'
      - dragon_egg
      - dragon egg
      '123':
      - '123'
      - redstone_lamp_off
      - redstone lamp off
      '124':
      - '124'
      - redstone_lamp_on
      - redstone lamp on
      '125':
      - '125'
      - wood_double_step
      - wood double step
      '126':
      - '126'
      - wood_step
      - wood step
      '127':
      - '127'
      - cocoa
      '128':
      - '128'
      - sandstone_stairs
      - sandstone stairs
      '129':
      - '129'
      - emerald_ore
      - emerald ore
      '130':
      - '130'
      - ender_chest
      - ender chest
      '131':
      - '131'
      - tripwire_hook
      - tripwire hook
      '132':
      - '132'
      - tripwire
      '133':
      - '133'
      - emerald_block
      - emerald block
      '134':
      - '134'
      - spruce_wood_stairs
      - spruce wood stairs
      '135':
      - '135'
      - birch_wood_stairs
      - birch wood stairs
      '136':
      - '136'
      - jungle_wood_stairs
      - jungle wood stairs
      '137':
      - '137'
      - command
      '138':
      - '138'
      - beacon
      '139':
      - '139'
      - cobble_wall
      - cobble wall
      '140':
      - '140'
      - flower_pot
      - flower pot
      '141':
      - '141'
      - carrot
      '142':
      - '142'
      - potato
      '143':
      - '143'
      - wood_button
      - wood button
      '144':
      - '144'
      - skull
      '145':
      - '145'
      - anvil
      '146':
      - '146'
      - trapped_chest
      - trapped chest
      '147':
      - '147'
      - gold_plate
      - gold plate
      '148':
      - '148'
      - iron_plate
      - iron plate
      '149':
      - '149'
      - redstone_comparator_off
      - redstone comparator off
      '150':
      - '150'
      - redstone_comparator_on
      - redstone comparator on
      '151':
      - '151'
      - daylight_detector
      - daylight detector
      '152':
      - '152'
      - redstone_block
      - redstone block
      '153':
      - '153'
      - quartz_ore
      - quartz ore
      '154':
      - '154'
      - hopper
      '155':
      - '155'
      - quartz_block
      - quartz block
      '156':
      - '156'
      - quartz_stairs
      - quartz stairs
      '157':
      - '157'
      - activator_rail
      - activator rail
      '158':
      - '158'
      - dropper
      '256':
      - '256'
      - iron_spade
      - iron spade
      '257':
      - '257'
      - iron_pickaxe
      - iron pickaxe
      '258':
      - '258'
      - iron_axe
      - iron axe
      '259':
      - '259'
      - flint_and_steel
      - flint and steel
      '260':
      - '260'
      - apple
      '261':
      - '261'
      - bow
      '262':
      - '262'
      - arrow
      '263':
      - '263'
      - coal
      '264':
      - '264'
      - diamond
      '265':
      - '265'
      - iron_ingot
      - iron ingot
      '266':
      - '266'
      - gold_ingot
      - gold ingot
      '267':
      - '267'
      - iron_sword
      - iron sword
      '268':
      - '268'
      - wood_sword
      - wood sword
      '269':
      - '269'
      - wood_spade
      - wood spade
      '270':
      - '270'
      - wood_pickaxe
      - wood pickaxe
      '271':
      - '271'
      - wood_axe
      - wood axe
      '272':
      - '272'
      - stone_sword
      - stone sword
      '273':
      - '273'
      - stone_spade
      - stone spade
      '274':
      - '274'
      - stone_pickaxe
      - stone pickaxe
      '275':
      - '275'
      - stone_axe
      - stone axe
      '276':
      - '276'
      - diamond_sword
      - diamond sword
      '277':
      - '277'
      - diamond_spade
      - diamond spade
      '278':
      - '278'
      - diamond_pickaxe
      - diamond pickaxe
      '279':
      - '279'
      - diamond_axe
      - diamond axe
      '280':
      - '280'
      - stick
      '281':
      - '281'
      - bowl
      '282':
      - '282'
      - mushroom_soup
      - mushroom soup
      '283':
      - '283'
      - gold_sword
      - gold sword
      '284':
      - '284'
      - gold_spade
      - gold spade
      '285':
      - '285'
      - gold_pickaxe
      - gold pickaxe
      '286':
      - '286'
      - gold_axe
      - gold axe
      '287':
      - '287'
      - string
      '288':
      - '288'
      - feather
      '289':
      - '289'
      - sulphur
      '290':
      - '290'
      - wood_hoe
      - wood hoe
      '291':
      - '291'
      - stone_hoe
      - stone hoe
      '292':
      - '292'
      - iron_hoe
      - iron hoe
      '293':
      - '293'
      - diamond_hoe
      - diamond hoe
      '294':
      - '294'
      - gold_hoe
      - gold hoe
      '295':
      - '295'
      - seeds
      '296':
      - '296'
      - wheat
      '297':
      - '297'
      - bread
      '298':
      - '298'
      - leather_helmet
      - leather helmet
      '299':
      - '299'
      - leather_chestplate
      - leather chestplate
      '300':
      - '300'
      - leather_leggings
      - leather leggings
      '301':
      - '301'
      - leather_boots
      - leather boots
      '302':
      - '302'
      - chainmail_helmet
      - chainmail helmet
      '303':
      - '303'
      - chainmail_chestplate
      - chainmail chestplate
      '304':
      - '304'
      - chainmail_leggings
      - chainmail leggings
      '305':
      - '305'
      - chainmail_boots
      - chainmail boots
      '306':
      - '306'
      - iron_helmet
      - iron helmet
      '307':
      - '307'
      - iron_chestplate
      - iron chestplate
      '308':
      - '308'
      - iron_leggings
      - iron leggings
      '309':
      - '309'
      - iron_boots
      - iron boots
      '310':
      - '310'
      - diamond_helmet
      - diamond helmet
      '311':
      - '311'
      - diamond_chestplate
      - diamond chestplate
      '312':
      - '312'
      - diamond_leggings
      - diamond leggings
      '313':
      - '313'
      - diamond_boots
      - diamond boots
      '314':
      - '314'
      - gold_helmet
      - gold helmet
      '315':
      - '315'
      - gold_chestplate
      - gold chestplate
      '316':
      - '316'
      - gold_leggings
      - gold leggings
      '317':
      - '317'
      - gold_boots
      - gold boots
      '318':
      - '318'
      - flint
      '319':
      - '319'
      - pork
      '320':
      - '320'
      - grilled_pork
      - grilled pork
      '321':
      - '321'
      - painting
      '322':
      - '322'
      - golden_apple
      - golden apple
      '323':
      - '323'
      - sign
      '324':
      - '324'
      - wood_door
      - wood door
      '325':
      - '325'
      - bucket
      '326':
      - '326'
      - water_bucket
      - water bucket
      '327':
      - '327'
      - lava_bucket
      - lava bucket
      '328':
      - '328'
      - minecart
      '329':
      - '329'
      - saddle
      '330':
      - '330'
      - iron_door
      - iron door
      '331':
      - '331'
      - redstone
      '332':
      - '332'
      - snow_ball
      - snow ball
      '333':
      - '333'
      - boat
      '334':
      - '334'
      - leather
      '335':
      - '335'
      - milk_bucket
      - milk bucket
      '336':
      - '336'
      - clay_brick
      - clay brick
      '337':
      - '337'
      - clay_ball
      - clay ball
      '338':
      - '338'
      - sugar_cane
      - sugar cane
      '339':
      - '339'
      - paper
      '340':
      - '340'
      - book
      '341':
      - '341'
      - slime_ball
      - slime ball
      '342':
      - '342'
      - storage_minecart
      - storage minecart
      '343':
      - '343'
      - powered_minecart
      - powered minecart
      '344':
      - '344'
      - egg
      '345':
      - '345'
      - compass
      '346':
      - '346'
      - fishing_rod
      - fishing rod
      '347':
      - '347'
      - watch
      '348':
      - '348'
      - glowstone_dust
      - glowstone dust
      '349':
      - '349'
      - raw_fish
      - raw fish
      '350':
      - '350'
      - cooked_fish
      - cooked fish
      '351':
      - '351'
      - ink_sack
      - ink sack
      '352':
      - '352'
      - bone
      '353':
      - '353'
      - sugar
      '354':
      - '354'
      - cake
      '355':
      - '355'
      - bed
      '356':
      - '356'
      - diode
      '357':
      - '357'
      - cookie
      '358':
      - '358'
      - map
      '359':
      - '359'
      - shears
      '360':
      - '360'
      - melon
      '361':
      - '361'
      - pumpkin_seeds
      - pumpkin seeds
      '362':
      - '362'
      - melon_seeds
      - melon seeds
      '363':
      - '363'
      - raw_beef
      - raw beef
      '364':
      - '364'
      - cooked_beef
      - cooked beef
      '365':
      - '365'
      - raw_chicken
      - raw chicken
      '366':
      - '366'
      - cooked_chicken
      - cooked chicken
      '367':
      - '367'
      - rotten_flesh
      - rotten flesh
      '368':
      - '368'
      - ender_pearl
      - ender pearl
      '369':
      - '369'
      - blaze_rod
      - blaze rod
      '370':
      - '370'
      - ghast_tear
      - ghast tear
      '371':
      - '371'
      - gold_nugget
      - gold nugget
      '372':
      - '372'
      - nether_stalk
      - nether stalk
      '373':
      - '373'
      - potion
      '374':
      - '374'
      - glass_bottle
      - glass bottle
      '375':
      - '375'
      - spider_eye
      - spider eye
      '376':
      - '376'
      - fermented_spider_eye
      - fermented spider eye
      '377':
      - '377'
      - blaze_powder
      - blaze powder
      '378':
      - '378'
      - magma_cream
      - magma cream
      '379':
      - '379'
      - brewing_stand_item
      - brewing stand item
      '380':
      - '380'
      - cauldron_item
      - cauldron item
      '381':
      - '381'
      - eye_of_ender
      - eye of ender
      '382':
      - '382'
      - speckled_melon
      - speckled melon
      '383':
      - '383'
      - monster_egg
      - monster egg
      '384':
      - '384'
      - exp_bottle
      - exp bottle
      '385':
      - '385'
      - fireball
      '386':
      - '386'
      - book_and_quill
      - book and quill
      '387':
      - '387'
      - written_book
      - written book
      '388':
      - '388'
      - emerald
      '389':
      - '389'
      - item_frame
      - item frame
      '390':
      - '390'
      - flower_pot_item
      - flower pot item
      '391':
      - '391'
      - carrot_item
      - carrot item
      '392':
      - '392'
      - potato_item
      - potato item
      '393':
      - '393'
      - baked_potato
      - baked potato
      '394':
      - '394'
      - poisonous_potato
      - poisonous potato
      '395':
      - '395'
      - empty_map
      - empty map
      '396':
      - '396'
      - golden_carrot
      - golden carrot
      '397':
      - '397'
      - skull_item
      - skull item
      '398':
      - '398'
      - carrot_stick
      - carrot stick
      '399':
      - '399'
      - nether_star
      - nether star
      '400':
      - '400'
      - pumpkin_pie
      - pumpkin pie
      '401':
      - '401'
      - firework
      '402':
      - '402'
      - firework_charge
      - firework charge
      '403':
      - '403'
      - enchanted_book
      - enchanted book
      '404':
      - '404'
      - redstone_comparator
      - redstone comparator
      '405':
      - '405'
      - nether_brick_item
      - nether brick item
      '406':
      - '406'
      - quartz
      '407':
      - '407'
      - explosive_minecart
      - explosive minecart
      '408':
      - '408'
      - hopper_minecart
      - hopper minecart
      '2256':
      - '2256'
      - gold_record
      - gold record
      '2257':
      - '2257'
      - green_record
      - green record
      '2258':
      - '2258'
      - record_3
      - record 3
      '2259':
      - '2259'
      - record_4
      - record 4
      '2260':
      - '2260'
      - record_5
      - record 5
      '2261':
      - '2261'
      - record_6
      - record 6
      '2262':
      - '2262'
      - record_7
      - record 7
      '2263':
      - '2263'
      - record_8
      - record 8
      '2264':
      - '2264'
      - record_9
      - record 9
      '2265':
      - '2265'
      - record_10
      - record 10
      '2266':
      - '2266'
      - record_11
      - record 11
      '2267':
      - '2267'
      - record_12
      - record 12
    
     
Thread Status:
Not open for further replies.

Share This Page