示例#1
0
public class SpectatorListener implements Listener {

  private UserManager um = SurvivalGames.getUserManager();

  @EventHandler
  public void onPlayerInteractEvent(PlayerInteractEvent event) {
    if (um.isSpectator(event.getPlayer().getName())) event.setCancelled(true);
  }

  @EventHandler
  public void onBlockBreak(BlockBreakEvent event) {
    if (um.isSpectator(event.getPlayer().getName())) event.setCancelled(true);
  }

  @EventHandler
  public void onBlockPlace(BlockPlaceEvent event) {
    if (um.isSpectator(event.getPlayer().getName())) event.setCancelled(true);
  }

  @EventHandler
  public void onItemPickup(PlayerPickupItemEvent event) {
    if (um.isSpectator(event.getPlayer().getName())) event.setCancelled(true);
  }

  @EventHandler
  public void onItemDrop(PlayerDropItemEvent event) {
    if (um.isSpectator(event.getPlayer().getName())) event.setCancelled(true);
  }

  @EventHandler
  public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
    if (um.isSpectator(event.getPlayer().getName())) event.setCancelled(true);
  }

  @EventHandler
  public void onEntityTarget(EntityTargetEvent event) {
    if (event.getTarget() instanceof Player) {
      Player p = (Player) event.getTarget();
      if (um.isSpectator(p.getName())) event.setCancelled(true);
    }
  }

  @EventHandler
  public void onExpChangeEvent(PlayerExpChangeEvent event) {
    if (um.isSpectator(event.getPlayer().getName())) event.setAmount(0);
  }

  @SuppressWarnings("deprecation")
  @EventHandler
  public void onEntityDamage(EntityDamageByEntityEvent event) {
    Player damager = null;
    if (event.getDamager() instanceof Player) {
      damager = (Player) event.getDamager();
    } else if (event.getDamager() instanceof Projectile) {
      Projectile pro = (Projectile) event.getDamager();
      if (pro.getShooter() instanceof Player) {
        damager = (Player) pro.getShooter();
      }
    }

    if (damager != null) {
      if (um.isSpectator(damager.getName())) event.setCancelled(true);
    }
  }

  @EventHandler
  public void onEntityDamage(EntityDamageEvent event) {
    if (event.getEntity() instanceof Player) {
      Player p = (Player) event.getEntity();
      if (um.isSpectator(p.getName())) event.setCancelled(true);
    }
  }

  @EventHandler
  public void onFoodLevelChange(FoodLevelChangeEvent event) {
    if (event.getEntity() instanceof Player) {
      Player p = (Player) event.getEntity();
      if (um.isSpectator(p.getName())) event.setCancelled(true);
    }
  }
}
 public void load() {
   game.setScoreboardPhase(
       SurvivalGames.getScoreboardManager().getNewScoreboardPhase(GameState.DEATHMATCH));
   start();
 }