Solved Help with Custom Enchants plugin.

Discussion in 'Spigot Plugin Help' started by EndureBlackouT, Aug 11, 2016.

  1. So, I am trying to make a custom enchants plugin and the only thing that isnt working right now is getting whether the sword has the lore or not and if it does then give the damaged player poison... here is the code:
    Code (Text):
    package me.endureblackout.customenchants;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class CustomEnchantsMain extends JavaPlugin implements Listener {
     
        public void onEnable() {
            getLogger().info("Custom Enchants enabled!");
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
        public void onDisable() {
            getLogger().info("Custom Enchants disabled!");
            saveConfig();
        }
     
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent e) {
            Player p = (Player) e.getPlayer();
         
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if(p.hasPermission("enchants.admin")) {
                    if(e.getClickedBlock().getState() instanceof Sign) {
                        if(p.getItemInHand().getType() == Material.GOLD_SWORD || p.getItemInHand().getType() ==  Material.WOOD_SWORD || p.getItemInHand().getType() == Material.IRON_SWORD || p.getItemInHand().getType() == Material.DIAMOND_SWORD) {
                            Sign s = (Sign) e.getClickedBlock().getState();
                            if(s.getLine(0).equalsIgnoreCase("[Enchant]") && s.getLine(1).equalsIgnoreCase("Poison") && s.getLine(2).equalsIgnoreCase("I")) {
                                List<String> lore = new ArrayList<String>();
                                lore.add(ChatColor.GRAY + "Poison I");
                                ItemStack sword = p.getItemInHand();
                                ItemMeta swordMeta = sword.getItemMeta();
                             
                                swordMeta.setLore(lore);
                                sword.setItemMeta(swordMeta);
                             
                                p.sendMessage(ChatColor.GREEN + "[Custom Enchants] Successfully enchanted with poison I!");
                            }
                             
                        }
                    }
                }
            }
        }
      //PROBLEM IS HERE
        @EventHandler
        public void onPlayerHitPlayer(EntityDamageByEntityEvent e) {
            if(e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
                Player hit = (Player) e.getEntity();
                Player hitter = (Player) e.getDamager();
                hitter.sendMessage("working");
             
                    for(String s : hitter.getItemInHand().getItemMeta().getLore()) {
                        if(s.startsWith(ChatColor.RESET + "Poison I")) {
                            double chance = getConfig().getDouble("Poison1.Chance");
                            hitter.sendMessage("working2");
                         
                            if(Math.random() <= chance) {
                                hit.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 100, 1));
                            }
                        }
                    }
            }
        }

    }
     
    the problem is here:
    Code (Text):
        @EventHandler
        public void onPlayerHitPlayer(EntityDamageByEntityEvent e) {
            if(e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
                Player hit = (Player) e.getEntity();
                Player hitter = (Player) e.getDamager();
                hitter.sendMessage("working");
               
                    for(String s : hitter.getItemInHand().getItemMeta().getLore()) {
                        if(s.startsWith(ChatColor.RESET + "Poison I")) {
                            double chance = getConfig().getDouble("Poison1.Chance");
                            hitter.sendMessage("working2");
                           
                            if(Math.random() <= chance) {
                                hit.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 100, 1));
                            }
                        }
                    }
            }
        }
     
    #1 EndureBlackouT, Aug 11, 2016
    Last edited: Aug 11, 2016
  2. insert Bukkit.getLogger().info("the item = " + s); to see what 's' is (or hitter.sendMesssage whatever)

    if you have color code in front of the 's', you need to strip it out before do startsWith()

    if I remember correctly, startsWith() checks wether the string starts with the given sequence

    so if there is color code in front of 's', it will not return true.

    to strip color, use ChatColor.stripColor(String) method though
     
  3. It doesnt even get passed the for(String s : hitter.getItemInHand().getItemMeta().getLore())... here is the error in console:
    Code (Text):
    [22:54:28 ERROR]: Could not pass event EntityDamageByEntityEvent to CustomEnchants v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEvent(CraftEventFactory.java:86) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:552) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:465) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:584) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.EntityLiving.d(EntityLiving.java:1101) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.EntityHuman.d(EntityHuman.java:859) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.java:742) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.EntityHuman.damageEntity(EntityHuman.java:800) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.EntityPlayer.damageEntity(EntityPlayer.java:496) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.EntityHuman.attack(EntityHuman.java:1001) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.EntityPlayer.attack(EntityPlayer.java:1063) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1355) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.a(SourceFile:52) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.a(SourceFile:11) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_60]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.NullPointerException
            at me.endureblackout.customenchants.CustomEnchantsMain.onPlayerHitPlayer(CustomEnchantsMain.java:70) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor2163.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-fdc1440-53fac9f]
            ... 26 more
     
  4. I remember getLore() can return null if there is no lore

    getItemMeta() can be null too

    you better check those things are null or not and return if they are null

    for example) if(someitme.getItemMeta() == null) return;
    if(someitem.getItemMeta().getLore() == null) return;
     
  5. Already fixed the issue thanks though!!