Solved Adding many items to hashmap

Discussion in 'Plugin Development' started by xepisolonxx, Jun 28, 2015.

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

    xepisolonxx

    I have been trying to work on this for 2m now but i cannot find a way to do this. What im trying to do is adding lots of various items to certain player so therefore i am using hashmaps but, i cannot because it tells me to put ItemStack[] but the items i want to put are under ItemStack

    Class:
    Code:
    public class kit implements CommandExecutor {
      public static  HashMap<String, ItemStack> rng = new HashMap<String, ItemStack>();
    
           
            @Override
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Only players can do that!");
                    return true;  
            }
               
                Player player = (Player)sender;
                if (GameState.isState(GameState.LOBBY)){
                if (cmd.getName().equalsIgnoreCase("kits") || cmd.getName().equalsIgnoreCase("kit")){       
                   
                 player.playSound(player.getLocation(), Sound.CHICKEN_EGG_POP, 1, 1);
                 organise(player);
                player.openInventory(Inv.inv);               
               
                        }
                }
               
                return false;
                }
           
                @SuppressWarnings("deprecation")
                public void organise(Player player) {
                  //assassin
                        ItemStack assassin = new ItemStack(Material.REDSTONE);
                        ItemMeta meta1 = assassin.getItemMeta();
                        meta1.setDisplayName(ChatColor.WHITE + "Assassin");
                        ArrayList<String> lores = new ArrayList<String>();
                        lores.add(ChatColor.BLUE + "Great for solo players. Killing is business");
                        lores.add(ChatColor.BLUE + "Business is good!");
                        lores.add(" ");
                        lores.add(ChatColor.BLUE + "Right click your redstone for 1.5s of speed");
                        lores.add(ChatColor.BLUE + "& strength but 4s of vulnerability.");
                   
                        meta1.setLore(lores);
                        assassin.setItemMeta(meta1);
                       
                        //thor
                        ItemStack thor = new ItemStack(Material.WOOD_AXE);
                        ItemMeta meta2 = thor.getItemMeta();
                        meta2.setDisplayName(ChatColor.WHITE + "Thor");
                        ArrayList<String> lores1 = new ArrayList<String>();
                        lores1.add(ChatColor.BLUE + "Hammer of thor at yours hands.");
                        lores1.add(ChatColor.BLUE + "You can have the power of a god!");
                        lores1.add("");
                        lores1.add(ChatColor.BLUE +  "Right click a block with your hammer");
                        lores1.add(ChatColor.BLUE + "to strike the highest block!");       
                        meta2.setLore(lores1);
                        thor.setItemMeta(meta2);
                       
                         //mobtamer
                        ItemStack mobtamer = new ItemStack(Material.EGG);
                        ItemMeta meta3 = mobtamer.getItemMeta();
                        meta3.setDisplayName(ChatColor.WHITE + "MobTamer");
                        ArrayList<String> lores2 = new ArrayList<String>();
                        lores2.add(ChatColor.BLUE + "Begin your journey.");
                        lores2.add(ChatColor.BLUE + "Gotta catch them all!");
                        lores2.add("");
                        lores2.add(ChatColor.BLUE +  "Hit a mob with an egg to");
                        lores2.add(ChatColor.BLUE + "make it drop its mob egg.");       
                        meta3.setLore(lores2);
                        mobtamer.setItemMeta(meta3);
                       
                         //enchanter
                        ItemStack enchanter = new ItemStack(Material.ENCHANTED_BOOK);
                        ItemMeta meta4 = enchanter.getItemMeta();
                        meta3.setDisplayName(ChatColor.WHITE + "Enchanter");
                        ArrayList<String> lores3 = new ArrayList<String>();
                        lores3.add(ChatColor.BLUE + "Kill mobs, mine ores.");
                        lores3.add(ChatColor.BLUE + "Aquire the exp.");
                        lores3.add("");
                        lores3.add(ChatColor.BLUE +  "Right click your enchanted");
                        lores3.add(ChatColor.BLUE + "book to open enchantment table.");       
                        meta4.setLore(lores3);
                        enchanter.setItemMeta(meta4);
                       
                         //ender
                        ItemStack ender = new ItemStack(Material.WOOL);
                        ItemMeta meta5 = ender.getItemMeta();
                        meta5.setDisplayName(ChatColor.WHITE + "Ender");
                        ArrayList<String> lores4 = new ArrayList<String>();
                        lores4.add(ChatColor.BLUE + "From the depths of the");
                        lores4.add(ChatColor.BLUE + "woods to mc.");
                        lores4.add("");
                        lores4.add(ChatColor.BLUE +  "Hit a player with a");
                        lores4.add(ChatColor.BLUE + "33% chance to blind them.");       
                        meta5.setLore(lores4);
                        ender.setItemMeta(meta5);
                        //fizz
                        ItemStack fizz = new ItemStack(Material.WATER_BUCKET);
                        ItemMeta meta6 = fizz.getItemMeta();
                        meta6.setDisplayName(ChatColor.WHITE + "Fizz");
                        ArrayList<String> lores5 = new ArrayList<String>();
                        lores5.add(ChatColor.BLUE + "Let me at 'em!");
                        lores5.add(ChatColor.BLUE + "The mighty shark stalks his prey.");
                        lores5.add(" ");
                        lores5.add(ChatColor.BLUE + "Jump into water for various buffs");
                        lores5.add(ChatColor.BLUE + " this includes strength & speed.");
                        meta6.setLore(lores5);
                        fizz.setItemMeta(meta6);
                        //worm
                        ItemStack worm = new ItemStack(Material.DIRT);
                        ItemMeta meta7 = worm.getItemMeta();
                        meta7.setDisplayName(ChatColor.WHITE + "Worm");
                        ArrayList<String> lores6 = new ArrayList<String>();
                        lores6.add(ChatColor.BLUE + "Early bird gets the worm!");
                        lores6.add(ChatColor.BLUE + "Gulp!");
                        lores6.add(" ");
                        lores6.add(ChatColor.BLUE + "Left click on any dirt blocks");
                        lores6.add(ChatColor.BLUE + " and instantly break it.");
                   
                        meta7.setLore(lores6);
                        worm.setItemMeta(meta7);
                        //sloth
                        ItemStack sloth = new ItemStack(Material.WOOL);
                        ItemMeta meta8 = sloth.getItemMeta();
                        meta8.setDisplayName(ChatColor.WHITE + "Sloth");
                        ArrayList<String> lores7 = new ArrayList<String>();
                        lores7.add(ChatColor.BLUE + "To sleepy to put message.");
                        lores7.add(ChatColor.BLUE + "Are we done yet?");
                        lores7.add(" ");
                        lores7.add(ChatColor.BLUE + "Has a 33% chance to");
                        lores7.add(ChatColor.BLUE + " give a player slowness.");
                   
                        meta8.setLore(lores7);
                        sloth.setItemMeta(meta8);
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
    
                       
                              Inv.inv.clear();
                              Inv.inv.setItem(0,Itemstack.Glass());
                              Inv.inv.setItem(1,Itemstack.Glass());
                              Inv.inv.setItem(2,Itemstack.Glass());
                              Inv.inv.setItem(3,Itemstack.Glass());
                              Inv.inv.setItem(4,Itemstack.RandomSelector());
                              Inv.inv.setItem(5,Itemstack.Glass());
                              Inv.inv.setItem(6,Itemstack.Glass());
                              Inv.inv.setItem(7,Itemstack.Glass());
                              Inv.inv.setItem(8,Itemstack.Glass());
    
                              if(player.hasPermission("kit.assassin"))
                                  rng.put(player.getName(),assassin);
                                  Inv.inv.addItem(assassin);
                              if(player.hasPermission("kit.thor"))
                                  rng.put(player.getName(),thor);
                                  Inv.inv.addItem(thor);
                              if(player.hasPermission("kit.mobtamer"))
                                  rng.put(player.getName(),mobtamer);
                                  Inv.inv.addItem(mobtamer);
                              if(player.hasPermission("kit.enchanter"))
                                  rng.put(player.getName(),enchanter);
                                  Inv.inv.addItem(enchanter);
                              if(player.hasPermission("kit.ender"))
                                  rng.put(player.getName(),ender);
                                  Inv.inv.addItem(ender);
                              if(player.hasPermission("kit.fizz"))
                                  rng.put(player.getName(),fizz);
                                  Inv.inv.addItem(fizz);
                              if(player.hasPermission("kit.worm"))
                                  rng.put(player.getName(),worm);
                                  Inv.inv.addItem(worm);
     
  2. Offline

    BagduFagdu

    @xepisolonxx
    If you try to loop using ItemStack, you should be getting
    "Can only iterate over an array or an instance of java.lang.Iterable", that's why you would have to use ItemStack[] in order to do an #addItem() loop.
     
  3. Offline

    tytwining

    Iterate through using this:

    Code:
    for(String s : rng.keySet()) {}
    Get the ItemStack:

    Code:
    rng.get(s)

    Also, why does it have to be a map?
     
  4. Offline

    xepisolonxx

    @tytwining it has to be map because it saves 2 objects the players name and the item to there name i cannot do arraylist because it isnt to a specific person i need a hashmap because im trying to get it within another class unless theres another way i am able to save the information to a specific person
     
  5. Offline

    Tecno_Wizard

    That run on though...

    What confuses me is what distinguishing data you would have in that itemstack array that allowed you to determine which index belongs to which player. As far as the info you have given us, this seems like a pointless operation unless you had some way to distinguish entries in that array, and even then it is pointless because you can determine the indices.
     
  6. Offline

    xepisolonxx

    @Tecno_Wizard Im trying to just find a way to add many certain types of items to a certain player String <Name> and retrieve the data instead of it being arraylist where you can't determine who its from
     
  7. Offline

    Tecno_Wizard

    @xepisolonxx, ah. Just in theory. That makes a lot more sense.
    I believe this can be done very efficiently with a stream, but at Bukkit we usually avoid Java 8 (which I hate sometimes. Lambda and streams are awesome).
    If you need a lot of data mapped to one player, you will need either a weak reference player to ArrayList or a UUID to ArrayList hashmap. Names won't work (remember name changing). Actually so many people have refused to switch to UUID's that a lot of servers are now breaking because of it. Please don't contribute.
    Get the ArrayList mapped to the player and use any of the data in the ArrayList you need.
    Why an ArrayList? Maps do not support identical keys.
     
  8. Offline

    xepisolonxx

    @Tecno_Wizard Ya thanks for the input but, i want to be able to store many things for other players as well not just one how can i go about doing this and also i realized i havent switched it to uuid i plan on doing it once i finish this
     
  9. Offline

    Tecno_Wizard

  10. Offline

    xepisolonxx

    @Tecno_Wizard English isn't my first language and its hard to juggle like many languages so sorry for run ons and thanks
     
  11. Offline

    Tecno_Wizard

    @xepisolonxx, i figured. Don't worry about it too much, but if you could give it a good attempt it would make it easier for us to read. French? The grammer mistaks so far indicate it.
     
  12. Offline

    xepisolonxx

    @Tecno_Wizard spanish but im also fluent in french and im not going to right really proper due to internet but i been the states for a lot of years and also know korean japanese polish but not as fluently as the others. Also you shouldn't be indicating promblems with speech this is a site for code not grammar and you may hurt others.
     
    Last edited: Jul 1, 2015
  13. Offline

    Tecno_Wizard

    @xepisolonxx, it was an explanation as to how I came to the conclusion, not an insult.
     
Thread Status:
Not open for further replies.

Share This Page