Solved Why is this location returning null?

Discussion in 'Spigot Plugin Development' started by dsevvv, Oct 18, 2021.

  1. My code thus far:

    So this is in my ChestManager class, logic works as intended. I am trying to overwrite the default loot tables of naturally generated chests.

    Code (Java):
    @EventHandler
    public static void onChestOpen(PlayerInteractEvent e) {


        if (e.hasBlock()) {
            Block block = e.getClickedBlock();

            assert block != null;
            if (block.getType() == Material.CHEST) {
                Chest chest = (Chest) block.getState();

                if(chest.getLootTable() != null){
                    LootContext.Builder builder = new LootContext.Builder(e.getClickedBlock().getLocation());
                    builder.lootingModifier(1);
                    builder.luck(1);
                    LootContext lootContext = builder.build();

                    if("minecraft:chests/nether_bridge".equalsIgnoreCase(chest.getLootTable().toString())){
                        chest.setLootTable(new NetherBridgeLoot());
                        chest.getLootTable().fillInventory(chest.getInventory(), new Random(), lootContext);
                        chest.update();

                    }
                }
            }
        }
    }
    This is my NetherBridgeLoot class

    Code (Java):
    public class NetherBridgeLoot implements LootTable {

        private final Plugin plugin = MyPlugin.getPlugin(MyPlugin.class);
        private final NamespacedKey key = new NamespacedKey(plugin, "rpg_nether_fortress_loot");
        private final Collection<ItemStack> items = new ArrayList<>();
        private final ItemStack dragonEgg = new ItemStack(Material.DRAGON_EGG);



        @Override
        public @NotNull Collection<ItemStack> populateLoot(@NotNull Random random, @NotNull LootContext context) {

            items.add(dragonEgg);
            return items;
        }

        @Override
        public void fillInventory(@NotNull Inventory inventory, @NotNull Random random, @NotNull LootContext context) {

        }

        @NotNull
        @Override
        public NamespacedKey getKey() {
            return key;
        }
    This is the error being produced

    Code (Text):
    [11:03:55] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to MyPlugin v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:504) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.level.PlayerInteractManager.a(PlayerInteractManager.java:485) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1527) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.network.protocol.game.PacketPlayInUseItem.a(PacketPlayInUseItem.java:33) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.network.protocol.game.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:30) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.TickTask.run(SourceFile:18) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.util.thread.IAsyncTaskHandler.executeTask(SourceFile:151) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.util.thread.IAsyncTaskHandler.executeNext(SourceFile:125) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.bf(MinecraftServer.java:1148) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.executeNext(MinecraftServer.java:1141) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.util.thread.IAsyncTaskHandler.awaitTasks(SourceFile:134) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.sleepForTick(MinecraftServer.java:1125) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1054) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:305) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.world.entity.Entity.getPositionVector()" because the return value of "net.minecraft.world.level.storage.loot.LootTableInfo.getContextParameter(net.minecraft.world.level.storage.loot.parameters.LootContextParameter)" is null
            at org.bukkit.craftbukkit.v1_17_R1.CraftLootTable.convertContext(CraftLootTable.java:127) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory.callLootGenerateEvent(CraftEventFactory.java:1644) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.world.level.storage.loot.LootTable.fillInventory(LootTable.java:134) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.world.level.storage.loot.LootTable.fillInventory(LootTable.java:126) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.craftbukkit.v1_17_R1.CraftLootTable.fillInventory(CraftLootTable.java:65) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at me.devonsevrens.rpgcraft.items.ChestManager.onChestOpen(ChestManager.java:35) ~[?:?]
            at jdk.internal.reflect.GeneratedMethodAccessor57.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
            at java.lang.reflect.Method.invoke(Method.java:567) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            ... 20 more
    Code (Text):
    [11:03:55] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to MyPlugin v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:504) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.level.PlayerInteractManager.a(PlayerInteractManager.java:485) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1527) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.network.protocol.game.PacketPlayInUseItem.a(PacketPlayInUseItem.java:33) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.network.protocol.game.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:30) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.TickTask.run(SourceFile:18) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.util.thread.IAsyncTaskHandler.executeTask(SourceFile:151) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.util.thread.IAsyncTaskHandler.executeNext(SourceFile:125) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.bf(MinecraftServer.java:1148) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.executeNext(MinecraftServer.java:1141) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.util.thread.IAsyncTaskHandler.awaitTasks(SourceFile:134) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.sleepForTick(MinecraftServer.java:1125) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1054) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:305) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.world.entity.Entity.getPositionVector()" because the return value of "net.minecraft.world.level.storage.loot.LootTableInfo.getContextParameter(net.minecraft.world.level.storage.loot.parameters.LootContextParameter)" is null
            at org.bukkit.craftbukkit.v1_17_R1.CraftLootTable.convertContext(CraftLootTable.java:127) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.craftbukkit.v1_17_R1.event.CraftEventFactory.callLootGenerateEvent(CraftEventFactory.java:1644) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.world.level.storage.loot.LootTable.fillInventory(LootTable.java:134) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at net.minecraft.world.level.storage.loot.LootTable.fillInventory(LootTable.java:126) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at org.bukkit.craftbukkit.v1_17_R1.CraftLootTable.fillInventory(CraftLootTable.java:65) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            at me.devonsevrens.rpgcraft.items.ChestManager.onChestOpen(ChestManager.java:35) ~[?:?]
            at jdk.internal.reflect.GeneratedMethodAccessor57.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
            at java.lang.reflect.Method.invoke(Method.java:567) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.17.1.jar:3257-Spigot-dc75aca-960f310]
            ... 20 more

    I am very confused why this isn't working. The location should never return null as this event can only be triggered by a player interacting with a chest located in the world.

    Please help!
     
  2. It doesn't appear to be your location that is null, but rather...

    Code (Java):
    Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.world.entity.Entity.getPositionVector()" because the return value of "net.minecraft.world.level.storage.loot.LootTableInfo.getContextParameter(net.minecraft.world.level.storage.loot.parameters.LootContextParameter)" is null
    The LootContextParameter you are supplying.
     
    • Agree Agree x 1
  3. Thanks SizzleMcGrizzle, I was so fixated on thinking the location was null I was unable to see the solution right in front of my face. Cheers and take care.
     
    • Like Like x 1