Solved McMMOPlayerXpGainEvent issue

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

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

    Supporter

    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.
    ht
    Code (Java):
    private final Hashtable<Character, Integer> ht = new Hashtable<Character, Integer>();
    When the plugin enables, these values are put into ht
    Code (Java):
    ht.put('i',1);
    ht.put('x',10);
    ht.put('c',100);
    ht.put('m',1000);
    ht.put('v',5);
    ht.put('l',50);
    ht.put('d',500);
    McMMOPlayerXpGainEvent
    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);
            }
        }
    procPlayerItem
    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);
                    }
                }
        }
    getEnchantmentLevel
    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);
        }
    fromRoman
    Code (Java):
        /*
         * This code is from "batman" at
         * https://stackoverflow.com/questions/9073150/converting-roman-numerals-to-decimal
         */

        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;
        }  
    Error
    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
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at com.gmail.nossr50.util.EventUtils.handleXpGainEvent(EventUtils.java:157) [mcMMO.jar:?]
        at com.gmail.nossr50.datatypes.player.McMMOPlayer.applyXpGain(McMMOPlayer.java:471) [mcMMO.jar:?]
        at com.gmail.nossr50.datatypes.player.McMMOPlayer.beginUnsharedXpGain(McMMOPlayer.java:439) [mcMMO.jar:?]
        at com.gmail.nossr50.datatypes.player.McMMOPlayer.beginXpGain(McMMOPlayer.java:429) [mcMMO.jar:?]
        at com.gmail.nossr50.runnables.skills.AwardCombatXpTask.run(AwardCombatXpTask.java:42) [mcMMO.jar:?]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-21fe707-e1ebe52]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    Caused by: java.lang.NullPointerException
        at me.randomHashTags.RandomPackage.api.CustomEnchantmentss.fromRoman(CustomEnchantmentss.java:935) ~[?:?]
        at me.randomHashTags.RandomPackage.api.CustomEnchantmentss.getEnchantmentLevel(CustomEnchantmentss.java:187) ~[?:?]
        at me.randomHashTags.RandomPackage.api.CustomEnchantmentss.procPlayerItem(CustomEnchantmentss.java:241) ~[?:?]
        at me.randomHashTags.RandomPackage.api.CustomEnchantsMCMMO.mcmmoPlayerXpGainEvent(CustomEnchantsMCMMO.java:27) ~[?:?]
        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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[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

    Supporter

    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.