Add a proper gamemode command

This commit is contained in:
Telesphoreo 2022-04-23 16:37:00 -05:00
parent 8126caf184
commit edeecb51f4
No known key found for this signature in database
GPG Key ID: B5CDDEBA526C0130
2 changed files with 103 additions and 2 deletions

View File

@ -0,0 +1,103 @@
package dev.plex.command.impl;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.CommandFailException;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.event.GameModeUpdateEvent;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@CommandParameters(name = "gamemode", usage = "/<command> <creative | survival | adventure | default | spectator> [player]", description = "Change your gamemode", aliases = "gm, egamemode, egamemode")
@CommandPermissions(level = Rank.OP, permission = "plex.gamemode", source = RequiredCommandSource.ANY)
public class GamemodeCMD extends PlexCommand
{
private GameMode gamemode;
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args)
{
if (args.length == 0)
{
return usage();
}
switch (args[0].toLowerCase())
{
case "creative", "c", "1" ->
{
gamemode = GameMode.CREATIVE;
update(sender, playerSender, GameMode.CREATIVE);
return null;
}
case "survival", "s", "0" ->
{
gamemode = GameMode.SURVIVAL;
update(sender, playerSender, GameMode.SURVIVAL);
return null;
}
case "adventure", "a", "2" ->
{
gamemode = GameMode.ADVENTURE;
update(sender, playerSender, GameMode.ADVENTURE);
return null;
}
case "default", "d", "5" ->
{
gamemode = plugin.getServer().getDefaultGameMode();
update(sender, playerSender, plugin.getServer().getDefaultGameMode());
return null;
}
case "spectator", "sp", "3", "6" ->
{
gamemode = GameMode.SPECTATOR;
checkRank(sender, Rank.ADMIN, "plex.gamemode.spectator");
update(sender, playerSender, GameMode.SPECTATOR);
return null;
}
}
if (args.length > 1)
{
checkRank(sender, Rank.ADMIN, "plex.gamemode.others");
Player player = getNonNullPlayer(args[1]);
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, player, gamemode));
}
return null;
}
private void update(CommandSender sender, Player playerSender, GameMode gameMode)
{
if (isConsole(sender))
{
throw new CommandFailException(PlexUtils.messageString("consoleMustDefinePlayer"));
}
if (!(playerSender == null))
{
Bukkit.getServer().getPluginManager().callEvent(new GameModeUpdateEvent(sender, playerSender.getPlayer(), gameMode));
}
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
if (args.length == 1)
{
return Arrays.asList("creative", "survival", "adventure", "spectator", "default");
}
if (args.length == 2)
{
return PlexUtils.getPlayerNameList();
}
return Collections.emptyList();
}
}

View File

@ -52,6 +52,4 @@ commands:
- "m:e:weather:_" - "m:e:weather:_"
- "m:e:worldborder:<gray>The worldborder does not need to be changed. This command is disabled." - "m:e:worldborder:<gray>The worldborder does not need to be changed. This command is disabled."
- "r:a:^(co|core|coreprotect) (rb|rollback|l|lookup|rl|reload):_" - "r:a:^(co|core|coreprotect) (rb|rollback|l|lookup|rl|reload):_"
- "r:a:^(gamemode|gm) [^ ]+ .*:<gray>You cannot change the gamemode of other players!"
- "r:a:^(gamemode|gm|egamemode|egm) (spectator|sp|3):<gray>You cannot use spectator mode!"
- "r:e:^[A-z]*:[A-z]*::<gray>Plugin specific commands are disabled." - "r:e:^[A-z]*:[A-z]*::<gray>Plugin specific commands are disabled."