Solved Help with hashmaps for my kits!

Discussion in 'Plugin Development' started by Tommy Raids, Dec 9, 2013.

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

    Tommy Raids

    Code:
    if (cmd.getName().equalsIgnoreCase("Reaper")) {
                    HashMap<String, String> Reaper = new HashMap<String, String>();
                    pi.clear();
                    ItemStack reaper = new ItemStack(Material.WOOD_HOE);
                    ItemMeta Rreaper = reaper.getItemMeta();
                    Rreaper.setDisplayName(ChatColor.DARK_GRAY + "Reaper");
                    reaper.setItemMeta(Rreaper);
                    ItemStack reapersword = new ItemStack(Material.DIAMOND_SWORD, 1);
                    reapersword.addEnchantment(Enchantment.DAMAGE_ALL, 3);
                    pi.addItem(reapersword);
                    pi.addItem(reaper);
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
                    pi.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
                    pi.setHelmet(new ItemStack(Material.IRON_HELMET));
                    pi.setBoots(new ItemStack(Material.IRON_BOOTS));
                    p.removePotionEffect(PotionEffectType.NIGHT_VISION);
                    p.removePotionEffect(PotionEffectType.SPEED);
                    p.removePotionEffect(PotionEffectType.JUMP);
                    p.removePotionEffect(PotionEffectType.FAST_DIGGING);
                    p.removePotionEffect(PotionEffectType.INVISIBILITY);
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10000000, 1));
                    p.sendMessage(ChatColor.DARK_PURPLE + "[Chaotic"
                              + ChatColor.GOLD + "Kits]"
                            + ChatColor.GRAY + "Granted the Reaper Kit!");
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                        }
                    }, 100);
                    return true;
                }
    Ok guys, I have a kit plugin. I want to make it so only people in a certain hashmap have the abilities to the kit. My example I need help with is Reaper. I have the events and everything down. So basically if you have a wooden hoe and hit someone with it it inflicts the wither potion effect on them for 10 seconds. But my problem is I don't know how to make it so that people with a different kit such as PvP can't craft a woodenhoe and then give people the reaper effect. I want to make it so only people in the reaper hashmap can do this. Please help. Here is some of the code. And my onEnable() stuff is above it. So I did not copy and paste it. Please help as much as you can! :D Thanks Code:
     
  2. Offline

    Wolfey

    Well if you're wondering how to use Hashmaps, it's with this: hasmap.put("something", "othersomething");, but if you're trying this with kits, I suggest arraylists.
     
  3. Offline

    Tommy Raids

    Wolfey That's what I thought, I tried that but it didn't prevent it. So could you try to help me with that?
     
  4. Offline

    Wolfey

    Code:java
    1.  
    2. ArrayList<String> reaper = new ArrayList<String>();
    3.  
    4. // Add player to arraylist
    5. // Remember you can add multiple of the same variables to an arraylist and it would not override it
    6. reaper.add(player.getName());
    7.  
    8. // Remove player from arraylist
    9. reaper.remove(player.getName());
    10.  
    11. // check if reaper contains any players
    12. if(reaper.contains(p.getName()) { }
    13.  

    With this, when a player performs the /reaper command, check if reaper contains the player, if they do, send the player a message saying "You already have this kit" or something like that. (one kit per life) if not, then add them to the arraylist, remember, you're gonna have to remove them from the arraylist when they die.

    when the player is about to use a reaper event, check if the reaper arraylist contains the player, if it does, then carry on with the event, otherwise the player does not have the reaper kit, and cannot perform the action, so just return it.
     
  5. Offline

    Compressions

    Tommy Raids If you are storing the player's name as the key and the kit name as the value, you can iterate through the HashMap's key-value pairs(entries).
    Code:
        public boolean hasKit(Player player, String kit) {
            Iterator<Map.Entry<String, String>> it = hashmap.entrySet().iterator();
            while(it.hasNext()) {
                String playerName = it.next().getKey();
                String kitName = it.next().getValue();
                if(player.getName().equals(playerName)) {
                    if(kit.equals(kitName)) {
                        return true;
                    }
                }
                it.remove();
            }
            return false;
        }
     
  6. Offline

    Tommy Raids

    Code:
    if (cmd.getName().equalsIgnoreCase("Reaper")) {
                    if(reaper.contains(p.getName())) {
                        p.sendMessage(ChatColor.GREEN + "You can only have 1 kit per life!");
                    }else{
                        reaper.add(p.getName());
                    }
                    pi.clear();
                    ItemStack Reaper = new ItemStack(Material.WOOD_HOE);
                    ItemMeta Rreaper = Reaper.getItemMeta();
                    Rreaper.setDisplayName(ChatColor.DARK_GRAY + "Reaper");
                    Reaper.setItemMeta(Rreaper);
                    ItemStack reapersword = new ItemStack(Material.DIAMOND_SWORD, 1);
                    reapersword.addEnchantment(Enchantment.DAMAGE_ALL, 3);
                    pi.addItem(reapersword);
                    pi.addItem(Reaper);
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
                    pi.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
                    pi.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
                    pi.setHelmet(new ItemStack(Material.IRON_HELMET));
                    pi.setBoots(new ItemStack(Material.IRON_BOOTS));
                    p.removePotionEffect(PotionEffectType.NIGHT_VISION);
                    p.removePotionEffect(PotionEffectType.SPEED);
                    p.removePotionEffect(PotionEffectType.JUMP);
                    p.removePotionEffect(PotionEffectType.FAST_DIGGING);
                    p.removePotionEffect(PotionEffectType.INVISIBILITY);
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10000000, 1));
                    p.sendMessage(ChatColor.DARK_PURPLE + "[Chaotic"
                              + ChatColor.GOLD + "Kits]"
                            + ChatColor.GRAY + "Granted the Reaper Kit!");
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                        }
                    }, 100);
                    return true;
                }
    Wolfey This is what I got. It still doesn't work. People with wooden hoes and are not in the arraylist can still cause wither. Here is the code. I have the remove array list event at bottom of code (not on here). If you would like to see it please tell me. Here is the code:
     
  7. Offline

    MrDynamo

    @Tommy Raids

    Listen for a player interact event (or something along the lines), check if the player is in arraylist, if they are then apply the wither effect to the damaged player, if not, don't
     
  8. Offline

    Wolfey

    Tommy Raids
    Here's a little modified version of your reaper command that's more organizer and better. You don't have to use it, but just thought I'd do it anyways.

    However you mind showing me your wither event code, there has to be something wrong there.
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("Reaper")) {
    3. if(reaper.contains(p.getName())) {
    4. p.sendMessage(ChatColor.GREEN + "You can only have 1 kit per life!");
    5. return true;
    6. }
    7. reaper.add(p.getName());
    8. PlayerInventory pi = p.getInventory();
    9. pi.clear();
    10. ItemStack Reaper = new ItemStack(Material.WOOD_HOE);
    11. ItemMeta Rreaper = Reaper.getItemMeta();
    12. Rreaper.setDisplayName(ChatColor.DARK_GRAY + "Reaper");
    13. Reaper.setItemMeta(Rreaper);
    14. ItemStack reapersword = new ItemStack(Material.DIAMOND_SWORD, 1);
    15. reapersword.addEnchantment(Enchantment.DAMAGE_ALL, 3);
    16. pi.addItem(reapersword);
    17. pi.addItem(Reaper);
    18. for(int i = 0; i < 26; i++) {
    19. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP, 1));
    20. }
    21. pi.setHelmet(new ItemStack(Material.IRON_HELMET));
    22. pi.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    23. pi.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    24. pi.setBoots(new ItemStack(Material.IRON_BOOTS));
    25. p.removePotionEffect(PotionEffectType.NIGHT_VISION);
    26. p.removePotionEffect(PotionEffectType.SPEED);
    27. p.removePotionEffect(PotionEffectType.JUMP);
    28. p.removePotionEffect(PotionEffectType.FAST_DIGGING);
    29. p.removePotionEffect(PotionEffectType.INVISIBILITY);
    30. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10000000, 1));
    31. p.sendMessage(ChatColor.DARK_PURPLE + "[Chaotic" + ChatColor.GOLD + "Kits]" + ChatColor.GRAY + "Granted the Reaper Kit!");
    32. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("pluginName"), new Runnable() {
    33. public void run() {
    34.  
    35. }
    36. }, 100);
    37. return true;
    38.  
     
  9. Offline

    Tommy Raids

    Code:
    @EventHandler
        public void reaperListener(EntityDamageByEntityEvent e){
            if(e.getEntity() instanceof Player && e.getDamager() instanceof Player){
                Player damager = (Player)e.getDamager();
                Player victim = (Player)e.getEntity();
                if(damager.getInventory().getItemInHand().getType() == Material.WOOD_HOE){
                    victim.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 100, 0));
                }
            }
        }
    
    Wolfey
     
  10. Offline

    Wolfey

    Tommy Raids, you are not checking at all if the player is in the reaper arraylist. Heres an updated code:
    Code:java
    1.  
    2. @EventHandler
    3. public void reaperListener(EntityDamageByEntityEvent e){
    4. if(e.getEntity() instanceof Player && e.getDamager() instanceof Player){
    5. Player damager = (Player)e.getDamager();
    6. Player victim = (Player)e.getEntity();
    7. if(reaper.contains(damager.getName())) {
    8. if(damager.getInventory().getItemInHand().getType() == Material.WOOD_HOE) {
    9. victim.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 100, 0));
    10. }
    11. }
    12. }
    13. }
    14.  
     
  11. Offline

    Tommy Raids

    Wolfey Wow, I forgot to do that! :D Thanks lol. Now its working. Thanks for putting up with my ignorance at coding :p
     
    Wolfey likes this.
Thread Status:
Not open for further replies.

Share This Page