Skip to content

Commit

Permalink
feat: /bw cheatIn command
Browse files Browse the repository at this point in the history
  • Loading branch information
Misat11 committed Sep 23, 2023
1 parent b1f201d commit cd98595
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 46 deletions.
3 changes: 2 additions & 1 deletion plugin/src/main/java/org/screamingsandals/bedwars/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ public void onEnable() {
new MainlobbyCommand();
new LeaderboardCommand();
new DumpCommand();
new CheatCommand();
new CheatCommand("cheat", false);
new CheatCommand("cheatIn", true);

BwCommandsExecutor cmd = new BwCommandsExecutor();
getCommand("bw").setExecutor(cmd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

public class CheatCommand extends BaseCommand {

public CheatCommand() {
super("cheat", ADMIN_PERMISSION, false, false);
public CheatCommand(String commandName, boolean allowConsole) {
super(commandName, ADMIN_PERMISSION, allowConsole, false);
}

@Override
Expand All @@ -53,14 +53,37 @@ public boolean execute(CommandSender sender, List<String> args) {
return true;
}

if (args.size() >= 1) {
int offset = isConsoleCommand() ? 1 : 0;

Game game;
Player defaultPlayer;
if (offset == 1) {
if (args.isEmpty()) {
sender.sendMessage(i18n("unknown_usage"));
return true;
}

game = Main.getGame(args.get(0));

if (game == null) {
sender.sendMessage(i18n("no_arena_found"));
return true;
}

defaultPlayer = null;
} else {
Player player = (Player) sender;
if (!Main.isPlayerInGame(player)) {
sender.sendMessage(i18n("you_arent_in_game"));
return true;
}
Game game = Main.getPlayerGameProfile(player).getGame();
if (args.get(0).equalsIgnoreCase("startemptygame")) {
game = Main.getPlayerGameProfile(player).getGame();
defaultPlayer = player;
}


if (args.size() >= offset + 1) {
if (args.get(offset).equalsIgnoreCase("startemptygame") && offset != 1) { // not allowed for console
if (game.getStatus() == GameStatus.WAITING) {
game.forceGameToStart = true;
sender.sendMessage(i18n("game_forced"));
Expand All @@ -70,37 +93,40 @@ public boolean execute(CommandSender sender, List<String> args) {
return true;
}

switch (args.get(0).toLowerCase()) {
switch (args.get(offset).toLowerCase()) {
case "give":
{
if (game.getStatus() != GameStatus.RUNNING) {
sender.sendMessage(i18n("cheat_game_not_running"));
return true;
}

if (args.size() < 2) {
if (args.size() < offset + 2) {
sender.sendMessage(i18n("unknown_usage"));
return true;
}
String resource = args.get(1);
String resource = args.get(offset + 1);
ItemSpawnerType type = Main.getSpawnerType(resource);
if (type == null) {
sender.sendMessage(i18n("admin_command_invalid_spawner_type"));
return true;
}
int stack = 1;
if (args.size() > 2) {
int stack = offset + 1;
if (args.size() > offset + 2) {
try {
stack = Integer.parseInt(args.get(2));
stack = Integer.parseInt(args.get(offset + 2));
} catch (Throwable ignored) {}
}
Player player1 = player;
if (args.size() > 3) {
player1 = Bukkit.getPlayer(args.get(3));
Player player1 = defaultPlayer;
if (args.size() > offset + 3) {
player1 = Bukkit.getPlayer(args.get(offset + 3));
if (player1 == null || !game.isPlayerInAnyTeam(player1)) {
sender.sendMessage(i18n("cheat_invalid_player"));
return true;
}
} else if (defaultPlayer == null) {
sender.sendMessage(i18n("unknown_usage"));
return true;
}
GamePlayer gamePlayer = Main.getPlayerGameProfile(player1);
if (gamePlayer.isSpectator) {
Expand All @@ -120,13 +146,16 @@ public boolean execute(CommandSender sender, List<String> args) {
return true;
}

Player player1 = player;
if (args.size() > 1) {
player1 = Bukkit.getPlayer(args.get(1));
Player player1 = defaultPlayer;
if (args.size() > offset + 1) {
player1 = Bukkit.getPlayer(args.get(offset + 1));
if (player1 == null || !game.isPlayerInAnyTeam(player1)) {
sender.sendMessage(i18n("cheat_invalid_player"));
return true;
}
} else if (defaultPlayer == null) {
sender.sendMessage(i18n("unknown_usage"));
return true;
}
GamePlayer gamePlayer = Main.getPlayerGameProfile(player1);
if (gamePlayer.isSpectator) {
Expand All @@ -148,11 +177,11 @@ public boolean execute(CommandSender sender, List<String> args) {
return true;
}

if (args.size() < 2) {
if (args.size() < offset + 2) {
sender.sendMessage(i18n("unknown_usage"));
return true;
}
String name = args.get(1);
String name = args.get(offset + 1);
Team team1 = game.getTeamFromName(name);
if (team1 == null) {
sender.sendMessage(i18n("admin_command_team_is_not_exists"));
Expand Down Expand Up @@ -192,25 +221,30 @@ public boolean execute(CommandSender sender, List<String> args) {
break;
case "jointeam":
{
if (args.size() >= 2) {
String name = args.get(1);
if (defaultPlayer == null) { // does not currently work with console
sender.sendMessage(i18n("cheat_please_provide_valid_cheat_type"));
return true;
}

if (args.size() >= offset + 2) {
String name = args.get(offset + 1);
Team team1 = game.getTeamFromName(name);
if (team1 == null) {
sender.sendMessage(i18n("admin_command_team_is_not_exists"));
return true;
}

if (game.getStatus() == GameStatus.WAITING) {
game.selectPlayerTeam(player, team1, true, true, false);
game.selectPlayerTeam(defaultPlayer, team1, true, true, false);
} else {
if (game.getCurrentTeamFromTeam(team1) == null) {
sender.sendMessage(i18n("team_not_in_game").replace("%name%", name));
return true;
}

game.selectPlayerTeam(player, team1, true, false, true);
game.selectPlayerTeam(defaultPlayer, team1, true, false, true);

GamePlayer gP = Main.getPlayerGameProfile(player);
GamePlayer gP = Main.getPlayerGameProfile(defaultPlayer);
CurrentTeam team = game.getPlayerTeam(gP);
if (team != null) {
if (gP.isSpectator) {
Expand All @@ -221,7 +255,7 @@ public boolean execute(CommandSender sender, List<String> args) {
}
}
} else {
GamePlayer gP = Main.getPlayerGameProfile(player);
GamePlayer gP = Main.getPlayerGameProfile(defaultPlayer);
if (game.getStatus() == GameStatus.WAITING) {
game.joinRandomTeam(gP, true, true, false);
} else {
Expand Down Expand Up @@ -254,30 +288,47 @@ public void completeTab(List<String> completion, CommandSender sender, List<Stri
return;
}

if (args.size() == 1) {
completion.addAll(Arrays.asList("give", "kill", "startemptygame", "destroybed", "destroyallbeds", "jointeam"));
int offset = isConsoleCommand() ? 1 : 0;

if (args.size() == 1 && offset == 1) {
completion.addAll(Main.getGameNames());
return;
}

if (args.size() == offset + 1) {
if (offset == 1) {
completion.addAll(Arrays.asList("give", "kill", "destroybed", "destroyallbeds"));
} else {
completion.addAll(Arrays.asList("give", "kill", "startemptygame", "destroybed", "destroyallbeds", "jointeam"));
}
return;
}
if (Main.isPlayerInGame((Player) sender)) {
if (args.size() > 1 && args.get(0).equals("give")) {
GamePlayer gPlayer = Main.getPlayerGameProfile((Player) sender);
if (args.size() == 2) {

Game game;
if (offset == 1) {
game = Main.getGame(args.get(0));
} else {
game = (Game) Main.getInstance().getGameOfPlayer((Player) sender);
}

if (game != null) {
if (args.size() > offset + 1 && args.get(offset).equals("give")) {
if (args.size() == offset + 2) {
completion.addAll(Main.getAllSpawnerTypes());
} else if (args.size() == 3) {
} else if (args.size() == offset + 3) {
completion.addAll(Arrays.asList("1", "2", "4", "8", "16", "32", "64"));
} else if (args.size() == 4) {
completion.addAll(gPlayer.getGame().getConnectedPlayers().stream().map(Player::getName).collect(Collectors.toList()));
} else if (args.size() == offset + 4) {
completion.addAll(game.getConnectedPlayers().stream().map(Player::getName).collect(Collectors.toList()));
}
}
if (args.size() > 1 && args.get(0).equals("kill")) {
GamePlayer gPlayer = Main.getPlayerGameProfile((Player) sender);
if (args.size() == 2) {
completion.addAll(gPlayer.getGame().getConnectedPlayers().stream().map(Player::getName).collect(Collectors.toList()));
if (args.size() > offset + 1 && args.get(offset).equals("kill")) {
if (args.size() == offset + 2) {
completion.addAll(game.getConnectedPlayers().stream().map(Player::getName).collect(Collectors.toList()));
}
}
if (args.size() > 1 && (args.get(0).equalsIgnoreCase("destroybed") || args.get(0).equalsIgnoreCase("jointeam"))) {
GamePlayer gPlayer = Main.getPlayerGameProfile((Player) sender);
if (args.size() == 2) {
completion.addAll(gPlayer.getGame().getRunningTeams().stream().map(Team::getName).collect(Collectors.toList()));
if (args.size() > offset + 1 && (args.get(offset).equalsIgnoreCase("destroybed") || args.get(offset).equalsIgnoreCase("jointeam"))) {
if (args.size() == offset + 2) {
completion.addAll(game.getRunningTeams().stream().map(Team::getName).collect(Collectors.toList()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public void sendConsoleHelp(ConsoleCommandSender console) {
console.sendMessage(i18nonly("help_bw_alljoin"));
console.sendMessage(i18nonly("help_bw_dump"));
console.sendMessage(i18nonly("help_bw_reload"));

if (Main.getConfigurator().config.getBoolean("enable-cheat-command-for-admins")) {
console.sendMessage(i18nonly("help_bw_cheatin_give"));
console.sendMessage(i18nonly("help_bw_cheatin_kill"));
console.sendMessage(i18nonly("help_bw_cheatin_destroybed"));
console.sendMessage(i18nonly("help_bw_cheatin_destroyallbeds"));
}
}

public void sendHelp(Player player) {
Expand Down Expand Up @@ -104,6 +111,10 @@ public void sendHelp(Player player) {
player.sendMessage(i18nonly("help_bw_cheat_destroyallbeds"));
player.sendMessage(i18nonly("help_bw_cheat_jointeam"));
player.sendMessage(i18nonly("help_bw_cheat_startemptygame"));
player.sendMessage(i18nonly("help_bw_cheatin_give"));
player.sendMessage(i18nonly("help_bw_cheatin_kill"));
player.sendMessage(i18nonly("help_bw_cheatin_destroybed"));
player.sendMessage(i18nonly("help_bw_cheatin_destroyallbeds"));
}

player.sendMessage(i18nonly("help_bw_admin_info"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1883,8 +1883,10 @@ public void run() {
}
}

teamSelectorInventory.destroy();
teamSelectorInventory = null;
if (teamSelectorInventory != null) {
teamSelectorInventory.destroy();
teamSelectorInventory = null;
}

if (gameScoreboard.getObjective("lobby") != null) {
gameScoreboard.getObjective("lobby").unregister();
Expand Down
6 changes: 5 additions & 1 deletion plugin/src/main/resources/languages/language_en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,8 @@ help_bw_cheat_destroyallbeds: "/bw cheat destroyallbeds - &cDestroys all beds in
help_bw_cheat_jointeam: "/bw cheat jointeam [team] - &cJoins an admin to the specified team or to current team ignoring its size (can also be used while the game is running, however the team must have some players)"
cheat_received_target_blocks_destroy: "&aBeds of all teams have been destroyed!"
kicking_in: "&cLeaving the arena in: &7%time%"
game_ended_too_early: "&cThe game ends too early. No one wins."
game_ended_too_early: "&cThe game ends too early. No one wins."
help_bw_cheatin_give: "/bw cheatIn <game> give <resource> <amount> <player> - &cGives player specified amount of resource"
help_bw_cheatin_kill: "/bw cheatIn <game> kill <player> - &cKills specified player"
help_bw_cheatin_destroybed: "/bw cheatIn <game> destroybed <team> - &cDestroys bed of the specific team"
help_bw_cheatin_destroyallbeds: "/bw cheatIn <game> destroyallbeds - &cDestroys all beds in the game"

0 comments on commit cd98595

Please sign in to comment.