From 2754d9f9ec697f297a3f29cd6ef5d829b274b076 Mon Sep 17 00:00:00 2001 From: Me4502 Date: Tue, 6 Dec 2016 14:19:44 +1000 Subject: [PATCH] Change adapter to allow for a 'isBest' system, meaning adapters can specify they are best used with version X. --- .../sponge/adapter/SpongeImplAdapter.java | 4 ++++ .../sponge/adapter/SpongeImplLoader.java | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/adapter/SpongeImplAdapter.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/adapter/SpongeImplAdapter.java index a626e76c9..9906d0c4d 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/adapter/SpongeImplAdapter.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/adapter/SpongeImplAdapter.java @@ -73,6 +73,10 @@ public interface SpongeImplAdapter { SpongeWorld getWorld(World world); + default boolean isBest() { + return true; + } + default Location adapt(org.spongepowered.api.world.Location loc, Vector3d rot) { Vector position = new Vector(loc.getX(), loc.getY(), loc.getZ()); diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/adapter/SpongeImplLoader.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/adapter/SpongeImplLoader.java index 0376902fe..2985175cf 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/adapter/SpongeImplLoader.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/adapter/SpongeImplLoader.java @@ -19,7 +19,9 @@ package com.sk89q.worldedit.sponge.adapter; +import com.google.common.collect.Lists; import com.sk89q.worldedit.util.io.Closer; +import org.spongepowered.api.Sponge; import java.io.File; import java.io.IOException; @@ -149,11 +151,12 @@ public class SpongeImplLoader { * @throws AdapterLoadException thrown if no adapter could be found */ public SpongeImplAdapter loadAdapter() throws AdapterLoadException { + List suitableAdapters = Lists.newArrayList(); for (String className : adapterCandidates) { try { Class cls = Class.forName(className); if (SpongeImplAdapter.class.isAssignableFrom(cls)) { - return (SpongeImplAdapter) cls.newInstance(); + suitableAdapters.add((SpongeImplAdapter) cls.newInstance()); } else { log.log(Level.WARNING, "Failed to load the Sponge adapter class '" + className + "' because it does not implement " + SpongeImplAdapter.class.getCanonicalName()); @@ -171,6 +174,14 @@ public class SpongeImplLoader { } } - throw new AdapterLoadException(LOAD_ERROR_MESSAGE); + if (suitableAdapters.isEmpty()) { + throw new AdapterLoadException(LOAD_ERROR_MESSAGE); + } else { + if (suitableAdapters.size() == 1) { + return suitableAdapters.get(0); + } else { + return suitableAdapters.stream().filter(SpongeImplAdapter::isBest).findFirst().orElse(suitableAdapters.get(0)); + } + } } }