Exemple #1
0
 public static boolean addHead(Player player, String skullOwner, int quantity) {
   PlayerInventory inv = player.getInventory();
   int firstEmpty = inv.firstEmpty();
   if (firstEmpty == -1) {
     return false;
   } else {
     inv.setItem(firstEmpty, Tools.Skull(skullOwner, quantity));
     return true;
   }
 }
  /** @see Runnable.run() */
  public void run() {
    if (!War.war.isLoaded()) {
      return;
    }
    for (Warzone zone : War.war.getWarzones()) {
      for (Team team : zone.getTeams()) {
        for (Player player : team.getPlayers()) {
          PlayerInventory playerInv = player.getInventory();
          Material teamBlockMaterial;
          if (zone.getWarzoneConfig().getBoolean(WarzoneConfig.BLOCKHEADS)) {
            teamBlockMaterial = team.getKind().getMaterial();
            // 1) Replace missing block head
            if (playerInv.getHelmet().getType() != teamBlockMaterial) {
              playerInv.setHelmet(this.createBlockHead(team));
            }
            // 2) Get rid of extra blocks in inventory: only keep one
            HashMap<Integer, ? extends ItemStack> blocks = playerInv.all(teamBlockMaterial);
            if (blocks.size() > 1
                || (blocks.size() == 1
                    && blocks.get(blocks.keySet().iterator().next()).getAmount() > 1)) {
              int i = 0;
              int removed = 0;
              for (ItemStack item : playerInv.getContents()) {
                // remove only same colored wool
                if (item != null
                    && item.getType() == teamBlockMaterial
                    && item.getData().getData() == team.getKind().getData()) {
                  playerInv.clear(i);
                  removed++;
                }
                i++;
              }

              int firstEmpty = playerInv.firstEmpty();
              if (firstEmpty > 0) {
                playerInv.setItem(firstEmpty, this.createBlockHead(team));
              }

              if (removed > 1) {
                War.war.badMsg(
                    player, "All that " + team.getName() + " wool must have been heavy!");
              }
            }
          } else {
            // enfore helmet (good thing?)
            if (team.getKind() == TeamKind.GOLD) {
              teamBlockMaterial = Material.GOLD_HELMET;
            } else if (team.getKind() == TeamKind.DIAMOND) {
              teamBlockMaterial = Material.DIAMOND_HELMET;
            } else if (team.getKind() == TeamKind.IRON) {
              teamBlockMaterial = Material.IRON_HELMET;
            } else {
              teamBlockMaterial = Material.LEATHER_HELMET;
            }
            if (playerInv.getHelmet() != null
                && playerInv.getHelmet().getType() != teamBlockMaterial) {
              playerInv.setHelmet(new ItemStack(teamBlockMaterial));
            }
            HashMap<Integer, ? extends ItemStack> helmets = playerInv.all(teamBlockMaterial);
            if (helmets.size() > 1
                || (helmets.size() == 1
                    && helmets.get(helmets.keySet().iterator().next()).getAmount() > 1)) {
              playerInv.remove(teamBlockMaterial);
              playerInv.setItem(playerInv.firstEmpty(), new ItemStack(teamBlockMaterial));
              War.war.badMsg(player, "All those helmets must have been heavy!");
            }
          }

          // check for thieves without their treasure in their hands
          if (zone.isFlagThief(player.getName())) {
            Team victim = zone.getVictimTeamForFlagThief(player.getName());
            player.setItemInHand(null);
            player
                .getInventory()
                .addItem(
                    new ItemStack(
                        victim.getKind().getMaterial(),
                        2240,
                        victim.getKind().getData(),
                        victim.getKind().getData()));
          } else if (zone.isBombThief(player.getName())) {
            player.setItemInHand(null);
            player.getInventory().addItem(new ItemStack(Material.TNT, 2240));
          } else if (zone.isCakeThief(player.getName())) {
            player.setItemInHand(null);
            player.getInventory().addItem(new ItemStack(Material.CAKE_BLOCK, 2240));
          }
        }
      }
    }
  }
Exemple #3
0
  /**
   * "/command" command controller.
   *
   * @param sender Command sender
   * @param cmd Command
   * @param label Command alias
   * @param args Other parameters
   * @return boolean
   */
  @Override
  public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    String cName = cmd.getName();

    if (cName.equalsIgnoreCase("spawner")) {
      if (args.length == 0) {

        // catch console
        if (!(sender instanceof Player)) {
          Main.language.sendMessage(sender, Main.language.getText(Keys.ConsoleUsageGive));
          return true;
        }

        // get spawner type
        if (sender.hasPermission("spawner.get")) {
          Player player = (Player) sender;
          Block target = SpawnerFunctions.findSpawnerBlock(player, 20);
          if (target.getType() == Material.MOB_SPAWNER) {
            SpawnerType spawnerType = SpawnerFunctions.getSpawner(target);
            if (spawnerType == null) {
              Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
              // it's no longer a valid spawner for some reason
              // maybe no longer in valid_entities so lets break it to stop spawns
              // target.breakNaturally();
              return true;
            }
            String text = SpawnerType.getTextFromType(spawnerType);
            Main.language.sendMessage(sender, text + " " + Main.language.getText(Keys.Spawner));
            return true;

          } else {
            Main.language.sendMessage(sender, Main.language.getText(Keys.LookAtASpawner));
            return true;
          }

        } else {
          Main.language.sendMessage(sender, Main.language.getText(Keys.NoPermission));
          return true;
        }

      } else if (args.length == 1) {

        // spawner reload
        if (args[0].equalsIgnoreCase("reload") && sender.hasPermission("spawner.reload")) {
          Main.instance.reloadFiles();
          Main.instance.getLogger().info("Reloaded");
          if ((sender instanceof Player)) {
            Main.language.sendMessage(sender, ChatColor.GREEN + "Spawner has been reloaded.");
          }
          return true;
        }

        // spawner help
        if (args[0].equalsIgnoreCase("help") && sender.hasPermission("spawner.help")) {

          String contents =
              SpawnerFunctions.readFile(
                  Main.instance.getDataFolder()
                      + System.getProperty("file.separator")
                      + "help.txt");

          Main.language.sendMessage(sender, contents);
          return true;
        }

        // list entities
        if (args[0].equalsIgnoreCase("list") && sender.hasPermission("spawner.list")) {

          String contents =
              SpawnerFunctions.readFile(
                  Main.instance.getDataFolder()
                      + System.getProperty("file.separator")
                      + "list.txt");

          Main.language.sendMessage(sender, contents);
          return true;
        }

        // catch console
        if (!(sender instanceof Player)) {
          Main.language.sendMessage(sender, Main.language.getText(Keys.ConsoleUsageGive));
          return true;
        }

        // set spawner type
        if (sender.hasPermission("spawner.set.all")
            || sender.hasPermission(
                "spawner.set." + SpawnerFunctions.convertAlias(args[0]).toLowerCase())) {
          Player player = (Player) sender;
          Block target = SpawnerFunctions.findSpawnerBlock(player, 20);
          if (target.getType() == Material.MOB_SPAWNER) {
            // set type of spawner player is targeting

            // setSpawner does it's own isValidEntity
            if (SpawnerFunctions.setSpawner(target, args[0])) {
              String type = SpawnerType.getTextFromName(args[0]);
              if (type == null) {
                Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                return true;
              }
              Main.language.sendMessage(sender, Main.language.getText(Keys.SpawnerChangedTo, type));
              return true;

            } else {
              Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
              return true;
            }

          } else {
            // otherwise set type of spawner in players hand

            PlayerInventory playerInv = player.getInventory();
            ItemStack itemInMainHand = playerInv.getItemInMainHand();
            ItemStack itemInOffHand = playerInv.getItemInOffHand();

            if (itemInMainHand.getType() == Material.MOB_SPAWNER
                || itemInOffHand.getType() == Material.MOB_SPAWNER) {

              if (SpawnerFunctions.isValidEntity(args[0])) {
                SpawnerType spawnerType = SpawnerType.fromName(args[0]);

                if (spawnerType == null) {
                  Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                  return true;
                }

                String spawnerName = SpawnerType.getTextFromName(args[0]);
                if (spawnerName == null) {
                  Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                  return true;
                }

                if (itemInMainHand.getType() == Material.MOB_SPAWNER) {

                  // get a new spawner
                  Spawner newSpawner =
                      SpawnerFunctions.makeSpawner(spawnerName, itemInMainHand.getAmount());

                  // set item in players hand
                  playerInv.setItemInMainHand(newSpawner);

                } else if (itemInOffHand.getType() == Material.MOB_SPAWNER) {

                  // get a new spawner
                  Spawner newSpawner =
                      SpawnerFunctions.makeSpawner(spawnerName, itemInOffHand.getAmount());

                  // set item in players hand
                  playerInv.setItemInOffHand(newSpawner);
                }

                Main.language.sendMessage(
                    sender, Main.language.getText(Keys.SpawnerChangedTo, spawnerName));
                return true;
              } else {
                Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                return true;
              }
            } else {
              Main.language.sendMessage(sender, Main.language.getText(Keys.NotPossible));
              return true;
            }
          }
        } else {
          Main.language.sendMessage(sender, Main.language.getText(Keys.NoPermission));
        }

        return true;

      } else if (args.length == 2) {

        if (args[0].equalsIgnoreCase("give")) {
          // give a spawner

          // catch console
          if (!(sender instanceof Player)) {
            Main.language.sendMessage(sender, Main.language.getText(Keys.ConsoleUsageGive));
            return true;
          }

          if (sender.hasPermission("spawner.give.all")
              || sender.hasPermission(
                  "spawner.give." + SpawnerFunctions.convertAlias(args[1]).toLowerCase())) {
            Player player = (Player) sender;

            if (SpawnerFunctions.isValidEntity(args[1])) {
              SpawnerType spawnerType = SpawnerFunctions.getSpawnerType(args[1]);
              if (spawnerType == null) {
                Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                return true;
              }

              String spawnerName = SpawnerType.getTextFromName(args[1]);
              if (spawnerName == null) {
                Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                return true;
              }

              // get a new spawner
              Spawner newSpawner = SpawnerFunctions.makeSpawner(spawnerName);

              // drop spawner at player location or add it to their inv if they have space
              PlayerInventory inventory = player.getInventory();
              if (inventory.firstEmpty() == -1) {
                player.getWorld().dropItem(player.getLocation().add(0, 1, 0), newSpawner);
              } else {
                int invSlot = inventory.firstEmpty();
                inventory.setItem(invSlot, newSpawner);

                // make sure to show the player the item
                player.updateInventory();
              }

              Main.language.sendMessage(
                  sender, Main.language.getText(Keys.GivenSpawner, spawnerName));
              return true;
            } else {
              Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
              return true;
            }
          } else {
            Main.language.sendMessage(sender, Main.language.getText(Keys.NoPermission));
          }

          return true;

        } else if (args[0].equalsIgnoreCase("remove")) {

          // /spawner remove <entity>
          if (sender instanceof Player && sender.hasPermission("spawner.remove")) {
            if (SpawnerFunctions.isValidEntity(args[1])) {
              int radius = Main.instance.getSpawnerConfig().getInt("remove_radius");
              SpawnerFunctions.removeEntities((Player) sender, args[1].toLowerCase(), radius);
            } else {
              Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
              return true;
            }
          }
        }

      } else if (args.length == 3) {

        if (args[0].equalsIgnoreCase("give")) {

          // amount parameter for give to self
          if (StringUtils.isNumeric(args[2])) {
            // /spawner give <entity> <amount>

            // catch console
            if (!(sender instanceof Player)) {
              Main.language.sendMessage(sender, Main.language.getText(Keys.ConsoleUsageGive));
              return true;
            }
            Player player = (Player) sender;

            if (SpawnerFunctions.isValidEntity(args[1])) {
              SpawnerType spawnerType = SpawnerFunctions.getSpawnerType(args[1]);
              if (spawnerType == null) {
                Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                return true;
              }

              String spawnerName = SpawnerType.getTextFromName(args[1]);
              if (spawnerName == null) {
                Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                return true;
              }

              // get a new spawner
              Spawner newSpawner =
                  SpawnerFunctions.makeSpawner(spawnerName, Integer.parseInt(args[2]));

              // drop spawner at player location or add it to their inv if they have space
              PlayerInventory inventory = player.getInventory();
              if (inventory.firstEmpty() == -1) {
                player.getWorld().dropItem(player.getLocation().add(0, 1, 0), newSpawner);
              } else {
                int invSlot = inventory.firstEmpty();
                inventory.setItem(invSlot, newSpawner);

                // make sure to show the player the item
                player.updateInventory();
              }

              Main.language.sendMessage(
                  sender, Main.language.getText(Keys.GivenSpawner, spawnerName));
              return true;
            } else {
              Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
              return true;
            }
          }

          // give a spawner to others
          if (sender.hasPermission("spawner.giveothers.all")
              || sender.hasPermission(
                  "spawner.giveothers." + SpawnerFunctions.convertAlias(args[1]).toLowerCase())) {
            // give a spawner
            // /spawner give <entity> <player>

            if (SpawnerFunctions.isValidEntity(args[1])) {
              SpawnerType spawnerType = SpawnerFunctions.getSpawnerType(args[1]);
              if (spawnerType == null) {
                Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                return true;
              }

              String spawnerName = SpawnerType.getTextFromName(args[1]);
              if (spawnerName == null) {
                Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                return true;
              }

              // get a new spawner
              Spawner newSpawner = SpawnerFunctions.makeSpawner(spawnerName);
              Player targetPlayer = Main.instance.getServer().getPlayer(args[2]);
              if (targetPlayer != null) {

                PlayerInventory inventory = targetPlayer.getInventory();
                int invSlot = inventory.firstEmpty();

                // drop spawner at player location or add it to their inv if they have space
                if (invSlot == -1) {

                  // if target player is online drop it at their feet and tell them
                  targetPlayer
                      .getWorld()
                      .dropItem(targetPlayer.getLocation().add(0, 1, 0), newSpawner);
                  Main.language.sendMessage(
                      targetPlayer, Main.language.getText(Keys.SpawnerDropped, spawnerName));
                  return true;

                } else {

                  inventory.setItem(invSlot, newSpawner);

                  // make sure to show the player the item
                  targetPlayer.updateInventory();

                  if (targetPlayer != null) {
                    Main.language.sendMessage(
                        targetPlayer, Main.language.getText(Keys.GivenSpawner, spawnerName));
                  } else {
                    Main.language.sendMessage(
                        sender, Main.language.getText(Keys.NotDeliveredOffline, args[2]));
                    return true;
                  }

                  String[] vars = new String[2];
                  vars[0] = spawnerName;
                  vars[1] = targetPlayer.getName();
                  Main.language.sendMessage(
                      sender, Main.language.getText(Keys.YouGaveSpawner, vars));
                }

                return true;

              } else {
                // tell sender the target didn't get the spawner
                Main.language.sendMessage(
                    sender, Main.language.getText(Keys.NotDeliveredOffline, args[2]));
                return true;
              }
            } else {
              Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
              return true;
            }

          } else {
            Main.language.sendMessage(sender, Main.language.getText(Keys.NoPermission));
          }

          return true;

        } else if (args[0].equalsIgnoreCase("remove")) {

          // /spawner remove <entity> <radius>
          if (sender instanceof Player && sender.hasPermission("spawner.remove")) {
            if (SpawnerFunctions.isValidEntity(args[1])) {
              int radius;
              try {
                radius = Integer.parseInt(args[2]);
              } catch (NumberFormatException e) {
                Main.language.sendMessage(
                    sender, ChatColor.RED + Main.language.getText(Keys.InvalidRadius));
                return false;
              }

              SpawnerFunctions.removeEntities((Player) sender, args[1].toLowerCase(), radius);
            } else {
              Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
              return true;
            }
          }
        }

      } else if (args.length == 4) {
        // give a spawner to others

        if (args[0].equalsIgnoreCase("give")) {

          // amount parameter for give to others
          if (StringUtils.isNumeric(args[3])) {
            if (sender.hasPermission("spawner.giveothers.all")
                || sender.hasPermission(
                    "spawner.giveothers." + SpawnerFunctions.convertAlias(args[1]).toLowerCase())) {
              // give a spawner
              // /spawner give <entity> <player> <amount>

              // amount parameter for give to self
              if (StringUtils.isNumeric(args[3])) {

                if (SpawnerFunctions.isValidEntity(args[1])) {
                  SpawnerType spawnerType = SpawnerFunctions.getSpawnerType(args[1]);
                  if (spawnerType == null) {
                    Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                    return true;
                  }

                  String spawnerName = SpawnerType.getTextFromName(args[1]);
                  if (spawnerName == null) {
                    Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                    return true;
                  }

                  // get a new spawner
                  Spawner newSpawner =
                      SpawnerFunctions.makeSpawner(spawnerName, Integer.parseInt(args[3]));

                  // set name
                  Player targetPlayer = Main.instance.getServer().getPlayer(args[2]);
                  if (targetPlayer != null) {

                    PlayerInventory inventory = targetPlayer.getInventory();
                    int invSlot = inventory.firstEmpty();

                    // drop spawner at player location or add it to their inv if they have space
                    if (invSlot == -1) {

                      // if target player is online drop it at their feet and tell them
                      targetPlayer
                          .getWorld()
                          .dropItem(targetPlayer.getLocation().add(0, 1, 0), newSpawner);
                      Main.language.sendMessage(
                          targetPlayer, Main.language.getText(Keys.SpawnerDropped, spawnerName));
                      return true;

                    } else {

                      inventory.setItem(invSlot, newSpawner);

                      // make sure to show the player the item
                      targetPlayer.updateInventory();

                      if (targetPlayer != null) {
                        Main.language.sendMessage(
                            targetPlayer, Main.language.getText(Keys.GivenSpawner, spawnerName));
                      } else {
                        Main.language.sendMessage(
                            sender, Main.language.getText(Keys.NotDeliveredOffline, args[2]));
                        return true;
                      }

                      String[] vars = new String[2];
                      vars[0] = spawnerName;
                      vars[1] = targetPlayer.getName();
                      Main.language.sendMessage(
                          sender, Main.language.getText(Keys.YouGaveSpawner, vars));
                    }

                    return true;

                  } else {
                    // tell sender the target didn't get the spawner
                    Main.language.sendMessage(
                        sender, Main.language.getText(Keys.NotDeliveredOffline, args[2]));
                    return true;
                  }
                } else {
                  Main.language.sendMessage(sender, Main.language.getText(Keys.InvalidSpawner));
                  return true;
                }
              }
            }
          }
        }
      } else {
        Main.language.sendMessage(sender, Main.language.getText(Keys.NoPermission));
      }
    }

    return true;
  }