From b5f16368e46ea92c0d1930863fb40e65e061b882 Mon Sep 17 00:00:00 2001 From: md5sha256 <42793301+md5sha256@users.noreply.github.com> Date: Sun, 7 Jan 2024 16:25:10 +0100 Subject: [PATCH] Refactor NMS to use PlatformProviders --- .../se/hyperver/hyperverse/Hyperverse.java | 19 +++++++- .../hyperverse/modules/HyperverseModule.java | 20 ++++---- .../hyperverse/platform/PlatformProvider.java | 28 +++++++++++ .../platform/PlatformProvisionException.java | 46 +++++++++++++++++++ .../platform/ReflectionPlatformProvider.java | 45 ++++++++++++++++++ .../util/versioning/VersionUtil.java | 2 - .../v1_17_1}/NMSImpl.java | 23 +--------- .../v1_18_2}/NMSImpl.java | 2 +- .../v1_19_4}/NMSImpl.java | 2 +- .../v1_20_4}/NMSImpl.java | 2 +- .../unsupported/NMSImpl.java | 2 +- 11 files changed, 151 insertions(+), 40 deletions(-) create mode 100644 hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/PlatformProvider.java create mode 100644 hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/PlatformProvisionException.java create mode 100644 hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/ReflectionPlatformProvider.java rename hyperverse-nms-1-17/src/main/java/se/hyperver/hyperverse/{spigotnms/v1_17_R1 => platform/v1_17_1}/NMSImpl.java (93%) rename hyperverse-nms-1-18/src/main/java/se/hyperver/hyperverse/{spigotnms/v1_18_R2 => platform/v1_18_2}/NMSImpl.java (99%) rename hyperverse-nms-1-19/src/main/java/se/hyperver/hyperverse/{spigotnms/v1_19_R3 => platform/v1_19_4}/NMSImpl.java (99%) rename hyperverse-nms-1-20/src/main/java/se/hyperver/hyperverse/{spigotnms/v1_20_R3 => platform/v1_20_4}/NMSImpl.java (99%) rename hyperverse-nms-unsupported/src/main/java/se/hyperver/hyperverse/{spigotnms => platform}/unsupported/NMSImpl.java (97%) diff --git a/hyperverse-core/src/main/java/se/hyperver/hyperverse/Hyperverse.java b/hyperverse-core/src/main/java/se/hyperver/hyperverse/Hyperverse.java index 68209e0e..d560d8d8 100644 --- a/hyperverse-core/src/main/java/se/hyperver/hyperverse/Hyperverse.java +++ b/hyperverse-core/src/main/java/se/hyperver/hyperverse/Hyperverse.java @@ -56,8 +56,12 @@ import se.hyperver.hyperverse.modules.HyperWorldFactory; import se.hyperver.hyperverse.modules.HyperverseModule; import se.hyperver.hyperverse.modules.TaskFactoryModule; +import se.hyperver.hyperverse.platform.PlatformProvider; +import se.hyperver.hyperverse.platform.ReflectionPlatformProvider; import se.hyperver.hyperverse.service.internal.SafeTeleportService; import se.hyperver.hyperverse.util.MessageUtil; +import se.hyperver.hyperverse.util.versioning.Version; +import se.hyperver.hyperverse.util.versioning.VersionUtil; import se.hyperver.hyperverse.world.HyperWorld; import se.hyperver.hyperverse.world.HyperWorldCreator; import se.hyperver.hyperverse.world.WorldConfiguration; @@ -69,6 +73,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.UUID; import java.util.logging.Logger; @@ -87,6 +92,13 @@ public final class Hyperverse extends JavaPlugin implements HyperverseAPI, Liste private final PluginFeatureManager pluginFeatureManager = new PluginFeatureManager(Bukkit.getServer()); private final ServicePipeline servicePipeline = ServicePipeline.builder().build(); + private final List supportedVersions = List.of( + Version.parse("1.17.1"), + Version.parse("1.18.2"), + Version.parse("1.19.4"), + Version.parse("1.20.4") + ); + private WorldManager worldManager; private Injector injector; private HyperDatabase hyperDatabase; @@ -120,10 +132,15 @@ public void onEnable() { throw new RuntimeException("Could not create Hyperverse main directory"); } } + Version currentMcVersion = VersionUtil.parseMinecraftVersion(Bukkit.getBukkitVersion()); + if (!this.supportedVersions.contains(currentMcVersion)) { + throw new UnsupportedOperationException("Current mc version: " + currentMcVersion + "is not supported"); + } + PlatformProvider platformProvider = new ReflectionPlatformProvider(currentMcVersion); try { this.injector = Guice.createInjector( Stage.PRODUCTION, - new HyperverseModule(getLogger(), this.servicePipeline, Bukkit.getServer(), this), + new HyperverseModule(getLogger(), this.servicePipeline, Bukkit.getServer(), platformProvider, this), new TaskFactoryModule() ); } catch (final Exception e) { diff --git a/hyperverse-core/src/main/java/se/hyperver/hyperverse/modules/HyperverseModule.java b/hyperverse-core/src/main/java/se/hyperver/hyperverse/modules/HyperverseModule.java index fe46cc4e..c21db5ae 100644 --- a/hyperverse-core/src/main/java/se/hyperver/hyperverse/modules/HyperverseModule.java +++ b/hyperverse-core/src/main/java/se/hyperver/hyperverse/modules/HyperverseModule.java @@ -23,7 +23,6 @@ import com.google.inject.Singleton; import com.google.inject.assistedinject.FactoryModuleBuilder; import com.google.inject.internal.ProviderMethodsModule; -import org.bukkit.Bukkit; import org.bukkit.Server; import org.bukkit.WorldCreator; import org.bukkit.plugin.Plugin; @@ -38,7 +37,9 @@ import se.hyperver.hyperverse.flags.FlagContainer; import se.hyperver.hyperverse.flags.GlobalWorldFlagContainer; import se.hyperver.hyperverse.flags.WorldFlagContainer; -import se.hyperver.hyperverse.spigotnms.unsupported.NMSImpl; +import se.hyperver.hyperverse.platform.PlatformProvider; +import se.hyperver.hyperverse.platform.PlatformProvisionException; +import se.hyperver.hyperverse.platform.unsupported.NMSImpl; import se.hyperver.hyperverse.teleportation.SimpleTeleportationManager; import se.hyperver.hyperverse.teleportation.TeleportationManager; import se.hyperver.hyperverse.util.HyperConfigShouldGroupProfiles; @@ -55,28 +56,25 @@ public final class HyperverseModule extends AbstractModule { - private static final @NonNull String CRAFTSERVER_CLASS_NAME = Bukkit.getServer().getClass().getName(); - private static final @NonNull String PACKAGE_VERSION = - CRAFTSERVER_CLASS_NAME.substring( - "org.bukkit.craftbukkit.".length(), - CRAFTSERVER_CLASS_NAME.indexOf('.', "org.bukkit.craftbukkit.".length()) - ); - private final Logger logger; private final Hyperverse hyperverse; private final ServicePipeline servicePipeline; private final Server server; + private final PlatformProvider platformProvider; + public HyperverseModule( final @NonNull Logger logger, final @NonNull ServicePipeline servicePipeline, final @NonNull Server server, + final @NonNull PlatformProvider platformProvider, final @NonNull Hyperverse hyperverse ) { this.logger = logger; this.hyperverse = hyperverse; this.servicePipeline = servicePipeline; this.server = server; + this.platformProvider = platformProvider; } @Override @@ -88,8 +86,8 @@ protected void configure() { // Resolve the NMS implementation Class nmsAdapter; try { - nmsAdapter = (Class) Class.forName("se.hyperver.hyperverse.spigotnms." + PACKAGE_VERSION + ".NMSImpl"); - } catch (final ClassNotFoundException ex) { + nmsAdapter = this.platformProvider.providePlatform(); + } catch (final PlatformProvisionException ex) { new RuntimeException("Server version unsupported", ex).printStackTrace(); this.logger.severe( "Could not find a compatible NMS adapter. Some of Hyperverse's functionality will fail exceptionally." diff --git a/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/PlatformProvider.java b/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/PlatformProvider.java new file mode 100644 index 00000000..9c9f1572 --- /dev/null +++ b/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/PlatformProvider.java @@ -0,0 +1,28 @@ +// +// Hyperverse - A minecraft world management plugin +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +package se.hyperver.hyperverse.platform; + +import org.jetbrains.annotations.NotNull; +import se.hyperver.hyperverse.util.NMS; + +@FunctionalInterface +public interface PlatformProvider { + + @NotNull Class providePlatform() throws PlatformProvisionException; + +} diff --git a/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/PlatformProvisionException.java b/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/PlatformProvisionException.java new file mode 100644 index 00000000..c8196e36 --- /dev/null +++ b/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/PlatformProvisionException.java @@ -0,0 +1,46 @@ +// +// Hyperverse - A minecraft world management plugin +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +package se.hyperver.hyperverse.platform; + +public class PlatformProvisionException extends Exception { + + public PlatformProvisionException() { + } + + public PlatformProvisionException(final String message) { + super(message); + } + + public PlatformProvisionException(final String message, final Throwable cause) { + super(message, cause); + } + + public PlatformProvisionException(final Throwable cause) { + super(cause); + } + + public PlatformProvisionException( + final String message, + final Throwable cause, + final boolean enableSuppression, + final boolean writableStackTrace + ) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/ReflectionPlatformProvider.java b/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/ReflectionPlatformProvider.java new file mode 100644 index 00000000..a513ec23 --- /dev/null +++ b/hyperverse-core/src/main/java/se/hyperver/hyperverse/platform/ReflectionPlatformProvider.java @@ -0,0 +1,45 @@ +// +// Hyperverse - A minecraft world management plugin +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +package se.hyperver.hyperverse.platform; + +import org.jetbrains.annotations.NotNull; +import se.hyperver.hyperverse.util.NMS; +import se.hyperver.hyperverse.util.versioning.Version; + +import java.util.Locale; + +public class ReflectionPlatformProvider implements PlatformProvider { + + private final Version minecraftVersion; + public ReflectionPlatformProvider(@NotNull final Version version) { + this.minecraftVersion = version; + } + + @Override + public @NotNull Class providePlatform() throws PlatformProvisionException { + String expectedPackage = minecraftVersion.original().toLowerCase(Locale.ENGLISH).replace('.', '_'); + String packageName = "se.hyperver.spigotnms.v" + expectedPackage; + try { + Class clazz = Class.forName(packageName + ".NMSImpl"); + return clazz.asSubclass(NMS.class); + } catch (ReflectiveOperationException ex) { + throw new PlatformProvisionException("Could not provide platform for version: " + minecraftVersion + "!", ex); + } + } + +} diff --git a/hyperverse-core/src/main/java/se/hyperver/hyperverse/util/versioning/VersionUtil.java b/hyperverse-core/src/main/java/se/hyperver/hyperverse/util/versioning/VersionUtil.java index 4df73a79..dba7dc71 100644 --- a/hyperverse-core/src/main/java/se/hyperver/hyperverse/util/versioning/VersionUtil.java +++ b/hyperverse-core/src/main/java/se/hyperver/hyperverse/util/versioning/VersionUtil.java @@ -21,8 +21,6 @@ public final class VersionUtil { - public static final VersionData MC_1_17_1 = new VersionData(1, 17, 1); - private VersionUtil() { throw new IllegalStateException("Cannot instantiate static utility class"); } diff --git a/hyperverse-nms-1-17/src/main/java/se/hyperver/hyperverse/spigotnms/v1_17_R1/NMSImpl.java b/hyperverse-nms-1-17/src/main/java/se/hyperver/hyperverse/platform/v1_17_1/NMSImpl.java similarity index 93% rename from hyperverse-nms-1-17/src/main/java/se/hyperver/hyperverse/spigotnms/v1_17_R1/NMSImpl.java rename to hyperverse-nms-1-17/src/main/java/se/hyperver/hyperverse/platform/v1_17_1/NMSImpl.java index e7c01b7d..42a93937 100644 --- a/hyperverse-nms-1-17/src/main/java/se/hyperver/hyperverse/spigotnms/v1_17_R1/NMSImpl.java +++ b/hyperverse-nms-1-17/src/main/java/se/hyperver/hyperverse/platform/v1_17_1/NMSImpl.java @@ -15,7 +15,7 @@ // along with this program. If not, see . // -package se.hyperver.hyperverse.spigotnms.v1_17_R1; +package se.hyperver.hyperverse.platform.v1_17_1; import co.aikar.taskchain.TaskChainFactory; import com.google.inject.Inject; @@ -33,27 +33,6 @@ import net.minecraft.world.level.entity.EntityLookup; import net.minecraft.world.level.entity.PersistentEntitySectionManager; import net.minecraft.world.level.portal.PortalForcer; -import java.io.InputStream; -import java.io.OutputStream; -import java.lang.reflect.Field; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Objects; -import java.util.Optional; -import net.minecraft.BlockUtil; -import net.minecraft.core.BlockPos; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.DoubleTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.NbtIo; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.level.dimension.DimensionType; -import net.minecraft.world.level.entity.EntityLookup; -import net.minecraft.world.level.entity.PersistentEntitySectionManager; -import net.minecraft.world.level.portal.PortalForcer; -import net.minecraft.world.phys.Vec3; import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.core.filter.RegexFilter; diff --git a/hyperverse-nms-1-18/src/main/java/se/hyperver/hyperverse/spigotnms/v1_18_R2/NMSImpl.java b/hyperverse-nms-1-18/src/main/java/se/hyperver/hyperverse/platform/v1_18_2/NMSImpl.java similarity index 99% rename from hyperverse-nms-1-18/src/main/java/se/hyperver/hyperverse/spigotnms/v1_18_R2/NMSImpl.java rename to hyperverse-nms-1-18/src/main/java/se/hyperver/hyperverse/platform/v1_18_2/NMSImpl.java index fdedcc6a..924429fd 100644 --- a/hyperverse-nms-1-18/src/main/java/se/hyperver/hyperverse/spigotnms/v1_18_R2/NMSImpl.java +++ b/hyperverse-nms-1-18/src/main/java/se/hyperver/hyperverse/platform/v1_18_2/NMSImpl.java @@ -15,7 +15,7 @@ // along with this program. If not, see . // -package se.hyperver.hyperverse.spigotnms.v1_18_R2; +package se.hyperver.hyperverse.platform.v1_18_2; import co.aikar.taskchain.TaskChainFactory; import com.google.inject.Inject; diff --git a/hyperverse-nms-1-19/src/main/java/se/hyperver/hyperverse/spigotnms/v1_19_R3/NMSImpl.java b/hyperverse-nms-1-19/src/main/java/se/hyperver/hyperverse/platform/v1_19_4/NMSImpl.java similarity index 99% rename from hyperverse-nms-1-19/src/main/java/se/hyperver/hyperverse/spigotnms/v1_19_R3/NMSImpl.java rename to hyperverse-nms-1-19/src/main/java/se/hyperver/hyperverse/platform/v1_19_4/NMSImpl.java index be3c677d..435cd01f 100644 --- a/hyperverse-nms-1-19/src/main/java/se/hyperver/hyperverse/spigotnms/v1_19_R3/NMSImpl.java +++ b/hyperverse-nms-1-19/src/main/java/se/hyperver/hyperverse/platform/v1_19_4/NMSImpl.java @@ -15,7 +15,7 @@ // along with this program. If not, see . // -package se.hyperver.hyperverse.spigotnms.v1_19_R3; +package se.hyperver.hyperverse.platform.v1_19_4; import co.aikar.taskchain.TaskChainFactory; import com.google.inject.Inject; diff --git a/hyperverse-nms-1-20/src/main/java/se/hyperver/hyperverse/spigotnms/v1_20_R3/NMSImpl.java b/hyperverse-nms-1-20/src/main/java/se/hyperver/hyperverse/platform/v1_20_4/NMSImpl.java similarity index 99% rename from hyperverse-nms-1-20/src/main/java/se/hyperver/hyperverse/spigotnms/v1_20_R3/NMSImpl.java rename to hyperverse-nms-1-20/src/main/java/se/hyperver/hyperverse/platform/v1_20_4/NMSImpl.java index ea8d1ff4..50570230 100644 --- a/hyperverse-nms-1-20/src/main/java/se/hyperver/hyperverse/spigotnms/v1_20_R3/NMSImpl.java +++ b/hyperverse-nms-1-20/src/main/java/se/hyperver/hyperverse/platform/v1_20_4/NMSImpl.java @@ -15,7 +15,7 @@ // along with this program. If not, see . // -package se.hyperver.hyperverse.spigotnms.v1_20_R3; +package se.hyperver.hyperverse.platform.v1_20_4; import co.aikar.taskchain.TaskChainFactory; import com.google.inject.Inject; diff --git a/hyperverse-nms-unsupported/src/main/java/se/hyperver/hyperverse/spigotnms/unsupported/NMSImpl.java b/hyperverse-nms-unsupported/src/main/java/se/hyperver/hyperverse/platform/unsupported/NMSImpl.java similarity index 97% rename from hyperverse-nms-unsupported/src/main/java/se/hyperver/hyperverse/spigotnms/unsupported/NMSImpl.java rename to hyperverse-nms-unsupported/src/main/java/se/hyperver/hyperverse/platform/unsupported/NMSImpl.java index 4244976c..8c751e30 100644 --- a/hyperverse-nms-unsupported/src/main/java/se/hyperver/hyperverse/spigotnms/unsupported/NMSImpl.java +++ b/hyperverse-nms-unsupported/src/main/java/se/hyperver/hyperverse/platform/unsupported/NMSImpl.java @@ -15,7 +15,7 @@ // along with this program. If not, see . // -package se.hyperver.hyperverse.spigotnms.unsupported; +package se.hyperver.hyperverse.platform.unsupported; import org.bukkit.Location; import org.bukkit.World;