3

With this small plugin, items that are stored i the arraylist "FLOATING" should not despawn, but yet they still do?

Ive tried using both ItemDespawnEvent and EntityDeathEvent and both did not work

    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;
        }
    }

Im looking for the items to not despawn, but in console it throws an error, then the item despawns.

This is the error it throws:

[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]

2 Answers2

1

The event.getEntity() returns an Item (the entity) not an ItemStack. There is a difference. Item is the drop itself, which you will see floating around, an Item has an ItemStack. An ItemStack is the item in your inventory, it holds data such as what type of item and how much it is etc.

This is why you're experiencing a ClassCastException, you do not have to cast the entity at all, it's an Item which has the method getItemStack() which can be used to retrieve the "real" item.

If all you want to do is disable the despawning, don't drop a new item, that would only require more resources and could lead to duplicates. Bukkit's event structure allows you to disable most of the events, including ItemDespawnEvent, meaning you could stop it from despawning.

The resulting code would be something like this:

@EventHandler
public void itemVanish2(ItemDespawnEvent e) {
    ItemStack i = e.getEntity().getItemStack();

    if(i.getItemMeta().getLore().contains("FLOATING"))
        event.setCancelled(true); // cancel the event, so item does not despawn
    }
}

Note that checking an item for lore may not be the best solution to identify it. What if the itemStack does not have itemMeta or lore? What if a user can edit lore? That would allow them to make undespawnable items, an easy path to issues. I recommend using MetaData instead, that way only your plugin can make these items

0

welcome to Stackoverflow

org.bukkit.craftbukkit.v1_14_R1.entity.CraftItem cannot be cast to org.bukkit.inventory.ItemStack

This seems to be the error. You are calling Item.getEntity() but the object that gets returned cant be casted to an ItemStack. ItemDespawnEvent.getEntity(). In Spigot 1.14.2 there is an method below the Object Item.getItemStack(), so try executing the below code:

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;
    }
}
Jonas
  • 198
  • 1
  • 14