How to edit properties of a Mob-Spawner?

Discussion in 'Plugin Development' started by benyji, May 26, 2020.

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

    benyji

    Whats good gamers,

    Im trying to alter properties of a spawner when the user right clicks on it. How do I access mob spawner properties? and is it possible I can make mob spawners spawn multiple mobs in one spawner? Honestly I don't know how to approach this.

    I want to get the spawner through bukkit's 'Block' utilitiy through a seperate method, is making the method a 'public Block' versus 'public void' achieving this? Thanks.
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public Block onBlockClick(PlayerInteractEvent event){
            Player plr = event.getPlayer();
            Block block = event.getClickedBlock(); // Define the block as the clicked block
            if(event.getAction() == Action.RIGHT_CLICK_BLOCK){ // Did they right click?
                if(block.getType() == Material.MOB_SPAWNER){ // Is the block a mob spawner?
                    if (plr.hasPermission("MultiSpawner.use")) { // Do they have perms to see the inventory?
                        applyGUI((Player) event.getPlayer()); // Apply the GUI to the player (Calling a method)
                    }
                }
            }
            return block;
        }
    Edit:
    You may be wondering, why would I need to return a block well,
    I wanted to access and change the spawner properties within the method as shown below, returning a block was the best way I could think of doing it, but I'm not even sure if it would work because I don't know how to access the spawner properties.
    Code:
      @EventHandler
        public void preventUIEdits(InventoryClickEvent event){ // EVENT HANDLER: ON PLAYER GUI CLICK
            Player player = (Player) event.getWhoClicked(); // Define the player who is using GUI
            if(player.getOpenInventory().getTopInventory().getTitle().equalsIgnoreCase(ChatColor.DARK_BLUE + "" + ChatColor.BOLD + "MultiSpawner")) // Identify GUI by title
            {
                if (event.getCurrentItem() != null) {
                    event.setCancelled(true); // Don't let them edit the GUI
                    event.getCurrentItem().getItemMeta();
                    // This -> (SpawnEggMeta) meta <- is called a cast. I assume you're going to learn what this is
                    // or you already know it
                    SpawnEggMeta meta = (SpawnEggMeta) event.getCurrentItem().getItemMeta();
                    switch  (meta.getSpawnedType()) {
                        case CREEPER:
                            selectedMobs.put(player.getName(), "creeper");
                           
     
  2. Offline

    Strahan

    You get a Block's BlockState then check if it is an instanceof CreatureSpawner. If so, cast it then you have access to the properties. It doesn't support spawning multiple mobs, you'd have to do some custom coding to make that happen. Extend the class and implement your own spawning code.
     
  3. For the multiple mob types, you could check the CreatureSpawnEvent, and given a certain percentage (say you want the spawner to be 50% sheep 50% cow) cancel the event, and spawn in your own mob.
     
  4. Offline

    Machine Maker

    @Strahan I think spawners do actually support more than 1 mob type. the Spawner tile entity has a SpawnPotentials NBT tag that accepts a weighted list of entities.

    @benyji You would ofc have to get into some NMS code to do this, but it should be possible.
     
Thread Status:
Not open for further replies.

Share This Page