tr7zw / Item-NBT-API

Add custom NBT tags to Items/Tiles/Entities without NMS!
https://www.spigotmc.org/resources/item-entity-tile-nbt-api.7939/
MIT License
569 stars 76 forks source link

NBTApiException #298

Open sanseyooyea opened 2 months ago

sanseyooyea commented 2 months ago

The using server is Paper 1.21.1

throw the exception while using api:

[14:50:23 ERROR]: Could not pass event InventoryOpenEvent to BanBugNBT v1.0-SNAPSHOT
de.tr7zw.nbtapi.NbtApiException: [Selfchecked][2.13.2]Exception while converting NMS ItemStack to NBTCompound!
    at item-nbt-api-plugin-2.13.2.jar/de.tr7zw.nbtapi.NBTReflectionUtil.convertNMSItemtoNBTCompound(NBTReflectionUtil.java:247) ~[item-nbt-api-plugin-2.13.2.jar:?]
    at item-nbt-api-plugin-2.13.2.jar/de.tr7zw.nbtapi.NBTItem.convertItemtoNBT(NBTItem.java:347) ~[item-nbt-api-plugin-2.13.2.jar:?]
    at item-nbt-api-plugin-2.13.2.jar/de.tr7zw.nbtapi.NBT.itemStackToNBT(NBT.java:447) ~[item-nbt-api-plugin-2.13.2.jar:?]
    at BanBugNBT-1.0-SNAPSHOT(3).jar/work.microhand.banbugnbt.listener.PlayerOpenInventoryListener.lambda$onPlayerOpenInventory$0(PlayerOpenInventoryListener.java:21) ~[BanBugNBT-1.0-SNAPSHOT (3).jar:?]
    at java.base/java.util.stream.MatchOps$1MatchSink.accept(MatchOps.java:90) ~[?:?]
    at java.base/java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:1034) ~[?:?]
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129) ~[?:?]
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527) ~[?:?]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513) ~[?:?]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[?:?]
    at java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230) ~[?:?]
    at java.base/java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196) ~[?:?]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:?]
    at java.base/java.util.stream.ReferencePipeline.anyMatch(ReferencePipeline.java:632) ~[?:?]
    at BanBugNBT-1.0-SNAPSHOT(3).jar/work.microhand.banbugnbt.listener.PlayerOpenInventoryListener.onPlayerOpenInventory(PlayerOpenInventoryListener.java:20) ~[BanBugNBT-1.0-SNAPSHOT (3).jar:?]
    at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor525.execute(Unknown Source) ~[?:?]
at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:77) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:?]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:84) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:1.21.1-57-b483da4]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:?]
    at io.papermc.paper.plugin.manager.PaperEventManager.callEvent(PaperEventManager.java:54) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.callEvent(PaperPluginManagerImpl.java:131) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:628) ~[paper-mojangapi-1.21.1-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.craftbukkit.event.CraftEventFactory.callInventoryOpenEventWithTitle(CraftEventFactory.java:1439) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.level.ServerPlayer.openMenu(ServerPlayer.java:1702) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.world.level.block.BarrelBlock.useWithoutItem(BarrelBlock.java:48) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.world.level.block.state.BlockBehaviour$BlockStateBase.useWithoutItem(BlockBehaviour.java:1247) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.level.ServerPlayerGameMode.useItemOn(ServerPlayerGameMode.java:589) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.network.ServerGamePacketListenerImpl.handleUseItemOn(ServerGamePacketListenerImpl.java:1942) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.network.protocol.game.ServerboundUseItemOnPacket.handle(ServerboundUseItemOnPacket.java:44) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.network.protocol.game.ServerboundUseItemOnPacket.handle(ServerboundUseItemOnPacket.java:11) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:56) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:151) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1537) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:201) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:125) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1514) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1507) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:135) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.MinecraftServer.managedBlock(MinecraftServer.java:1466) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:1473) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1318) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:330) ~[paper-1.21.1.jar:1.21.1-57-b483da4]
    at java.base/java.lang.Thread.run(Thread.java:1583) ~[?:?]
Caused by: de.tr7zw.nbtapi.NbtApiException: [Selfchecked][2.13.2]Error while calling the method 'save', loaded: true, Enum: NMSITEM_SAVE_MODERN, Passed Class: class net.minecraft.world.item.ItemStack Args: [net.minecraft.core.RegistryAccess$1FrozenAccess@2c0818e8]
    at item-nbt-api-plugin-2.13.2.jar/de.tr7zw.nbtapi.utils.nmsmappings.ReflectionMethod.run(ReflectionMethod.java:399) ~[item-nbt-api-plugin-2.13.2.jar:?]

and my code is below:

package work.microhand.banbugnbt.listener;

import de.tr7zw.nbtapi.NBT;
import de.tr7zw.nbtapi.iface.ReadWriteNBT;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.Inventory;

import java.util.Arrays;

/**
 * @author SanseYooyea
 */
public class PlayerOpenInventoryListener implements Listener {
    @EventHandler
    public void onPlayerOpenInventory(InventoryOpenEvent event) {
        System.out.println(event.getPlayer().getName() + " is opening a inventory");
        Inventory inventory = event.getInventory();
        if (Arrays.stream(inventory.getContents()).anyMatch(item -> {
            ReadWriteNBT nbt = NBT.itemStackToNBT(item);
            String json = nbt.toString();
            return json.contains("wurst");
        })) {
            event.getPlayer().sendMessage("§c| You can't open the inventory !");
            event.setCancelled(true);
        }
    }
}
tr7zw commented 2 months ago

Looks like that stacktrace is cut off? Also you are not filtering empty/null itemstacks, that might be the cause. Then I'll add a fitting error message.

sanseyooyea commented 2 months ago

getContents method dont have filiter for Material.AIR? I remeber it will not cause null. i wll add null check after sleep

tr7zw commented 2 months ago

Empty items in old versions is null, in newer versions its an AIR item with amount 0.

tr7zw commented 2 months ago

And getConents gives a full array of the slots of that inventory, including empty slots so when setting an inventory to the content of another inventory, the empty slots are kept.

tr7zw commented 1 month ago

FYI you can also just use NBT.itemStackArrayToNBT(itemStacks) to just convert the entire inventory contents array into nbt.