有了这个小插件,存储在arraylist“floating”中的项不应该被丢弃,但它们仍然可以吗?
我试过同时使用itemdespawnevent和entitydeathevent,但都不起作用

    public void itemVanish2(ItemDespawnEvent e) {
        if(e.getEntity().getType() == EntityType.DROPPED_ITEM) {
            ItemStack i = (ItemStack) e.getEntity();

            if(i.getItemMeta().getLore().contains("FLOATING"))
                e.getEntity().getWorld().dropItem(e.getEntity().getLocation(), i);
        } else {
            return;
        }
    }

    public void itemVanish(EntityDeathEvent e) {
        if (e.getEntity().getType() == EntityType.DROPPED_ITEM) {
            ItemStack i = (ItemStack) e.getEntity();

            if (i.getItemMeta().getLore().contains("FLOATING"))
                e.getEntity().getWorld().dropItem(e.getEntity().getLocation(), i);

        } else {
            return;
        }
    }

我在寻找的项目不被剥夺,但在控制台它抛出一个错误,然后项目剥夺。
这是它抛出的错误:
[06:49:03] [Server thread/ERROR]: Could not pass event ItemDespawnEvent to FloatingShopItems v1.0.0
org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:520) ~[spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:505) ~[spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callItemDespawnEvent(CraftEventFactory.java:621) ~[spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at net.minecraft.server.v1_14_R1.EntityItem.tick(EntityItem.java:129) ~[spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at net.minecraft.server.v1_14_R1.WorldServer.entityJoinedWorld(WorldServer.java:570) ~[spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at net.minecraft.server.v1_14_R1.World.a(World.java:745) [spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at net.minecraft.server.v1_14_R1.WorldServer.doTick(WorldServer.java:346) [spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at net.minecraft.server.v1_14_R1.MinecraftServer.b(MinecraftServer.java:1057) [spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:396) [spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:956) [spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:801) [spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_212]
Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_14_R1.entity.CraftItem cannot be cast to org.bukkit.inventory.ItemStack
        at mad.madster.floatingitems.FloatingItems.itemVanish2(FloatingItems.java:138) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_212]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_212]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.14.2.jar:git-Spigot-093165d-ba575a5]

最佳答案

欢迎使用StackOverflow
org.bukkit.craftbukkit.v1_14_R1.entity.CraftItem cannot be cast to org.bukkit.inventory.ItemStack
这似乎是个错误。您正在调用Item.getEntity(),但返回的对象不能强制转换为itemstack。ItemDespawnEvent.getEntity()。在spigot 1.14.2中,对象Item.getItemStack()下面有一个方法,请尝试执行以下代码:

public void itemVanish2(ItemDespawnEvent e) {
    if(e.getEntity().getType() == EntityType.DROPPED_ITEM) {
        ItemStack i = (ItemStack) e.getEntity().getItemStack​();

        if(i.getItemMeta().getLore().contains("FLOATING"))
            e.getEntity().getWorld().dropItem(e.getEntity().getLocation(), i);
    } else {
        return;
    }
}

10-08 14:30