Solved org.bukkit.block.dispenser problem

Discussion in 'Plugin Development' started by Etsijä, May 25, 2014.

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

    Etsijä

    I've made a plugin which runs fine and does its job. But after a while the plugin starts to spam this to the console:

    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.block.CraftDropper cannot be
    cast to org.bukkit.block.Dispenser
            at com.github.etsija.impacttnt.ImpactTNTEvents.onDispense(ImpactTNTEvents.java:175) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor387.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_40]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_40]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[craftbukki
    t.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            ... 15 more
    Here's the relevant code:

    Code:java
    1. // Handles the dispenser dispense event, when it has TNT to dispense
    2. @EventHandler
    3. public void onDispense(final BlockDispenseEvent event) {
    4. CannonDispenser c = null;
    5. org.bukkit.material.Dispenser d = (org.bukkit.material.Dispenser) event.getBlock().getState().getData();
    6. // This is line 175 from which the Java errors come
    7. org.bukkit.block.Dispenser d2 = (org.bukkit.block.Dispenser) event.getBlock().getState();
    8. BlockFace face = d.getFacing();
    9. ItemStack i = event.getItem();
    10. Location loc = event.getBlock().getLocation();
    11. final World world = event.getBlock().getWorld();
    12. final Location dispLocation = event.getBlock().getRelative(face).getLocation();
    13. dispLocation.setY(dispLocation.getY() + 1);
    14. // Safety radius squared (less complicated to calculate when srqt() left out)
    15. final double squaredSafetyDistance = ImpactTNT.safetyRadius * ImpactTNT.safetyRadius;
    16.  
    17. // The item to dispense is valid TNT and the dispensers work as cannons
    18. if (ImpactTNT.dispenserCannon &&
    19. (i.getType() == Material.TNT) &&
    20. passedNameCheckForDispenser(event)) {
    21. event.setCancelled(true);
    22.  
    23. // Find the CannonDispenser entry from the list of cannons. If this dispenser is not yet on the list, add it
    24. c = ImpactTNT.cannons.get(loc);
    25. if (c == null) {
    26. c = new CannonDispenser(0, 45);
    27. ImpactTNT.cannons.put(loc, c);
    28. }
    29.  
    30. // Create the TNT entity and set up an entity controller for it
    31. Entity entity = createTNT(world, dispLocation, squaredSafetyDistance);
    32.  
    33. // Shoot the TNT from the dispenser, using the speedFactor as a modifier
    34. // Also use this dispenser cannon's direction settings!
    35. Vector v = calcVelocityVector(face, c.getDirection(), c.getAngle());
    36. entity.setVelocity(v.multiply(c.getPower()));
    37. d2.getInventory().removeItem(i);
    38. }
    39. }


    What's wrong with line 175?
     
  2. Offline

    mine-care

    Hmm mabe u have to use some it's because u are casting to dispenser (you could use Dispenser d = blah blah) and mabe u have to check if (event.getblock.....) and then cast to dispenser..
     
  3. Offline

    rsod

    Basically you should read the error.
    So item is dispensing from dropper, and you always casting it to dispenser, you should check if block is actually dispenser and only after you sure that it's dispenser, cast it to dispenser.
     
    Garris0n likes this.
  4. Offline

    Etsijä

    @rsodAh, now I see where the error is. Thanks for pointing it out. I agree I should've read the error message more carefully.

    Fix was extremely easy: just added this to the beginning of the onDispense() method.

    if (event.getBlock().getType() != Material.DISPENSER) return;
     
  5. Offline

    ZodiacTheories

    Etsijä

    also, instead of doing org.bukkit.block.Dispenser, just import Dispenser
     
  6. Offline

    Etsijä

    That wouldn't work, I need to import the ...material.Dispenser and ...block.Dispenser separately, to access their respective needed methods.
     
  7. Etsijä You'd be fine importing one of them and having the fully qualified reference for the other :p But not really much need, I guess.
     
  8. Offline

    ZodiacTheories

  9. ZodiacTheories True, but as I said, there'll need to be at least one fully qualified reference.
     
Thread Status:
Not open for further replies.

Share This Page