Grasscutter/src/main/java/emu/grasscutter/command/commands/GiveCommand.java

206 lines
8.8 KiB
Java
Raw Normal View History

2022-04-21 06:48:24 +08:00
package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GameData;
2022-04-21 06:48:24 +08:00
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.inventory.ItemType;
2022-04-27 12:24:25 +08:00
import emu.grasscutter.game.player.Player;
2022-04-21 06:48:24 +08:00
import emu.grasscutter.game.props.ActionReason;
import java.util.LinkedList;
import java.util.List;
2022-04-23 22:11:45 +08:00
@Command(label = "give", usage = "give [player] <itemId|itemName> [amount] [level]", description = "Gives an item to you or the specified player", aliases = {
"g", "item", "giveitem"}, permission = "player.give")
2022-04-21 06:48:24 +08:00
public final class GiveCommand implements CommandHandler {
@Override
public void execute(Player sender, List<String> args) {
int target, item, lvl, amount = 1, refinement = 0;
2022-04-21 06:48:24 +08:00
if (sender == null && args.size() < 2) {
2022-04-23 22:11:45 +08:00
CommandHandler.sendMessage(null, "Usage: give <player> <itemId|itemName> [amount] [level]");
2022-04-21 06:48:24 +08:00
return;
}
switch (args.size()) {
default: // *No args*
CommandHandler.sendMessage(sender, "Usage: give [player] <itemId|itemName> [amount]");
return;
case 1: // <itemId|itemName>
try {
item = Integer.parseInt(args.get(0));
target = sender.getUid();
2022-04-23 22:11:45 +08:00
lvl = 1;
2022-04-21 06:48:24 +08:00
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, "Invalid item id.");
return;
}
break;
case 2: // <itemId|itemName> [amount] | [player] <itemId|itemName>
try {
target = Integer.parseInt(args.get(0));
2022-04-23 22:11:45 +08:00
lvl = 1;
2022-04-21 06:48:24 +08:00
if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
target = sender.getUid();
item = Integer.parseInt(args.get(0));
amount = Integer.parseInt(args.get(1));
} else {
item = Integer.parseInt(args.get(1));
}
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, "Invalid item or player ID.");
return;
}
break;
2022-04-23 22:11:45 +08:00
case 3: // [player] <itemId|itemName> [amount] | <itemId|itemName> [amount] [level]
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
target = sender.getUid();
item = Integer.parseInt(args.get(0));
amount = Integer.parseInt(args.get(1));
lvl = Integer.parseInt(args.get(2));
} else {
item = Integer.parseInt(args.get(1));
amount = Integer.parseInt(args.get(2));
lvl = 1;
}
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, "Invalid item or player ID.");
return;
}
break;
case 4: // [player] <itemId|itemName> [amount] [level] | <itemId|itemName> [amount] [level] [refinement]
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
target = sender.getUid();
item = Integer.parseInt(args.get(0));
amount = Integer.parseInt(args.get(1));
lvl = Integer.parseInt(args.get(2));
refinement = Integer.parseInt(args.get(3));
} else {
item = Integer.parseInt(args.get(1));
amount = Integer.parseInt(args.get(2));
lvl = Integer.parseInt(args.get(3));
}
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, "Invalid item or player ID.");
return;
}
break;
case 5: // [player] <itemId|itemName> [amount] [level] [refinement]
2022-04-21 06:48:24 +08:00
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
CommandHandler.sendMessage(sender, "Invalid player ID.");
return;
2022-04-23 22:11:45 +08:00
} else {
item = Integer.parseInt(args.get(1));
amount = Integer.parseInt(args.get(2));
lvl = Integer.parseInt(args.get(3));
refinement = Integer.parseInt(args.get(4));
2022-04-21 06:48:24 +08:00
}
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, "Invalid item or player ID.");
return;
}
break;
}
Player targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target);
2022-04-21 06:48:24 +08:00
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, "Player not found.");
return;
}
ItemData itemData = GameData.getItemDataMap().get(item);
2022-04-21 06:48:24 +08:00
if (itemData == null) {
CommandHandler.sendMessage(sender, "Invalid item id.");
return;
}
if (refinement != 0) {
if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
if (refinement < 1 || refinement > 5) {
CommandHandler.sendMessage(sender, "Refinement must be between 1 and 5.");
return;
}
} else {
CommandHandler.sendMessage(sender, "Refinement is only applicable to weapons.");
return;
}
}
2022-04-21 06:48:24 +08:00
this.item(targetPlayer, itemData, amount, lvl, refinement);
2022-04-21 06:48:24 +08:00
if (!itemData.isEquip()) {
2022-04-23 22:11:45 +08:00
CommandHandler.sendMessage(sender, String.format("Given %s of %s to %s.", amount, item, target));
} else if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
CommandHandler.sendMessage(sender,
String.format("Given %s with level %s, refinement %s %s times to %s", item, lvl, refinement, amount, target));
} else {
2022-04-23 22:11:45 +08:00
CommandHandler.sendMessage(sender,
String.format("Given %s with level %s %s times to %s", item, lvl, amount, target));
}
2022-04-21 06:48:24 +08:00
}
private void item(Player player, ItemData itemData, int amount, int lvl, int refinement) {
2022-04-21 06:48:24 +08:00
if (itemData.isEquip()) {
List<GameItem> items = new LinkedList<>();
2022-04-23 22:11:45 +08:00
for (int i = 0; i < amount; i++) {
GameItem item = new GameItem(itemData);
if (item.isEquipped()) {
// check item max level
if (item.getItemType() == ItemType.ITEM_WEAPON) {
if (lvl > 90) lvl = 90;
} else {
if (lvl > 21) lvl = 21;
}
}
2022-04-23 22:11:45 +08:00
item.setCount(amount);
item.setLevel(lvl);
if (lvl > 80) {
2022-04-23 22:11:45 +08:00
item.setPromoteLevel(6);
} else if (lvl > 70) {
item.setPromoteLevel(5);
} else if (lvl > 60) {
item.setPromoteLevel(4);
} else if (lvl > 50) {
item.setPromoteLevel(3);
} else if (lvl > 40) {
item.setPromoteLevel(2);
} else if (lvl > 20) {
item.setPromoteLevel(1);
2022-04-23 22:11:45 +08:00
}
if (item.getItemType() == ItemType.ITEM_WEAPON) {
if (refinement > 0) {
item.setRefinement(refinement - 1);
} else {
item.setRefinement(0);
}
}
2022-04-23 22:11:45 +08:00
items.add(item);
}
player.getInventory().addItems(items, ActionReason.SubfieldDrop);
2022-04-21 06:48:24 +08:00
} else {
GameItem item = new GameItem(itemData);
item.setCount(amount);
player.getInventory().addItem(item, ActionReason.SubfieldDrop);
2022-04-21 06:48:24 +08:00
}
}
}