Solved Setting a spawners spawn type through an ItemStack

Discussion in 'Spigot Plugin Development' started by PCPSells, Mar 10, 2020.

Thread Status:
Not open for further replies.
  1. I'm attempting to make an ItemStack that is a Mob Spawner of a specific type.
    Version 1.11.2

    Code (Text):
    [Server thread/ERROR]: Could not pass event BlockBreakEvent to ACStackedSpawners v1.0.0
        at$1.execute( ~[server.jar:git-Spigot-7d78b81-d5e7885]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[server.jar:git-Spigot-7d78b81-d5e7885]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [server.jar:git-Spigot-7d78b81-d5e7885]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PlayerInteractManager.breakBlock( [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PlayerInteractManager.a( [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a( [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PacketPlayInBlockDig.a(SourceFile:40) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PacketPlayInBlockDig.a(SourceFile:10) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$ [server.jar:git-Spigot-7d78b81-d5e7885]
        at java.util.concurrent.Executors$ [?:1.8.0_191]
        at [?:1.8.0_191]
        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D( [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D( [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C( [server.jar:git-Spigot-7d78b81-d5e7885]
        at [server.jar:git-Spigot-7d78b81-d5e7885]
        at [?:1.8.0_191]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_11_R1.inventory.CraftMetaBlockState cannot be cast to org.bukkit.block.BlockState
        at me.PCPSells.ACSS.StackedSpawner.getSpawnerAsItemStack( ~[?:?]
        at ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_191]
        at sun.reflect.NativeMethodAccessorImpl.invoke( ~[?:1.8.0_191]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_191]
        at java.lang.reflect.Method.invoke( ~[?:1.8.0_191]
        at$1.execute( ~[server.jar:git-Spigot-7d78b81-d5e7885]
        ... 17 more
    Code (Java):
        public ItemStack getSpawnerAsItemStack() {
            ItemStack item = new ItemStack(Material.MOB_SPAWNER, 1);
            ItemMeta itemMeta = item.getItemMeta();
            BlockState bs = (BlockState) itemMeta;
            CreatureSpawner cs = (CreatureSpawner) bs;
            return item;
    Line 73:
    Code (Java):
    BlockState bs = (BlockState) itemMeta;
    Any help would be greatly appreciated! :)
  2. You can't cast item meta to BlockState. You're going to have to set the type of the spawner after the player has placed it and you have an actual MobSpawner to interact with and update.
    • Informative Informative x 1
  3. I'm not looking to change the type of spawner upon placement. The method above is for whenever a player breaks the mob spawner.
    I'm looking to cancel the break and drop the Mob spawner that is broken.

    If I were to add a CreatureSpawner to the method, how would I go about setting that data properly?
    #3 PCPSells, Mar 11, 2020
    Last edited: Mar 11, 2020
  4. I Used to use this but that was some time ago so I don’t know if it works or not but it should.

    Code (Text):
    ItemStack testSpawner = new ItemStack(Material.MOB_SPAWNER, 1);
    BlockStateMeta bsm = (BlockStateMeta) testSpawner.getItemMeta();
    CreatureSpawner cs = (CreatureSpawner) bsm.getBlockState();

    • Informative Informative x 1
  5. I figured it out, thank you though!! :)
Thread Status:
Not open for further replies.