From 9ff498133db76864b7c1b1ef00951d1e54df156b Mon Sep 17 00:00:00 2001 From: Chaos Date: Thu, 27 Jan 2022 21:36:42 +0100 Subject: [PATCH 1/4] Move Provider to SpigotCore --- .../steamwar/providers/BauSystemProvider.java | 56 +++++++++++++++++++ .../providers/impl/BauSystem1Provider.java | 28 ++++++++++ .../providers/impl/BauSystem2Provider.java | 31 ++++++++++ 3 files changed, 115 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/providers/BauSystemProvider.java create mode 100644 SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem1Provider.java create mode 100644 SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem2Provider.java 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); + } + } +} -- 2.39.5 From 4e801ea4eb414f918682b9d77935d393d8dd1199 Mon Sep 17 00:00:00 2001 From: Chaos Date: Fri, 28 Jan 2022 15:37:54 +0100 Subject: [PATCH 2/4] Change Providers --- .../de/steamwar/providers/BauServerInfo.java | 59 +++++++++++++++++++ .../steamwar/providers/BauSystemProvider.java | 56 ------------------ .../providers/impl/BauSystem1Provider.java | 28 --------- .../providers/impl/BauSystem2Provider.java | 31 ---------- 4 files changed, 59 insertions(+), 115 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java delete mode 100644 SpigotCore_Main/src/de/steamwar/providers/BauSystemProvider.java delete mode 100644 SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem1Provider.java delete mode 100644 SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem2Provider.java diff --git a/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java b/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java new file mode 100644 index 0000000..059b0ba --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java @@ -0,0 +1,59 @@ +package de.steamwar.providers; + +import de.steamwar.sql.SteamwarUser; +import org.bukkit.Bukkit; + +import java.util.UUID; +import java.util.function.Supplier; +import java.util.logging.Level; + +public class BauServerInfo { + + private static boolean isBauserver; + private static int bauOwner; + + static { + isBauserver = Bukkit.getPluginManager().getPlugin("BauSystem") != null; + if(isBauserver) { + try { + bauOwner = Integer.parseInt(Bukkit.getWorlds().get(0).getName()); + if(SteamwarUser.get(bauOwner) == null) { + Bukkit.getLogger().log(Level.SEVERE, "World name is not a UserID", new IllegalStateException("World name is not a UserID")); + } + } catch (NumberFormatException e) { + isBauserver = false; + Bukkit.getLogger().log(Level.SEVERE, "World name is not a Integer", new IllegalStateException("World name is not a Integer")); + } + } else { + bauOwner = -1; + } + } + + public static UUID getOwner() { + if(isBauserver) { + return SteamwarUser.get(bauOwner).getUUID(); + } else { + throw new IllegalStateException("This is not a BauServer"); + } + } + + public static int getOwnerId() { + if(isBauserver) { + return bauOwner; + } else { + throw new IllegalStateException("This is not a BauServer"); + } + } + + public static boolean isBauServer() { + return isBauserver; + } + + public static T runWithBauServerInfo(Supplier supplier, T defaults) { + if(isBauserver) { + return supplier.get(); + } else { + return defaults; + } + } +} diff --git a/SpigotCore_Main/src/de/steamwar/providers/BauSystemProvider.java b/SpigotCore_Main/src/de/steamwar/providers/BauSystemProvider.java deleted file mode 100644 index b59f3d9..0000000 --- a/SpigotCore_Main/src/de/steamwar/providers/BauSystemProvider.java +++ /dev/null @@ -1,56 +0,0 @@ -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 deleted file mode 100644 index b9ab63d..0000000 --- a/SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem1Provider.java +++ /dev/null @@ -1,28 +0,0 @@ -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 deleted file mode 100644 index f6f861b..0000000 --- a/SpigotCore_Main/src/de/steamwar/providers/impl/BauSystem2Provider.java +++ /dev/null @@ -1,31 +0,0 @@ -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); - } - } -} -- 2.39.5 From c593e23b80f893690bc96008e6dfa8631e63b549 Mon Sep 17 00:00:00 2001 From: Chaos Date: Fri, 28 Jan 2022 15:55:18 +0100 Subject: [PATCH 3/4] Simplify --- .../de/steamwar/providers/BauServerInfo.java | 67 ++++++++----------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java b/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java index 059b0ba..ed500f8 100644 --- a/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java +++ b/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java @@ -1,59 +1,50 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.providers; import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import java.util.UUID; -import java.util.function.Supplier; import java.util.logging.Level; public class BauServerInfo { - - private static boolean isBauserver; - private static int bauOwner; + private static Integer bauOwner = null; static { - isBauserver = Bukkit.getPluginManager().getPlugin("BauSystem") != null; - if(isBauserver) { + try { + bauOwner = Integer.parseInt(Bukkit.getWorlds().get(0).getName()); + } catch (NumberFormatException e) { try { - bauOwner = Integer.parseInt(Bukkit.getWorlds().get(0).getName()); - if(SteamwarUser.get(bauOwner) == null) { - Bukkit.getLogger().log(Level.SEVERE, "World name is not a UserID", new IllegalStateException("World name is not a UserID")); - } - } catch (NumberFormatException e) { - isBauserver = false; - Bukkit.getLogger().log(Level.SEVERE, "World name is not a Integer", new IllegalStateException("World name is not a Integer")); + bauOwner = SteamwarUser.get(UUID.fromString(Bukkit.getWorlds().get(0).getName())).getId(); + } catch (IllegalArgumentException exception) { + Bukkit.getLogger().log(Level.SEVERE, "World name is not a user", new IllegalStateException("World name is not a user")); } - } else { - bauOwner = -1; } } - public static UUID getOwner() { - if(isBauserver) { - return SteamwarUser.get(bauOwner).getUUID(); - } else { - throw new IllegalStateException("This is not a BauServer"); - } - } - - public static int getOwnerId() { - if(isBauserver) { - return bauOwner; - } else { - throw new IllegalStateException("This is not a BauServer"); - } + public static Integer getOwnerId() { + return bauOwner; } public static boolean isBauServer() { - return isBauserver; - } - - public static T runWithBauServerInfo(Supplier supplier, T defaults) { - if(isBauserver) { - return supplier.get(); - } else { - return defaults; - } + return bauOwner != null; } } -- 2.39.5 From 79d9a8b902ff54520e3b387b74e61555e2b1e271 Mon Sep 17 00:00:00 2001 From: Chaos Date: Fri, 28 Jan 2022 15:57:45 +0100 Subject: [PATCH 4/4] Ignored --- SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java b/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java index ed500f8..b7de60b 100644 --- a/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java +++ b/SpigotCore_Main/src/de/steamwar/providers/BauServerInfo.java @@ -23,7 +23,6 @@ import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import java.util.UUID; -import java.util.logging.Level; public class BauServerInfo { private static Integer bauOwner = null; @@ -34,9 +33,7 @@ public class BauServerInfo { } catch (NumberFormatException e) { try { bauOwner = SteamwarUser.get(UUID.fromString(Bukkit.getWorlds().get(0).getName())).getId(); - } catch (IllegalArgumentException exception) { - Bukkit.getLogger().log(Level.SEVERE, "World name is not a user", new IllegalStateException("World name is not a user")); - } + } catch (IllegalArgumentException ignored) {} } } -- 2.39.5