Commit d8b2e787 authored by AnimeGitB's avatar AnimeGitB Committed by Melledy
Browse files

Commands Targeting rework

parent 5aba7da7
......@@ -117,29 +117,37 @@ public final class CommandMap {
String[] split = rawMessage.split(" ");
List<String> args = new LinkedList<>(Arrays.asList(split));
String label = args.remove(0);
// Check for special case
String playerId = (player == null) ? consoleId : player.getAccount().getId();
if (label == "target") { // Sets or clears default targetPlayer
if (args.size() < 1) {
// Check for special cases - currently only target command
String targetUidStr = null;
if (label.startsWith("@")) { // @[UID]
targetUidStr = label.substring(1);
} else if (label == "target") { // target [[@]UID]
targetUidStr = args.get(0);
if (targetUidStr.startsWith("@")) {
targetUidStr = targetUidStr.substring(1);
}
}
if (targetUidStr == "") { // Clears default targetPlayer
targetPlayers.remove(playerId);
CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_cleared);
} else {
return;
} else if (targetUidStr != null) { // Sets default targetPlayer to the UID given
try {
String sUid = args.get(0);
int uid = Integer.parseInt(sUid);
int uid = Integer.parseInt(targetUidStr);
targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid);
if (targetPlayer == null) {
CommandHandler.sendMessage(player, Grasscutter.getLanguage().Player_not_found_or_offline);
} else {
targetPlayers.put(playerId, targetPlayer);
CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_set.replace("{uid}", sUid));
CommandHandler.sendMessage(player, Grasscutter.getLanguage().Target_set.replace("{uid}", targetUidStr));
}
} catch (NumberFormatException e) {
CommandHandler.sendMessage(player, Grasscutter.getLanguage().Invalid_UID);
}
}
return;
}
// Get command handler.
CommandHandler handler = this.commands.get(label);
if (handler == null) {
......
......@@ -3,7 +3,6 @@ package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.gacha.GachaRecord;
import emu.grasscutter.game.player.Player;
import java.util.List;
......@@ -13,12 +12,12 @@ import java.util.List;
public final class ChangeSceneCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
if (args.size() < 1) {
if (args.size() != 1) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Change_screen_usage);
return;
}
......@@ -26,19 +25,19 @@ public final class ChangeSceneCommand implements CommandHandler {
try {
int sceneId = Integer.parseInt(args.get(0));
if (sceneId == sender.getSceneId()) {
if (sceneId == targetPlayer.getSceneId()) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Change_screen_you_in_that_screen);
return;
}
boolean result = sender.getWorld().transferPlayerToScene(sender, sceneId, sender.getPos());
boolean result = targetPlayer.getWorld().transferPlayerToScene(targetPlayer, sceneId, targetPlayer.getPos());
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Change_screen + sceneId);
if (!result) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Change_screen_not_exist);
}
} catch (Exception e) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Change_screen_usage);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_arguments);
}
}
}
......@@ -17,22 +17,16 @@ public final class ClearCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
int target;
String cmdSwitch = "";
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
Inventory playerInventory = sender.getInventory();
try {
if (args.size() == 1) {
cmdSwitch = args.get(0);
target = sender.getUid();
}else {
cmdSwitch = args.get(1);
target = Integer.parseInt(args.get(0));
if (args.size() < 1) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Clear_usage);
return;
}
switch (cmdSwitch) {
Inventory playerInventory = targetPlayer.getInventory();
switch (args.get(0)) {
case "wp" -> {
playerInventory.getItems().values().stream()
.filter(item -> item.getItemType() == ItemType.ITEM_WEAPON)
......@@ -92,15 +86,5 @@ public final class ClearCommand implements CommandHandler {
sender.dropMessage(Grasscutter.getLanguage().Clear_everything.replace("{name}", targetPlayer.getNickname()));
}
}
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return;
}
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return;
}
}
}
......@@ -7,32 +7,44 @@ import emu.grasscutter.game.player.Player;
import java.util.List;
@Command(label = "coop", usage = "coop",
@Command(label = "coop", usage = "coop [host UID]",
description = "Forces someone to join the world of others", permission = "server.coop")
public final class CoopCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 2) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_usage);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
Player host = sender;
switch (args.size()) {
case 0: // Summon target to self
break;
case 1: // Summon target to argument
try {
int tid = Integer.parseInt(args.get(0));
int hostId = Integer.parseInt(args.get(1));
Player host = sender.getServer().getPlayerByUid(hostId);
Player want = sender.getServer().getPlayerByUid(tid);
if (host == null || want == null) {
host = sender.getServer().getPlayerByUid(hostId);
if (host == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_is_offline);
return;
}
if (want.isInMultiplayer()) {
sender.getServer().getMultiplayerManager().leaveCoop(want);
}
sender.getServer().getMultiplayerManager().applyEnterMp(want, hostId);
sender.getServer().getMultiplayerManager().applyEnterMpReply(host, tid, true);
break;
} catch (Exception e) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return;
}
default:
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_usage);
return;
}
if (targetPlayer.isInMultiplayer()) {
sender.getServer().getMultiplayerManager().leaveCoop(targetPlayer);
}
sender.getServer().getMultiplayerManager().applyEnterMp(targetPlayer, host.getUid());
sender.getServer().getMultiplayerManager().applyEnterMpReply(host, targetPlayer.getUid(), true);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Coop_success.replace("{host}", host.getNickname()).replace("{target}", targetPlayer.getNickname()));
}
}
......@@ -17,41 +17,51 @@ public final class DropCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
if (targetPlayer == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Target_needed);
return;
}
if (args.size() < 1) {
int item = 0;
int amount = 1;
switch (args.size()) {
case 2:
try {
amount = Integer.parseInt(args.get(1));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_amount);
return;
} // Slightly cheeky here: no break so it falls through to initialize the first argument too
case 1:
try {
item = Integer.parseInt(args.get(0));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_id);
return;
}
break;
default:
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Drop_usage);
return;
}
try {
int item = Integer.parseInt(args.get(0));
int amount = 1;
if (args.size() > 1) amount = Integer.parseInt(args.get(1));
ItemData itemData = GameData.getItemDataMap().get(item);
if (itemData == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_id);
return;
}
if (itemData.isEquip()) {
float range = (5f + (.1f * amount));
for (int i = 0; i < amount; i++) {
Position pos = sender.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
EntityItem entity = new EntityItem(sender.getScene(), sender, itemData, pos, 1);
sender.getScene().addEntity(entity);
Position pos = targetPlayer.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, pos, 1);
targetPlayer.getScene().addEntity(entity);
}
} else {
EntityItem entity = new EntityItem(sender.getScene(), sender, itemData, sender.getPos().clone().addY(3f), amount);
sender.getScene().addEntity(entity);
EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, targetPlayer.getPos().clone().addY(3f), amount);
targetPlayer.getScene().addEntity(entity);
}
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Drop_dropped_of.replace("{amount}", Integer.toString(amount)).replace("{item}", Integer.toString(item)));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_or_player_id);
}
}
}
\ No newline at end of file
......@@ -12,8 +12,8 @@ import java.util.List;
public final class EnterDungeonCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
if (targetPlayer == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Target_needed);
return;
}
......@@ -25,12 +25,12 @@ public final class EnterDungeonCommand implements CommandHandler {
try {
int dungeonId = Integer.parseInt(args.get(0));
if (dungeonId == sender.getSceneId()) {
if (dungeonId == targetPlayer.getSceneId()) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().EnterDungeon_you_in_that_dungeon);
return;
}
boolean result = sender.getServer().getDungeonManager().enterDungeon(sender.getSession().getPlayer(), 0, dungeonId);
boolean result = targetPlayer.getServer().getDungeonManager().enterDungeon(targetPlayer.getSession().getPlayer(), 0, dungeonId);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().EnterDungeon_changed_to_dungeon + dungeonId);
if (!result) {
......
......@@ -13,61 +13,34 @@ import emu.grasscutter.game.player.Player;
import java.util.*;
@Command(label = "giveall", usage = "giveall [player] [amount]",
@Command(label = "giveall", usage = "giveall [amount]",
description = "Gives all items", aliases = {"givea"}, permission = "player.giveall", threading = true)
public final class GiveAllCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
int target, amount = 99999;
switch (args.size()) {
case 0: // *no args*
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
target = sender.getUid();
break;
int amount = 99999;
case 1: // [player]
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return;
}
}catch (NumberFormatException ignored){
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return;
}
switch (args.size()) {
case 0:
break;
case 2: // [player] [amount]
case 1: // [amount]
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
target = sender.getUid();
amount = Integer.parseInt(args.get(0));
} else {
amount = Integer.parseInt(args.get(1));
}
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveAll_invalid_amount_or_playerId);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_amount);
return;
}
break;
default: // invalid
CommandHandler.sendMessage(null, Grasscutter.getLanguage().GiveAll_usage);
return;
}
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return;
}
this.giveAllItems(targetPlayer, amount);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveAll_done);
}
......
......@@ -14,43 +14,54 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Command(label = "giveart", usage = "giveart [player] <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", description = "Gives the player a specified artifact", aliases = {"gart"}, permission = "player.giveart")
@Command(label = "giveart", usage = "giveart <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]", description = "Gives the player a specified artifact", aliases = {"gart"}, permission = "player.giveart")
public final class GiveArtifactCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
int size = args.size(), target, itemId, mainPropId, level = 1;
ArrayList<Integer> appendPropIdList = new ArrayList<>();
String msg = Grasscutter.getLanguage().GiveArtifact_usage;
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
if (sender == null && size < 2) {
CommandHandler.sendMessage(null, msg);
if (args.size() < 2) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveArtifact_usage);
return;
}
if (size >= 2) {
try {
int itemId;
try {
int last = Integer.parseInt(args.get(size - 1));
if (last >= 1 && last <= 21) {
level = last;
size--;
itemId = Integer.parseInt(args.remove(0));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_artifact_id);
return;
}
ItemData itemData = GameData.getItemDataMap().get(itemId);
if (itemData.getItemType() != ItemType.ITEM_RELIQUARY) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_artifact_id);
return;
}
int mainPropId;
try {
mainPropId = Integer.parseInt(args.remove(0));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_arguments);
return;
}
int level = 1;
try {
int last = Integer.parseInt(args.get(args.size()-1));
if (last > 0 && last < 21) { // Luckily appendPropIds aren't in the range of [1,20]
level = last;
args.remove(args.size()-1);
}
target = Integer.parseInt(args.get(0));
int fromIdx;
if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
target = sender.getUid();
itemId = Integer.parseInt(args.get(0));
mainPropId = Integer.parseInt(args.get(1));
fromIdx = 2;
} else {
target = Integer.parseInt(args.get(0));
itemId = Integer.parseInt(args.get(1));
mainPropId = Integer.parseInt(args.get(2));
fromIdx = 3;
} catch (NumberFormatException ignored) { // Could be a stat,times string so no need to panic
}
args.subList(fromIdx, size).forEach(it -> {
ArrayList<Integer> appendPropIdList = new ArrayList<>();
try {
args.forEach(it -> {
String[] arr;
int n = 1;
if ((arr = it.split(",")).length == 2) {
......@@ -63,23 +74,7 @@ public final class GiveArtifactCommand implements CommandHandler {
appendPropIdList.addAll(Collections.nCopies(n, Integer.parseInt(it)));
});
} catch (Exception ignored) {
CommandHandler.sendMessage(sender, msg);
return;
}
} else {
CommandHandler.sendMessage(sender, msg);
return;
}
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return;
}
ItemData itemData = GameData.getItemDataMap().get(itemId);
if (itemData.getItemType() != ItemType.ITEM_RELIQUARY) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveArtifact_invalid_artifact_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_arguments);
return;
}
......@@ -90,7 +85,7 @@ public final class GiveArtifactCommand implements CommandHandler {
item.getAppendPropIdList().addAll(appendPropIdList);
targetPlayer.getInventory().addItem(item, ActionReason.SubfieldDrop);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveArtifact_given.replace("{itemId}", Integer.toString(itemId)).replace("target", Integer.toString(target)));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveArtifact_given.replace("{itemId}", Integer.toString(itemId)).replace("target", Integer.toString(targetPlayer.getUid())));
}
}
......@@ -10,75 +10,57 @@ import emu.grasscutter.game.player.Player;
import java.util.List;
@Command(label = "givechar", usage = "givechar <playerId> <avatarId> [level]",
@Command(label = "givechar", usage = "givechar <avatarId> [level]",
description = "Gives the player a specified character", aliases = {"givec"}, permission = "player.givechar")
public final class GiveCharCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
int target, avatarId, level = 1, ascension;
if (sender == null && args.size() < 2) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().GiveChar_usage);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
int avatarId;
int level = 1;
switch (args.size()) {
default:
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveChar_usage);
return;
case 2:
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null && sender != null) {
target = sender.getUid();
level = Integer.parseInt(args.get(1));
avatarId = Integer.parseInt(args.get(0));
} else {
avatarId = Integer.parseInt(args.get(1));
}
} catch (NumberFormatException ignored) {
// TODO: Parse from avatar name using GM Handbook.
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveChar_invalid_avatar_or_player_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_avatar_level);
return;
}
break;
case 3:
} // Cheeky fall-through to parse first argument too
case 1:
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return;
}
avatarId = Integer.parseInt(args.get(1));
level = Integer.parseInt(args.get(2));
avatarId = Integer.parseInt(args.get(0));
} catch (NumberFormatException ignored) {
// TODO: Parse from avatar name using GM Handbook.
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveChar_invalid_avatar_or_player_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_avatar_id);
return;
}
break;
}
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
default:
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveChar_usage);
return;
}
AvatarData avatarData = GameData.getAvatarDataMap().get(avatarId);
if (avatarData == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveChar_invalid_avatar_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_avatar_id);
return;
}
// Check level.
if (level > 90) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveChar_invalid_avatar_level);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_avatar_level);
return;
}
// Calculate ascension level.
int ascension;
if (level <= 40) {
ascension = (int) Math.ceil(level / 20f);
} else {
......@@ -93,6 +75,6 @@ public final class GiveCharCommand implements CommandHandler {
avatar.recalcStats();
targetPlayer.addAvatar(avatar);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveChar_given.replace("{avatarId}", Integer.toString(avatarId)).replace("{level}", Integer.toString(level)).replace("{target}", Integer.toString(target)));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().GiveChar_given.replace("{avatarId}", Integer.toString(avatarId)).replace("{level}", Integer.toString(level)).replace("{target}", Integer.toString(targetPlayer.getUid())));
}
}
......@@ -13,119 +13,58 @@ import emu.grasscutter.game.props.ActionReason;
import java.util.LinkedList;
import java.util.List;
@Command(label = "give", usage = "give [player] <itemId|itemName> [amount] [level]", description = "Gives an item to you or the specified player", aliases = {
@Command(label = "give", usage = "give <itemId|itemName> [amount] [level]", description = "Gives an item to you or the specified player", aliases = {
"g", "item", "giveitem"}, permission = "player.give")
public final class GiveCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
int target, item, lvl, amount = 1, refinement = 0;
if (sender == null && args.size() < 2) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Give_usage);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
int item;
int lvl = 1;
int amount = 1;
int refinement = 0;
switch (args.size()) {
default: // *No args*
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_usage);
return;
case 1: // <itemId|itemName>
case 4: // <itemId|itemName> [amount] [level] [refinement]
try {
item = Integer.parseInt(args.get(0));
target = sender.getUid();
lvl = 1;
refinement = Integer.parseInt(args.get(3));
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_refinement);
return;
}
break;
case 2: // <itemId|itemName> [amount] | [player] <itemId|itemName>
} // Fallthrough
case 3: // <itemId|itemName> [amount] [level]
try {
target = Integer.parseInt(args.get(0));
lvl = 1;
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));
}
lvl = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_or_player_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_level);
return;
}
break;
case 3: // [player] <itemId|itemName> [amount] | <itemId|itemName> [amount] [level]
} // Fallthrough
case 2: // <itemId|itemName> [amount]
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, Grasscutter.getLanguage().Invalid_item_or_player_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_amount);
return;
}
break;
case 4: // [player] <itemId|itemName> [amount] [level] | <itemId|itemName> [amount] [level] [refinement]
} // Fallthrough
case 1: // <itemId|itemName>
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, Grasscutter.getLanguage().Invalid_item_or_player_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_id);
return;
}
break;
case 5: // [player] <itemId|itemName> [amount] [level] [refinement]
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return;
} 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));
}
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_or_player_id);
default: // *No args*
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_usage);
return;
}
break;
}
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return;
}
ItemData itemData = GameData.getItemDataMap().get(item);
if (itemData == null) {
......@@ -147,9 +86,9 @@ public final class GiveCommand implements CommandHandler {
this.item(targetPlayer, itemData, amount, lvl, refinement);
if (!itemData.isEquip()) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_given.replace("{amount}", Integer.toString(amount)).replace("{item}", Integer.toString(item)).replace("{target}", Integer.toString(target)));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_given.replace("{amount}", Integer.toString(amount)).replace("{item}", Integer.toString(item)).replace("{target}", Integer.toString(targetPlayer.getUid())));
} else if (itemData.getItemType() == ItemType.ITEM_WEAPON) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_given_with_level_and_refinement.replace("{item}", Integer.toString(item)).replace("{lvl}", Integer.toString(lvl)).replace("{refinement}", Integer.toString(refinement)).replace("{amount}", Integer.toString(amount)).replace("{target}", Integer.toString(target)));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_given_with_level_and_refinement.replace("{item}", Integer.toString(item)).replace("{lvl}", Integer.toString(lvl)).replace("{refinement}", Integer.toString(refinement)).replace("{amount}", Integer.toString(amount)).replace("{target}", Integer.toString(targetPlayer.getUid())));
} else {
CommandHandler.sendMessage(sender,Grasscutter.getLanguage().Give_given_level.replace("{item}", Integer.toString(item)).replace("{lvl}", Integer.toString(lvl)).replace("{amount}", Integer.toString(amount)));
}
......
......@@ -7,37 +7,34 @@ import emu.grasscutter.game.player.Player;
import java.util.List;
@Command(label = "godmode", usage = "godmode [playerId]",
description = "Prevents you from taking damage", permission = "player.godmode")
@Command(label = "godmode", usage = "godmode [on|off|toggle]",
description = "Prevents you from taking damage. Defaults to toggle.", permission = "player.godmode")
public final class GodModeCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
return; // TODO: toggle player's godmode statue from console or other players
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
int target;
boolean enabled = !targetPlayer.inGodmode();
if (args.size() == 1) {
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
target = sender.getUid();
}
} catch (NumberFormatException e) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return;
switch (args.get(0).toLowerCase()) {
case "on":
enabled = true;
break;
case "off":
enabled = false;
break;
case "toggle":
break; // Already toggled
default:
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Godmode_status);
}
} else {
target = sender.getUid();
}
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
return;
}
targetPlayer.setGodmode(!targetPlayer.inGodmode());
sender.dropMessage(Grasscutter.getLanguage().Godmode_status.replace("{status}", (targetPlayer.inGodmode() ? Grasscutter.getLanguage().Enabled : Grasscutter.getLanguage().Disabled)).replace("{name}", targetPlayer.getNickname()));
targetPlayer.setGodmode(enabled);
sender.dropMessage(Grasscutter.getLanguage().Godmode_status.replace("{status}", (enabled ? Grasscutter.getLanguage().Enabled : Grasscutter.getLanguage().Disabled)).replace("{name}", targetPlayer.getNickname()));
}
}
......@@ -15,12 +15,12 @@ import java.util.List;
public final class HealCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
sender.getTeamManager().getActiveTeam().forEach(entity -> {
targetPlayer.getTeamManager().getActiveTeam().forEach(entity -> {
boolean isAlive = entity.isAlive();
entity.setFightProperty(
FightProperty.FIGHT_PROP_CUR_HP,
......
......@@ -7,23 +7,21 @@ import emu.grasscutter.game.player.Player;
import java.util.List;
@Command(label = "kick", usage = "kick <player>",
@Command(label = "kick", usage = "kick",
description = "Kicks the specified player from the server (WIP)", permission = "server.kick")
public final class KickCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
int target = Integer.parseInt(args.get(0));
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
if (sender != null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Kick_player_kick_player.replace("{sendUid}", Integer.toString(sender.getAccount().getPlayerUid())).replace("{sendName}", sender.getAccount().getUsername()).replace("kickUid", Integer.toString(target)).replace("{kickName}", targetPlayer.getAccount().getUsername()));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Kick_player_kick_player.replace("{sendUid}", Integer.toString(sender.getAccount().getPlayerUid())).replace("{sendName}", sender.getAccount().getUsername()).replace("kickUid", Integer.toString(targetPlayer.getUid())).replace("{kickName}", targetPlayer.getAccount().getUsername()));
} else {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Kick_server_player.replace("{kickUid}", Integer.toString(target)).replace("{kickName}", targetPlayer.getAccount().getUsername()));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Kick_server_player.replace("{kickUid}", Integer.toString(targetPlayer.getUid())).replace("{kickName}", targetPlayer.getAccount().getUsername()));
}
targetPlayer.getSession().close();
......
......@@ -10,57 +10,43 @@ import emu.grasscutter.game.world.Scene;
import java.util.List;
@Command(label = "killall", usage = "killall [playerUid] [sceneId]",
@Command(label = "killall", usage = "killall [sceneId]",
description = "Kill all entities", permission = "server.killall")
public final class KillAllCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
Scene mainScene;
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
Scene scene = targetPlayer.getScene();
try {
switch (args.size()) {
case 0: // *No args*
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Kill_usage);
return;
}
mainScene = sender.getScene();
break;
case 1: // [playerUid]
targetPlayer = Grasscutter.getGameServer().getPlayerByUid(Integer.parseInt(args.get(0)));
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found_or_offline);
return;
}
mainScene = targetPlayer.getScene();
case 1: // [sceneId]
scene = targetPlayer.getWorld().getSceneById(Integer.parseInt(args.get(0)));
break;
case 2: // [playerUid] [sceneId]
targetPlayer = Grasscutter.getGameServer().getPlayerByUid(Integer.parseInt(args.get(0)));
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found_or_offline);
default:
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Kill_usage);
return;
}
Scene scene = sender.getWorld().getSceneById(Integer.parseInt(args.get(1)));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_arguments);
}
if (scene == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Kill_scene_not_found_in_player_world);
return;
}
mainScene = scene;
break;
default:
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Kill_usage);
return;
}
// Separate into list to avoid concurrency issue
List<GameEntity> toKill = mainScene.getEntities().values().stream()
final Scene sceneF = scene;
List<GameEntity> toKill = sceneF.getEntities().values().stream()
.filter(entity -> entity instanceof EntityMonster)
.toList();
toKill.stream().forEach(entity -> mainScene.killEntity(entity, 0));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Kill_kill_monsters_in_scene.replace("{size}", Integer.toString(toKill.size())).replace("{id}", Integer.toString(mainScene.getId())));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_arguments);
}
toKill.stream().forEach(entity -> sceneF.killEntity(entity, 0));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Kill_kill_monsters_in_scene.replace("{size}", Integer.toString(toKill.size())).replace("{id}", Integer.toString(scene.getId())));
}
}
......@@ -12,44 +12,14 @@ import emu.grasscutter.server.packet.send.PacketLifeStateChangeNotify;
import java.util.List;
@Command(label = "killcharacter", usage = "killcharacter [playerId]", aliases = {"suicide", "kill"},
@Command(label = "killcharacter", usage = "killcharacter", aliases = {"suicide", "kill"},
description = "Kills the players current character", permission = "player.killcharacter")
public final class KillCharacterCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
int target;
if (sender == null) {
// from console
if (args.size() == 1) {
try {
target = Integer.parseInt(args.get(0));
} catch (NumberFormatException e) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Invalid_playerId);
return;
}
} else {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().KillCharacter_usage);
return;
}
} else {
if (args.size() == 1) {
try {
target = Integer.parseInt(args.get(0));
if (Grasscutter.getGameServer().getPlayerByUid(target) == null) {
target = sender.getUid();
}
} catch (NumberFormatException e) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_playerId);
return;
}
} else {
target = sender.getUid();
}
}
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found_or_offline);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
......
......@@ -8,22 +8,26 @@ import emu.grasscutter.game.player.Player;
import java.util.List;
@Command(label = "permission", usage = "permission <add|remove> <username> <permission>",
@Command(label = "permission", usage = "permission <add|remove> <permission>",
description = "Grants or removes a permission for a user", permission = "*")
public final class PermissionCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 3) {
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
if (args.size() != 2) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Permission_usage);
return;
}
String action = args.get(0);
String username = args.get(1);
String permission = args.get(2);
String permission = args.get(1);
Account account = Grasscutter.getGameServer().getAccountByName(username);
Account account = targetPlayer.getAccount();
if (account == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Account_not_find);
return;
......
......@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.utils.Position;
import java.util.List;
......@@ -13,11 +14,12 @@ public final class PositionCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
sender.dropMessage(Grasscutter.getLanguage().Position_message.replace("{x}", Float.toString(sender.getPos().getX())).replace("{y}", Float.toString(sender.getPos().getY())).replace("{z}", Float.toString(sender.getPos().getZ())).replace("{id}", Integer.toString(sender.getSceneId())));
Position pos = targetPlayer.getPos();
sender.dropMessage(Grasscutter.getLanguage().Position_message.replace("{x}", Float.toString(pos.getX())).replace("{y}", Float.toString(pos.getY())).replace("{z}", Float.toString(pos.getZ())).replace("{id}", Integer.toString(sender.getSceneId())));
}
}
......@@ -16,16 +16,16 @@ public final class ResetConstCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, Grasscutter.getLanguage().Run_this_command_in_game);
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
if (args.size() > 0 && args.get(0).equalsIgnoreCase("all")) {
sender.getAvatars().forEach(this::resetConstellation);
sender.dropMessage(Grasscutter.getLanguage().ResetConst_reset_all);
targetPlayer.getAvatars().forEach(this::resetConstellation);
targetPlayer.dropMessage(Grasscutter.getLanguage().ResetConst_reset_all);
} else {
EntityAvatar entity = sender.getTeamManager().getCurrentAvatarEntity();
EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity();
if (entity == null) {
return;
}
......
......@@ -12,14 +12,8 @@ import java.util.List;
public final class ResetShopLimitCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
CommandHandler.sendMessage(sender,Grasscutter.getLanguage().ResetShopLimit_usage);
return;
}
int target = Integer.parseInt(args.get(0));
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Player_not_found);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Target_needed);
return;
}
......
......@@ -108,40 +108,44 @@ public final class SendMailCommand implements CommandHandler {
mailBuilder.constructionStage++;
}
case 3 -> {
// Literally just copy-pasted from the give command lol.
int item, lvl, amount = 1;
int item;
int lvl = 1;
int amount = 1;
int refinement = 0;
switch (args.size()) {
default -> { // *No args*
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().SendMail_usage);
case 4: // <itemId|itemName> [amount] [level] [refinement] // TODO: this requires Mail support but there's no harm leaving it here for now
try {
refinement = Integer.parseInt(args.get(3));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_refinement);
return;
}
case 1 -> { // <itemId|itemName>
} // Fallthrough
case 3: // <itemId|itemName> [amount] [level]
try {
item = Integer.parseInt(args.get(0));
lvl = 1;
lvl = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_level);
return;
}
}
case 2 -> { // <itemId|itemName> [amount]
lvl = 1;
item = Integer.parseInt(args.get(0));
} // Fallthrough
case 2: // <itemId|itemName> [amount]
try {
amount = Integer.parseInt(args.get(1));
}
case 3 -> { // <itemId|itemName> [amount] [level]
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_amount);
return;
} // Fallthrough
case 1: // <itemId|itemName>
try {
item = Integer.parseInt(args.get(0));
amount = Integer.parseInt(args.get(1));
lvl = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
// TODO: Parse from item name using GM Handbook.
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_or_player_id);
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Invalid_item_id);
return;
}
}
break;
default: // *No args*
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().Give_usage);
return;
}
mailBuilder.mail.itemList.add(new Mail.MailItem(item, amount, lvl));
CommandHandler.sendMessage(sender, Grasscutter.getLanguage().SendMail_send.replace("{amount}", Integer.toString(amount)).replace("{item}", Integer.toString(item)).replace("{lvl}", Integer.toString(lvl)));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment