1.15.2 Error in console, not sure what is causing it

Discussion in 'Spigot Plugin Development' started by DapperNurd, Jan 24, 2020.

  1. All the code is running properly but I kind of wanted to clear this error before adding more things. I have no idea what is causing it, however.

    Code (Text):
    [15:31:18 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'restoredrops' in plugin OrganizedDeathDrops v1.0-SNAPSHOT
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.15.1.jar:git-Paper-58]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.15.1.jar:git-Paper-58]
            at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:740) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1820) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1628) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[patched_1.15.1.jar:git-Paper-58]        at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.MinecraftServer.aZ(MinecraftServer.java:1037) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:1030) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:1014) ~[patched_1.15.1.jar:git-Paper-58]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:937) ~[patched_1.15.1.jar:git-Paper-58]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
            at me.nurdco.organizeddeathdrops.OrganizedDeathDrops.restoreInventory(OrganizedDeathDrops.java:54) ~[?:?]
            at me.nurdco.organizeddeathdrops.commands.DropsCommand.onCommand(DropsCommand.java:29) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.15.1.jar:git-Paper-58]
            ... 17 more
     
  2. Something is throwing NullPointerException, in OrganizedDeathDrops at line 54.

    Caused by: java.lang.NullPointerException
    at me.nurdco.organizeddeathdrops.OrganizedDeathDrops.restoreInventory(OrganizedDeathDrops.java:54) ~[?:?]
    at me.nurdco.organizeddeathdrops.commands.DropsCommand.onCommand(DropsCommand.java:29) ~[?:?]
     
  3. Optic_Fusion1

    Resource Staff

    If that's your plugin we need the source
     
  4. @eXpresS
    @Optic_Fusion1

    Code (Java):
    package me.nurdco.organizeddeathdrops;


    import me.nurdco.organizeddeathdrops.commands.DropsCommand;
    import me.nurdco.organizeddeathdrops.events.OnDeathEvent;
    import net.md_5.bungee.api.chat.BaseComponent;
    import net.md_5.bungee.api.chat.ClickEvent;
    import net.md_5.bungee.api.chat.ComponentBuilder;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.BookMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;

    public final class OrganizedDeathDrops extends JavaPlugin{

        ItemStack[] droppedItems;

        public Map<String, ItemStack[]> itemsMap = new HashMap<String, ItemStack[]>();
        public Map<String, ItemStack[]> armorMap = new HashMap<String, ItemStack[]>();
        public Map<List<String>, InventorySnapshot> individualBooks = new HashMap<java.util.List<String>, InventorySnapshot>();
        public float xp;
        private OnDeathEvent listener = null;

        @Override
        public void onEnable() {
            this.listener = new OnDeathEvent(this);
            System.out.println("[OrganizedDeathDrops] Starting...");
            getServer().getPluginManager().registerEvents(listener, this );
            getConfig().options().copyDefaults();
            saveDefaultConfig();
            getCommand("restoreDrops").setExecutor(new DropsCommand(this));
        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
            System.out.println("[OrganizedDeathDrops] Shutting down...");
        }

        public void restoreInventory(Player p, ItemStack[] invContents, ItemStack[] armorContents){
            PlayerInventory inventory = p.getInventory();

            ItemStack[] newInv;


            inventory.setContents(individualBooks.get(inventory.getItemInMainHand().getItemMeta().getLore()).contents);
            inventory.setArmorContents(individualBooks.get(inventory.getItemInMainHand().getItemMeta().getLore()).armor);
            individualBooks.remove(inventory.getItemInMainHand().getItemMeta().getLore());



           /* for (int i = 0; i < barrelCount; i++) {
                Barrel barrel = placeBarrel(player, i == 0 ? location : location.clone()
                        .add(0, i, 0));
                if (barrel == null) {
                    // If barrel placement fails skip it. This can lead to not all drops being processed.
                    // Remaining items not stored in barrels will be dropped on the floor.
                    break;
                }
                placedBarrelCount++;

                ItemStack[] dropPartition = new ItemStack[barrelCapacity];
                for (int j = 0; drops.size() > 0 && j < barrelCapacity; j++) {
                    dropPartition[j] = drops.remove(0);
                }
                barrel.getInventory().setContents(dropPartition);
            }*/


        }

        public void storeContents(Player p, ItemStack[] items, ItemStack[] armor, float newXP) {
            itemsMap.put(p.getName(), items);
            armorMap.put(p.getName(), armor);
            xp = newXP;
        }

        public ItemStack createBook(UUID uuid, String name) {
            ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
            BookMeta bookMeta = (BookMeta) book.getItemMeta();

            BaseComponent[] page = new ComponentBuilder("[Click here to restore dropped items]")
                    .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/restoredrops"))
                    .create();


            bookMeta.spigot().addPage(page);

            bookMeta.setTitle(name);
            bookMeta.setAuthor("OrganizedDeathDrops");
            book.setItemMeta(bookMeta);

            return book;
        }
    }

     
    Code (Java):
    package me.nurdco.organizeddeathdrops.commands;

    import me.nurdco.organizeddeathdrops.OrganizedDeathDrops;
    import me.nurdco.organizeddeathdrops.events.OnDeathEvent;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;

    public class DropsCommand implements CommandExecutor {

        private OrganizedDeathDrops main;

        public DropsCommand(OrganizedDeathDrops main) {
            this.main = main;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if(sender instanceof Player) {
                Player player = (Player) sender;
                if (command.getName().equalsIgnoreCase("restoreDrops")) {
                    //Inventory droppedItems = Bukkit.createInventory(player, 45, "Dropped Items");
                    //player.openInventory(droppedItems);
                    main.restoreInventory(player, player.getInventory().getContents(), player.getInventory().getArmorContents());
                    //return true;
                }
                if (command.getName().equalsIgnoreCase("debug")) {
                    player.sendMessage(String.valueOf(main.itemsMap));
                    player.sendMessage(String.valueOf(main.armorMap));
                    return true;
                }

            } else {
                System.out.println(ChatColor.RED +"You need to be a player to execute this command.");
            }


            return true;
        }
    }
     
    Code (Java):
    package me.nurdco.organizeddeathdrops.events;

    import me.nurdco.organizeddeathdrops.InventorySnapshot;
    import me.nurdco.organizeddeathdrops.OrganizedDeathDrops;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerEggThrowEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import java.util.Arrays;
    import java.util.List;
    import java.util.Random;

    public class OnDeathEvent implements Listener {

        private OrganizedDeathDrops main = null;

        public ItemStack book = null;

        public InventorySnapshot invSnap;

        public OnDeathEvent(OrganizedDeathDrops main) {
            this.main = main;
        }

        @EventHandler
        public void onDeath(final PlayerDeathEvent e) {
            if(e.getEntity() instanceof Player) {
                final Player player =  (Player) e.getEntity();
                player.sendMessage("Death Detected");

                List<ItemStack> drops = e.getDrops();

                Random rand = new Random();
                int randomNum = (int) (Math.random() * 0 + 9999);
                player.sendMessage(String.valueOf(randomNum));

                book = main.createBook(player.getUniqueId(), player.getName());
                ItemMeta bookMeta = book.getItemMeta();
                bookMeta.setLore(Arrays.asList(ChatColor.RED + "- Only Works in MAIN HAND Slot -", ChatColor.DARK_GRAY + "Unique ID: " + String.valueOf(randomNum)));
                book.setItemMeta(bookMeta);
                player.sendMessage("LORE SET");

                player.getWorld().dropItemNaturally(player.getLocation(), book);

                invSnap = new InventorySnapshot();
                invSnap.contents = player.getInventory().getContents();
                invSnap.armor = player.getInventory().getArmorContents();
                invSnap.book = book;
                invSnap.player = player.getUniqueId();

                System.out.println(Arrays.toString(invSnap.contents));

                main.individualBooks.put(book.getItemMeta().getLore(), invSnap);

                //main.storeContents(player, player.getInventory().getContents(), player.getInventory().getArmorContents(), e.getNewExp());
                e.getDrops().clear();
            }
        }

        @EventHandler
        public void test(PlayerEggThrowEvent e) {
            System.out.println(invSnap);
            System.out.println(book);
        }
    }
     
    Code (Java):
    package me.nurdco.organizeddeathdrops;

    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;

    import java.util.UUID;

    public class InventorySnapshot {
        public ItemStack[] contents;
        public ItemStack[] armor;
        public ItemStack book;
        public UUID player;
    }
     
     
  5. Line 54 is
    Code (Java):
            inventory.setArmorContents(individualBooks.get(inventory.getItemInMainHand().getItemMeta().getLore()).armor);
     
     
  6. Here's a trick on how to debug your line 54, switch it to this:
    Code (Text):
     inventory
    .setArmorContents(
    individualBooks
    .get(
    inventory
    .getItemInMainHand()
    .getItemMeta()
    .getLore())
    .armor);
    Now rerun the plugin and check the logs to see what line is causing the nullpointerexception and adapt your code on assuming that the line number is returning (obj != null)
     
  7. I changed it to this:
    Code (Java):
            inventory
                    .setContents(
                            individualBooks
                                    .get(
                                            inventory
                                                    .getItemInMainHand()
                                                    .getItemMeta()
                                                    .getLore())
                                    .contents);
            inventory
                    .setArmorContents(
                            individualBooks
                                    .get(
                                            inventory
                                                    .getItemInMainHand()
                                                    .getItemMeta()
                                                    .getLore())
                                    .armor);
     
    and now the error says it's on line 65, which is the .get( under the armor. I don't really understand how that is making an error though but the contents one isn't.
     
  8. inventory.getItemInMainHand().getItemMeta().getLore() is returning null
    Are you check if the item in main hand != null, itemmeta of that item != null and getLore of that item != null?