diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2024-07-05 03:06:34 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2024-07-05 03:06:34 -0400 |
commit | ca576a1dd19247d1dc2ca294cce042a7a77ca296 (patch) | |
tree | c3d4b9e3b6c0d9224f83faa13e0bcb63d8463c55 | |
parent | c48825154e0741d3ca4ef52e00598d5372950d19 (diff) | |
download | LeaveCmd-ca576a1dd19247d1dc2ca294cce042a7a77ca296.tar.gz LeaveCmd-ca576a1dd19247d1dc2ca294cce042a7a77ca296.tar.bz2 LeaveCmd-ca576a1dd19247d1dc2ca294cce042a7a77ca296.zip |
Change to world substring matching
-rw-r--r-- | LeaveCmd/src/config.yml | 10 | ||||
-rw-r--r-- | LeaveCmd/src/me/sowgro/LeaveCmd/Leave.java | 53 | ||||
-rw-r--r-- | LeaveCmd/src/me/sowgro/LeaveCmd/LeaveCmd.java | 35 | ||||
-rw-r--r-- | LeaveCmd/src/me/sowgro/LeaveCmd/Main.java | 63 | ||||
-rw-r--r-- | LeaveCmd/src/plugin.yml | 9 | ||||
-rw-r--r-- | README.md | 27 |
6 files changed, 125 insertions, 72 deletions
diff --git a/LeaveCmd/src/config.yml b/LeaveCmd/src/config.yml index 6b0b2e2..d3342f6 100644 --- a/LeaveCmd/src/config.yml +++ b/LeaveCmd/src/config.yml @@ -1,7 +1,5 @@ +# World prefix : Leave command commands: - //ex: "command" - //hg: "hungergames leave" - -worlds: - //world: ex - //arena: hg
\ No newline at end of file + "ex-": "command" + #HG-Atlantis -> /hungergames leave + "HG-": "hungergames leave"
\ No newline at end of file diff --git a/LeaveCmd/src/me/sowgro/LeaveCmd/Leave.java b/LeaveCmd/src/me/sowgro/LeaveCmd/Leave.java new file mode 100644 index 0000000..275420c --- /dev/null +++ b/LeaveCmd/src/me/sowgro/LeaveCmd/Leave.java @@ -0,0 +1,53 @@ +package me.sowgro.LeaveCmd; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; + +import java.util.Collections; +import java.util.List; + +import static me.sowgro.LeaveCmd.Main.plugin; + +public class Leave implements CommandExecutor, TabCompleter { + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + + if (!(sender instanceof Player p)) { + sender.sendMessage("This cannot be run by the console"); + return false; + } + + ConfigurationSection cfgSection = plugin.getConfig().getConfigurationSection("commands"); + if (cfgSection == null) { + sender.sendMessage("The LeaveCmd config file is not setup correctly."); + return false; + } + + String worldName = p.getWorld().getName(); + for (String s : cfgSection.getKeys(false)) { + if (worldName.contains(s)) { + + String command = cfgSection.getString(s); + if (command == null) { + sender.sendMessage("An error occurred leaving the current game. "); + return false; + } + + p.performCommand(command); + return true; + } + } + sender.sendMessage("Unable to leave current game."); + return true; + } + + @Override + public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) { + return Collections.emptyList(); + } +} diff --git a/LeaveCmd/src/me/sowgro/LeaveCmd/LeaveCmd.java b/LeaveCmd/src/me/sowgro/LeaveCmd/LeaveCmd.java new file mode 100644 index 0000000..1651f4a --- /dev/null +++ b/LeaveCmd/src/me/sowgro/LeaveCmd/LeaveCmd.java @@ -0,0 +1,35 @@ +package me.sowgro.LeaveCmd; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +import java.util.Collections; +import java.util.List; + +import static me.sowgro.LeaveCmd.Main.plugin; + +public class LeaveCmd implements CommandExecutor, TabCompleter { + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (args[0].equals("reload")) { + plugin.reloadConfig(); + sender.sendMessage("Reloaded."); + return true; + } + + sender.sendMessage("Unknown command"); + return false; + } + + @Override + public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (args.length == 1 && label.equals("leavecmd")) { + return Collections.singletonList("reload"); + } + + return Collections.emptyList(); + } +} diff --git a/LeaveCmd/src/me/sowgro/LeaveCmd/Main.java b/LeaveCmd/src/me/sowgro/LeaveCmd/Main.java index 016c633..275ea3a 100644 --- a/LeaveCmd/src/me/sowgro/LeaveCmd/Main.java +++ b/LeaveCmd/src/me/sowgro/LeaveCmd/Main.java @@ -1,57 +1,24 @@ package me.sowgro.LeaveCmd; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -public class Main extends JavaPlugin -{ - @Override - public void onEnable() - { - this.saveDefaultConfig(); - //startup, reloads, plugin reloads - } - +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Set; + +public class Main extends JavaPlugin { + public static Main plugin; + @Override - public void onDisable() - { - //shutdown, reloads + public void onEnable() { + plugin = this; + Objects.requireNonNull(getCommand("leavecmd")).setExecutor(new LeaveCmd()); + Objects.requireNonNull(getCommand("leave")).setExecutor(new Leave()); } - - // /leave - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) - { - if (label.equalsIgnoreCase("leave")) - { - if (sender instanceof Player) - { - try - { - String world = ((Player) sender).getWorld().getName(); //gets world of player - String game = this.getConfig().getConfigurationSection("worlds").getString(world); //gets the game of that world - String command = this.getConfig().getConfigurationSection("commands").getString(game); //gets the command of that game - ((Player) sender).performCommand(command); //executes the command - } - catch(Exception e) - { - sender.sendMessage("The current game cannot be left."); - } - } - else - { - getLogger().info("This command cannot be used by the console"); - } - } - if (label.equalsIgnoreCase("leaveCmd")) - { - if (args[0].equalsIgnoreCase("reload")) - { - this.reloadConfig(); - sender.sendMessage("Reloaded."); - } - } - return false; - } -} +} diff --git a/LeaveCmd/src/plugin.yml b/LeaveCmd/src/plugin.yml index 3a2179e..801de36 100644 --- a/LeaveCmd/src/plugin.yml +++ b/LeaveCmd/src/plugin.yml @@ -2,16 +2,17 @@ main: me.sowgro.LeaveCmd.Main name: LeaveCmd author: sowgro version: 1.0 -api-version: 1.19 +api-version: 1.16 + commands: leave: - description: test + description: Leave the current game permission: leaveCmd.leave leaveCmd: - description: test2 permission: leaveCmd.admin + permissions: leaveCmd.leave: default: true - leaveCmd.admin: + leaveCmd.reload: default: op
\ No newline at end of file @@ -1,22 +1,21 @@ # LeaveCmd -Universal leave command for spigot 1.19 +Universal leave command for spigot 1.16+ Instead of players needing to remember /hg leave or /bw leave, they can just use /leave. The plugin decides what command to use based on the world the player is in. -## Commands: -- /leave -- /leavecmd reload +### Commands / Permissions -## Permissions: -- leaveCmd.leave -- leaveCmd.admin +| Command | Description | Permission | +|--------------------|------------------------|-------------------| +| `/leave` | Leave the current game | `leaveCmd.leave` | +| `/leavecmd reload` | Reload the config | `leavecmd.relaod` | -## Config: - commands: - //ex: "command" - //hg: "hungergames leave" - worlds: - //world: ex - //arena: hg +### Configuration +`/leave` will match against the first key that is a substring of the wold name. + +```yaml +commands: + "HG-": "hg leave" +``` |