forked from SteamWar/SteamWar
Add Linkage to CommonCore and implement SpigotLinker used in BauSystem
This commit is contained in:
@@ -38,41 +38,29 @@ import de.steamwar.bausystem.utils.TickListener;
|
|||||||
import de.steamwar.bausystem.utils.TickManager;
|
import de.steamwar.bausystem.utils.TickManager;
|
||||||
import de.steamwar.bausystem.worlddata.WorldData;
|
import de.steamwar.bausystem.worlddata.WorldData;
|
||||||
import de.steamwar.command.AbstractValidator;
|
import de.steamwar.command.AbstractValidator;
|
||||||
import de.steamwar.command.SWCommand;
|
|
||||||
import de.steamwar.command.SWCommandUtils;
|
import de.steamwar.command.SWCommandUtils;
|
||||||
import de.steamwar.core.CRIUSleepEvent;
|
import de.steamwar.core.CRIUSleepEvent;
|
||||||
import de.steamwar.core.Core;
|
|
||||||
import de.steamwar.core.WorldEditRendererCUIEditor;
|
import de.steamwar.core.WorldEditRendererCUIEditor;
|
||||||
import de.steamwar.linkage.LinkedInstance;
|
import de.steamwar.linkage.AbstractLinker;
|
||||||
import de.steamwar.linkage.MaxVersion;
|
import de.steamwar.linkage.SpigotLinker;
|
||||||
import de.steamwar.linkage.MinVersion;
|
|
||||||
import de.steamwar.linkage.PluginCheck;
|
|
||||||
import de.steamwar.linkage.api.Disable;
|
|
||||||
import de.steamwar.linkage.api.Enable;
|
|
||||||
import de.steamwar.message.Message;
|
import de.steamwar.message.Message;
|
||||||
import de.steamwar.network.packets.PacketHandler;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.HandlerList;
|
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
import org.bukkit.scheduler.BukkitTask;
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Field;
|
import java.io.OutputStream;
|
||||||
import java.util.HashMap;
|
import java.io.PrintStream;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class BauSystem extends JavaPlugin implements Listener {
|
public class BauSystem extends JavaPlugin implements Listener {
|
||||||
|
|
||||||
@@ -83,7 +71,7 @@ public class BauSystem extends JavaPlugin implements Listener {
|
|||||||
@Getter
|
@Getter
|
||||||
private static BauSystem instance;
|
private static BauSystem instance;
|
||||||
|
|
||||||
private final Map<Class<?>, Object> instances = new HashMap<>();
|
private SpigotLinker linker;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
@@ -96,115 +84,43 @@ public class BauSystem extends JavaPlugin implements Listener {
|
|||||||
SWUtils.setBausystem(instance);
|
SWUtils.setBausystem(instance);
|
||||||
|
|
||||||
RegionSystem.INSTANCE.load();
|
RegionSystem.INSTANCE.load();
|
||||||
/*
|
|
||||||
try {
|
|
||||||
PrototypeLoader.load();
|
|
||||||
RegionLoader.load();
|
|
||||||
} catch (SecurityException e) {
|
|
||||||
Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
|
|
||||||
Bukkit.shutdown();
|
|
||||||
System.exit(1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
new Updater(PrototypeLoader.file, PrototypeLoader::load);
|
|
||||||
new Updater(RegionLoader.file, RegionLoader::load);
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWCommandUtils.addValidator(Player.class, validator(Permission.BUILD));
|
SWCommandUtils.addValidator(Player.class, validator(Permission.BUILD));
|
||||||
SWCommandUtils.addValidator(CommandSender.class, validator(Permission.BUILD));
|
SWCommandUtils.addValidator(CommandSender.class, validator(Permission.BUILD));
|
||||||
SWCommandUtils.addValidator("supervisor", validator(Permission.SUPERVISOR));
|
SWCommandUtils.addValidator("supervisor", validator(Permission.SUPERVISOR));
|
||||||
SWCommandUtils.addValidator("owner", validator(Permission.OWNER));
|
SWCommandUtils.addValidator("owner", validator(Permission.OWNER));
|
||||||
|
|
||||||
instances.put(BauServer.class, BauServer.getInstance());
|
linker = new SpigotLinker(BauSystem.getInstance(), BauSystem.MESSAGE) {
|
||||||
List<Class<?>> classes = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/META-INF/annotations/de.steamwar.linkage.Linked")))
|
@Override
|
||||||
.lines()
|
protected void linkObject(Object any) {
|
||||||
.map(s -> {
|
super.linkObject(any);
|
||||||
try {
|
if (any instanceof LuaLib) {
|
||||||
return Class.forName(s, false, BauSystem.class.getClassLoader());
|
SteamWarLuaPlugin.add((LuaLib) any);
|
||||||
} catch (ClassNotFoundException | NoClassDefFoundError e) {
|
|
||||||
if (e.getMessage().equals(s)) {
|
|
||||||
Bukkit.shutdown();
|
|
||||||
throw new SecurityException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
classes.forEach(clazz -> {
|
|
||||||
MinVersion minVersion = clazz.getAnnotation(MinVersion.class);
|
|
||||||
MaxVersion maxVersion = clazz.getAnnotation(MaxVersion.class);
|
|
||||||
PluginCheck[] pluginChecks = clazz.getAnnotationsByType(PluginCheck.class);
|
|
||||||
if (minVersion != null && Core.getVersion() < minVersion.value()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (maxVersion != null && Core.getVersion() > maxVersion.value()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (PluginCheck pluginCheck : pluginChecks) {
|
|
||||||
if (pluginCheck.has() == PluginCheck.Has.THIS && Bukkit.getPluginManager().getPlugin(pluginCheck.value()) != null) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if (pluginCheck.has() == PluginCheck.Has.NOT && Bukkit.getPluginManager().getPlugin(pluginCheck.value()) == null) {
|
if (any instanceof ScoreboardElement) {
|
||||||
continue;
|
BauScoreboard.addElement((ScoreboardElement) any);
|
||||||
}
|
}
|
||||||
return;
|
if (any instanceof BauGuiItem) {
|
||||||
}
|
BauGUI.addItem((BauGuiItem) any);
|
||||||
|
}
|
||||||
Object any;
|
if (any instanceof PanzernAlgorithm) {
|
||||||
try {
|
Panzern.add((PanzernAlgorithm) any);
|
||||||
any = clazz.newInstance();
|
}
|
||||||
} catch (InstantiationException | IllegalAccessException e) {
|
if (any instanceof ConfigConverter) {
|
||||||
getLogger().log(Level.SEVERE, e.getMessage(), e);
|
Config.addConfigConverter((ConfigConverter) any);
|
||||||
Bukkit.shutdown();
|
}
|
||||||
throw new SecurityException(e.getMessage());
|
if (any instanceof BoundingBoxLoader) {
|
||||||
}
|
((BoundingBoxLoader) any).load();
|
||||||
|
|
||||||
instances.put(clazz, any);
|
|
||||||
if (any instanceof Enable) {
|
|
||||||
((Enable) any).enable();
|
|
||||||
}
|
|
||||||
if (any instanceof SWCommand) {
|
|
||||||
((SWCommand) any).setMessage(BauSystem.MESSAGE);
|
|
||||||
}
|
|
||||||
if (any instanceof Listener) {
|
|
||||||
Bukkit.getPluginManager().registerEvents((Listener) any, BauSystem.getInstance());
|
|
||||||
}
|
|
||||||
if (any instanceof PacketHandler) {
|
|
||||||
((PacketHandler) any).register();
|
|
||||||
}
|
|
||||||
if (any instanceof LuaLib) {
|
|
||||||
SteamWarLuaPlugin.add((LuaLib) any);
|
|
||||||
}
|
|
||||||
if (any instanceof ScoreboardElement) {
|
|
||||||
BauScoreboard.addElement((ScoreboardElement) any);
|
|
||||||
}
|
|
||||||
if (any instanceof BauGuiItem) {
|
|
||||||
BauGUI.addItem((BauGuiItem) any);
|
|
||||||
}
|
|
||||||
if (any instanceof PanzernAlgorithm) {
|
|
||||||
Panzern.add((PanzernAlgorithm) any);
|
|
||||||
}
|
|
||||||
if (any instanceof ConfigConverter) {
|
|
||||||
Config.addConfigConverter((ConfigConverter) any);
|
|
||||||
}
|
|
||||||
if (any instanceof BoundingBoxLoader) {
|
|
||||||
((BoundingBoxLoader) any).load();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
instances.forEach((clazz, o) -> {
|
|
||||||
for (Field field : clazz.getFields()) {
|
|
||||||
if (field.getAnnotation(LinkedInstance.class) != null) {
|
|
||||||
try {
|
|
||||||
field.set(o, instances.get(field.getType()));
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
try {
|
||||||
|
linker.addLinkableInstance(BauServer.getInstance());
|
||||||
|
linker.link();
|
||||||
|
} catch (AbstractLinker.LinkException e) {
|
||||||
|
getLogger().log(Level.SEVERE, "Could not link a class.", e);
|
||||||
|
Bukkit.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
TickListener.impl.init();
|
TickListener.impl.init();
|
||||||
|
|
||||||
@@ -221,15 +137,7 @@ public class BauSystem extends JavaPlugin implements Listener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
instances.forEach((aClass, o) -> {
|
linker.unlink();
|
||||||
if (o instanceof Listener) {
|
|
||||||
HandlerList.unregisterAll((Listener) o);
|
|
||||||
}
|
|
||||||
if (o instanceof Disable) {
|
|
||||||
((Disable) o).disable();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
WorldData.write();
|
WorldData.write();
|
||||||
RegionSystem.INSTANCE.save();
|
RegionSystem.INSTANCE.save();
|
||||||
Config.getInstance().saveAll();
|
Config.getInstance().saveAll();
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import de.steamwar.bausystem.linkage.specific.BauGuiItem;
|
|||||||
import de.steamwar.bausystem.region.FlagOptional;
|
import de.steamwar.bausystem.region.FlagOptional;
|
||||||
import de.steamwar.bausystem.region.Region;
|
import de.steamwar.bausystem.region.Region;
|
||||||
import de.steamwar.bausystem.region.flags.Flag;
|
import de.steamwar.bausystem.region.flags.Flag;
|
||||||
import de.steamwar.core.Core;
|
|
||||||
import de.steamwar.inventory.SWItem;
|
import de.steamwar.inventory.SWItem;
|
||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
import de.steamwar.sql.SteamwarUser;
|
import de.steamwar.sql.SteamwarUser;
|
||||||
|
|||||||
-1
@@ -23,7 +23,6 @@ import de.steamwar.bausystem.region.Region;
|
|||||||
import de.steamwar.core.TrickyTrialsWrapper;
|
import de.steamwar.core.TrickyTrialsWrapper;
|
||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.EntityType;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.BlockPlaceEvent;
|
import org.bukkit.event.block.BlockPlaceEvent;
|
||||||
|
|||||||
@@ -25,8 +25,6 @@ import de.steamwar.bausystem.region.Region;
|
|||||||
import de.steamwar.bausystem.region.RegionUtils;
|
import de.steamwar.bausystem.region.RegionUtils;
|
||||||
import de.steamwar.bausystem.region.flags.ColorMode;
|
import de.steamwar.bausystem.region.flags.ColorMode;
|
||||||
import de.steamwar.bausystem.region.flags.Flag;
|
import de.steamwar.bausystem.region.flags.Flag;
|
||||||
import de.steamwar.bausystem.region.utils.RegionExtensionType;
|
|
||||||
import de.steamwar.bausystem.region.utils.RegionType;
|
|
||||||
import de.steamwar.bausystem.utils.PasteBuilder;
|
import de.steamwar.bausystem.utils.PasteBuilder;
|
||||||
import de.steamwar.command.SWCommand;
|
import de.steamwar.command.SWCommand;
|
||||||
import de.steamwar.command.TypeValidator;
|
import de.steamwar.command.TypeValidator;
|
||||||
|
|||||||
-3
@@ -23,12 +23,9 @@ import de.steamwar.bausystem.BauSystem;
|
|||||||
import de.steamwar.bausystem.region.Region;
|
import de.steamwar.bausystem.region.Region;
|
||||||
import de.steamwar.bausystem.utils.ScoreboardElement;
|
import de.steamwar.bausystem.utils.ScoreboardElement;
|
||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Linked
|
@Linked
|
||||||
public class RegionScoreboardElement implements ScoreboardElement {
|
public class RegionScoreboardElement implements ScoreboardElement {
|
||||||
|
|||||||
-2
@@ -25,8 +25,6 @@ import de.steamwar.bausystem.features.script.ScriptRunner;
|
|||||||
import de.steamwar.bausystem.features.script.lua.SteamWarGlobalLuaPlugin;
|
import de.steamwar.bausystem.features.script.lua.SteamWarGlobalLuaPlugin;
|
||||||
import de.steamwar.bausystem.features.script.lua.libs.StorageLib;
|
import de.steamwar.bausystem.features.script.lua.libs.StorageLib;
|
||||||
import de.steamwar.bausystem.region.Region;
|
import de.steamwar.bausystem.region.Region;
|
||||||
import de.steamwar.bausystem.region.utils.RegionExtensionType;
|
|
||||||
import de.steamwar.bausystem.region.utils.RegionType;
|
|
||||||
import de.steamwar.core.TrickyTrialsWrapper;
|
import de.steamwar.core.TrickyTrialsWrapper;
|
||||||
import de.steamwar.linkage.Linked;
|
import de.steamwar.linkage.Linked;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|||||||
-4
@@ -34,16 +34,12 @@ import de.steamwar.linkage.LinkedInstance;
|
|||||||
import de.steamwar.techhider.TechHider;
|
import de.steamwar.techhider.TechHider;
|
||||||
import net.md_5.bungee.api.ChatMessageType;
|
import net.md_5.bungee.api.ChatMessageType;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|||||||
-1
@@ -28,7 +28,6 @@ import de.steamwar.linkage.Linked;
|
|||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.inventory.ClickType;
|
import org.bukkit.event.inventory.ClickType;
|
||||||
import org.bukkit.inventory.ItemFlag;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.PotionMeta;
|
import org.bukkit.inventory.meta.PotionMeta;
|
||||||
import org.bukkit.potion.PotionEffectType;
|
import org.bukkit.potion.PotionEffectType;
|
||||||
|
|||||||
-37
@@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.specific;
|
|
||||||
|
|
||||||
import de.steamwar.bausystem.region.Region;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
public interface ScoreboardItem {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns one Scoreboard line. If {@code null} result will be ignored.
|
|
||||||
* If return value contains {@code '?'} it will be replaced to the color
|
|
||||||
* code of the current {@link Region}.
|
|
||||||
*
|
|
||||||
* @param player the player to create the scoreboard line for
|
|
||||||
* @param region the region the player is in
|
|
||||||
* @return the String to send, can be {@code null}
|
|
||||||
*/
|
|
||||||
String getString(Player player, Region region);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2024 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
steamwar.java
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
compileOnly(libs.classindex)
|
||||||
|
}
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.linkage;
|
||||||
|
|
||||||
|
import de.steamwar.linkage.api.Disable;
|
||||||
|
import de.steamwar.linkage.api.Enable;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import lombok.experimental.StandardException;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public abstract class AbstractLinker<T> {
|
||||||
|
|
||||||
|
protected final T plugin;
|
||||||
|
private Map<Class<?>, Object> instances = new HashMap<>();
|
||||||
|
|
||||||
|
protected AbstractLinker(@NonNull T plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
instances.put(plugin.getClass(), plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@StandardException
|
||||||
|
public static class LinkException extends Exception {
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void link() throws LinkException {
|
||||||
|
List<Class<?>> classes;
|
||||||
|
try {
|
||||||
|
classes = new BufferedReader(new InputStreamReader(plugin.getClass().getResourceAsStream("/META-INF/annotations/de.steamwar.linkage.Linked")))
|
||||||
|
.lines()
|
||||||
|
.map(s -> {
|
||||||
|
try {
|
||||||
|
return Class.forName(s, false, plugin.getClass().getClassLoader());
|
||||||
|
} catch (ClassNotFoundException | NoClassDefFoundError e) {
|
||||||
|
throw new SecurityException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
Throwable cause = e.getCause();
|
||||||
|
throw new LinkException(cause.getMessage(), cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
classes.forEach(clazz -> {
|
||||||
|
MinVersion minVersion = clazz.getAnnotation(MinVersion.class);
|
||||||
|
MaxVersion maxVersion = clazz.getAnnotation(MaxVersion.class);
|
||||||
|
if (!versionCheck(clazz, minVersion, maxVersion)) return;
|
||||||
|
PluginCheck[] pluginChecks = clazz.getAnnotationsByType(PluginCheck.class);
|
||||||
|
for (PluginCheck pluginCheck : pluginChecks) {
|
||||||
|
if (!pluginCheck(clazz, pluginCheck)) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object any;
|
||||||
|
try {
|
||||||
|
any = clazz.newInstance();
|
||||||
|
} catch (InstantiationException | IllegalAccessException e) {
|
||||||
|
throw new SecurityException(e.getMessage());
|
||||||
|
}
|
||||||
|
instances.put(clazz, any);
|
||||||
|
|
||||||
|
if (any instanceof Enable) {
|
||||||
|
((Enable) any).enable();
|
||||||
|
}
|
||||||
|
linkObject(any);
|
||||||
|
});
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
Throwable cause = e.getCause();
|
||||||
|
throw new LinkException(cause.getMessage(), cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
instances.forEach((clazz, o) -> {
|
||||||
|
for (Field field : clazz.getFields()) {
|
||||||
|
if (field.getAnnotation(LinkedInstance.class) != null) {
|
||||||
|
System.out.println("Setting " + field.getName() + " to " + instances.get(field.getType()));
|
||||||
|
try {
|
||||||
|
field.set(o, instances.get(field.getType()));
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new SecurityException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
Throwable cause = e.getCause();
|
||||||
|
throw new LinkException(cause.getMessage(), cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void unlink() {
|
||||||
|
instances.forEach((aClass, any) -> {
|
||||||
|
unlinkObject(any);
|
||||||
|
if (any instanceof Disable) {
|
||||||
|
((Disable) any).disable();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void addLinkableInstance(@NonNull Object instance) {
|
||||||
|
instances.put(instance.getClass(), instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {@code true} if the clazz passes the checks {@code false} otherwise
|
||||||
|
*/
|
||||||
|
protected boolean versionCheck(@NonNull Class<?> clazz, MinVersion minVersion, MaxVersion maxVersion) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {@code true} if the clazz passes the checks {@code false} otherwise
|
||||||
|
*/
|
||||||
|
protected boolean pluginCheck(@NonNull Class<?> clazz, PluginCheck pluginCheck) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* There is no need in calling {@link Enable#enable()} by this method.
|
||||||
|
*/
|
||||||
|
protected abstract void linkObject(Object any);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* There is no need in calling {@link Disable#disable()} ()} by this method.
|
||||||
|
*/
|
||||||
|
protected abstract void unlinkObject(Object any);
|
||||||
|
}
|
||||||
@@ -25,4 +25,5 @@ dependencies {
|
|||||||
api(project(":CommonCore:SQL"))
|
api(project(":CommonCore:SQL"))
|
||||||
api(project(":CommonCore:Network"))
|
api(project(":CommonCore:Network"))
|
||||||
api(project(":CommonCore:Data"))
|
api(project(":CommonCore:Data"))
|
||||||
|
api(project(":CommonCore:Linkage"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.linkage;
|
||||||
|
|
||||||
|
import de.steamwar.command.SWCommand;
|
||||||
|
import de.steamwar.core.Core;
|
||||||
|
import de.steamwar.message.Message;
|
||||||
|
import de.steamwar.network.packets.PacketHandler;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
public class SpigotLinker extends AbstractLinker<JavaPlugin> {
|
||||||
|
|
||||||
|
private final Message message;
|
||||||
|
|
||||||
|
public SpigotLinker(@NonNull JavaPlugin plugin, Message message) {
|
||||||
|
super(plugin);
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean versionCheck(@NonNull Class<?> clazz, MinVersion minVersion, MaxVersion maxVersion) {
|
||||||
|
if (minVersion != null && Core.getVersion() < minVersion.value()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (maxVersion != null && Core.getVersion() > maxVersion.value()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean pluginCheck(@NonNull Class<?> clazz, PluginCheck pluginCheck) {
|
||||||
|
if (pluginCheck.has() == PluginCheck.Has.THIS && Bukkit.getPluginManager().getPlugin(pluginCheck.value()) != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (pluginCheck.has() == PluginCheck.Has.NOT && Bukkit.getPluginManager().getPlugin(pluginCheck.value()) == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void linkObject(Object any) {
|
||||||
|
if (any instanceof SWCommand) {
|
||||||
|
((SWCommand) any).setMessage(message);
|
||||||
|
}
|
||||||
|
if (any instanceof Listener) {
|
||||||
|
Bukkit.getPluginManager().registerEvents((Listener) any, plugin);
|
||||||
|
}
|
||||||
|
if (any instanceof PacketHandler) {
|
||||||
|
((PacketHandler) any).register();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void unlinkObject(Object any) {
|
||||||
|
if (any instanceof Listener) {
|
||||||
|
HandlerList.unregisterAll((Listener) any);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-1
@@ -187,7 +187,8 @@ include(
|
|||||||
"CommonCore",
|
"CommonCore",
|
||||||
"CommonCore:Data",
|
"CommonCore:Data",
|
||||||
"CommonCore:SQL",
|
"CommonCore:SQL",
|
||||||
"CommonCore:Network"
|
"CommonCore:Network",
|
||||||
|
"CommonCore:Linkage"
|
||||||
)
|
)
|
||||||
|
|
||||||
include(
|
include(
|
||||||
|
|||||||
Reference in New Issue
Block a user