Untitled

mail@pastecode.io avatar
unknown
java
a month ago
1.3 kB
3
Indexable
Never
package org.alexdev.alexandria.listeners;

import java.util.List;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;

public class BlockListener implements Listener {
  @EventHandler
  public void onBlockBreakEvent(BlockBreakEvent event) {
    Block block = event.getBlock();
    if (!isBreakableSpawner(block)) {
      event.setCancelled(true);
      return;
    } 
  }
  
  @EventHandler
  public void onBlockExplodeEvent(BlockExplodeEvent event) {
    List<Block> blocks = event.blockList();
    for (Block block : blocks) {
      if (!isBreakableSpawner(block)) {
        event.setCancelled(true);
        return;
      } 
    } 
  }
  
  private boolean isBreakableSpawner(Block block) {
    if (block.getType() != Material.SPAWNER)
      return true; 
    if (!(block.getState() instanceof CreatureSpawner))
      return true; 
    CreatureSpawner spawner = (CreatureSpawner)block.getState();
    if (spawner.getSpawnedType() == EntityType.SILVERFISH)
      return true; 
    return false;
  }
}
Leave a Comment