From ac54f8ef17ca12cec6f177951385a9c467283bf5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 17 Apr 2021 17:44:58 +0200 Subject: [PATCH] Add Disable Add Enable --- .../src/de/steamwar/bausystem/BauSystem.java | 6 +--- .../steamwar/bausystem/config/BauServer.java | 3 ++ .../features/tpslimit/TPSLimitCommand.java | 9 +++++- .../features/tpslimit/TPSWarpUtils.java | 3 +- .../steamwar/bausystem/linkage/Disable.java | 24 +++++++++++++++ .../de/steamwar/bausystem/linkage/Enable.java | 24 +++++++++++++++ .../bausystem/linkage/LinkageType.java | 22 ++++++++------ .../bausystem/linkage/LinkageUtils.java | 30 +++++++++++++------ 8 files changed, 95 insertions(+), 26 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/Disable.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/Enable.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 8b472695..9e86bac9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -19,7 +19,6 @@ package de.steamwar.bausystem; -import de.steamwar.bausystem.config.BauServer; import de.steamwar.bausystem.linkage.LinkageUtils; import lombok.Getter; import net.md_5.bungee.api.ChatColor; @@ -36,14 +35,11 @@ public class BauSystem extends JavaPlugin implements Listener { @Override public void onEnable() { instance = this; - LinkageUtils.link(); - - new BauServer(); } @Override public void onDisable() { - + LinkageUtils.unlink(); } } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/config/BauServer.java b/BauSystem_Main/src/de/steamwar/bausystem/config/BauServer.java index 8bc86ac6..3ef8e516 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/config/BauServer.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/config/BauServer.java @@ -19,12 +19,15 @@ package de.steamwar.bausystem.config; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; import de.steamwar.sql.SteamwarUser; import lombok.Getter; import org.bukkit.Bukkit; import java.util.UUID; +@Linked(LinkageType.PLAIN) public class BauServer { @Getter diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java index fac64595..0c68d030 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSLimitCommand.java @@ -21,6 +21,7 @@ package de.steamwar.bausystem.features.tpslimit; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.linkage.Enable; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.command.SWCommand; @@ -39,7 +40,8 @@ import java.util.Arrays; import java.util.List; @Linked(LinkageType.COMMAND) -public class TPSLimitCommand extends SWCommand { +@Linked(LinkageType.LINK) +public class TPSLimitCommand extends SWCommand implements Enable { @Getter(AccessLevel.PACKAGE) private static TPSLimitCommand instance = null; @@ -56,6 +58,11 @@ public class TPSLimitCommand extends SWCommand { } } + @Override + public void enable() { + TPSWarpUtils.init(); + } + @Register(help = true) public void genericHelp(Player p, String... args) { p.sendMessage(BauSystem.PREFIX + "Jetziges TPS limit: " + TPSLimitUtils.currentTPSLimit); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java index 9ee9b52c..7b9f64c7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tpslimit/TPSWarpUtils.java @@ -32,8 +32,7 @@ public class TPSWarpUtils { private static long nanoOffset = 0; private static long nanoDOffset = 0; - @SuppressWarnings("unused") - public static void init() { + static void init() { VersionedRunnable.call(new VersionedRunnable(() -> warp = false, 8), new VersionedRunnable(() -> { Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> nanoOffset += nanoDOffset, 1, 1); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/Disable.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/Disable.java new file mode 100644 index 00000000..ef3377d4 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/Disable.java @@ -0,0 +1,24 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.bausystem.linkage; + +public interface Disable { + void disable(); +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/Enable.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/Enable.java new file mode 100644 index 00000000..e1c5aa5b --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/Enable.java @@ -0,0 +1,24 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.bausystem.linkage; + +public interface Enable { + void enable(); +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java index 5f16a7ca..edd9a372 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java @@ -25,6 +25,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.RequiredArgsConstructor; import org.bukkit.Bukkit; +import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import java.util.function.Consumer; @@ -33,16 +34,19 @@ import java.util.function.Predicate; @RequiredArgsConstructor @AllArgsConstructor public enum LinkageType { - LISTENER(clazz -> { - for (Class interfaceClazz : clazz.getInterfaces()) { - if (interfaceClazz == Listener.class) { - return true; - } - } - return false; - }, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())), - COMMAND(clazz -> clazz.getSuperclass() == SWCommand.class); + LISTENER(2, false, Listener.class::isAssignableFrom, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())), + UNLINK_LISTENER(2, true, Listener.class::isAssignableFrom, o -> HandlerList.unregisterAll((Listener) o)), + COMMAND(-1, false, SWCommand.class::isAssignableFrom), + PLAIN(1, false, clazz -> true), + UNLINK(0, true, Disable.class::isAssignableFrom, o -> ((Disable) o).disable()), + LINK(0, false, Enable.class::isAssignableFrom, o -> ((Enable) o).enable()); + @Getter + private final int order; + + @Getter + private final boolean unlink; + @Getter private final Predicate> linkagePredicate; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java index 33969266..3a03015e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java @@ -28,17 +28,26 @@ import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.util.HashSet; -import java.util.Set; +import java.util.*; @UtilityClass public class LinkageUtils { + private Map, Object> objectMap = new HashMap<>(); + public void link() { + internalLinkOrUnlink(false); + } + + public void unlink() { + internalLinkOrUnlink(true); + } + + private void internalLinkOrUnlink(boolean unlink) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/de.steamwar.bausystem/" + Linked.class.getTypeName())))) { bufferedReader.lines().forEach(s -> { try { - link(Class.forName(s)); + linkOrUnlink(Class.forName(s), unlink); } catch (ClassNotFoundException e) { // ignored } @@ -50,29 +59,32 @@ public class LinkageUtils { } } - private void link(Class clazz) { + private void linkOrUnlink(Class clazz, boolean unlink) { Linked[] linkages = clazz.getDeclaredAnnotationsByType(Linked.class); if (linkages.length == 0) { return; } - Set linkageTypeSet = new HashSet<>(); + List linkageTypeList = new ArrayList<>(); for (Linked linked : linkages) { if (linked == null) { continue; } LinkageType linkageType = linked.value(); if (linkageType.getLinkagePredicate().test(clazz)) { - linkageTypeSet.add(linked.value()); + linkageTypeList.add(linked.value()); } } - if (linkageTypeSet.isEmpty()) { + if (linkageTypeList.isEmpty()) { return; } - Object object = constructInstance(clazz); - linkageTypeSet.forEach(linkageType -> linkageType.getLinkageConsumer().accept(object)); + linkageTypeList.removeIf(linkageType -> linkageType.isUnlink() != unlink); + linkageTypeList.sort(Comparator.comparingInt(LinkageType::getOrder)); + + Object object = objectMap.computeIfAbsent(clazz, LinkageUtils::constructInstance); + linkageTypeList.forEach(linkageType -> linkageType.getLinkageConsumer().accept(object)); } private Object constructInstance(Class clazz) {