mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Format and comment code
makes the code look nicer
This commit is contained in:
parent
ed97de417e
commit
037dfc2003
@ -19,12 +19,15 @@ import java.util.Locale;
|
|||||||
/**
|
/**
|
||||||
* Command that make a player change gamemode, made in
|
* Command that make a player change gamemode, made in
|
||||||
* the style of the vanilla /gamemode command.
|
* the style of the vanilla /gamemode command.
|
||||||
|
*
|
||||||
|
* @see https://minecraft.fandom.com/wiki/Commands/gamemode
|
||||||
*/
|
*/
|
||||||
public class GamemodeCommand extends Command {
|
public class GamemodeCommand extends Command {
|
||||||
|
|
||||||
public GamemodeCommand() {
|
public GamemodeCommand() {
|
||||||
super("gamemode", "gm");
|
super("gamemode", "gm");
|
||||||
|
|
||||||
|
//GameMode parameter
|
||||||
ArgumentEnum<GameMode> gamemode = ArgumentType.Enum("gamemode", GameMode.class).setFormat(ArgumentEnum.Format.LOWER_CASED);
|
ArgumentEnum<GameMode> gamemode = ArgumentType.Enum("gamemode", GameMode.class).setFormat(ArgumentEnum.Format.LOWER_CASED);
|
||||||
gamemode.setCallback((sender, exception) -> {
|
gamemode.setCallback((sender, exception) -> {
|
||||||
sender.sendMessage(
|
sender.sendMessage(
|
||||||
@ -33,57 +36,102 @@ public class GamemodeCommand extends Command {
|
|||||||
.append(Component.text("!")), MessageType.SYSTEM);
|
.append(Component.text("!")), MessageType.SYSTEM);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Targets parameter, can accept multiple players
|
||||||
ArgumentEntity player = ArgumentType.Entity("targets").onlyPlayers(true);
|
ArgumentEntity player = ArgumentType.Entity("targets").onlyPlayers(true);
|
||||||
|
|
||||||
|
//Upon invalid usage, print the correct usage of the command to the sender
|
||||||
setDefaultExecutor((sender, context) -> {
|
setDefaultExecutor((sender, context) -> {
|
||||||
sender.sendMessage(Component.text("Usage: /" + context.getCommandName() + " <gamemode> [targets]", NamedTextColor.RED), MessageType.SYSTEM);
|
//The used alias
|
||||||
|
String commandName = context.getCommandName();
|
||||||
|
|
||||||
|
sender.sendMessage(Component.text("Usage: /" + commandName + " <gamemode> [targets]", NamedTextColor.RED), MessageType.SYSTEM);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Command Syntax for /gamemode <gamemode>
|
||||||
addSyntax((sender, context) -> {
|
addSyntax((sender, context) -> {
|
||||||
|
//Limit execution to players only
|
||||||
if (!sender.isPlayer()) {
|
if (!sender.isPlayer()) {
|
||||||
sender.sendMessage(Component.text("Please run this command in-game.", NamedTextColor.RED));
|
sender.sendMessage(Component.text("Please run this command in-game.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
//Check permission, this could be replaced with hasPermission
|
||||||
if (sender.asPlayer().getPermissionLevel() < 2) {
|
if (sender.asPlayer().getPermissionLevel() < 2) {
|
||||||
sender.sendMessage(Component.text("You don't have permission to use this command.", NamedTextColor.RED));
|
sender.sendMessage(Component.text("You don't have permission to use this command.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Arguments
|
||||||
GameMode mode = context.get(gamemode);
|
GameMode mode = context.get(gamemode);
|
||||||
|
|
||||||
|
//Set the gamemode for the sender
|
||||||
executeSelf(sender.asPlayer(), mode);
|
executeSelf(sender.asPlayer(), mode);
|
||||||
}, gamemode);
|
}, gamemode);
|
||||||
|
|
||||||
|
//Command Syntax for /gamemode <gamemode> [targets]
|
||||||
addSyntax((sender, context) -> {
|
addSyntax((sender, context) -> {
|
||||||
|
//Check permission for players only
|
||||||
|
//This allows the console to use this syntax too
|
||||||
if (sender.isPlayer() && sender.asPlayer().getPermissionLevel() < 2) {
|
if (sender.isPlayer() && sender.asPlayer().getPermissionLevel() < 2) {
|
||||||
sender.sendMessage(Component.text("You don't have permission to use this command.", NamedTextColor.RED));
|
sender.sendMessage(Component.text("You don't have permission to use this command.", NamedTextColor.RED));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Arguments
|
||||||
EntityFinder finder = context.get(player);
|
EntityFinder finder = context.get(player);
|
||||||
GameMode mode = context.get(gamemode);
|
GameMode mode = context.get(gamemode);
|
||||||
executeOthers(sender.asPlayer(), mode, finder.find(sender));
|
|
||||||
|
//Set the gamemode for the targets
|
||||||
|
executeOthers(sender, mode, finder.find(sender));
|
||||||
}, gamemode, player);
|
}, gamemode, player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the gamemode for the specified entities, and
|
||||||
|
* notifies them (and the sender) in the chat.
|
||||||
|
*/
|
||||||
private void executeOthers(CommandSender sender, GameMode mode, List<Entity> entities) {
|
private void executeOthers(CommandSender sender, GameMode mode, List<Entity> entities) {
|
||||||
if (entities.size() == 0) {
|
if (entities.size() == 0) {
|
||||||
|
//If there are no players that could be modified, display an error message
|
||||||
if (sender.isPlayer()) sender.sendMessage(Component.translatable("argument.entity.notfound.player", NamedTextColor.RED), MessageType.SYSTEM);
|
if (sender.isPlayer()) sender.sendMessage(Component.translatable("argument.entity.notfound.player", NamedTextColor.RED), MessageType.SYSTEM);
|
||||||
else sender.sendMessage(Component.text("No player was found", NamedTextColor.RED), MessageType.SYSTEM);
|
else sender.sendMessage(Component.text("No player was found", NamedTextColor.RED), MessageType.SYSTEM);
|
||||||
} else for (Entity entity : entities) {
|
} else for (Entity entity : entities) {
|
||||||
if (entity instanceof Player) {
|
if (entity instanceof Player) {
|
||||||
Player p = (Player) entity;
|
Player p = (Player) entity;
|
||||||
if (p == sender) {
|
if (p == sender) {
|
||||||
|
//If the player is the same as the sender, call
|
||||||
|
//executeSelf to display one message instead of two
|
||||||
executeSelf(sender.asPlayer(), mode);
|
executeSelf(sender.asPlayer(), mode);
|
||||||
} else {
|
} else {
|
||||||
p.setGameMode(mode);
|
p.setGameMode(mode);
|
||||||
p.sendMessage(Component.translatable("gameMode.changed").args(Component.translatable("gameMode." + mode.name().toLowerCase(Locale.ROOT))), MessageType.SYSTEM);
|
|
||||||
sender.sendMessage(Component.translatable("commands.gamemode.success.other").args(p.getDisplayName() == null ? p.getName() : p.getDisplayName(), Component.translatable("gameMode." + mode.name().toLowerCase(Locale.ROOT))), MessageType.SYSTEM);
|
//Create necessary components
|
||||||
|
String gamemodeString = "gameMode." + mode.name().toLowerCase(Locale.ROOT);
|
||||||
|
Component gamemodeComponent = Component.translatable(gamemodeString);
|
||||||
|
Component playerName = p.getDisplayName() == null ? p.getName() : p.getDisplayName();
|
||||||
|
|
||||||
|
//Send a message to the changed player
|
||||||
|
p.sendMessage(Component.translatable("gameMode.changed", gamemodeComponent), MessageType.SYSTEM);
|
||||||
|
|
||||||
|
//Send a message to the sender
|
||||||
|
sender.sendMessage(Component.translatable("commands.gamemode.success.other", playername, gamemodeComponent), MessageType.SYSTEM);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the gamemode for the executing Player, and
|
||||||
|
* notifies them in the chat.
|
||||||
|
*/
|
||||||
private void executeSelf(Player sender, GameMode mode) {
|
private void executeSelf(Player sender, GameMode mode) {
|
||||||
sender.setGameMode(mode);
|
sender.setGameMode(mode);
|
||||||
sender.sendMessage(Component.translatable("commands.gamemode.success.self").args(Component.translatable("gameMode." + mode.name().toLowerCase(Locale.ROOT))), MessageType.SYSTEM);
|
|
||||||
|
//The translation keys 'gameMode.survival', 'gameMode.creative', etc.
|
||||||
|
//correspond to the translated game mode names.
|
||||||
|
String gamemodeString = "gameMode." + mode.name().toLowerCase(Locale.ROOT);
|
||||||
|
Component gamemodeComponent = Component.translatable(gamemodeString);
|
||||||
|
|
||||||
|
//Send the translated message to the player.
|
||||||
|
sender.sendMessage(Component.translatable("commands.gamemode.success.self", gamemodeComponent), MessageType.SYSTEM);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user