Solved McMMOPlayerXpGainEvent issue

Discussion in 'Spigot Plugin Development' started by RandomHashTags, Jul 28, 2018.

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


    I am getting each individual string of the item's lore.
    The wooden sword only has "Training V" on it, with no ChatColor.
    I hit a summoned zombie once with the sword, and the Damage Events are executed.

    Any other CustomEnchant that is not executed from the McMMOPlayerXpGainEvent works without any issues.
    There are about 100 other enchants that work with EntityDamageByEntityEvent, EntityDamageEvent, EntityDeathEvent, and other events.

    The McMMOPlayerXpGainEvent event somehow messes with the fromRoman method.
    Some things to note:
    Code (Java):
    CustomEnchantsMCMMO extends CustomEnchantmentss
    McMMO version: 1.5.04-SNAPSHOT-b82
    Server Version: Spigot 1.8.8
    Any help with resolving this issue is very much appreciated.
    Code (Java):
    private final Hashtable<Character, Integer> ht = new Hashtable<Character, Integer>();
    When the plugin enables, these values are put into ht
    Code (Java):
    Code (Java):
    @EventHandler(priority = EventPriority.HIGHEST)
        private void mcmmoPlayerXpGainEvent(McMMOPlayerXpGainEvent event) {
            if(!event.isCancelled()) {
                Bukkit.broadcastMessage(event.getEventName() + ";" + event.getSkill().name());
                procPlayerArmor(event, event.getPlayer());
                procPlayerItem(event, event.getPlayer(), null);
    Code (Text):
    public void procPlayerItem(Event event, Player player, ItemStack is) {
            final ItemStack h = is == null ? player.getInventory().getItemInHand() : is;
            if(h != null && h.hasItemMeta() && h.getItemMeta().hasLore())
                for(String s : h.getItemMeta().getLore()) {
                    CustomEnchant e = CustomEnchant.valueOf(s);
                    if(e != null) {
                        Bukkit.broadcastMessage("procPlayerItem;string=" + s + ";enchant=" + e.getName());
                        procEnchant(event, e, getEnchantmentLevel(s), h, player);
    Code (Text):
    public int getEnchantmentLevel(String string) {
            string = ChatColor.stripColor(string.split(" ")[string.split(" ").length - 1].toLowerCase().replace("i", "1").replace("v", "2").replace("x", "3").replaceAll("\\p{L}", "").replace("1", "i").replace("2", "v").replace("3", "x").replaceAll("\\p{N}", "").replaceAll("\\p{P}", "").replaceAll("\\p{S}", "").replaceAll("\\p{M}", "").replaceAll("\\p{Z}", "").toUpperCase());
            return fromRoman(string);
    Code (Java):
         * This code is from "batman" at

        private int fromRoman(String num) {
            num = ChatColor.stripColor(num.toLowerCase());
            int intNum = 0, prev = 0;
            for(int i = num.length() - 1; i >= 0; i--) {
                final char character = num.charAt(i);
                Bukkit.broadcastMessage("fromRoman;character=" + character + ";ht contains " + character + "=" + (ht.keySet().contains(character)));
                int temp = ht.get(character);                         // Line 935
                if(temp < prev) intNum -= temp;
                else            intNum += temp;
                prev = temp;
            return intNum;
    Code (Text):
    [16:28:08] [Server thread/INFO]: RandomHashTags issued server command: /spawnmob zombie 1
    [16:28:09] [Server thread/INFO]: executeAttributes(Training);EntityDamageByEntityEvent
    [16:28:09] [Server thread/INFO]: procPlayerItem;string=Training V;enchant=Training
    [16:28:09] [Server thread/INFO]: fromRoman;character=v;ht contains v=true
    [16:28:09] [Server thread/INFO]: executeAttributes(Training);PlayerItemDamageEvent
    [16:28:09] [Server thread/INFO]: McMMOPlayerXpGainEvent;SWORDS
    [16:28:09] [Server thread/INFO]: procPlayerItem;string=Training V;Training
    [16:28:09] [Server thread/INFO]: fromRoman;character=v;ht contains v=false
    [16:28:09] [Server thread/ERROR]: Could not pass event McMMOPlayerXpGainEvent to RandomPackage v16-alpha6
        at$1.execute( ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at [mcMMO.jar:?]
        at [mcMMO.jar:?]
        at [mcMMO.jar:?]
        at [mcMMO.jar:?]
        at [mcMMO.jar:?]
        at [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat( [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at Source) [?:1.8.0_171]
    Caused by: java.lang.NullPointerException
        at me.randomHashTags.RandomPackage.api.CustomEnchantmentss.fromRoman( ~[?:?]
        at me.randomHashTags.RandomPackage.api.CustomEnchantmentss.getEnchantmentLevel( ~[?:?]
        at me.randomHashTags.RandomPackage.api.CustomEnchantmentss.procPlayerItem( ~[?:?]
        at me.randomHashTags.RandomPackage.api.CustomEnchantsMCMMO.mcmmoPlayerXpGainEvent( ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_171]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_171]
        at$1.execute( ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
        ... 15 more
    #1 RandomHashTags, Jul 28, 2018
    Last edited: Jul 28, 2018
    • Like Like x 1
    • Useful Useful x 1
  2. RandomHashTags


    This has been fixed by making the ht static.
    Code (Text):
    private static final Hashtable<Character, Integer> ht = new Hashtable<Character, Integer>();
    It was throwing the error because (I assume) I am extending the CustomEnchantsMCMMO from the class where the methods are (CustomEnchantmentss), and the ht is associated with the instance, rather than the class, resulting in the ht being empty.

    Thanks Hwiggy#5280 from discord for saying
    • "the only way that line can cause NPE is because ht would be undefined or null at that codepoint"
    Something instantly clicked, and now this is solved!
Thread Status:
Not open for further replies.