Skip to content

Commit

Permalink
Refactor NMS to use PlatformProviders
Browse files Browse the repository at this point in the history
  • Loading branch information
md5sha256 committed Jan 8, 2024
1 parent c1d754d commit b5f1636
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Version> 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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -88,8 +86,8 @@ protected void configure() {
// Resolve the NMS implementation
Class<? extends NMS> nmsAdapter;
try {
nmsAdapter = (Class<? extends NMS>) 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."
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
//

package se.hyperver.hyperverse.platform;

import org.jetbrains.annotations.NotNull;
import se.hyperver.hyperverse.util.NMS;

@FunctionalInterface
public interface PlatformProvider {

@NotNull Class<? extends NMS> providePlatform() throws PlatformProvisionException;

}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
//

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);
}

}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
//

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<? extends NMS> 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

package se.hyperver.hyperverse.spigotnms.unsupported;
package se.hyperver.hyperverse.platform.unsupported;

import org.bukkit.Location;
import org.bukkit.World;
Expand Down

0 comments on commit b5f1636

Please sign in to comment.