Solved Custom Enchants

Discussion in 'Plugin Development' started by Albkad, Feb 20, 2017.

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

    Albkad

    Hey, I am coding a CustomEnchants plugin and I am trying to make a HealthBoost Enchant. But I cant seem to figure it out. I have tried this
    Code:
    package me.albkad.netherenchants;
    
    import java.util.List;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class PlayerHealthEvent implements Listener {
        Main plugin;
       
       
       
        public PlayerHealthEvent(Main instance) {
            plugin = instance;
        }
           
       
        public void onChestPlate(PlayerMoveEvent e) {
            Player p = (Player) e.getPlayer();
           
            if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore().contains(ChatColor.GOLD + "HealthBoost")) {
                p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000000, 1));
               
            } else if (p.getPlayer().hasPotionEffect(PotionEffectType.HEALTH_BOOST) && p.getPlayer().getActivePotionEffects().contains("~")) {
                p.removePotionEffect(PotionEffectType.HEALTH_BOOST);
           
            }
           
           
           
        }
        public void onChestPlate2(PlayerJoinEvent e) {
            Player p = (Player) e.getPlayer();
           
            if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore().contains(ChatColor.GOLD + "HealthBoost")) {
                p.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000, 1));
            }
           
           
           
        }
        public void onChestPlate3(PlayerItemHeldEvent e) {
            Player p = (Player) e.getPlayer();
           
            if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore().contains(ChatColor.GOLD + "HealthBoost")) {
                p.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000, 1));
            }
           
           
           
        }
    
    }
    
    But it doesnt work. And It doesnt Give Me Any Errors Either.
    Help Is Really Appriciated!
     
  2. Online

    timtower Moderator Moderator

    @Albkad You are missing the @Eventhandler's
     
  3. Offline

    Albkad

    @timtower oh.......... Thx. But how do i remove the potion effects afterwards? do u know?

    I get this Error Code In The Console: https://gyazo.com/cd05f1c88532a048f8751885e1f017bd

    This Is What I Changed My Code Into
    Code:
    package me.albkad.netherenchants;
    
    import java.util.List;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class PlayerHealthEvent implements Listener {
        Main plugin;
       
       
        public PlayerHealthEvent(Main instance) {
            plugin = instance;
        }
           
        @EventHandler
        public void onChestPlate(PlayerMoveEvent e) {
            Player p = (Player) e.getPlayer();
           
            if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore().contains(ChatColor.GOLD + "HealthBoost")) {
                p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000000, 1));
                return;
            } else {
                p.getPlayer().removePotionEffect(PotionEffectType.HEALTH_BOOST);
                return;
           
            }
           
           
           
        }
        @EventHandler
        public void onChestPlate2(PlayerJoinEvent e) {
            Player p = (Player) e.getPlayer();
           
            if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore().contains(ChatColor.GOLD + "HealthBoost")) {
                p.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000, 1));
            }
           
           
           
        }
        @EventHandler
        public void onChestPlate3(PlayerItemHeldEvent e) {
            Player p = (Player) e.getPlayer();
           
            if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore().contains(ChatColor.GOLD + "HealthBoost")) {
                p.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000, 1));
            }
           
           
           
        }
    
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 20, 2017
  4. Online

    timtower Moderator Moderator

    @Albkad Do a null check on the chestplate, check if there is item meta, check if there is a lore
     
  5. Offline

    Albkad

    Code:
    package me.albkad.netherenchants;
    
    import java.util.List;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class PlayerHealthEvent implements Listener {
        Main plugin;
       
       
        public PlayerHealthEvent(Main instance) {
            plugin = instance;
        }
           
        @EventHandler
        public void onChestPlate(PlayerMoveEvent e) {
            Player p = (Player) e.getPlayer();
           
            if (p.getPlayer().getInventory().getChestplate().getItemMeta() == null); {
                p.getPlayer().removePotionEffect(PotionEffectType.HEALTH_BOOST);
            }
            if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore() == null); {
                p.getPlayer().removePotionEffect(PotionEffectType.HEALTH_BOOST);
            }
            if (p.getPlayer().getInventory().getChestplate() == null); {
                p.getPlayer().removePotionEffect(PotionEffectType.HEALTH_BOOST);
            }
               
            if (p.getPlayer().getInventory().getChestplate().hasItemMeta() && p.getPlayer().getInventory().getChestplate().getItemMeta().hasLore()) {
                if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore().contains(ChatColor.GOLD + "HealthBoost")) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000000, 1));
                    return;   
            }
           
           
        }
           
           
           
        }
    }
    
    This isnt working :( Could u explain more?

    @timtower
     
    Last edited by a moderator: Feb 20, 2017
  6. Offline

    ipodtouch0218

    Check if the chestplate is null BEFORE checking the ItemMeta and lore.
     
  7. Offline

    Zombie_Striker

    This is the wrong chatcolor. Use the org.bukkit import.

    Remove the cast. There is no reason to cast a player to a player.


    You are doing this all in the wrong order. Check if the item is null first, then if the meta is null, then if the lore is null. Also, use the OR operator {||} and return after removing the potion.

    Because of the above, if you return, then you do not need this if statement.
     
  8. Offline

    Albkad

    @Zombie_Striker I have done Everything u said and it works but it just gives me a error code in console when i move after i have taken of the chestplate.
    Code:
    package me.albkad.netherenchants;
    
    import java.util.List;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.ChatColor;
    
    
    public class PlayerHealthEvent implements Listener {
        Main plugin;
       
       
        public PlayerHealthEvent(Main instance) {
            plugin = instance;
        }
           
        @EventHandler
        public void onChestPlate(PlayerMoveEvent e) {
            Player p = e.getPlayer();
           
           
            if (p.getPlayer().getInventory().getChestplate() == null || p.getPlayer().getInventory().getChestplate().getItemMeta() == null || p.getPlayer().getInventory().getChestplate().getItemMeta().getLore() == null); {
                p.getPlayer().removePotionEffect(PotionEffectType.HEALTH_BOOST);
            }
           
            if (p.getPlayer().getInventory().getChestplate().getItemMeta().getLore().contains(ChatColor.GOLD + "HealthBoost")) {
                p.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000000, 1));
            }
           
           
               
               
           
           
           
        }
    }
    
    Console: https://gyazo.com/fbb13dc27355f7c84c0befe129e68dfb
     
  9. Offline

    Zombie_Striker

    @Albkad
    You're not returning. After removing the potion effect, return.
     
  10. Offline

    Albkad

    @Zombie_Striker If i return it tells me the code under is unreachable. How do i fix this?

    @Zombie_Striker Nvm Thx I Found the return Problem. it was the semicolon () == null); {

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  11. Offline

    Zombie_Striker

    @Albkad
    Where are you putting the return? If you put it directly after you remove the potion, then the error should go away.
     
  12. Offline

    PQE

    Why not use InventoryMoveItemEvent and check if the player has moved a chestplate to an armor slot? Calling every time a player moves is resource-extensive and you don't seem to use the getTo() or getFrom() methods.
     
Thread Status:
Not open for further replies.

Share This Page