Custom Enchants

Discussion in 'Plugin Development' started by CheeseNips, Dec 24, 2015.

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

    CheeseNips

    How would i add give a potion effect if the item has the designated lore? this may be very simple sorry. Show some code please
     
    Last edited: Dec 24, 2015
  2. Offline

    XsergeiX

    You mean something like "if player wears armor that contains in first lore line word *regenetation* then set player regeretaion"?

    Code:java
    1.  
    2. public Map<String, Integer> tasks = new HashMap<String, Integer>();
    3.  
    4. @EventHandler
    5. public void onLogin(final PlayerJoinEvent e){
    6. // instead of MainClass.main use your own path to your main Plugin Class
    7. int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(MainClass.main, new Runnable() {
    8.  
    9. @Override
    10. public void run() {
    11. //code that you write here will execute every 20 ticks(1 sec)
    12. PlayerInventory inv = e.getPlayer().getInventory();
    13. if(inv.getChestplate()!=null && isHealingChestPlate(inv.getChestplate())){
    14. Player p = e.getPlayer();
    15. p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 2, 1));
    16. // OR
    17. p.setHealth(Math.min(p.getMaxHealth(), p.getHealth()+1));
    18. }
    19. }
    20.  
    21. private boolean isHealingChestPlate(ItemStack item){
    22. if(!item.hasItemMeta()){
    23. return false;
    24. }
    25. List<String> lore = item.getItemMeta().getLore();
    26. return (lore!=null && lore.size()>0 && lore.get(0).equals("Effect: Healing"));
    27. }
    28. }, 20L, 20L);
    29. tasks.put(e.getPlayer().getDisplayName(), task);
    30. }
    31.  
    32. @EventHandler
    33. public void onLogin(PlayerQuitEvent e){
    34. Bukkit.getScheduler().cancelTask(tasks.get(e.getPlayer().getDisplayName()));
    35. tasks.remove(e.getPlayer().getDisplayName());
    36. }
    37.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 24, 2015
  3. Offline

    CheeseNips

    @XsergeiX I get a Error at list<string>

    Code
    Code:
    package xyz.cheesenips;
    
    import java.awt.List;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Healing_1 implements Listener {
      
        public Map<String, Integer> tasks = new HashMap<String, Integer>();
       
           @EventHandler
           public void onLogin(final PlayerJoinEvent e){
             // instead of MainClass.main use your own path to your main Plugin Class
             int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(Core.main, new Runnable() {
       
               @Override
               public void run() {
                 //code that you write here will execute every 20 ticks(1 sec)
                 PlayerInventory inv = e.getPlayer().getInventory();
                 if(inv.getChestplate()!=null && isHealingChestPlate(inv.getChestplate())){
                   Player p = e.getPlayer();
                   p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 2, 1));
                   // OR
                   //p.setHealth(Math.min(p.getMaxHealth(), p.getHealth()+1));
                 }
               }
       
               private boolean isHealingChestPlate(ItemStack item){
                 if(!item.hasItemMeta()){
                   return false;
                 }
                 List<String> lore = item.getItemMeta().getLore();
                 return (lore!=null && lore.size()>0 && lore.get(0).equals(ChatColor.BLUE + "Healing I"));
               }
             }, 20L, 20L);
             tasks.put(e.getPlayer().getDisplayName(), task);
           }
       
           @EventHandler
           public void onLogin(PlayerQuitEvent e){
             Bukkit.getScheduler().cancelTask(tasks.get(e.getPlayer().getDisplayName()));
             tasks.remove(e.getPlayer().getDisplayName());
           }
    
    }
     
  4. Offline

    XsergeiX

    @CheeseNips on which line you get Exception and what Exception

    oh wait,
    import java.awt.List;

    that's your mistake
    don't use awt classes, instead you need this:
    import java.util.List;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 24, 2015
  5. Offline

    mcdorli

    Please don't spoonfeed code in the future. OK?
     
  6. Offline

    XsergeiX

    @mcdorli Yea, sorry for that, i'm new at helping :D
     
  7. Offline

    CheeseNips

    @XsergeiX Okay that's fixed now i have a error at [​IMG]
    Code:
    Code:
    package xyz.cheesenips;
    
    import java.util.List;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Healing_1 implements Listener {
       
        public Map<String, Integer> tasks = new HashMap<String, Integer>();
         
          @EventHandler
          public void onLogin(final PlayerJoinEvent e){
            // instead of MainClass.main use your own path to your main Plugin Class
            int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(xyz.cheesenips.Core, new Runnable() {
         
              @Override
              public void run() {
                //code that you write here will execute every 20 ticks(1 sec)
                PlayerInventory inv = e.getPlayer().getInventory();
                if(inv.getChestplate()!=null && isHealingChestPlate(inv.getChestplate())){
                  Player p = e.getPlayer();
                  p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 2, 1));
                  // OR
                  //p.setHealth(Math.min(p.getMaxHealth(), p.getHealth()+1));
                }
              }
         
              private boolean isHealingChestPlate(ItemStack item){
                if(!item.hasItemMeta()){
                  return false;
                }
                List<String> lore = item.getItemMeta().getLore();
                return (lore!=null && lore.size()>0 && lore.get(0).equals(ChatColor.BLUE + "Healing I"));
              }
            }, 20L, 20L);
            tasks.put(e.getPlayer().getDisplayName(), task);
          }
         
          @EventHandler
          public void onLogin(PlayerQuitEvent e){
            Bukkit.getScheduler().cancelTask(tasks.get(e.getPlayer().getDisplayName()));
            tasks.remove(e.getPlayer().getDisplayName());
          }
    
    }
    
    
    
    
     
  8. Offline

    mcdorli

    You need to put there an instance of the main class, not a path to it. This is why we won't spoonfeed. Do you understand the code?
     
  9. Offline

    CheeseNips

    @mcdorli My main class is named Core so it would be Core.main ?
     
  10. Offline

    mcdorli

    If there is a static main variable inside your main class. Yes. But please, don't use static, just use constructors, and pass an instance around.
     
  11. Offline

    CheeseNips

    @mcdorli here's my main class

    Code:
    package xyz.cheesenips;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
    
       
         public void onEnable() {
             Bukkit.getServer().getLogger().info("nCE Plugin Enabled!");
    }
    
    public void onDisable() {
             Bukkit.getServer().getLogger().info("nCE Plugin Disabled!");
    }
    }
    
     
  12. Offline

    mcdorli

    Here's my advice
    https://docs.oracle.com/javase/tutorial/
    and the package name should be more convenient
     
  13. Offline

    CheeseNips

    @mcdorli I fixed all the errors now this when i run it
    http://pastebin.com/Q1PkCrxC

    @mcdorli Edit i got the plugin to work but when i have the lore it doesnt work??

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 24, 2015
  14. Offline

    Tirco

    Then it's time to do some debugging! Add in some console logging to the places that the code might stop working.
    Does your separate classes load, does your scheduler initialize? Once you find out where the code stops working, then it's easier to find yourself a fix ^^
     
Thread Status:
Not open for further replies.

Share This Page