From 3f577f41281618b23e022ba8ed727ce9d41c3d8e Mon Sep 17 00:00:00 2001 From: rtm516 Date: Wed, 24 Jan 2024 21:20:30 +0000 Subject: [PATCH] Add fetching MC versions and Console from the extensions API (#4168) * Add fetching MC versions and Console from the extensions API * Address reviews, expose custom MinecraftVersion interface * Rename of McVersion -> MinecraftVersionImpl; proper nonnull annotation * fluent consoleCommandSource(), change MinecraftVersion#name() to versionString() * Javadocs adjustments * Create impl package and move `MinecraftVersionImpl` there * api version bump --------- Co-authored-by: onebeastchris --- .../org/geysermc/geyser/api/GeyserApi.java | 26 ++++++++++ .../geyser/api/util/MinecraftVersion.java | 49 +++++++++++++++++++ .../java/org/geysermc/geyser/GeyserImpl.java | 30 ++++++++++-- .../geyser/impl/MinecraftVersionImpl.java | 31 ++++++++++++ gradle.properties | 2 +- 5 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 api/src/main/java/org/geysermc/geyser/api/util/MinecraftVersion.java create mode 100644 core/src/main/java/org/geysermc/geyser/impl/MinecraftVersionImpl.java diff --git a/api/src/main/java/org/geysermc/geyser/api/GeyserApi.java b/api/src/main/java/org/geysermc/geyser/api/GeyserApi.java index 0d6007c1c..a9327d0db 100644 --- a/api/src/main/java/org/geysermc/geyser/api/GeyserApi.java +++ b/api/src/main/java/org/geysermc/geyser/api/GeyserApi.java @@ -29,12 +29,14 @@ 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.CommandSource; import org.geysermc.geyser.api.connection.GeyserConnection; import org.geysermc.geyser.api.event.EventBus; import org.geysermc.geyser.api.event.EventRegistrar; import org.geysermc.geyser.api.extension.ExtensionManager; import org.geysermc.geyser.api.network.BedrockListener; import org.geysermc.geyser.api.network.RemoteServer; +import org.geysermc.geyser.api.util.MinecraftVersion; import org.geysermc.geyser.api.util.PlatformType; import java.nio.file.Path; @@ -134,6 +136,30 @@ public interface GeyserApi extends GeyserApiBase { @NonNull PlatformType platformType(); + /** + * Gets the version of Java Minecraft that is supported. + * + * @return the supported version of Java Minecraft + */ + @NonNull + MinecraftVersion supportedJavaVersion(); + + /** + * Gets a list of Bedrock Minecraft versions that are supported. + * + * @return the list of supported Bedrock Minecraft versions + */ + @NonNull + List supportedBedrockVersions(); + + /** + * Gets the {@link CommandSource} for the console. + * + * @return the console command source + */ + @NonNull + CommandSource consoleCommandSource(); + /** * Gets the current {@link GeyserApiBase} instance. * diff --git a/api/src/main/java/org/geysermc/geyser/api/util/MinecraftVersion.java b/api/src/main/java/org/geysermc/geyser/api/util/MinecraftVersion.java new file mode 100644 index 000000000..34a4b59af --- /dev/null +++ b/api/src/main/java/org/geysermc/geyser/api/util/MinecraftVersion.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019-2023 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.util; + +import org.checkerframework.checker.nullness.qual.NonNull; + +/** + * Represents a Minecraft version. + */ +public interface MinecraftVersion { + + /** + * Gets the Minecraft version as a String. + * Example: "1.20.2", or "1.20.40/1.20.41" + * + * @return the version string + */ + @NonNull String versionString(); + + /** + * Gets the protocol version of this Minecraft version. + * + * @return the protocol version + */ + int protocolVersion(); +} diff --git a/core/src/main/java/org/geysermc/geyser/GeyserImpl.java b/core/src/main/java/org/geysermc/geyser/GeyserImpl.java index aef2288d6..13212b161 100644 --- a/core/src/main/java/org/geysermc/geyser/GeyserImpl.java +++ b/core/src/main/java/org/geysermc/geyser/GeyserImpl.java @@ -42,7 +42,11 @@ import net.kyori.adventure.text.format.NamedTextColor; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; +import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec; import org.geysermc.api.Geyser; +import org.geysermc.geyser.api.command.CommandSource; +import org.geysermc.geyser.api.util.MinecraftVersion; +import org.geysermc.geyser.api.util.PlatformType; import org.geysermc.cumulus.form.Form; import org.geysermc.cumulus.form.util.FormBuilder; import org.geysermc.erosion.packet.Packets; @@ -60,14 +64,15 @@ import org.geysermc.geyser.api.event.lifecycle.GeyserShutdownEvent; import org.geysermc.geyser.api.network.AuthType; import org.geysermc.geyser.api.network.BedrockListener; import org.geysermc.geyser.api.network.RemoteServer; -import org.geysermc.geyser.api.util.PlatformType; import org.geysermc.geyser.command.GeyserCommandManager; import org.geysermc.geyser.configuration.GeyserConfiguration; import org.geysermc.geyser.entity.EntityDefinitions; import org.geysermc.geyser.erosion.UnixSocketClientListener; import org.geysermc.geyser.event.GeyserEventBus; import org.geysermc.geyser.extension.GeyserExtensionManager; +import org.geysermc.geyser.impl.MinecraftVersionImpl; import org.geysermc.geyser.level.WorldManager; +import org.geysermc.geyser.network.GameProtocol; import org.geysermc.geyser.network.netty.GeyserServer; import org.geysermc.geyser.registry.BlockRegistries; import org.geysermc.geyser.registry.Registries; @@ -111,8 +116,8 @@ public class GeyserImpl implements GeyserApi { .enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); public static final String NAME = "Geyser"; - public static final String GIT_VERSION = "${gitVersion}"; // A fallback for running in IDEs - public static final String VERSION = "${version}"; // A fallback for running in IDEs + public static final String GIT_VERSION = "${gitVersion}"; + public static final String VERSION = "${version}"; public static final String BUILD_NUMBER = "${buildNumber}"; public static final String BRANCH = "${branch}"; @@ -683,6 +688,25 @@ public class GeyserImpl implements GeyserApi { return platformType; } + @Override + public @NonNull MinecraftVersion supportedJavaVersion() { + return new MinecraftVersionImpl(GameProtocol.getJavaMinecraftVersion(), GameProtocol.getJavaProtocolVersion()); + } + + @Override + public @NonNull List supportedBedrockVersions() { + ArrayList versions = new ArrayList<>(); + for (BedrockCodec codec : GameProtocol.SUPPORTED_BEDROCK_CODECS) { + versions.add(new MinecraftVersionImpl(codec.getMinecraftVersion(), codec.getProtocolVersion())); + } + return Collections.unmodifiableList(versions); + } + + @Override + public @NonNull CommandSource consoleCommandSource() { + return getLogger(); + } + public int buildNumber() { if (!this.isProductionEnvironment()) { return 0; diff --git a/core/src/main/java/org/geysermc/geyser/impl/MinecraftVersionImpl.java b/core/src/main/java/org/geysermc/geyser/impl/MinecraftVersionImpl.java new file mode 100644 index 000000000..121d33c64 --- /dev/null +++ b/core/src/main/java/org/geysermc/geyser/impl/MinecraftVersionImpl.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019-2024 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.impl; + +import org.geysermc.geyser.api.util.MinecraftVersion; + +public record MinecraftVersionImpl(String versionString, int protocolVersion) implements MinecraftVersion { +} diff --git a/gradle.properties b/gradle.properties index b6425f76d..93db310ff 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,5 +7,5 @@ org.gradle.vfs.watch=false group=org.geysermc id=geyser -version=2.2.0-SNAPSHOT +version=2.2.1-SNAPSHOT description=Allows for players from Minecraft: Bedrock Edition to join Minecraft: Java Edition servers. \ No newline at end of file