Solved Getting spawned type of spawner

Discussion in 'Spigot Help' started by mlefeb01, Jun 12, 2019 at 8:44 AM.

  1. Hi, I am trying to get the spawned type of a mob spawner from a block break event. I have reviewed the java doc and I am 99.9% sure this cast should work, but it doesn't. If anyone can help me, I would greatly appreciate it.

    Error Line
    Code (Java):
    final EntityType type = ((CreatureSpawner) block.getState()).getSpawnedType();
    Full Code
    Code (Java):
        @EventHandler
        public void onSpawnerMine(BlockBreakEvent event) {
            // Checks to see if the broken block is a mob spawner
            if (event.getBlock().getType() != Material.MOB_SPAWNER) {
                return;
            }

            // Check to see if the player is holding an item AND their item has the silk touch enchant
            if (event.getPlayer().getItemInHand() == null || !event.getPlayer().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) {
                return;
            }

            // Check to see if mining spawners with silk touch is enabled
            if (!config.getBoolean("spawners.mine-with-silk")) {
                return;
            }

            // If said checks are passed, grab the event block and event player to improve readability of this methods code
            final Block block = event.getBlock();
            final Player player = event.getPlayer();

            // Handle the breaking of the block by canceling the event and setting the type of the block to air
            event.setCancelled(true);
            block.setType(Material.AIR);

            // Run % chance to actually drop the spawner
            final double r = Math.random() * 100;
            if (!(r <= config.getDouble("spawners.mine-with-silk-chance"))) {
                player.sendMessage(Utils.color(config.getString("spawners.messages.failure-mined-spawner")));
                return;
            }

            // If the spawner type is one of the allowed types, give the player a spawner of that type. If not a pig spawner
            final EntityType type = ((CreatureSpawner) block.getState()).getSpawnedType();
            if (spawnerTypes.contains(type)) {
                block.getWorld().dropItemNaturally(block.getLocation(), createSpawner(type));
                player.sendMessage(Utils.color(config.getString("spawners.messages.success-mined-spawner")
                        .replace("%type%", formatEntityName(type))));
            } else {
                block.getWorld().dropItemNaturally(block.getLocation(), createSpawner(EntityType.PIG));
                player.sendMessage(Utils.color(config.getString("spawners.messages.success-mined-spawner")
                        .replace("%type%", formatEntityName(type))));
            }

        }
     
  2. Hello,

    Can you post the entire error message?
     
  3. Useful
    Code (Text):
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.block.CraftBlockState cannot be cast to org.bukkit.block.CreatureSpawner
    Full Error
    Code (Text):
    [04:22:14 ERROR]: Could not pass event BlockBreakEvent to KarismicFactions v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-TacoSpigot-fd3209f3]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[spigot.jar:git-TacoSpigot-fd3209f3]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-TacoSpigot-fd3209f3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [spigot.jar:git-TacoSpigot-fd3209f3]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.breakBlock(PlayerInteractManager.java:286) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PlayerInteractManager.a(PlayerInteractManager.java:215) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:640) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:40) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockDig.a(SourceFile:10) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-TacoSpigot-fd3209f3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_211]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_211]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:776) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:715) [spigot.jar:git-TacoSpigot-fd3209f3]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:618) [spigot.jar:git-TacoSpigot-fd3209f3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.block.CraftBlockState cannot be cast to org.bukkit.block.CreatureSpawner
            at com.github.mlefeb01.karismicfactions.spawners.SilkSpawner.onSpawnerMine(SilkSpawner.java:98) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_211]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_211]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_211]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[spigot.jar:git-TacoSpigot-fd3209f3]
            ... 18 more
     
  4. You are replacing the 'block' and then continuing to test the 'block's paramaters.
     
  5. I noticed that too :ROFLMAO: not my proudest coding moment. It was 4:30am when I posted this, probably should've gotten some sleep instead.
     
    • Friendly Friendly x 1

Share This Page