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

170 lines
6.0 KiB
Java
Raw Normal View History

2022-04-24 14:01:03 +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-24 14:01:03 +08:00
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.inventory.GameItem;
2022-04-25 15:17:55 +08:00
import emu.grasscutter.game.inventory.ItemType;
2022-04-27 12:24:25 +08:00
import emu.grasscutter.game.player.Player;
2022-04-24 14:01:03 +08:00
2022-04-24 21:52:50 +08:00
import java.util.*;
2022-04-24 14:01:03 +08:00
import static emu.grasscutter.utils.Language.translate;
@Command(label = "giveall", usage = "giveall [amount]", aliases = {"givea"}, permission = "player.giveall", permissionTargeted = "player.giveall.others", threading = true, description = "commands.giveAll.description")
2022-04-29 10:21:30 +08:00
public final class GiveAllCommand implements CommandHandler {
2022-04-26 16:35:06 +08:00
2022-04-24 14:01:03 +08:00
@Override
2022-05-04 14:32:09 +08:00
public void execute(Player sender, Player targetPlayer, List<String> args) {
2022-05-04 17:16:42 +08:00
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, translate(sender, "commands.execution.need_target"));
2022-05-04 17:16:42 +08:00
return;
}
int amount = 99999;
2022-04-24 14:01:03 +08:00
switch (args.size()) {
2022-05-04 17:16:42 +08:00
case 0:
2022-04-24 14:01:03 +08:00
break;
2022-05-04 17:16:42 +08:00
case 1: // [amount]
2022-04-24 14:01:03 +08:00
try {
2022-05-04 17:16:42 +08:00
amount = Integer.parseInt(args.get(0));
2022-04-26 16:35:06 +08:00
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.amount"));
2022-04-24 14:01:03 +08:00
return;
}
break;
2022-04-26 16:35:06 +08:00
default: // invalid
CommandHandler.sendMessage(sender, translate(sender, "commands.giveAll.usage"));
2022-04-26 16:35:06 +08:00
return;
2022-04-24 14:01:03 +08:00
}
2022-04-26 16:35:06 +08:00
this.giveAllItems(targetPlayer, amount);
CommandHandler.sendMessage(sender, translate(targetPlayer, "commands.giveAll.success", targetPlayer.getNickname()));
2022-04-24 14:01:03 +08:00
}
public void giveAllItems(Player player, int amount) {
CommandHandler.sendMessage(player, translate(player, "commands.giveAll.started"));
2022-04-26 16:35:06 +08:00
for (AvatarData avatarData: GameData.getAvatarDataMap().values()) {
2022-04-26 16:35:06 +08:00
//Exclude test avatar
if (isTestAvatar(avatarData.getId())) continue;
Avatar avatar = new Avatar(avatarData);
2022-04-26 16:35:06 +08:00
avatar.setLevel(90);
avatar.setPromoteLevel(6);
for(int i = 1;i <= 6;++i){
avatar.getTalentIdList().add((avatar.getAvatarId()-10000000)*10+i);
2022-04-26 16:35:06 +08:00
}
// This will handle stats and talents
avatar.recalcStats();
player.addAvatar(avatar);
}
2022-04-24 21:52:50 +08:00
2022-04-26 16:35:06 +08:00
//some test items
List<GameItem> itemList = new ArrayList<>();
for (ItemData itemdata: GameData.getItemDataMap().values()) {
2022-04-26 16:35:06 +08:00
//Exclude test item
if (isTestItem(itemdata.getId())) continue;
2022-04-24 14:01:03 +08:00
if (itemdata.isEquip()) {
if (itemdata.getItemType() == ItemType.ITEM_WEAPON) {
for (int i = 0; i < 5; ++i) {
GameItem item = new GameItem(itemdata);
item.setLevel(90);
item.setPromoteLevel(6);
item.setRefinement(4);
itemList.add(item);
2022-04-25 15:17:55 +08:00
}
2022-04-24 14:01:03 +08:00
}
2022-04-26 16:35:06 +08:00
}
else {
GameItem item = new GameItem(itemdata);
item.setCount(amount);
itemList.add(item);
2022-04-24 14:01:03 +08:00
}
}
2022-04-29 14:13:09 +08:00
int packetNum = 10;
int itemLength = itemList.size();
2022-04-26 16:35:06 +08:00
int number = itemLength / packetNum;
int remainder = itemLength % packetNum;
int offset = 0;
for (int i = 0; i < packetNum; ++i) {
if (remainder > 0) {
player.getInventory().addItems(itemList.subList(i * number + offset, (i + 1) * number + offset + 1));
2022-04-26 16:35:06 +08:00
--remainder;
++offset;
}
else {
player.getInventory().addItems(itemList.subList(i * number + offset, (i + 1) * number + offset));
2022-04-26 16:35:06 +08:00
}
}
}
2022-04-24 21:52:50 +08:00
2022-04-26 16:35:06 +08:00
public boolean isTestAvatar(int avatarId) {
return avatarId < 10000002 || avatarId >= 11000000;
}
public boolean isTestItem(int itemId) {
for (Range range: testItemRanges) {
if (range.check(itemId)) {
return true;
2022-04-24 14:01:03 +08:00
}
2022-04-26 16:35:06 +08:00
}
2022-04-29 10:21:30 +08:00
return testItemsList.contains(itemId);
2022-04-26 16:35:06 +08:00
}
static class Range {
2022-04-29 10:21:30 +08:00
private final int min, max;
2022-04-26 16:35:06 +08:00
public Range(int min, int max) {
if(min > max){
min ^= max;
max ^= min;
min ^= max;
}
2022-04-29 10:21:30 +08:00
2022-04-26 16:35:06 +08:00
this.min = min;
this.max = max;
}
public boolean check(int value) {
return value >= this.min && value <= this.max;
2022-04-24 14:01:03 +08:00
}
}
2022-04-26 16:35:06 +08:00
private static final Range[] testItemRanges = new Range[] {
new Range(106, 139),
new Range(1000, 1099),
2022-04-29 14:13:09 +08:00
new Range(2001, 3022),
new Range(23300, 23340),
new Range(23383, 23385),
new Range(78310, 78554),
new Range(99310, 99554),
new Range(100001, 100187),
new Range(100210, 100214),
new Range(100303, 100398),
new Range(100414, 100425),
new Range(100454, 103008),
new Range(109000, 109492),
new Range(115001, 118004),
new Range(141001, 141072),
new Range(220050, 221016),
2022-04-26 16:35:06 +08:00
};
private static final Integer[] testItemsIds = new Integer[] {
2022-04-29 14:13:09 +08:00
210, 211, 314, 315, 317, 1005, 1007, 1105, 1107, 1201, 1202,10366,
101212, 11411, 11506, 11507, 11508, 12505, 12506, 12508, 12509, 13503,
13506, 14411, 14503, 14505, 14508, 15411, 15504, 15505, 15506, 15508,
20001, 10002, 10003, 10004, 10005, 10006, 10008,100231,100232,100431,
101689,105001,105004, 106000,106001,108000,110000
2022-04-26 16:35:06 +08:00
};
private static final Collection<Integer> testItemsList = Arrays.asList(testItemsIds);
2022-04-24 14:01:03 +08:00
}
2022-04-26 16:35:06 +08:00