Skip to content

Commit

Permalink
Refactor command abstraction and implement /geyser as help
Browse files Browse the repository at this point in the history
  • Loading branch information
Konicai committed Sep 5, 2023
1 parent 065abb0 commit acd53a4
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 126 deletions.
39 changes: 34 additions & 5 deletions core/src/main/java/org/geysermc/geyser/command/GeyserCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.meta.CommandMeta;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -35,7 +36,6 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public abstract class GeyserCommand implements org.geysermc.geyser.api.command.Command {

Expand Down Expand Up @@ -153,20 +153,49 @@ public final List<String> aliases() {
return Collections.unmodifiableList(aliases);
}

/**
* @return the first (literal) argument of this command, which comes before {@link #name()}.
*/
public String rootCommand() {
return "geyser";
}

/**
* Creates a new command builder with {@link #rootCommand()}, {@link #name()}, and {@link #aliases()} built on it.
* The Applicable from {@link #meta()} is also applied to the builder.
*/
@Contract(value = "_ -> new", pure = true)
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
public final Command.Builder<GeyserCommandSource> baseBuilder(CommandManager<GeyserCommandSource> manager) {
return manager.commandBuilder(rootCommand())
.literal(name, aliases.toArray(new String[0]))
.permission(permission)
.apply(meta());
}

/**
* @return an Applicable that applies {@link #BEDROCK_ONLY} and {@link #PLAYER_ONLY} as meta,
* according to {@link #isBedrockOnly()} and {@link #isExecutableOnConsole()} (respectively).
*/
public Command.Builder.Applicable<GeyserCommandSource> meta() {
return builder -> builder
.meta(BEDROCK_ONLY, isBedrockOnly())
.meta(PLAYER_ONLY, !isExecutableOnConsole())
.permission(permission);
.meta(PLAYER_ONLY, !isExecutableOnConsole());
}

/**
* Registers this command to the given command manager.
* This method may be overridden to register more than one command.
* <br><br>
* The default implementation is that {@link #baseBuilder(CommandManager)} with {@link #execute(CommandContext)}
* applied as the handler is registered to the manager.
*/
public void register(CommandManager<GeyserCommandSource> manager) {
manager.command(builder(manager));
manager.command(baseBuilder(manager).handler(this::execute));
}

/**
* Executes this command
* @param context the context with which this command should be executed
*/
public abstract void execute(CommandContext<GeyserCommandSource> context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

package org.geysermc.geyser.command.defaults;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.text.MinecraftLocale;

Expand All @@ -40,16 +40,15 @@ public AdvancedTooltipsCommand(String name, String description, String permissio
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(context -> context.getSender().connection().ifPresent(session -> {
String onOrOff = session.isAdvancedTooltips() ? "off" : "on";
session.setAdvancedTooltips(!session.isAdvancedTooltips());
session.sendMessage(ChatColor.BOLD + ChatColor.YELLOW
+ MinecraftLocale.getLocaleString("debug.prefix", session.locale())
+ " " + ChatColor.RESET
+ MinecraftLocale.getLocaleString("debug.advanced_tooltips." + onOrOff, session.locale()));
session.getInventoryTranslator().updateInventory(session, session.getPlayerInventory());
}));
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserSession session = context.getSender().connection().orElseThrow();

String onOrOff = session.isAdvancedTooltips() ? "off" : "on";
session.setAdvancedTooltips(!session.isAdvancedTooltips());
session.sendMessage(ChatColor.BOLD + ChatColor.YELLOW
+ MinecraftLocale.getLocaleString("debug.prefix", session.locale())
+ " " + ChatColor.RESET
+ MinecraftLocale.getLocaleString("debug.advanced_tooltips." + onOrOff, session.locale()));
session.getInventoryTranslator().updateInventory(session, session.getPlayerInventory());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

package org.geysermc.geyser.command.defaults;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.session.GeyserSession;

public class AdvancementsCommand extends GeyserCommand {

Expand All @@ -38,10 +38,8 @@ public AdvancementsCommand(String name, String description, String permission) {
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(context ->
context.getSender().connection().ifPresent(session ->
session.getAdvancementsCache().buildAndShowMenuForm()));
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserSession session = context.getSender().connection().orElseThrow();
session.getAdvancementsCache().buildAndShowMenuForm();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ public ConnectionTestCommand(GeyserImpl geyser, String name, String description,
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
public void register(CommandManager<GeyserCommandSource> manager) {
manager.command(baseBuilder(manager)
.argument(StringArgument.of(ADDRESS))
.argument(IntegerArgument.<GeyserCommandSource>builder(PORT)
.asOptionalWithDefault(19132)
.withMax(65535).withMin(0)
.build())
.handler(this::execute);
.handler(this::execute));
}

private void execute(CommandContext<GeyserCommandSource> context) {
@Override
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserCommandSource source = context.getSender();
String address = context.get(ADDRESS);
int port = context.get(PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ public class DumpCommand extends GeyserCommand {

public DumpCommand(GeyserImpl geyser, String name, String description, String permission) {
super(name, description, permission, TriState.NOT_SET);

this.geyser = geyser;
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
public void register(CommandManager<GeyserCommandSource> manager) {
manager.command(baseBuilder(manager)
.argument(createArgument())
.handler(this::execute);
.handler(this::execute));
}

private StringArrayArgument<GeyserCommandSource> createArgument() {
// todo suggestions might be broken
return StringArrayArgument.optional(ARGUMENTS, (context, input) -> SUGGESTIONS);
}

@Override
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserCommandSource source = context.getSender();
String[] args = context.getOrDefault(ARGUMENTS, new String[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

package org.geysermc.geyser.command.defaults;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.extension.Extension;
Expand All @@ -44,16 +42,10 @@ public class ExtensionsCommand extends GeyserCommand {

public ExtensionsCommand(GeyserImpl geyser, String name, String description, String permission) {
super(name, description, permission, TriState.TRUE);

this.geyser = geyser;
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(this::execute);
}

public void execute(CommandContext<GeyserCommandSource> context) {
GeyserCommandSource source = context.getSender();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,22 @@ public HelpCommand(GeyserImpl geyser, String name, String description, String pe
super(name, description, permission, TriState.TRUE);
this.baseCommand = baseCommand;
this.commands = commands.values();

this.aliases = Collections.singletonList("?");
}

@Override
public cloud.commandframework.Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(this::execute);
public void register(CommandManager<GeyserCommandSource> manager) {
super.register(manager);

// Also register just `/geyser`
manager.command(manager.commandBuilder(rootCommand())
.permission(permission())
.apply(meta())
.handler(this::execute));
}

private void execute(CommandContext<GeyserCommandSource> context) {
@Override
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserCommandSource source = context.getSender();
boolean bedrockPlayer = source.connection().isPresent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

package org.geysermc.geyser.command.defaults;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
Expand All @@ -42,21 +41,17 @@ public class ListCommand extends GeyserCommand {

public ListCommand(GeyserImpl geyser, String name, String description, String permission) {
super(name, description, permission, TriState.NOT_SET);

this.geyser = geyser;
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(context -> {
GeyserCommandSource source = context.getSender();
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserCommandSource source = context.getSender();

String message = GeyserLocale.getPlayerLocaleString("geyser.commands.list.message", source.locale(),
geyser.getSessionManager().size(),
geyser.getSessionManager().getAllSessions().stream().map(GeyserSession::bedrockUsername).collect(Collectors.joining(" ")));
String message = GeyserLocale.getPlayerLocaleString("geyser.commands.list.message", source.locale(),
geyser.getSessionManager().size(),
geyser.getSessionManager().getAllSessions().stream().map(GeyserSession::bedrockUsername).collect(Collectors.joining(" ")));

source.sendMessage(message);
});
source.sendMessage(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

package org.geysermc.geyser.command.defaults;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
Expand All @@ -40,8 +39,8 @@ public OffhandCommand(GeyserImpl geyser, String name, String description, String
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(context -> context.getSender().connection().ifPresent(GeyserSession::requestOffhandSwap));
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserSession session = context.getSender().connection().orElseThrow();
session.requestOffhandSwap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

package org.geysermc.geyser.command.defaults;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
Expand All @@ -43,14 +42,11 @@ public ReloadCommand(GeyserImpl geyser, String name, String description, String
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(context -> {
GeyserCommandSource source = context.getSender();
source.sendMessage(GeyserLocale.getPlayerLocaleString("geyser.commands.reload.message", source.locale()));
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserCommandSource source = context.getSender();
source.sendMessage(GeyserLocale.getPlayerLocaleString("geyser.commands.reload.message", source.locale()));

geyser.getSessionManager().disconnectAll("geyser.commands.reload.kick");
geyser.reload();
});
geyser.getSessionManager().disconnectAll("geyser.commands.reload.kick");
geyser.reload();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

package org.geysermc.geyser.command.defaults;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.util.SettingsUtils;

public class SettingsCommand extends GeyserCommand {
Expand All @@ -40,10 +40,8 @@ public SettingsCommand(GeyserImpl geyser, String name, String description, Strin
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(context ->
context.getSender().connection().ifPresent(session ->
session.sendForm(SettingsUtils.buildForm(session))));
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserSession session = context.getSender().connection().orElseThrow();
session.sendForm(SettingsUtils.buildForm(session));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

package org.geysermc.geyser.command.defaults;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.context.CommandContext;
import com.github.steveice10.mc.protocol.data.game.ClientCommand;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundClientCommandPacket;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.session.GeyserSession;

public class StatisticsCommand extends GeyserCommand {

Expand All @@ -41,12 +41,11 @@ public StatisticsCommand(GeyserImpl geyser, String name, String description, Str
}

@Override
public Command.Builder<GeyserCommandSource> builder(CommandManager<GeyserCommandSource> manager) {
return super.builder(manager)
.handler(context -> context.getSender().connection().ifPresent(session -> {
session.setWaitingForStatistics(true);
ServerboundClientCommandPacket packet = new ServerboundClientCommandPacket(ClientCommand.STATS);
session.sendDownstreamPacket(packet);
}));
public void execute(CommandContext<GeyserCommandSource> context) {
GeyserSession session = context.getSender().connection().orElseThrow();

session.setWaitingForStatistics(true);
ServerboundClientCommandPacket packet = new ServerboundClientCommandPacket(ClientCommand.STATS);
session.sendDownstreamPacket(packet);
}
}
Loading

0 comments on commit acd53a4

Please sign in to comment.