Example #1
0
  public void leave(SpleefPlayer player) {
    SpectateLeaveEvent event = new SpectateLeaveEvent(game, player);
    game.getEventBus().callEvent(event);

    PlayerStateHolder state = player.getPlayerState(this);
    if (state != null) {
      state.apply(player.getBukkitPlayer(), true);
      player.removePlayerState(this);
    } else {
      // Ugh, something went wrong
      player.sendMessage(getI18N().getString(Messages.Player.ERROR_ON_INVENTORY_LOAD));
    }

    spectators.remove(player);
  }
Example #2
0
  public boolean spectate(SpleefPlayer player, Game game) {
    SpectateEnterEvent enterEvent = new SpectateEnterEvent(game, player);
    game.getEventBus().callEvent(enterEvent);

    if (enterEvent.isCancelled()) {
      return false;
    }

    final Player bukkitPlayer = player.getBukkitPlayer();

    GameMode gameMode = bukkitPlayer.getGameMode();
    bukkitPlayer.setGameMode(GameMode.SURVIVAL);
    player.savePlayerState(this, gameMode);
    PlayerStateHolder.applyDefaultState(bukkitPlayer);

    spectators.add(player);

    Bukkit.getScheduler()
        .runTask(
            game.getHeavySpleef().getPlugin(),
            new Runnable() {

              @Override
              public void run() {
                MaterialData data = config.getFlagSection().getLeaveItem();
                MetadatableItemStack stack = new MetadatableItemStack(data.toItemStack(1));
                ItemMeta meta = stack.getItemMeta();
                meta.setDisplayName(
                    getI18N().getString(Messages.Player.LEAVE_SPECTATE_DISPLAYNAME));
                meta.setLore(
                    Lists.newArrayList(getI18N().getString(Messages.Player.LEAVE_SPECTATE_LORE)));
                stack.setItemMeta(meta);

                stack.setMetadata(LEAVE_ITEM_KEY, null);

                bukkitPlayer.getInventory().setItem(RIGHT_HOTBAR_SLOT, stack);
                bukkitPlayer.updateInventory();
              }
            });

    player.teleport(getValue());

    SpectateEnteredEvent enteredEvent = new SpectateEnteredEvent(game, player);
    game.getEventBus().callEvent(enteredEvent);
    return true;
  }