diff --git a/SpigotCore_Main/src/de/steamwar/providers/BauSystemProvider.java b/SpigotCore_Main/src/de/steamwar/providers/BauSystemProvider.java new file mode 100644 index 0000000..b59f3d9 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/providers/BauSystemProvider.java @@ -0,0 +1,56 @@ +package de.steamwar.providers; + +import de.steamwar.providers.impl.*; +import org.bukkit.Bukkit; + +import java.util.function.Consumer; +import java.util.function.Function; + +public interface BauSystemProvider { + + static T use(Function function, T def) { + return BauSystemProviderImplementor.use(function, def); + } + + static void use(Consumer consumer) { + BauSystemProviderImplementor.use(consumer); + } + + int getOwner(); + + class BauSystemProviderImplementor { + private BauSystemProviderImplementor() {} + + static boolean hasBausystem; + static BauSystemProvider bauSystemProvider; + + static { + try { + Class.forName("de.steamwar.bausystem.BauSystem"); + hasBausystem = true; + switch (Bukkit.getPluginManager().getPlugin("BauSystem").getDescription().getVersion()) { + case "2.0": + bauSystemProvider = new BauSystem2Provider(); + break; + case "1.0": + bauSystemProvider = new BauSystem1Provider(); + break; + default: + hasBausystem = false; + } + } catch (Exception e) { + hasBausystem = false; + } + } + + static T use(Function function, T def) { + if(!hasBausystem) return def; + return function.apply(bauSystemProvider); + } + + static void use(Consumer consumer) { + if(!hasBausystem) return; + consumer.accept(bauSystemProvider); + } + } +} diff --git a/SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem1Provider.java b/SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem1Provider.java new file mode 100644 index 0000000..b9ab63d --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem1Provider.java @@ -0,0 +1,28 @@ +package de.steamwar.providers.impl; + +import de.steamwar.providers.BauSystemProvider; + +import java.lang.reflect.Method; + +public class BauSystem1Provider implements BauSystemProvider { + + private static final Method BAUSYSTEM_GET_OWNER_ID; + + static { + try { + Class bausystem = Class.forName("de.steamwar.bausystem.BauSystem"); + BAUSYSTEM_GET_OWNER_ID = bausystem.getDeclaredMethod("getOwnerID"); + } catch (Exception e) { + throw new SecurityException(e); + } + } + + @Override + public int getOwner() { + try { + return (int) BAUSYSTEM_GET_OWNER_ID.invoke(null); + } catch (Exception e) { + throw new SecurityException(e); + } + } +} diff --git a/SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem2Provider.java b/SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem2Provider.java new file mode 100644 index 0000000..f6f861b --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem2Provider.java @@ -0,0 +1,31 @@ +package de.steamwar.providers.impl; + +import de.steamwar.providers.BauSystemProvider; + +import java.lang.reflect.Method; + +public class BauSystem2Provider implements BauSystemProvider { + + private static final Method BAU_SERVER_GET_OWNER_ID; + private static final Object BAU_SERVER_INSTANCE; + + static { + try { + Class bauServer = Class.forName("de.steamwar.bausystem.config.BauServer"); + Method bauServerGet = bauServer.getDeclaredMethod("getInstance"); + BAU_SERVER_GET_OWNER_ID = bauServer.getMethod("getOwnerID"); + BAU_SERVER_INSTANCE = bauServerGet.invoke(null); + } catch (Exception e) { + throw new SecurityException(e); + } + } + + @Override + public int getOwner() { + try { + return (int) BAU_SERVER_GET_OWNER_ID.invoke(BAU_SERVER_INSTANCE); + } catch (Exception e) { + throw new SecurityException(e); + } + } +}