org.julekstn.firedrop.functions.ItemDropHandler cannot be cast to

Discussion in 'Plugin Development' started by JulekSTN, Mar 3, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    JulekSTN

    Hi,

    I have a problem with a plugin I made, on the old_beta 1.7.3 bukkit version.
    It was supposed to drop a specific item when a dirt block is broken.

    But i am getting this error:
    Code:
    10:52:52 [SEVERE] Could not pass event BLOCK_BREAK to FireDrop
    java.lang.ClassCastException: org.julekstn.firedrop.functions.ItemDropHandler cannot be cast to org.bukkit.event.block.BlockListener
            at org.bukkit.plugin.java.JavaPluginLoader$37.execute(JavaPluginLoader.java:497)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:338)
            at net.minecraft.server.ItemInWorldManager.c(ItemInWorldManager.java:157)
            at net.minecraft.server.ItemInWorldManager.a(ItemInWorldManager.java:121)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:481)
            at net.minecraft.server.Packet14BlockDig.a(SourceFile:42)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:89)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:453)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:363)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    Main.java:
    Code:
    package org.julekstn.firedrop;import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.julekstn.firedrop.functions.ItemDropHandler;
    import static org.bukkit.Bukkit.getLogger;
    
    public class Main extends JavaPlugin {
    @Override
    public void onDisable() {
    getLogger().info("Disabled FireDrop v1.0 by JulekSTN");
    }
    
    @Override
    public void onEnable() {
    PluginManager pm = this.getServer().getPluginManager();pm.registerEvent(Event.Type.BLOCK_BREAK, new ItemDropHandler(this), Event.Priority.Highest, this);
    new ItemDropHandler(this);getLogger().info("Enabled FireDrop v1.0 by JulekSTN");
    }
    }
    ItemDropHandler.java:
    Code:
    package org.julekstn.firedrop.functions;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    import org.julekstn.firedrop.Main;
    
    public class ItemDropHandler implements Listener {
    private final Main plugin; public ItemDropHandler(Main plugin) {
    this.plugin = plugin;
    }
    
    public void onBlockBreak(BlockBreakEvent event) {
    Player player = event.getPlayer();
    Block block = event.getBlock();
    Material material = block.getType();
    if(material == Material.DIRT) {
    player.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Material.FIRE));}
    }
    }
    
    Could you help me out?

    Kind regards,
    JulekSTN
     
    Last edited by a moderator: Mar 3, 2021
  2. Offline

    KarimAKL

    @JulekSTN Edit: Nevermind, I missed the stack trace you posted.

    Try making ItemDropHandler implement "BlockListener" instead of "Listener."
     
  3. Offline

    JulekSTN

    @KarimAKL
    Hi,

    It changed from implements to extends, and fixed the BlockListener thing, but now i am getting this:

    10:01:57 [SEVERE] Could not pass event PLAYER_INTERACT to FireDrop
    java.lang.ClassCastException: org.julekstn.firedrop.functions.ItemDropHandler cannot be cast to org.bukkit.event.player.PlayerListener
    at org.bukkit.plugin.java.JavaPluginLoader$11.execute(JavaPluginLoader.java:314)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:338)
    at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:161)
    at net.minecraft.server.ItemInWorldManager.dig(ItemInWorldManager.java:63)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:478)
    at net.minecraft.server.Packet14BlockDig.a(SourceFile:42)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:89)
    at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
    at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:453)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:363)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)

    Can you help me?
     
  4. Offline

    Newdel

    Still the same error

    And why are you trying to use PluginManager#registerEvent(...) the whole time? Just use PluginManager#registerEvents(...) and easy
     
  5. Offline

    KarimAKL

    @JulekSTN is using version 1.7.3, so I am not sure if that method exists yet. If it does, you should use it, @JulekSTN.

    Do you know how to read stack traces? If not, you can read this. This is basically the same problem as earlier, but with the PLAYER_INTERACT event & PlayerListener, instead of the BLOCK_BREAK event & BlockListener.
     
  6. Offline

    JulekSTN

    @KarimAKL
    Okay, i changed my code a bit but the error still persists.

    Here it is:

    Main:
    Code:
    package org.julekstn.firedrop;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.julekstn.firedrop.functions.ItemDropHandler;
    import static org.bukkit.Bukkit.getLogger;
    
    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
    ItemDropHandler itemDropHandler;
    
    @Override
    public void onDisable() {
    itemDropHandler = null;
    getLogger().info("Disabled FireDrop v1.0 by JulekSTN");
    }
    
    @Override
    public void onEnable() {
    PluginManager pm = this.getServer().getPluginManager();
    itemDropHandler = new ItemDropHandler();
    pm.registerEvent(Event.Type.PLAYER_INTERACT, itemDropHandler, Event.Priority.Highest, this);
    getLogger().info("Enabled FireDrop v1.0 by JulekSTN");
    }
    }
    
    ItemDropHandler:
    Code:
    package org.julekstn.firedrop.functions;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    public class ItemDropHandler implements Listener {
    public ItemDropHandler(){}
    
    @SuppressWarnings("unused")
    public void onInteract(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    Action action = event.getAction();
    Block block = event.getClickedBlock();
    Material material = block.getType();
    if (event.hasBlock() && action == Action.LEFT_CLICK_BLOCK && material == Material.FIRE) {
    player.getWorld().dropItemNaturally(block.getLocation(), new ItemStack(Material.FIRE, 1));
    }
    }
    }
    @Newdel The registerEvents function doesn't exist in this version.

    I still need some help. The error is the same as in my previous post.
     
    Last edited by a moderator: Mar 4, 2021
  7. Offline

    KarimAKL

    @JulekSTN You are implementing Listener, not PlayerListener.
     
  8. Offline

    JulekSTN

    @KarimAKL But PlayerListener is not an interface, so i can't implement it. I only can extend it.
    I tried extending PlayerListener, but then it wouldn't even register the event because it:

    - Didn't show up the error :D
    - But didn't drop the thing, so it wasn't working :(
     
  9. Offline

    KarimAKL

    @JulekSTN I do not have the Javadocs for such an old version, so I cannot tell whether it is an interface or a class. Regardless, you need to inherit it.

    Did you try printing something in the event method without any conditions applied beforehand?
     
  10. Offline

    JulekSTN

    @KarimAKL Here are the javadocs:
    https://web.archive.org/web/20111007191822/http://jd.bukkit.org/apidocs

    Also, I printed some values at the start of my function, but they don't show up.
    I think the problem is somewhere in these lines:

    PluginManager pm = this.getServer().getPluginManager();
    itemDropHandler = new ItemDropHandler();
    pm.registerEvent(Event.Type.PLAYER_INTERACT, itemDropHandler, Event.Priority.Highest, this);
     
    Last edited: Mar 5, 2021
  11. Offline

    KarimAKL

    @JulekSTN The Javadocs you linked do not seem to be for the version you are using. A Wayback machine to Javadocs that are for the latest version of the API is probably not reliable.

    Does the @EventHandler annotation exist in the version you are using? You might be missing that.
     
  12. Offline

    JulekSTN

    @KarimAKL I don't know what you are talking about, this is a javadocs page for this version. At the top it says Bukkit-0.0.1 and it is a page from 2011.

    The EventHandler annotation doesn't exist in that version, as you can see in the attachment. (it isn't here as the EventHandler annotation is there in newer versions of the page).
     

    Attached Files:

  13. Offline

    timtower Administrator Administrator Moderator

    @JulekSTN How about using something that is not made 10 years ago?
     
  14. Offline

    KarimAKL

    According to the Wayback Machine link you posted, PluginManager#registerEvent has at minimum 5 parameters. You are passing 4. PluginManager#registerEvents(Listener, Plugin) exists in the Wayback Machine as well.

    This is what I mean by it being unreliable. Regardless, I have no more ideas as to how you could fix this.

    Out of curiosity, why are you using such an old version?
     
  15. Offline

    JulekSTN

Thread Status:
Not open for further replies.

Share This Page