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

    Error:
    Code (Text):
    [Server thread/ERROR]: Could not pass event BlockBreakEvent to ACStackedSpawners v1.0.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[server.jar:git-Spigot-7d78b81-d5e7885]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[server.jar:git-Spigot-7d78b81-d5e7885]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [server.jar:git-Spigot-7d78b81-d5e7885]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PlayerInteractManager.breakBlock(PlayerInteractManager.java:291) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PlayerInteractManager.a(PlayerInteractManager.java:124) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:828) [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$1.run(SourceFile:13) [server.jar:git-Spigot-7d78b81-d5e7885]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_191]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?: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(MinecraftServer.java:739) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [server.jar:git-Spigot-7d78b81-d5e7885]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [server.jar:git-Spigot-7d78b81-d5e7885]
        at java.lang.Thread.run(Thread.java:748) [?: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(StackedSpawner.java:73) ~[?:?]
        at me.PCPSells.ACSS.events.BreakSpawner.onStackedSpawnerBreak(BreakSpawner.java:19) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_191]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_191]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_191]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_191]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[server.jar:git-Spigot-7d78b81-d5e7885]
        ... 17 more
    Code (Java):
    @SuppressWarnings("deprecation")
        public ItemStack getSpawnerAsItemStack() {
            ItemStack item = new ItemStack(Material.MOB_SPAWNER, 1);
            ItemMeta itemMeta = item.getItemMeta();
            BlockState bs = (BlockState) itemMeta;
            CreatureSpawner cs = (CreatureSpawner) bs;
            cs.setCreatureTypeByName(getMobType());
            item.setItemMeta(itemMeta);
            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();

    cs.setSpawnedType(EntityType.BAT);
    bsm.setBlockState(cs);
    testSpawner.setItemMeta(bsm);
     
    • Informative Informative x 1
  5. I figured it out, thank you though!! :)
     
Thread Status:
Not open for further replies.