Potion effect with sets of armor

Discussion in 'Plugin Development' started by blizzblang, Dec 19, 2012.

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

    blizzblang

    Currently i am working on plugin that adds a special set of armor,just diamond armor with a custom name and enchantments, but i need to add potion effects when it is all equipped.
    I just need to know what event(s) to use to detect if a player has all 4 pieces on or not.
    I try my best to find a simple solution by looking at threads, but this is a last resort decision.
    I appreciate all help i receive.
    I can post my code if needed.
    Thanks :)
     
  2. Offline

    bobacadodl

    You should probably perform a check on InventoryClickEvent
     
  3. Offline

    blizzblang

    I've tried to create a listener with no luck, could you give me a proper example or a link to one?
     
  4. Offline

    fireblast709

    Code snippets... code snippets...
    Code:java
    1. @EventHandler
    2. public void onIClick(InventoryClickEvent event)
    3. {
    4. if(event.getSlotType() == InventoryType.SlotType.ARMOR)
    5. {
    6. ItemStack[] armor = event.getPlayer().getInventory().getArmorContents();
    7. int armorcount = 0, newcount;
    8. for(ItemStack piece : armor)
    9. {
    10. if(piece != null)
    11. {
    12. try
    13. {
    14. String[] mat_type = piece.getType().name().split("_");
    15. if(!(mat_type[1].equals("BOOTS") || mat_type[1].equals("LEGGINGS") || mat_type[1].equals("CHESTPLATE") || mat_type[1].equals("HELMET")))
    16. {
    17. continue;
    18. }
    19. if(mat_type[0].equals("DIAMOND")) armorcount += 341;
    20. if(mat_type[0].equals("CHAINMAIL")) armorcount += 85;
    21. if(mat_type[0].equals("GOLD")) armorcount += 21;
    22. if(mat_type[0].equals("IRON")) armorcount += 5;
    23. if(mat_type[0].equals("LEATHER")) armorcount += 1;
    24. }
    25. {
    26. continue;
    27. }
    28. }
    29. }
    30.  
    31. if(armorcount == 4)
    32. {
    33. // Full leather armor
    34. }
    35. else if(armorcount == 20)
    36. {
    37. // Full iron armor
    38. }
    39. else if(armorcount == 84)
    40. {
    41. // Full gold armor
    42. }
    43. else if(armorcount == 340)
    44. {
    45. // Full chainmail armor
    46. }
    47. else if(armorcount == 1364)
    48. {
    49. // Full diamond armor
    50. }
    51.  
    52. }
    53. }
     
  5. Offline

    blizzblang

    I will try this out, thanks :)
     
  6. Offline

    SoThatsIt

    you could set up a repeating synced thread that runs every second then check if they have full armour on then make the potion effect last for 1 or 2 seconds if they do.
     
  7. Offline

    fireblast709

    The solution I posted uses less resources :3
     
  8. Offline

    blizzblang

    Didn't work, or i'm not doing it right...
     
  9. Offline

    Rprrr

    You're probably doing something wrong.. Fireblast is always right. D: :p
     
  10. Yes, 99.99% of the times. Fireblast is amazing at providing support :)
     
  11. Offline

    blizzblang

    I get error
    Code:
    package blizzblang;
     
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
     
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.Server;
    import org.bukkit.block.Block;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentWrapper;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.InventoryView;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public final class main extends JavaPlugin implements Listener {
       
        Logger log;
        Server server;
       
        PluginDescriptionFile pdf;
          FileConfiguration config;
         
            public int tid = 0;
            public static int running = 0;
            public static long interval = 10;
          @Override
            public void onEnable(){
              addRecipe(); 
              PluginManager manager = getServer().getPluginManager();
                manager.registerEvents(this, this);
             
            }
       
            @Override
            public void onDisable() {
           
            }
           
       
            public void addRecipe() {
                int effectId = 0;  //use the enchantment code you want here
                int enchantmentLevel = 4;
                ItemStack nano_chest = new ItemStack(Material.DIAMOND_CHESTPLATE,1); 
                ItemMeta im = nano_chest.getItemMeta();
                im.setDisplayName("§rNano suit armor");
                ArrayList<String> lore = new ArrayList<String>();
                lore.add("§rNano armor");
                im.setLore(lore);
                nano_chest.setItemMeta(im);
     
               
                Enchantment myEnchantment = new EnchantmentWrapper(effectId);  //new enchantment of effect id
                nano_chest.addEnchantment(myEnchantment, enchantmentLevel);  //enchant the item
               
                ShapedRecipe recipe = new ShapedRecipe(nano_chest);
                recipe.shape("CBC", "BAB", "BBB");
                recipe.setIngredient('C', Material.DIAMOND_BLOCK);
                recipe.setIngredient('B', Material.OBSIDIAN);
                recipe.setIngredient('A', Material.BEACON);
                this.getServer().addRecipe(recipe);
            }
           
            @EventHandler
            public void onIClick(InventoryClickEvent event)
            {
                if(event.getSlotType() == InventoryType.SlotType.ARMOR)
                {
                    ItemStack[] armor = ((Player) event).getPlayer().getInventory().getArmorContents();
                    int armorcount = 0, newcount;
                    for(ItemStack piece : armor)
                    {
                        if(piece != null)
                        {
                            try
                            {
                                String[] mat_type = piece.getType().name().split("_");
                                if(!(mat_type[1].equals("BOOTS") || mat_type[1].equals("LEGGINGS") || mat_type[1].equals("CHESTPLATE") || mat_type[1].equals("HELMET")))
                                {
                                    continue;
                                }
                                if(mat_type[0].equals("DIAMOND")) armorcount += 341;
                                if(mat_type[0].equals("CHAINMAIL")) armorcount += 85;
                                if(mat_type[0].equals("GOLD")) armorcount += 21;
                                if(mat_type[0].equals("IRON")) armorcount += 5;
                                if(mat_type[0].equals("LEATHER")) armorcount += 1;
                            }
                            catch(ArrayIndexOutOfBoundsException ex)
                            {
                                continue;
                            }
                        }
                    }
           
                    if(armorcount == 4)
                    {
                        // Full leather armor
                    }
                    else if(armorcount == 20)
                    {
                        // Full iron armor
                    }
                    else if(armorcount == 84)
                    {
                        // Full gold armor
                    }
                    else if(armorcount == 340)
                    {
                        // Full chainmail armor
                    }
                    else if(armorcount == 1364)
                    {
                        getLogger().info("Diamond");
                    }
           
                }
            }
       
         
    }
    
     
  12. Offline

    EnvisionRed

    Please learn to read your own stack traces
     
  13. Offline

    ShakenU

    blizz- if your so noobish to find out what "your own stack traces mean" (i know this b/c i skype with him), it means Troublesoot what is causing your errors. Thanks
     
  14. Offline

    blizzblang

    Code:
    @EventHandler
    public void onIClick(InventoryClickEvent event)
    {
        if(event.getSlotType() == InventoryType.SlotType.ARMOR)
        {
            ItemStack[] armor = event.getPlayer().getInventory().getArmorContents();
            int armorcount = 0, newcount;
            for(ItemStack piece : armor)
            {
                if(piece != null)
                {
                    try
                    {
                        String[] mat_type = piece.getType().name().split("_");
                        if(!(mat_type[1].equals("BOOTS") || mat_type[1].equals("LEGGINGS") || mat_type[1].equals("CHESTPLATE") || mat_type[1].equals("HELMET")))
                        {
                            continue;
                        }
                        if(mat_type[0].equals("DIAMOND")) armorcount += 341;
                        if(mat_type[0].equals("CHAINMAIL")) armorcount += 85;
                        if(mat_type[0].equals("GOLD")) armorcount += 21;
                        if(mat_type[0].equals("IRON")) armorcount += 5;
                        if(mat_type[0].equals("LEATHER")) armorcount += 1;
                    }
                    catch(ArrayIndexOutOfBoundsException ex)
                    {
                        continue;
                    }
                }
            }
     
            if(armorcount == 4)
            {
                // Full leather armor
            }
            else if(armorcount == 20)
            {
                // Full iron armor
            }
            else if(armorcount == 84)
            {
                // Full gold armor
            }
            else if(armorcount == 340)
            {
                // Full chainmail armor
            }
            else if(armorcount == 1364)
            {
                // Full diamond armor
            }
     
        }
    }
    Eclipse says "The method getPlayer() is undefined for the type InventoryClickEvent" at "ItemStack[] armor = event.getPlayer().getInventory().getArmorContents();"
     
  15. Offline

    fireblast709

    That is why they have javadocs my friend :3. Yes I was wrong this time (sorry Rprrr, TfT_02 ;D). Use .getWhoClicked(), I was too lazy to open Netbeans :p
     
  16. Haha, you do tend to be right most of the times. :) And if you're not, you're still close enough.
     
  17. Offline

    blizzblang

    Thanks i appreciate all the help. But how do i make it so that it applies a potion effect that lasts as long as the plyer wears the armor. Should i use a different event?
     
  18. Offline

    fireblast709

    You should use a scheduled task that you cancel when he takes a part of it off (and removes the effect)
     
  19. Offline

    blizzblang

    What speed should i set it too i.e. once per min or every 5 sec.?
     
  20. Offline

    fireblast709

    depends if you do one task per player (which you can schedule for longer times) or one scheduler (which should have a short delay between runs)
     
  21. Offline

    blizzblang

    Which is less resource heavy?
     
  22. Offline

    fireblast709

    Depends on the amount of tasks run. Using one task has a constant resource usage, while using a task per player can have none to a high amount of resource usage
     
  23. Offline

    blizzblang

    The task will be checking players for a certain type of armor,them apply a potion effect,or remove it.
    what is the even for armor being put on?
     
  24. Offline

    fireblast709

    InventoryClickEvent. Check if the InventoryType.SlotType == InventoryType.SlotType.ARMOR
     
  25. Offline

    blizzblang

    Ive modifieid the code slightly to compare the lore of the item to somthing, if they match you get a potion effect, but it isnt working. So how would i compare the lore?
     
  26. Offline

    fireblast709

    usually you get the ItemMeta, get the Lore list, get the first line (assuming you use that for your lore) and .equals() that
     
  27. Offline

    blizzblang

    I'll try this thanks!
     
  28. Offline

    TheTinySpider

  29. Offline

    fireblast709

    Good point. You can always check the armor in the scheduled task, though that might leave you with the problem that you have the potioneffect for a second or two without the full set
     
  30. Offline

    blizzblang

    Is there an event when armor breaks? i could just use that, right?
     
Thread Status:
Not open for further replies.

Share This Page