JitseB / NPCLib

(Minecraft) NPCLib – Basic non-player character library.
MIT License
197 stars 49 forks source link

Exception in thread "pool-14-thread-2" java.lang.NullPointerException: "this.npcLib" is null" #179

Closed LucasitoInDev closed 1 year ago

LucasitoInDev commented 2 years ago

Hi, i have this issue, today i started to use your API, but i dont know what happen...

Server sofware: Spigot 1.12.2 (Lobby)

Java version: [TESTED IN] 17, 16, 11, 8

Full error LOG: ( https://mclo.gs/paf7sIm )

[02:29:21 WARN]: Exception in thread "pool-14-thread-1" java.lang.NullPointerException
[02:29:21 WARN]:        at es.crowty.lobby.npc.NPCManager.lambda$load$0(NPCManager.java:60)
[02:29:21 WARN]:        at com.pistonproject.api.npc.api.skin.MineSkinFetcher.lambda$fetchSkinFromIdAsync$0(MineSkinFetcher.java:49)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)

[02:29:21 WARN]: Exception in thread "pool-14-thread-2" java.lang.NullPointerException
[02:29:21 WARN]:        at es.crowty.lobby.npc.NPCManager.lambda$load$1(NPCManager.java:75)
[02:29:21 WARN]:        at com.pistonproject.api.npc.api.skin.MineSkinFetcher.lambda$fetchSkinFromIdAsync$0(MineSkinFetcher.java:49)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[02:29:21 WARN]:        at java.base/java.lang.Thread.run(Thread.java:829)

[02:29:21 WARN]: Exception in thread "pool-14-thread-3" java.lang.NullPointerException
[02:29:21 WARN]:        at es.crowty.lobby.npc.NPCManager.lambda$load$2(NPCManager.java:89)
[02:29:21 WARN]:        at com.pistonproject.api.npc.api.skin.MineSkinFetcher.lambda$fetchSkinFromIdAsync$0(MineSkinFetcher.java:49)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[02:29:21 WARN]:        at java.base/java.lang.Thread.run(Thread.java:829)

[02:29:21 INFO]: [NPCLibPlugin] Enabling NPCLibPlugin v2.11.1-SNAPSHOT
[02:29:21 INFO]: [NPCLibPlugin] NPCLib classes loaded

[02:29:21 WARN]: Exception in thread "pool-14-thread-4" java.lang.NullPointerException
[02:29:21 WARN]:        at es.crowty.lobby.npc.NPCManager.lambda$load$3(NPCManager.java:103)
[02:29:21 WARN]:        at com.pistonproject.api.npc.api.skin.MineSkinFetcher.lambda$fetchSkinFromIdAsync$0(MineSkinFetcher.java:49)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[02:29:21 WARN]:        at java.base/java.lang.Thread.run(Thread.java:829)

[02:29:21 WARN]: Exception in thread "pool-14-thread-5" java.lang.NullPointerException
[02:29:21 WARN]:        at es.crowty.lobby.npc.NPCManager.lambda$load$4(NPCManager.java:117)
[02:29:21 WARN]:        at com.pistonproject.api.npc.api.skin.MineSkinFetcher.lambda$fetchSkinFromIdAsync$0(MineSkinFetcher.java:49)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[02:29:21 WARN]:        at java.base/java.lang.Thread.run(Thread.java:829)

[02:29:21 WARN]: Exception in thread "pool-14-thread-6" java.lang.NullPointerException
[02:29:21 WARN]:        at es.crowty.lobby.npc.NPCManager.lambda$load$5(NPCManager.java:131)
[02:29:21 WARN]:        at com.pistonproject.api.npc.api.skin.MineSkinFetcher.lambda$fetchSkinFromIdAsync$0(MineSkinFetcher.java:49)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[02:29:21 WARN]:        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[02:29:21 WARN]:        at java.base/java.lang.Thread.run(Thread.java:829)

Main class:

public class LobbyCORE extends JavaPlugin {
    public static LobbyCORE pl;
    private NPCManager npcManager;
    private NPCLib library;

    public void onEnable() {
        pl = this;
        saveDefaultConfig();
        this.library = new NPCLib(this);
        this.npcManager = new NPCManager();

        new BukkitRunnable() {
            public void run() {
                for (World w : Bukkit.getWorlds()) {
                    w.setGameRuleValue("doDaylightCycle", "false");
                    w.setGameRuleValue("randomTickSpeed", "0");
                    w.setGameRuleValue("doMobSpawning", "false");
                    w.setGameRuleValue("doEntityDrops", "false");
                    w.setGameRuleValue("doFireTick", "false");
                    w.setGameRuleValue("doTileDrops", "false");
                    w.setGameRuleValue("doWeatherCycle", "false");
                    w.setGameRuleValue("disableElytraMovementCheck", "true");
                    w.setGameRuleValue("mobGriefing", "false");
                    w.setGameRuleValue("showDeathMessages", "false");
                    w.setGameRuleValue("spectatorsGenerateChunks", "false");
                    w.setTime(6000L);
                }
            }
        }.runTaskLater((Plugin)pl, 20L);
    }

    public void onDisable() {
        pl = null;
    }

    public static LobbyCORE getInstance() {
        return pl;
    }

    public NPCLib getNPCLib() {
        return library;
    }

    public NPCManager getNpcManager() {
        return this.npcManager;
    }
}

"NPCManager" class:

package es.crowty.lobby.npc;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import es.crowty.lobby.util.Color;
import es.crowty.lobby.util.SoundCompatibility;
import net.jitse.npclib.NPCLib;
import net.jitse.npclib.api.NPC;
import net.jitse.npclib.api.events.NPCInteractEvent;
import net.jitse.npclib.api.skin.MineSkinFetcher;
import net.jitse.npclib.api.state.NPCSlot;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Arrays;

import static es.crowty.lobby.LobbyCORE.pl;

public class NPCManager implements Listener {
    private NPCLib npcLib;

    //Lista de NPC's que hay
    private NPC skypvp;
    private NPC survival;
    private NPC crowtydice;
    private NPC buildbattle;
    private NPC skywars;
    private NPC skyblock;

    public NPCManager() {
        load();
    }

    public void display(Player player) {
        skypvp.show(player);
        survival.show(player);
        crowtydice.show(player);
        buildbattle.show(player);
        skywars.show(player);
        skyblock.show(player);
    }

    private void load() {
        int skinSkyPVP = 660791393;
        int skinSurvival = 377875510;
        int skinCrowtyDice = 469698812;
        int skinBuildBattle = 877034420;
        int skinSkyWars = 1216180572;
        int skinSkyBlock = 1327830242;

        MineSkinFetcher.fetchSkinFromIdAsync(skinSkyPVP, skin -> {
            skypvp = npcLib.createNPC(Arrays.asList(pl.getConfig().getString("Npc.skypvp.nombre"), "§8    §r", "", "§7§oClick para ir"));
            skypvp = npcLib.createNPC(Arrays.asList(pl.getConfig().getString("Npc.skypvp.nombre"), "§8    §r", "", "§7§oClick para ir"));
            skypvp.setLocation(new Location(Bukkit.getWorlds().get(0),
                    pl.getConfig().getDouble("Npc.skypvp.X"),
                    pl.getConfig().getDouble("Npc.skypvp.y"),
                    pl.getConfig().getDouble("Npc.skypvp.Z"),
                    -115,
                    -4));
            skypvp.setItem(NPCSlot.MAINHAND, new ItemStack(Material.WOOD_SWORD));
            skypvp.setItem(NPCSlot.OFFHAND, new ItemStack(Material.FEATHER));
            skypvp.setSkin(skin);
            skypvp.create();
        });

        MineSkinFetcher.fetchSkinFromIdAsync(skinSurvival, skin -> {
            survival = npcLib.createNPC(Arrays.asList(pl.getConfig().getString("Npc.survival.nombre"), "§8    §r", "", "§7§oClick para ir"));
            survival.setLocation(new Location(Bukkit.getWorlds().get(0),
                    pl.getConfig().getDouble("Npc.survival.X"),
                    pl.getConfig().getDouble("Npc.survival.y"),
                    pl.getConfig().getDouble("Npc.survival.Z"),
                    -105,
                    -4));
            survival.setItem(NPCSlot.MAINHAND, new ItemStack(Material.FISHING_ROD));
            survival.setItem(NPCSlot.OFFHAND, new ItemStack(Material.RAW_FISH));
            survival.setSkin(skin);
            survival.create();
        });

        MineSkinFetcher.fetchSkinFromIdAsync(skinCrowtyDice, skin -> {
            crowtydice = npcLib.createNPC(Arrays.asList(pl.getConfig().getString("Npc.crowtydice.nombre"), "§8    §r", "", "§7§oClick para ir"));
            crowtydice.setLocation(new Location(Bukkit.getWorlds().get(0),
                    pl.getConfig().getDouble("Npc.crowtydice.X"),
                    pl.getConfig().getDouble("Npc.crowtydice.y"),
                    pl.getConfig().getDouble("Npc.crowtydice.Z"),
                    -95,
                    -4));
            crowtydice.setItem(NPCSlot.MAINHAND, new ItemStack(Material.SLIME_BALL));
            crowtydice.setItem(NPCSlot.OFFHAND, new ItemStack(Material.DIAMOND_AXE));
            crowtydice.setSkin(skin);
            crowtydice.create();
        });

        MineSkinFetcher.fetchSkinFromIdAsync(skinBuildBattle, skin -> {
            buildbattle = npcLib.createNPC(Arrays.asList(pl.getConfig().getString("Npc.buildbattle.nombre"), "§8    §r", "", "§7§oClick para ir"));
            buildbattle.setLocation(new Location(Bukkit.getWorlds().get(0),
                    pl.getConfig().getDouble("Npc.buildbattle.X"),
                    pl.getConfig().getDouble("Npc.buildbattle.y"),
                    pl.getConfig().getDouble("Npc.buildbattle.Z"),
                    -85,
                    -4));
            buildbattle.setItem(NPCSlot.MAINHAND, new ItemStack(Material.CLAY_BRICK));
            buildbattle.setItem(NPCSlot.OFFHAND, new ItemStack(Material.PURPLE_GLAZED_TERRACOTTA));
            buildbattle.setSkin(skin);
            buildbattle.create();
        });

        MineSkinFetcher.fetchSkinFromIdAsync(skinSkyWars, skin -> {
            skywars = npcLib.createNPC(Arrays.asList(pl.getConfig().getString("Npc.skywars.nombre"), "§8    §r", "", "§7§oClick para ir"));
            skywars.setLocation(new Location(Bukkit.getWorlds().get(0),
                    pl.getConfig().getDouble("Npc.skywars.X"),
                    pl.getConfig().getDouble("Npc.skywars.y"),
                    pl.getConfig().getDouble("Npc.skywars.Z"),
                    -75,
                    -4));
            skywars.setItem(NPCSlot.MAINHAND, new ItemStack(Material.BOW));
            skywars.setItem(NPCSlot.OFFHAND, new ItemStack(Material.SPLASH_POTION));
            skywars.setSkin(skin);
            skywars.create();
        });

        MineSkinFetcher.fetchSkinFromIdAsync(skinSkyBlock, skin -> {
            skyblock = npcLib.createNPC(Arrays.asList(pl.getConfig().getString("Npc.skyblock.nombre"), "§8    §r", "", "§7§oClick para ir"));
            skyblock.setLocation(new Location(Bukkit.getWorlds().get(0),
                    pl.getConfig().getDouble("Npc.skyblock.X"),
                    pl.getConfig().getDouble("Npc.skyblock.y"),
                    pl.getConfig().getDouble("Npc.skyblock.Z"),
                    -65,
                    -4));
            skyblock.setItem(NPCSlot.MAINHAND, new ItemStack(Material.GRASS_PATH));
            skyblock.setItem(NPCSlot.OFFHAND, new ItemStack(Material.LAVA_BUCKET));
            skyblock.setSkin(skin);
            skyblock.create();
        });
    }

    @EventHandler
    public void onNPCInteract(NPCInteractEvent e) {
        Player p = e.getWhoClicked();

        //NPC DE SKYPVP
        if (e.getNPC() == skypvp) {
            if (p.hasPermission(pl.getConfig().getString("Npc.skypvp.permiso"))) {
                new BukkitRunnable() {
                    public void run() {
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect");
                        out.writeUTF("skypvp");
                        p.sendPluginMessage((Plugin)pl, "BungeeCord", out.toByteArray());
                    }
                }.runTaskLater((Plugin)pl, 20L);
            } else {
                p.playSound(p.getLocation(), SoundCompatibility.VILLAGER_NO.bukkitSound(), 1.0F, 1.5F);
            }
        }

        //NPC DE SURVIVAL
        if (e.getNPC() == survival) {
            if (p.hasPermission(pl.getConfig().getString("Npc.survival.permiso"))) {
                new BukkitRunnable() {
                    public void run() {
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect");
                        out.writeUTF("survival");
                        p.sendPluginMessage((Plugin)pl, "BungeeCord", out.toByteArray());
                    }
                }.runTaskLater((Plugin)pl, 20L);
            } else {
                p.playSound(p.getLocation(), SoundCompatibility.VILLAGER_NO.bukkitSound(), 1.0F, 1.5F);
            }
        }

        //NPC DE CROWTYDICE
        if (e.getNPC() == crowtydice) {
            if (p.hasPermission(pl.getConfig().getString("Npc.crowtydice.permiso"))) {
                new BukkitRunnable() {
                    public void run() {
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect");
                        out.writeUTF("CGames");
                        p.sendPluginMessage((Plugin)pl, "BungeeCord", out.toByteArray());
                    }
                }.runTaskLater((Plugin)pl, 20L);
            } else {
                p.playSound(p.getLocation(), SoundCompatibility.VILLAGER_NO.bukkitSound(), 1.0F, 1.5F);
            }
        }

        //NPC DE BUILDBATTLE
        if (e.getNPC() == buildbattle) {
            if (p.hasPermission(pl.getConfig().getString("Npc.buildbattle.permiso"))) {
                new BukkitRunnable() {
                    public void run() {
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect");
                        out.writeUTF("BBattle");
                        p.sendPluginMessage((Plugin)pl, "BungeeCord", out.toByteArray());
                    }
                }.runTaskLater((Plugin)pl, 20L);
            } else {
                p.playSound(p.getLocation(), SoundCompatibility.VILLAGER_NO.bukkitSound(), 1.0F, 1.5F);
            }
        }

        //NPC DE SKYWARS
        if (e.getNPC() == buildbattle) {
            if (p.hasPermission(pl.getConfig().getString("Npc.skywars.permiso"))) {
                new BukkitRunnable() {
                    public void run() {
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect");
                        out.writeUTF("SkWars");
                        p.sendPluginMessage((Plugin)pl, "BungeeCord", out.toByteArray());
                    }
                }.runTaskLater((Plugin)pl, 20L);
            } else {
                p.playSound(p.getLocation(), SoundCompatibility.VILLAGER_NO.bukkitSound(), 1.0F, 1.5F);
            }
        }

        //NPC DE SKYBLOCK
        if (e.getNPC() == buildbattle) {
            if (p.hasPermission(pl.getConfig().getString("Npc.skyblock.permiso"))) {
                new BukkitRunnable() {
                    public void run() {
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("Connect");
                        out.writeUTF("SkyBlock");
                        p.sendPluginMessage((Plugin)pl, "BungeeCord", out.toByteArray());
                    }
                }.runTaskLater((Plugin)pl, 20L);
            } else {
                p.playSound(p.getLocation(), SoundCompatibility.VILLAGER_NO.bukkitSound(), 1.0F, 1.5F);
            }
        }

    }

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        Player player = e.getPlayer();
        display(player);
    }

}
cybooo commented 2 years ago

You are not initializing your npcLib variable in NPCManager, so it's always null.

npcLib = new NPCLib(<YOUR PLUGIN INSTANCE>);

LucasitoInDev commented 2 years ago

You are not initializing your npcLib variable in NPCManager, so it's always null.

npcLib = new NPCLib(<YOUR PLUGIN INSTANCE>);

Ok, i will try something