diff options
author | Myles <43725835+MylesAndMore@users.noreply.github.com> | 2023-06-17 20:48:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-17 20:48:51 +0200 |
commit | ee11892063a796c602948676be4de22d3f717131 (patch) | |
tree | b15435cb788f0a221a7393739908b4c84baa51ad /src/main/java/com/MylesAndMore/Tumble/commands | |
parent | 3c48bd3f9587ae9459d789f70ba1ebaaf691209b (diff) | |
parent | 19d8ffbc6659c7de13b81a587dae7081078649c6 (diff) | |
download | Tumble-ee11892063a796c602948676be4de22d3f717131.tar.gz Tumble-ee11892063a796c602948676be4de22d3f717131.tar.bz2 Tumble-ee11892063a796c602948676be4de22d3f717131.zip |
Merge pull request #7 from MylesAndMore/dev
refactoring!
Diffstat (limited to 'src/main/java/com/MylesAndMore/Tumble/commands')
5 files changed, 385 insertions, 0 deletions
diff --git a/src/main/java/com/MylesAndMore/Tumble/commands/Reload.java b/src/main/java/com/MylesAndMore/Tumble/commands/Reload.java new file mode 100644 index 0000000..ffc6dd8 --- /dev/null +++ b/src/main/java/com/MylesAndMore/Tumble/commands/Reload.java @@ -0,0 +1,22 @@ +package com.MylesAndMore.Tumble.commands; + +import com.MylesAndMore.Tumble.plugin.Constants; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +public class Reload implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + if (sender.hasPermission("tumble.reload")) { + Constants.getPlugin().reloadConfig(); + sender.sendMessage(ChatColor.GREEN + "Tumble configuration reloaded successfully."); + } + else { + sender.sendMessage(ChatColor.RED + Constants.getPermissionMessage()); + } + return true; + } +} diff --git a/src/main/java/com/MylesAndMore/Tumble/commands/SetAutoStart.java b/src/main/java/com/MylesAndMore/Tumble/commands/SetAutoStart.java new file mode 100644 index 0000000..b3da74e --- /dev/null +++ b/src/main/java/com/MylesAndMore/Tumble/commands/SetAutoStart.java @@ -0,0 +1,94 @@ +package com.MylesAndMore.Tumble.commands; + +import com.MylesAndMore.Tumble.plugin.Constants; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + +public class SetAutoStart implements CommandExecutor{ + @Override + public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + if (sender.hasPermission("tumble.autostart")) { + if (Constants.getGameWorld() != null) { + if (Constants.getLobbyWorld() != null) { + if (args.length == 2) { + // Check the player # argument and parse it into an int + int args0; + try { + args0 = Integer.parseInt(args[0]); + } catch (NumberFormatException nfe){ + sender.sendMessage(ChatColor.RED + "Player amount must be a valid number."); + return true; + } catch (Exception e){ + sender.sendMessage(ChatColor.RED + "Invalid player amount."); + return true; + } + // PlayerAmount & enable/disable were entered + if ((args0 >= 2) && (args0 <= 8)) { + if (Objects.equals(args[1], "enable")) { + // Write values to the config + Constants.getPlugin().getConfig().set("autoStart.players", args0); + Constants.getPlugin().getConfig().set("autoStart.enabled", true); + Constants.getPlugin().saveConfig(); + sender.sendMessage(ChatColor.GREEN + "Configuration saved!"); + sender.sendMessage(ChatColor.GREEN + "Run " + ChatColor.GRAY + "/tumble:reload " + ChatColor.GREEN + "the changes to take effect."); + } + else if (Objects.equals(args[1], "disable")) { + Constants.getPlugin().getConfig().set("autoStart.players", args0); + Constants.getPlugin().getConfig().set("autoStart.enabled", false); + Constants.getPlugin().saveConfig(); + sender.sendMessage(ChatColor.GREEN + "Configuration saved!"); + sender.sendMessage(ChatColor.GREEN + "Run " + ChatColor.GRAY + "/tumble:reload " + ChatColor.GREEN + "the changes to take effect."); + } + else { + return false; + } + } + else { + sender.sendMessage(ChatColor.RED + "Please enter a player amount between two and eight!"); + } + } + else if (args.length == 1) { + // Only PlayerAmount was entered + int args0; + try { + args0 = Integer.parseInt(args[0]); + } catch (NumberFormatException nfe){ + sender.sendMessage(ChatColor.RED + "Player amount must be a valid number."); + return true; + } catch (Exception e){ + sender.sendMessage(ChatColor.RED + "Invalid player amount."); + return true; + } + if ((args0 >= 2) && (args0 <= 8)) { + Constants.getPlugin().getConfig().set("autoStart.players", args0); + Constants.getPlugin().saveConfig(); + sender.sendMessage(ChatColor.GREEN + "Configuration saved!"); + sender.sendMessage(ChatColor.GREEN + "Run " + ChatColor.GRAY + "/tumble:reload " + ChatColor.GREEN + "the changes to take effect."); + } + else { + sender.sendMessage(ChatColor.RED + "Please enter a player amount between two and eight!"); + } + } + else { + return false; + } + } + else { + sender.sendMessage(ChatColor.RED + "Please link a lobby world first!"); + } + } + else { + sender.sendMessage(ChatColor.RED + "Please link a game world first!"); + } + } + else { + sender.sendMessage(ChatColor.RED + Constants.getPermissionMessage()); + } + return true; + } +} diff --git a/src/main/java/com/MylesAndMore/Tumble/commands/SetWinnerLoc.java b/src/main/java/com/MylesAndMore/Tumble/commands/SetWinnerLoc.java new file mode 100644 index 0000000..38e6444 --- /dev/null +++ b/src/main/java/com/MylesAndMore/Tumble/commands/SetWinnerLoc.java @@ -0,0 +1,110 @@ +package com.MylesAndMore.Tumble.commands; + +import com.MylesAndMore.Tumble.plugin.Constants; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class SetWinnerLoc implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + if (sender.hasPermission("tumble.winlocation")) { + if (Constants.getLobbyWorld() != null) { + if (sender instanceof Player) { + // Check the sender entered the correct number of args + if (args.length == 3) { + double args0 = 0; + double args1 = 0; + double args2 = 0; + try { + args0 = Double.parseDouble(args[0]); + args1 = Double.parseDouble(args[1]); + args2 = Double.parseDouble(args[2]); + } catch (NumberFormatException nfe){ + sender.sendMessage(ChatColor.RED + "Input arguments must be valid numbers."); + } catch (Exception e){ + sender.sendMessage(ChatColor.RED + "Invalid input arguments."); + } + // Check if any of the args were 0 (this will cause future problems, so we prevent it here) + if (!((args0 == 0) || (args1 == 0) || (args2 == 0))) { + Constants.getPlugin().getConfig().set("winnerTeleport.x", args0); + Constants.getPlugin().getConfig().set("winnerTeleport.y", args1); + Constants.getPlugin().getConfig().set("winnerTeleport.z", args2); + Constants.getPlugin().saveConfig(); + sender.sendMessage(ChatColor.GREEN + "Win location successfully set!"); + sender.sendMessage(ChatColor.GREEN + "Run " + ChatColor.GRAY + "/tumble:reload " + ChatColor.GREEN + "the changes to take effect."); + } + else { + sender.sendMessage(ChatColor.RED + "Your coordinates cannot be zero!"); + sender.sendMessage(ChatColor.RED + "Use something like 0.5 (the middle of the block) instead."); + } + } + // If the sender entered no args, use their current location + else if (args.length == 0) { + Location senderPos = ((Player) sender).getLocation(); + // if so, check if any of their locations are zero + if (!((senderPos.getX() == 0) || (senderPos.getY() == 0) || (senderPos.getZ() == 0))) { + // set the config values to their current pos + Constants.getPlugin().getConfig().set("winnerTeleport.x", senderPos.getX()); + Constants.getPlugin().getConfig().set("winnerTeleport.y", senderPos.getY()); + Constants.getPlugin().getConfig().set("winnerTeleport.z", senderPos.getZ()); + Constants.getPlugin().saveConfig(); + sender.sendMessage(ChatColor.GREEN + "Win location successfully set!"); + sender.sendMessage(ChatColor.GREEN + "Run " + ChatColor.GRAY + "/tumble:reload " + ChatColor.GREEN + "the changes to take effect."); + } + else { + sender.sendMessage(ChatColor.RED + "Your coordinates cannot be zero!"); + sender.sendMessage(ChatColor.RED + "Use something like 0.5 (the middle of the block) instead."); + } + } + else { + return false; + } + } + else if (sender instanceof ConsoleCommandSender) { + if (args.length == 3) { + double args0 = 0; + double args1 = 0; + double args2 = 0; + try { + args0 = Double.parseDouble(args[0]); + args1 = Double.parseDouble(args[1]); + args2 = Double.parseDouble(args[2]); + } catch (NumberFormatException nfe){ + sender.sendMessage(ChatColor.RED + "Input arguments must be valid numbers."); + } catch (Exception e){ + sender.sendMessage(ChatColor.RED + "Invalid input arguments."); + } + if (!((args0 == 0) || (args1 == 0) || (args2 == 0))) { + Constants.getPlugin().getConfig().set("winnerTeleport.x", args0); + Constants.getPlugin().getConfig().set("winnerTeleport.y", args1); + Constants.getPlugin().getConfig().set("winnerTeleport.z", args2); + Constants.getPlugin().saveConfig(); + sender.sendMessage(ChatColor.GREEN + "Win location successfully set!"); + sender.sendMessage(ChatColor.GREEN + "Run " + ChatColor.GRAY + "/tumble:reload " + ChatColor.GREEN + "the changes to take effect."); + } + else { + sender.sendMessage(ChatColor.RED + "Your coordinates cannot be zero!"); + sender.sendMessage(ChatColor.RED + "Use something like 0.5 (the middle of the block) instead."); + } + } + else { + return false; + } + } + } + else { + sender.sendMessage(ChatColor.RED + "Please link a lobby world first!"); + } + } + else { + sender.sendMessage(ChatColor.RED + Constants.getPermissionMessage()); + } + return true; + } +} diff --git a/src/main/java/com/MylesAndMore/Tumble/commands/SetWorldConfig.java b/src/main/java/com/MylesAndMore/Tumble/commands/SetWorldConfig.java new file mode 100644 index 0000000..90e0a96 --- /dev/null +++ b/src/main/java/com/MylesAndMore/Tumble/commands/SetWorldConfig.java @@ -0,0 +1,74 @@ +package com.MylesAndMore.Tumble.commands; + +import com.MylesAndMore.Tumble.plugin.Constants; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.GameRule; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + +public class SetWorldConfig implements CommandExecutor { + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + // Catch for null arguments + if (args.length == 2) { + if (sender.hasPermission("tumble.link")){ + // Initialize vars for their respective command arguments + String world = args[0]; + String worldType = args[1]; + if (Objects.equals(worldType, "lobby")) { + // Check if the world is actually a world on the server + if (Bukkit.getWorld(world) != null) { + // Check if the world has already been configured + if (!Objects.equals(Constants.getGameWorld(), world)) { + // Set the specified value of the world in the config under lobbyWorld + Constants.getPlugin().getConfig().set("lobbyWorld", world); + Constants.getPlugin().saveConfig(); + sender.sendMessage(ChatColor.GREEN + "Lobby world successfully linked: " + ChatColor.GRAY + world); + sender.sendMessage(ChatColor.GREEN + "Please restart your server for the changes to take effect; " + ChatColor.RED + "reloading the plugin is insufficient!"); + } + else { + sender.sendMessage(ChatColor.RED + "That world has already been linked, please choose/create another world!"); + } + } + else { + sender.sendMessage(ChatColor.RED + "Failed to find a world named " + ChatColor.GRAY + world); + } + } + else if (Objects.equals(args[1], "game")) { + if (Bukkit.getWorld(world) != null) { + if (!Objects.equals(Constants.getLobbyWorld(), world)) { + Constants.getPlugin().getConfig().set("gameWorld", world); + Constants.getPlugin().saveConfig(); + // Set the gamerule of doImmediateRespawn in the gameWorld for later + Objects.requireNonNull(Bukkit.getWorld(world)).setGameRule(GameRule.DO_IMMEDIATE_RESPAWN, true); + Objects.requireNonNull(Bukkit.getWorld(world)).setGameRule(GameRule.KEEP_INVENTORY, true); + sender.sendMessage(ChatColor.GREEN + "Game world successfully linked: " + ChatColor.GRAY + world); + sender.sendMessage(ChatColor.GREEN + "Please restart your server for the changes to take effect; " + ChatColor.RED + "reloading the plugin is insufficient!"); + } + else { + sender.sendMessage(ChatColor.RED + "That world has already been linked, please choose/create another world!"); + } + } + else { + sender.sendMessage(ChatColor.RED + "Failed to find a world named " + ChatColor.GRAY + world); + } + } + else { + sender.sendMessage(ChatColor.RED + "Allowed world types are " + ChatColor.GRAY + "lobby " + ChatColor.RED + "and " + ChatColor.GRAY + "game" + ChatColor.RED + "."); + } + } + else { + sender.sendMessage(ChatColor.RED + Constants.getPermissionMessage()); + } + } + else { + return false; + } + return true; + } +} diff --git a/src/main/java/com/MylesAndMore/Tumble/commands/StartGame.java b/src/main/java/com/MylesAndMore/Tumble/commands/StartGame.java new file mode 100644 index 0000000..706b33a --- /dev/null +++ b/src/main/java/com/MylesAndMore/Tumble/commands/StartGame.java @@ -0,0 +1,85 @@ +package com.MylesAndMore.Tumble.commands; + +import com.MylesAndMore.Tumble.game.Game; +import com.MylesAndMore.Tumble.plugin.Constants; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + +public class StartGame implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + if (sender.hasPermission("tumble.start")) { + if (Constants.getLobbyWorld() != null) { + if (Constants.getPlayersInLobby().size() > 1) { + if (Constants.getGameWorld() != null) { + if (!Objects.equals(Game.getGame().getGameState(), "waiting")) { + sender.sendMessage(ChatColor.BLUE + "Generating layers, please wait."); + // Use multiverse to load game world--if the load was successful, start game + if (Constants.getMVWorldManager().loadWorld(Constants.getGameWorld())) { + // If there is no starting argument, + if (args.length == 0) { + // pull which gamemode to initiate from the config file + if (!Game.getGame().startGame(Constants.getGameType())) { + // Sender feedback for if the game failed to start + if (Objects.equals(Game.getGame().getGameState(), "starting")) { + sender.sendMessage(ChatColor.RED + "A game is already starting!"); + } + else if (Objects.equals(Game.getGame().getGameState(), "running")) { + sender.sendMessage(ChatColor.RED + "A game is already running!"); + } + else { + sender.sendMessage(ChatColor.RED + "Failed to recognize game of type " + ChatColor.GRAY + Constants.getPlugin().getConfig().getString("gameMode")); + } + } + } + // If there was an argument for gameType, pass that instead + else { + if (!Game.getGame().startGame(args[0])) { + // Sender feedback for if the game failed to start + if (Objects.equals(Game.getGame().getGameState(), "starting")) { + sender.sendMessage(ChatColor.RED + "A game is already starting!"); + } + else if (Objects.equals(Game.getGame().getGameState(), "running")) { + sender.sendMessage(ChatColor.RED + "A game is already running!"); + } + else { + sender.sendMessage(ChatColor.RED + "Failed to recognize game of type " + ChatColor.GRAY + args[0]); + } + } + } + } + // If load was unsuccessful, give feedback + // Note: this should not occur unless the config file was edited externally, + // because the plugin prevents adding "worlds" that are not actually present to the config. + else { + sender.sendMessage(ChatColor.RED + "Failed to find a world named " + ChatColor.GRAY + Constants.getGameWorld()); + sender.sendMessage(ChatColor.RED + "Is the configuration file correct?"); + } + } + else { + sender.sendMessage(ChatColor.RED + "A game is already queued to begin!"); + } + } + else { + sender.sendMessage(ChatColor.RED + "Please link a game world first!"); + } + } + else { + sender.sendMessage(ChatColor.RED + "You can't start a game with yourself!"); + } + } + else { + sender.sendMessage(ChatColor.RED + "Please link a lobby world first!"); + } + } + else { + sender.sendMessage(ChatColor.RED + Constants.getPermissionMessage()); + } + return true; + } +} |