Solved Spawner.setSpawnedType Broken?

Discussion in 'Spigot Plugin Development' started by MissingPotato, Sep 15, 2020.

  1. Hey! I have been trying for the past 2 hours to set the spawner's type without using NBT data, but seems like CreatureSpawner is completely broken?

    The following code does literally nothing, it's not clean since I'm trying to figure out how to do it properly, but can't seem to manage it...

    Delays do NOT work
    Block Updates do NOT work ( Forced or not )

    Anyone have any idea?

    Code (Java):
    @EventHandler
        public void onPlace(BlockPlaceEvent e) {
            Player p = e.getPlayer();
            ItemStack hand = p.getInventory().getItemInMainHand();

            if ( hand != null ) {


                    // Grab the spawner
                    Block block = e.getBlockPlaced();

                    Main.me.getServer().getScheduler().scheduleSyncDelayedTask(Main.me, new Runnable() {
                        @Override
                        public void run() {
                            CreatureSpawner spawner = (CreatureSpawner) e.getBlockPlaced().getLocation().getBlock().getState();
                            // Set the spawner type
                            spawner.setSpawnedType(EntityType.ZOMBIE);
                            e.getBlockPlaced().getState().update();
                            Bukkit.getServer().broadcastMessage(((CreatureSpawner) e.getBlockPlaced().getState()).getSpawnedType().toString() );
                        }
                    }, 40L);


                    // Spawn in the minecart
                    ArmorStand stand = (ArmorStand) block.getWorld().spawnEntity(block.getLocation().clone().add(new Vector(.5f, 0, .5f)), EntityType.ARMOR_STAND);

                    stand.setCustomName(this.title);
                    stand.setCustomNameVisible(true);
                    stand.setAI(false);
                    stand.setCollidable(false);
                    stand.setGravity(false);

                    stand.setVisible(true);


                    // Now add persistent data to detect the Spawner down the line
                    PersistentDataContainer container = stand.getPersistentDataContainer();
                    // Set the identifier
                    container.set(BaseItem.getNameSpace("entity_spawner_identifier"), PersistentDataType.STRING, this.GetName(true));

            }
        }
     
  2. You're doing it wrong.
    Call spawner.update() instead
     
  3. I do...
    Code (Java):
    spawner.setSpawnedType(EntityType.ZOMBIE);
    e.getBlockPlaced().getState().update();
     
  4. You clearly don't.
     
  5. Oh wow never mind, seems like you HAVE to use the spawner.update();

    Thanks.
     
  6. It's right there "e.getBlockPlaced().getState().update();"
    I was just not using the spawner.update()

    I was getting the state of the block again from the event. That is wrong, you need to use spawner.update()

    on spawner.update() it actually applies the NBT Data.
     
  7. It is called a state because you get a snapshot of that block.
    After modifying the state you need to apply the modified changes.

    With getState you only get the previous state again.
     
  8. Thanks, I am indeed stupid.