Add Disable
Add Enable
Dieser Commit ist enthalten in:
Ursprung
032ff79810
Commit
ac54f8ef17
@ -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();
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
24
BauSystem_Main/src/de/steamwar/bausystem/linkage/Disable.java
Normale Datei
24
BauSystem_Main/src/de/steamwar/bausystem/linkage/Disable.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.linkage;
|
||||
|
||||
public interface Disable {
|
||||
void disable();
|
||||
}
|
24
BauSystem_Main/src/de/steamwar/bausystem/linkage/Enable.java
Normale Datei
24
BauSystem_Main/src/de/steamwar/bausystem/linkage/Enable.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.linkage;
|
||||
|
||||
public interface Enable {
|
||||
void enable();
|
||||
}
|
@ -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,15 +34,18 @@ 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<Class<?>> linkagePredicate;
|
||||
|
@ -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<Class<?>, 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<LinkageType> linkageTypeSet = new HashSet<>();
|
||||
List<LinkageType> 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) {
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren