/** Load the server configuration. */
  private void loadConfig() {
    config.load();

    // modifiable values
    spawnRadius = config.getInt(ServerConfig.Key.SPAWN_RADIUS);
    whitelistEnabled = config.getBoolean(ServerConfig.Key.WHITELIST);
    idleTimeout = config.getInt(ServerConfig.Key.PLAYER_IDLE_TIMEOUT);
    craftingManager.initialize();

    // special handling
    warnState = Warning.WarningState.value(config.getString(ServerConfig.Key.WARNING_STATE));
    try {
      defaultGameMode = GameMode.valueOf(config.getString(ServerConfig.Key.GAMEMODE));
    } catch (IllegalArgumentException | NullPointerException e) {
      defaultGameMode = GameMode.SURVIVAL;
    }

    // server icon
    defaultIcon = new GlowServerIcon();
    try {
      File file = config.getFile("server-icon.png");
      if (file.isFile()) {
        defaultIcon = new GlowServerIcon(file);
      }
    } catch (Exception e) {
      logger.log(Level.WARNING, "Failed to load server-icon.png", e);
    }
  }
Beispiel #2
0
 @Override
 public GameMode unmarshal(Object o) {
   GameMode gm = null;
   if (o != null) {
     gm = GameMode.valueOf((String) o);
   }
   return gm;
 }
Beispiel #3
0
 public GameMode setDefaultGameMode(String mode) {
   GameMode gameMode;
   try {
     gameMode = GameMode.valueOf(mode);
   } catch (Throwable t) {
     logger.severe("Unknown game mode specified. Defaulting to survival");
     gameMode = GameMode.SURVIVAL;
   }
   setDefaultGameMode(gameMode);
   return getDefaultGameMode();
 }
 @Override
 public boolean parseValue(String value) {
   try {
     return this.setValue(GameMode.getByValue(Integer.parseInt(value)));
   } catch (NumberFormatException nfe) {
     try {
       return this.setValue(GameMode.valueOf(value.toUpperCase()));
     } catch (Exception e) {
       return false;
     }
   }
 }
 private GameMode verifyGameMode(String string) {
   GameMode gm = null;
   try {
     gm = GameMode.valueOf(string);
   } catch (Exception e) {
     /* ignore */
   }
   if (gm == null) {
     try {
       gm = GameMode.getByValue(Integer.valueOf(string));
     } catch (Exception e) {
       /* do nothing */
     }
     if (gm == null) {
       throw new IllegalArgumentException("&cGamemode " + string + " not found");
     }
   }
   return gm;
 }
 public Object parseValue(String value) throws Exception {
   /// Handle values for this option
   switch (this) {
     case HEALTHP:
     case HEALTH:
     case POOLMONEY:
     case WITHINDISTANCE:
     case MONEY:
       return Double.valueOf(value);
     case LEVELRANGE:
       return MinMax.valueOf(value);
     case DISGUISEALLAS:
       return value;
     case MAGIC:
     case MAGICP:
     case HUNGER:
     case EXPERIENCE:
     case INVULNERABLE:
       return Integer.valueOf(value);
     case FLIGHTSPEED:
       return Float.valueOf(value);
     case ENCHANTS:
       return EffectUtil.parseArg(value, 0, 120);
     case DOCOMMANDS:
       return value;
     case GIVECLASS:
       return ArenaClassController.getClass(value);
     case GAMEMODE:
       GameMode gm;
       try {
         gm = GameMode.getByValue(Integer.valueOf(value));
       } catch (Throwable e) {
         gm = GameMode.valueOf(value.toUpperCase());
       }
       return gm; // multiply by number of ticks per second
     default:
       break;
   }
   return null;
 }
  @Override
  public void executeTyped(Listener listener, PlayerChangedWorldEvent event) throws EventException {

    boolean saveRequired = false;
    String fromName = event.getFrom().getName();
    String toName = event.getPlayer().getWorld().getName();

    logger.finer("Player change from " + fromName + " to " + toName);

    try {

      // Save old game mode if required
      String fromWorldMode = plugin.getConfig().getString("worlds." + fromName + ".mode");
      if (fromWorldMode != null) {
        ModeOptions fromMode = ModeOptions.valueOf(fromWorldMode);
        if (fromMode == ModeOptions.REMEMBER) {
          GameMode fromGameMode =
              GameMode.valueOf(plugin.getConfig().getString("worlds." + fromName + ".gamemode"));
          String rememberWorldString =
              plugin
                  .getConfig()
                  .getString(
                      "players." + event.getPlayer().getUniqueId().toString() + "." + fromName);
          { // transition code
            String rememberWorldStringOld =
                plugin
                    .getConfig()
                    .getString("players." + event.getPlayer().getName() + "." + fromName);
            if (rememberWorldStringOld != null) {
              rememberWorldString = rememberWorldStringOld;
              plugin
                  .getConfig()
                  .set(
                      "players." + event.getPlayer().getUniqueId().toString() + "." + fromName,
                      rememberWorldString);
              plugin
                  .getConfig()
                  .set("players." + event.getPlayer().getName() + "." + fromName, null);
              saveRequired = true;
            }
          }
          if (event.getPlayer().getGameMode() != fromGameMode) {
            if (rememberWorldString == null
                || GameMode.valueOf(rememberWorldString) != event.getPlayer().getGameMode()) {
              logger.info("The player has a gamemode different to default. Saving");
              plugin
                  .getConfig()
                  .set(
                      "players." + event.getPlayer().getUniqueId().toString() + "." + fromName,
                      event.getPlayer().getGameMode().toString());
              saveRequired = true;
            }
          }
          if (rememberWorldString != null && event.getPlayer().getGameMode() == fromGameMode) {
            logger.info("The player used to have a unique gamemode but now it is the default");
            plugin.getConfig().set("players." + event.getPlayer().getName(), null);
            saveRequired = true;
          }
        }
      }

      // Change to new gamemode
      String newWorldMode = plugin.getConfig().getString("worlds." + toName + ".mode");
      if (newWorldMode != null) {
        ModeOptions toMode = ModeOptions.valueOf(newWorldMode);
        GameMode toGameMode =
            GameMode.valueOf(plugin.getConfig().getString("worlds." + toName + ".gamemode"));

        if (toMode == ModeOptions.REMEMBER) {
          String rememberWorldString =
              plugin
                  .getConfig()
                  .getString(
                      "players." + event.getPlayer().getUniqueId().toString() + "." + toName);
          { // transition code
            String rememberWorldStringOld =
                plugin
                    .getConfig()
                    .getString("players." + event.getPlayer().getName() + "." + toName);
            if (rememberWorldStringOld != null) {
              rememberWorldString = rememberWorldStringOld;
              plugin
                  .getConfig()
                  .set(
                      "players." + event.getPlayer().getUniqueId().toString() + "." + toName,
                      rememberWorldString);
              plugin.getConfig().set("players." + event.getPlayer().getName() + "." + toName, null);
              saveRequired = true;
            }
          }
          if (rememberWorldString != null) {
            toGameMode = GameMode.valueOf(rememberWorldString);
          }
        }
        if (event.getPlayer().getGameMode() != toGameMode) {
          logger.finer("Changing game mode to " + toGameMode);
          event.getPlayer().setGameMode(toGameMode);
        }
      } else {
        logger.fine("There is no setting for " + toName);
      }
    } finally {
      if (saveRequired) {
        plugin.saveConfig();
      }
    }
  }
  @EventHandler
  public void onPlayerInteract(PlayerInteractEvent pie) {
    Player player = pie.getPlayer();
    Game g = Main.getInstance().getGameManager().getGameOfPlayer(player);

    if (g == null) {
      if (pie.getAction() != Action.RIGHT_CLICK_BLOCK
          && pie.getAction() != Action.RIGHT_CLICK_AIR) {
        return;
      }

      Block clicked = pie.getClickedBlock();

      if (clicked == null) {
        return;
      }

      if (!(clicked.getState() instanceof Sign)) {
        return;
      }

      Game game = Main.getInstance().getGameManager().getGameBySignLocation(clicked.getLocation());
      if (game == null) {
        return;
      }

      if (game.playerJoins(player)) {
        player.sendMessage(ChatWriter.pluginMessage(ChatColor.GREEN + Main._l("success.joined")));
      }
      return;
    }

    if (g.getState() == GameState.STOPPED) {
      return;
    }

    Material interactingMaterial = pie.getMaterial();
    Block clickedBlock = pie.getClickedBlock();

    if (g.getState() == GameState.RUNNING) {
      if (pie.getAction() == Action.PHYSICAL) {
        if (clickedBlock != null
            && (clickedBlock.getType() == Material.WHEAT
                || clickedBlock.getType() == Material.SOIL)) {
          pie.setCancelled(true);
          return;
        }
      }

      if (pie.getAction() != Action.RIGHT_CLICK_BLOCK
          && pie.getAction() != Action.RIGHT_CLICK_AIR) {
        return;
      }

      if (clickedBlock != null) {
        if (clickedBlock.getType() == Material.LEVER
            && !g.isSpectator(player)
            && pie.getAction() == Action.RIGHT_CLICK_BLOCK) {
          if (!g.getRegion().isPlacedUnbreakableBlock(clickedBlock)) {
            g.getRegion().addPlacedUnbreakableBlock(clickedBlock, clickedBlock.getState());
          }
          return;
        }
      }

      if (g.isSpectator(player)) {
        if (interactingMaterial == Material.SLIME_BALL) {
          g.playerLeave(player, false);
          return;
        }

        if (interactingMaterial == Material.COMPASS) {
          g.openSpectatorCompass(player);
          pie.setCancelled(true);
          return;
        }
      }

      // Spectators want to block
      if (clickedBlock != null) {
        try {
          GameMode.valueOf("SPECTATOR");
        } catch (Exception ex) {
          for (Player p : g.getFreePlayers()) {
            if (!g.getRegion().isInRegion(p.getLocation())) {
              continue;
            }

            if (pie.getClickedBlock().getLocation().distance(p.getLocation()) < 2) {
              Location oldLocation = p.getLocation();
              if (oldLocation.getY() >= pie.getClickedBlock().getLocation().getY()) {
                oldLocation.setY(oldLocation.getY() + 2);
              } else {
                oldLocation.setY(oldLocation.getY() - 2);
              }

              p.teleport(oldLocation);
            }
          }
        }
      }

      if (clickedBlock != null) {
        if (clickedBlock.getType() == Material.ENDER_CHEST && !g.isSpectator(player)) {
          pie.setCancelled(true);

          Block chest = pie.getClickedBlock();
          Team chestTeam = g.getTeamOfEnderChest(chest);
          Team playerTeam = g.getPlayerTeam(player);

          if (chestTeam == null) {
            return;
          }

          if (chestTeam.equals(playerTeam)) {
            player.openInventory(chestTeam.getInventory());
          } else {
            player.sendMessage(
                ChatWriter.pluginMessage(ChatColor.RED + Main._l("ingame.noturteamchest")));
          }

          return;
        }
      }

      return;
    } else if (g.getState() == GameState.WAITING) {
      if (interactingMaterial == null) {
        pie.setCancelled(true);
        return;
      }

      if (pie.getAction() == Action.PHYSICAL) {
        if (clickedBlock != null
            && (clickedBlock.getType() == Material.WHEAT
                || clickedBlock.getType() == Material.SOIL)) {
          pie.setCancelled(true);
          return;
        }
      }

      if (pie.getAction() != Action.RIGHT_CLICK_BLOCK
          && pie.getAction() != Action.RIGHT_CLICK_AIR) {
        return;
      }

      switch (interactingMaterial) {
        case BED:
          pie.setCancelled(true);
          if (!g.isAutobalanceEnabled()) {
            g.getPlayerStorage(player).openTeamSelection(g);
          }

          break;
        case DIAMOND:
          pie.setCancelled(true);
          if (player.isOp() || player.hasPermission("bw.setup")) {
            g.start(player);
          } else if (player.hasPermission("bw.vip.forcestart")) {
            GameLobbyCountdownRule rule = Main.getInstance().getLobbyCountdownRule();
            if (rule.isRuleMet(g)) {
              g.start(player);
            } else {
              if (rule == GameLobbyCountdownRule.PLAYERS_IN_GAME
                  || rule == GameLobbyCountdownRule.ENOUGH_TEAMS_AND_PLAYERS) {
                player.sendMessage(
                    ChatWriter.pluginMessage(
                        ChatColor.RED + Main._l("lobby.notenoughplayers-rule0")));
              } else {
                player.sendMessage(
                    ChatWriter.pluginMessage(
                        ChatColor.RED + Main._l("lobby.notenoughplayers-rule1")));
              }
            }
          }
          break;
        case SLIME_BALL:
          pie.setCancelled(true);
          g.playerLeave(player, false);
          break;
        case LEATHER_CHESTPLATE:
          pie.setCancelled(true);
          player.updateInventory();
          break;
        default:
          break;
      }
    }
  }