Add Disable
Add Enable
Dieser Commit ist enthalten in:
Ursprung
032ff79810
Commit
ac54f8ef17
@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
package de.steamwar.bausystem;
|
package de.steamwar.bausystem;
|
||||||
|
|
||||||
import de.steamwar.bausystem.config.BauServer;
|
|
||||||
import de.steamwar.bausystem.linkage.LinkageUtils;
|
import de.steamwar.bausystem.linkage.LinkageUtils;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
@ -36,14 +35,11 @@ public class BauSystem extends JavaPlugin implements Listener {
|
|||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
instance = this;
|
instance = this;
|
||||||
|
|
||||||
LinkageUtils.link();
|
LinkageUtils.link();
|
||||||
|
|
||||||
new BauServer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
LinkageUtils.unlink();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -19,12 +19,15 @@
|
|||||||
|
|
||||||
package de.steamwar.bausystem.config;
|
package de.steamwar.bausystem.config;
|
||||||
|
|
||||||
|
import de.steamwar.bausystem.linkage.LinkageType;
|
||||||
|
import de.steamwar.bausystem.linkage.Linked;
|
||||||
import de.steamwar.sql.SteamwarUser;
|
import de.steamwar.sql.SteamwarUser;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Linked(LinkageType.PLAIN)
|
||||||
public class BauServer {
|
public class BauServer {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@ -21,6 +21,7 @@ package de.steamwar.bausystem.features.tpslimit;
|
|||||||
|
|
||||||
import de.steamwar.bausystem.BauSystem;
|
import de.steamwar.bausystem.BauSystem;
|
||||||
import de.steamwar.bausystem.Permission;
|
import de.steamwar.bausystem.Permission;
|
||||||
|
import de.steamwar.bausystem.linkage.Enable;
|
||||||
import de.steamwar.bausystem.linkage.LinkageType;
|
import de.steamwar.bausystem.linkage.LinkageType;
|
||||||
import de.steamwar.bausystem.linkage.Linked;
|
import de.steamwar.bausystem.linkage.Linked;
|
||||||
import de.steamwar.command.SWCommand;
|
import de.steamwar.command.SWCommand;
|
||||||
@ -39,7 +40,8 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Linked(LinkageType.COMMAND)
|
@Linked(LinkageType.COMMAND)
|
||||||
public class TPSLimitCommand extends SWCommand {
|
@Linked(LinkageType.LINK)
|
||||||
|
public class TPSLimitCommand extends SWCommand implements Enable {
|
||||||
|
|
||||||
@Getter(AccessLevel.PACKAGE)
|
@Getter(AccessLevel.PACKAGE)
|
||||||
private static TPSLimitCommand instance = null;
|
private static TPSLimitCommand instance = null;
|
||||||
@ -56,6 +58,11 @@ public class TPSLimitCommand extends SWCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enable() {
|
||||||
|
TPSWarpUtils.init();
|
||||||
|
}
|
||||||
|
|
||||||
@Register(help = true)
|
@Register(help = true)
|
||||||
public void genericHelp(Player p, String... args) {
|
public void genericHelp(Player p, String... args) {
|
||||||
p.sendMessage(BauSystem.PREFIX + "Jetziges TPS limit: " + TPSLimitUtils.currentTPSLimit);
|
p.sendMessage(BauSystem.PREFIX + "Jetziges TPS limit: " + TPSLimitUtils.currentTPSLimit);
|
||||||
|
@ -32,8 +32,7 @@ public class TPSWarpUtils {
|
|||||||
private static long nanoOffset = 0;
|
private static long nanoOffset = 0;
|
||||||
private static long nanoDOffset = 0;
|
private static long nanoDOffset = 0;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
static void init() {
|
||||||
public static void init() {
|
|
||||||
VersionedRunnable.call(new VersionedRunnable(() -> warp = false, 8),
|
VersionedRunnable.call(new VersionedRunnable(() -> warp = false, 8),
|
||||||
new VersionedRunnable(() -> {
|
new VersionedRunnable(() -> {
|
||||||
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> nanoOffset += nanoDOffset, 1, 1);
|
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.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@ -33,16 +34,19 @@ import java.util.function.Predicate;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum LinkageType {
|
public enum LinkageType {
|
||||||
LISTENER(clazz -> {
|
LISTENER(2, false, Listener.class::isAssignableFrom, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())),
|
||||||
for (Class<?> interfaceClazz : clazz.getInterfaces()) {
|
UNLINK_LISTENER(2, true, Listener.class::isAssignableFrom, o -> HandlerList.unregisterAll((Listener) o)),
|
||||||
if (interfaceClazz == Listener.class) {
|
COMMAND(-1, false, SWCommand.class::isAssignableFrom),
|
||||||
return true;
|
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());
|
||||||
return false;
|
|
||||||
}, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())),
|
|
||||||
COMMAND(clazz -> clazz.getSuperclass() == SWCommand.class);
|
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final int order;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final boolean unlink;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final Predicate<Class<?>> linkagePredicate;
|
private final Predicate<Class<?>> linkagePredicate;
|
||||||
|
|
||||||
|
@ -28,17 +28,26 @@ import java.io.IOException;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.HashSet;
|
import java.util.*;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class LinkageUtils {
|
public class LinkageUtils {
|
||||||
|
|
||||||
|
private Map<Class<?>, Object> objectMap = new HashMap<>();
|
||||||
|
|
||||||
public void link() {
|
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())))) {
|
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/de.steamwar.bausystem/" + Linked.class.getTypeName())))) {
|
||||||
bufferedReader.lines().forEach(s -> {
|
bufferedReader.lines().forEach(s -> {
|
||||||
try {
|
try {
|
||||||
link(Class.forName(s));
|
linkOrUnlink(Class.forName(s), unlink);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
// ignored
|
// 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);
|
Linked[] linkages = clazz.getDeclaredAnnotationsByType(Linked.class);
|
||||||
if (linkages.length == 0) {
|
if (linkages.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<LinkageType> linkageTypeSet = new HashSet<>();
|
List<LinkageType> linkageTypeList = new ArrayList<>();
|
||||||
for (Linked linked : linkages) {
|
for (Linked linked : linkages) {
|
||||||
if (linked == null) {
|
if (linked == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
LinkageType linkageType = linked.value();
|
LinkageType linkageType = linked.value();
|
||||||
if (linkageType.getLinkagePredicate().test(clazz)) {
|
if (linkageType.getLinkagePredicate().test(clazz)) {
|
||||||
linkageTypeSet.add(linked.value());
|
linkageTypeList.add(linked.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (linkageTypeSet.isEmpty()) {
|
if (linkageTypeList.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object object = constructInstance(clazz);
|
linkageTypeList.removeIf(linkageType -> linkageType.isUnlink() != unlink);
|
||||||
linkageTypeSet.forEach(linkageType -> linkageType.getLinkageConsumer().accept(object));
|
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) {
|
private Object constructInstance(Class<?> clazz) {
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren