Implement support for adding Geyser subcommands

This commit is contained in:
RednedEpic 2022-01-16 15:09:53 -06:00
parent 57345fa102
commit 30303d5f16
90 changed files with 1207 additions and 402 deletions

View file

@ -26,6 +26,12 @@
<version>3.19.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>event-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
@ -33,10 +39,10 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>event-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.27</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.geysermc</groupId>

View file

@ -29,6 +29,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.api.Geyser;
import org.geysermc.api.GeyserApiBase;
import org.geysermc.geyser.api.command.CommandManager;
import org.geysermc.geyser.api.connection.GeyserConnection;
import org.geysermc.geyser.api.event.EventBus;
import org.geysermc.geyser.api.extension.ExtensionManager;
@ -89,6 +90,13 @@ public interface GeyserApi extends GeyserApiBase {
*/
ExtensionManager extensionManager();
/**
* Gets the {@link CommandManager}.
*
* @return the command manager
*/
CommandManager commandManager();
/**
* Gets the {@link EventBus} for handling
* Geyser events.

View file

@ -0,0 +1,124 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.command;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.GeyserApi;
import java.util.Collections;
import java.util.List;
/**
* Represents a command.
*/
public interface Command {
/**
* Gets the command name.
*
* @return the command name
*/
@NonNull
String name();
/**
* Gets the command description.
*
* @return the command description
*/
@NonNull
String description();
/**
* Gets the permission node associated with
* this command.
*
* @return the permission node for this command
*/
@NonNull
String permission();
/**
* Gets the aliases for this command.
*
* @return the aliases for this command
*/
@NonNull
List<String> aliases();
/**
* Gets if this command is executable on console.
*
* @return if this command is executable on console
*/
boolean isExecutableOnConsole();
/**
* Gets the subcommands associated with this
* command. Mainly used within the Geyser Standalone
* GUI to know what subcommands are supported.
*
* @return the subcommands associated with this command
*/
@NonNull
default List<String> subCommands() {
return Collections.emptyList();
}
/**
* Used to send a deny message to Java players if this command can only be used by Bedrock players.
*
* @return true if this command can only be used by Bedrock players.
*/
default boolean isBedrockOnly() {
return false;
}
static <T extends CommandSource> Command.Builder<T> builder(Class<T> sourceType) {
return GeyserApi.api().commandManager().provideBuilder(sourceType);
}
interface Builder<T extends CommandSource> {
Builder<T> name(String name);
Builder<T> description(String description);
Builder<T> permission(String permission);
Builder<T> aliases(List<String> aliases);
Builder<T> executableOnConsole(boolean executableOnConsole);
Builder<T> subCommands(List<String> subCommands);
Builder<T> bedrockOnly(boolean bedrockOnly);
Builder<T> executor(CommandExecutor<T> executor);
Command build();
}
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.command;
/**
* Handles executing a command.
*
* @param <T> the command source
*/
public interface CommandExecutor<T extends CommandSource> {
/**
* Executes the given {@link Command} with the given
* {@link CommandSource}.
*
* @param source the command source
* @param command the command
* @param args the arguments
*/
void execute(T source, Command command, String[] args);
}

View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.command;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Map;
/**
* Manages Bedrock commands within Geyser.
*/
public abstract class CommandManager {
/**
* Provides a {@link Command.Builder}.
*
* @param sourceType the command source type
* @param <T> the type
* @return a command builder
*/
protected abstract <T extends CommandSource> Command.Builder<T> provideBuilder(Class<T> sourceType);
/**
* Registers the given {@link Command}.
*
* @param command the command to register
*/
public abstract void register(@NonNull Command command);
/**
* Unregisters the given {@link Command}.
*
* @param command the command to unregister
*/
public abstract void unregister(@NonNull Command command);
/**
* Gets all the registered {@link Command}s.
*
* @return all the registered commands
*/
@NonNull
public abstract Map<String, Command> commands();
}

View file

@ -0,0 +1,79 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.command;
/**
* Represents an instance capable of sending commands.
*/
public interface CommandSource {
/**
* The name of the command source.
*
* @return the name of the command source
*/
String name();
/**
* Sends the given message to the command source
*
* @param message the message to send
*/
void sendMessage(String message);
/**
* Sends the given messages to the command source
*
* @param messages the messages to send
*/
default void sendMessage(String[] messages) {
for (String message : messages) {
sendMessage(message);
}
}
/**
* If this source is the console.
*
* @return true if this source is the console
*/
boolean isConsole();
/**
* Returns the locale of the command source.
*
* @return the locale of the command source.
*/
String locale();
/**
* Checks if this command source has the given permission
*
* @param permission The permission node to check
* @return true if this command source has a permission
*/
boolean hasPermission(String permission);
}

View file

@ -26,9 +26,10 @@
package org.geysermc.geyser.api.connection;
import org.geysermc.api.connection.Connection;
import org.geysermc.geyser.api.command.CommandSource;
/**
* Represents a player connection used in Geyser.
*/
public interface GeyserConnection extends Connection {
public interface GeyserConnection extends Connection, CommandSource {
}

View file

@ -40,18 +40,7 @@ public interface EventBus {
/**
* Subscribes to the given event see {@link EventSubscription}.
*
* @param eventClass the class of the event
* @param consumer the consumer for handling the event
* @param <T> the event class
* @return the event subscription
*/
@NonNull
<T extends Event> EventSubscription<T> subscribe(@NonNull Class<T> eventClass, @NonNull Consumer<? super T> consumer);
/**
* Subscribes to the given event see {@link EventSubscription}.
*
* The difference between this method and {@link #subscribe(Class, Consumer)}
* The difference between this method and {@link ExtensionEventBus#subscribe(Class, Consumer)}
* is that this method takes in an extension parameter which allows for
* the event to be unsubscribed upon extension disable and reloads.
*
@ -79,6 +68,13 @@ public interface EventBus {
*/
void register(@NonNull Extension extension, @NonNull Object eventHolder);
/**
* Unregisters all events from a given {@link Extension}.
*
* @param extension the extension
*/
void unregisterAll(@NonNull Extension extension);
/**
* Fires the given {@link Event}.
*

View file

@ -63,7 +63,7 @@ public interface EventSubscription<T extends Event> {
*
* @return the extension that owns this subscription
*/
@Nullable
@NonNull
Extension owner();
/**

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.function.Consumer;
/**
* An {@link EventBus} with additional methods that implicitly
* set the extension instance.
*
*/
public interface ExtensionEventBus extends EventBus {
/**
* Subscribes to the given event see {@link EventSubscription}.
*
* @param eventClass the class of the event
* @param consumer the consumer for handling the event
* @param <T> the event class
* @return the event subscription
*/
@NonNull
<T extends Event> EventSubscription<T> subscribe(@NonNull Class<T> eventClass, @NonNull Consumer<? super T> consumer);
/**
* Registers events for the given listener.
*
* @param eventHolder the listener
*/
void register(@NonNull Object eventHolder);
/**
* Unregisters all events for this extension.
*/
void unregisterAll();
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event.connection;
import lombok.RequiredArgsConstructor;
import org.geysermc.geyser.api.connection.GeyserConnection;
import org.geysermc.geyser.api.event.Event;
/**
* An event that contains a {@link GeyserConnection}.
*/
@RequiredArgsConstructor
public abstract class ConnectionEvent implements Event {
private final GeyserConnection connection;
/**
* Gets the {@link GeyserConnection}.
*
* @return the connection
*/
public GeyserConnection connection() {
return this.connection;
}
}

View file

@ -0,0 +1,81 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event.downstream;
import org.geysermc.geyser.api.connection.GeyserConnection;
import org.geysermc.geyser.api.event.Cancellable;
import org.geysermc.geyser.api.event.connection.ConnectionEvent;
import java.util.Set;
/**
* Called when the downstream server defines the commands available on the server.
*/
public class ServerDefineCommandsEvent extends ConnectionEvent implements Cancellable {
private final Set<? extends CommandInfo> commands;
private boolean cancelled;
public ServerDefineCommandsEvent(GeyserConnection connection, Set<? extends CommandInfo> commands) {
super(connection);
this.commands = commands;
}
/**
* A mutable collection of the commands sent over.
*
* @return a mutable collection of the commands sent over
*/
public Set<? extends CommandInfo> commands() {
return this.commands;
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public interface CommandInfo {
/**
* Gets the name of the command.
*
* @return the name of the command
*/
String name();
/**
* Gets the description of the command.
*
* @return the description of the command
*/
String description();
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event.lifecycle;
import org.geysermc.geyser.api.command.Command;
import org.geysermc.geyser.api.command.CommandManager;
import org.geysermc.geyser.api.event.Event;
import java.util.Map;
/**
* Called when commands are defined within Geyser.
*
* @param commandManager the command manager
* @param commands a mutable list of the currently
* registered default commands
*/
public record GeyserDefineCommandsEvent(CommandManager commandManager, Map<String, Command> commands) implements Event {
}

View file

@ -27,6 +27,7 @@ package org.geysermc.geyser.api.extension;
import org.geysermc.api.GeyserApiBase;
import org.geysermc.geyser.api.GeyserApi;
import org.geysermc.geyser.api.event.ExtensionEventBus;
import java.nio.file.Path;
@ -80,6 +81,15 @@ public interface Extension {
return this.extensionLoader().dataFolder(this);
}
/**
* Gets the {@link ExtensionEventBus}.
*
* @return the extension event bus
*/
default ExtensionEventBus eventBus() {
return this.extensionLoader().eventBus(this);
}
/**
* Gets the {@link ExtensionManager}.
*

View file

@ -26,6 +26,7 @@
package org.geysermc.geyser.api.extension;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.event.ExtensionEventBus;
import java.nio.file.Path;
@ -68,6 +69,15 @@ public abstract class ExtensionLoader {
@NonNull
protected abstract ExtensionDescription description(@NonNull Extension extension);
/**
* Gets the given {@link Extension}'s {@link ExtensionEventBus}.
*
* @param extension the extension
* @return the extension's event bus
*/
@NonNull
protected abstract ExtensionEventBus eventBus(@NonNull Extension extension);
/**
* Gets the {@link ExtensionLogger} for the given {@link Extension}.
*