示例#1
0
  @Override
  public boolean fromPlayer(
      General plugin, Player sender, Command command, String commandLabel, String[] args) {
    if (Toolbox.lacksPermission(plugin, sender, "give items", "general.give")) return true;
    if (args.length < 1 || args[0].equalsIgnoreCase("help")) return SHOW_USAGE;

    who = sender;
    item = null;
    amount = 1;

    switch (args.length) {
      case 1: // /give <item>[:<data>]
        item = Items.validate(args[0]);
        break;
      case 2: // /give <item>[:<data>] <amount> OR /give <item>[:<data>] <player>
        item = Items.validate(args[0]);
        try {
          who = sender;
          amount = Integer.valueOf(args[1]);
        } catch (NumberFormatException x) {
          who = Toolbox.playerMatch(args[1]);
          if (who == null) {
            Messaging.send(sender, "&rose;The amount must be an integer.");
            Messaging.send(sender, "&rose;There is no player named &f" + args[1] + "&rose;.");
            return true;
          }
        }
        break;
      case 3: // /give <item>[:<data>] <amount> <player> OR /give <player> <item>[:<data>] <amount>
        try {
          amount = Integer.valueOf(args[2]);
          who = Toolbox.getPlayer(args[0], sender);
          if (who == null) return true;
          item = Items.validate(args[1]);
        } catch (NumberFormatException ex) {
          who = Toolbox.getPlayer(args[2], sender);
          if (who == null) return true;
          item = Items.validate(args[0]);
          try {
            amount = Integer.valueOf(args[1]);
          } catch (NumberFormatException x) {
            Messaging.send(sender, "&rose;The amount must be an integer.");
            return true;
          }
        }
        break;
      default:
        return SHOW_USAGE;
    }

    if (item == null || !item.isIdValid()) {
      Messaging.send(sender, "&rose;Invalid item.");
      return true;
    }

    if (!item.isDataValid()) {
      Messaging.send(
          sender,
          "&f"
              + item.getVariant()
              + "&rose; is not a valid data type for &f"
              + Items.name(item)
              + "&rose;.");
      return true;
    }

    if (amount < 0
        && Toolbox.lacksPermission(
            plugin, sender, "give infinite stacks of items", "general.give.infinite")) return true;
    // Make sure this player is allowed this particular item
    if (!canGetItem(sender)) {
      Messaging.send(sender, "&2You're not allowed to get &f" + Items.name(item) + "&2.");
      return true;
    }

    boolean isGift = !who.getName().equals(sender.getName());
    doGive(isGift);
    if (isGift) {
      Messaging.send(
          sender,
          "&2Gave &f"
              + (amount < 0 ? "infinite" : amount)
              + "&2 of &f"
              + Items.name(item)
              + "&2 to &f"
              + who.getName()
              + "&2!");
    }

    return true;
  }
示例#2
0
  @Override
  public boolean fromConsole(
      General plugin, CommandSender sender, Command command, String commandLabel, String[] args) {
    if (args.length < 1 || args[0].equalsIgnoreCase("help")) return SHOW_USAGE;

    who = null;
    item = null;
    amount = 1;

    switch (args.length) {
      case 2: // give <item>[:<data>] <player>
        who = Toolbox.getPlayer(args[1], sender);
        if (who == null) return true;
        item = Items.validate(args[0]);
        break;
      case 3: // give <item>[:<data>] <amount> <player> OR give <player> <item>[:<data>] <amount>
        try {
          amount = Integer.valueOf(args[2]);
          who = Toolbox.getPlayer(args[0], sender);
          if (who == null) return true;
          item = Items.validate(args[1]);
        } catch (NumberFormatException ex) {
          who = Toolbox.getPlayer(args[2], sender);
          if (who == null) return true;
          item = Items.validate(args[0]);
          try {
            amount = Integer.valueOf(args[1]);
          } catch (NumberFormatException x) {
            Messaging.send(sender, "&rose;The amount must be an integer.");
            return true;
          }
        }
        break;
      default:
        return SHOW_USAGE;
    }

    if (item == null || !item.isIdValid()) {
      Messaging.send(sender, "&rose;Invalid item.");
      return true;
    }

    if (!item.isDataValid()) {
      Messaging.send(
          sender,
          "&f"
              + item.getVariant()
              + "&rose; is not a valid data type for &f"
              + Items.name(item)
              + "&rose;.");
      return true;
    }

    doGive(true);
    Messaging.send(
        sender,
        "&2Gave &f"
            + (amount < 0 ? "infinite" : amount)
            + "&2 of &f"
            + Items.name(item)
            + "&2 to &f"
            + who.getName()
            + "&2!");

    return true;
  }