Генерация предметов через таблицу лута

При клике ПКМ игроку должен выдаваться случайный предмет из таблицы лута

Но я получаю ошибку:

java.lang.NullPointerException: null
    at java.util.Objects.requireNonNull(Objects.java:203) ~[?:1.8.0_381] {}
    at ru.honeymc.mod.items.IronCase.use(IronCase.java:35) ~[main/:?] {re:classloading}
    at net.minecraft.item.ItemStack.use(ItemStack.java:216) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {re:classloading,xf:fml:forge:filled_map.4,xf:fml:forge:itemstack}
    at net.minecraft.client.multiplayer.PlayerController.useItem(PlayerController.java:328) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.startUseItem(Minecraft.java:1391) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.handleKeybinds(Minecraft.java:1647) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.tick(Minecraft.java:1462) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.runTick(Minecraft.java:954) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.Minecraft.run(Minecraft.java:607) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
    at net.minecraft.client.main.Main.main(Main.java:184) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_381] {}
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_381] {}
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_381] {}
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_381] {}
    at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:52) ~[forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.1.3.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.1.3.jar:?] {}
    at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.1.3.jar:?] {}
    at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.1.3.jar:?] {}
    at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.1.3.jar:?] {}
    at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:108) [forge-1.16.5-36.2.34_mapped_official_1.16.5-recomp.jar:?] {}

Мой код:

package ru.honeymc.mod.items;

import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootParameterSet;
import net.minecraft.loot.LootTable;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import ru.honeymc.mod.CreativeTab;
import ru.honeymc.mod.HoneyMC;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.Objects;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class IronCase extends Item {
    public static final ResourceLocation LOOT_TABLE_LOCATION = Objects.requireNonNull(ResourceLocation.tryParse("data/" + HoneyMC.MOD_ID +  "/loot_tables/iron_case.json"));

    public IronCase() {super(new Properties().tab(CreativeTab.CREATIVE_TAB));}

    public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
        ActionResult<ItemStack> actionResult = super.use(world, player, hand);

        ItemStack itemStack = actionResult.getObject();
        itemStack.setCount(itemStack.getCount() - 1);
        player.setItemInHand(hand, itemStack);

        LootTable lootTable = Objects.requireNonNull(world.getServer()).getLootTables().get(LOOT_TABLE_LOCATION);
        List<ItemStack> loot = lootTable.getRandomItems(new LootContext.Builder(Objects.requireNonNull(world.getServer().getLevel(world.dimension()))).create(new LootParameterSet.Builder().build()));
        for (ItemStack item : loot) {
            player.addItem(item);
        }
        return actionResult;
    }
}

Версия Forge: 1.16.5 - 36.2.34

P.S. Я абсолютный новичок, как в Java так и в Forge


Ответы (1 шт):

Автор решения: Rinat Abdullin

Проблема возникает на строчке:

        LootTable lootTable = Objects.requireNonNull(world.getServer()).getLootTables().get(LOOT_TABLE_LOCATION);

и конкретно вот тут Objects.requireNonNull(world.getServer()) метод getServer() возвращает null, отсюда и исключение. Убедитесь, что переданный объект world правильно заполнен.

→ Ссылка