Pass plugin instance instead of using static getInstance() method

This commit is contained in:
filoghost 2021-07-06 21:21:57 +02:00
parent 71de987628
commit 1871fbc8b7
7 changed files with 45 additions and 27 deletions

View File

@ -19,9 +19,10 @@ import me.filoghost.holographicdisplays.plugin.bridge.bungeecord.BungeeServerTra
import me.filoghost.holographicdisplays.plugin.bridge.placeholderapi.PlaceholderAPIHook;
import me.filoghost.holographicdisplays.plugin.bridge.protocollib.ProtocolLibHook;
import me.filoghost.holographicdisplays.plugin.commands.HologramCommandManager;
import me.filoghost.holographicdisplays.plugin.commands.InternalHologramEditor;
import me.filoghost.holographicdisplays.plugin.disk.ConfigManager;
import me.filoghost.holographicdisplays.plugin.disk.Settings;
import me.filoghost.holographicdisplays.plugin.disk.HologramDatabase;
import me.filoghost.holographicdisplays.plugin.disk.Settings;
import me.filoghost.holographicdisplays.plugin.disk.upgrade.LegacySymbolsUpgrade;
import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologram;
import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramManager;
@ -125,12 +126,15 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
TickingTask tickingTask = new TickingTask(tickClock, placeholderLineTracker);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, tickingTask, 0, 1);
HologramCommandManager commandManager = new HologramCommandManager(configManager, internalHologramManager, nmsManager);
HologramCommandManager commandManager = new HologramCommandManager(
this,
new InternalHologramEditor(internalHologramManager, configManager),
nmsManager);
commandManager.register(this);
registerListener(new InteractListener(nmsManager));
registerListener(new SpawnListener(nmsManager));
registerListener(new ChunkListener(nmsManager, internalHologramManager, apiHologramManager));
registerListener(new ChunkListener(this, nmsManager, internalHologramManager, apiHologramManager));
UpdateNotificationListener updateNotificationListener = new UpdateNotificationListener();
registerListener(updateNotificationListener);
@ -143,7 +147,7 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
int pluginID = 3123;
new MetricsLite(this, pluginID);
updateNotificationListener.runAsyncUpdateCheck();
updateNotificationListener.runAsyncUpdateCheck(this);
if (errorCollector.hasErrors()) {
errorCollector.logToConsole();
@ -160,7 +164,7 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
}
public void load(boolean deferHologramsCreation, ErrorCollector errorCollector) {
DefaultPlaceholders.resetAndRegister(placeholderRegistry, animationRegistry, bungeeServerTracker);
DefaultPlaceholders.resetAndRegister(this, placeholderRegistry, animationRegistry, bungeeServerTracker);
internalHologramManager.clearAll();

View File

@ -10,7 +10,6 @@ import me.filoghost.fcommons.ping.MinecraftServerPinger;
import me.filoghost.fcommons.ping.PingParseException;
import me.filoghost.fcommons.ping.PingResponse;
import me.filoghost.holographicdisplays.common.DebugLogger;
import me.filoghost.holographicdisplays.plugin.HolographicDisplays;
import me.filoghost.holographicdisplays.plugin.disk.ServerAddress;
import me.filoghost.holographicdisplays.plugin.disk.Settings;
import org.bukkit.Bukkit;
@ -28,14 +27,16 @@ public class BungeeServerTracker {
private static final long UNTRACK_AFTER_TIME_WITHOUT_REQUESTS = TimeUnit.MINUTES.toMillis(10);
private final Plugin plugin;
private final ConcurrentMap<String, TrackedServer> trackedServers;
private final BungeeMessenger bungeeMessenger;
private int taskID = -1;
public BungeeServerTracker(Plugin plugin) {
trackedServers = new ConcurrentHashMap<>();
bungeeMessenger = BungeeMessenger.registerNew(plugin, this::updateServerInfoFromBungee);
this.plugin = plugin;
this.trackedServers = new ConcurrentHashMap<>();
this.bungeeMessenger = BungeeMessenger.registerNew(plugin, this::updateServerInfoFromBungee);
}
public void restart(int updateInterval, TimeUnit timeUnit) {
@ -45,8 +46,11 @@ public class BungeeServerTracker {
Bukkit.getScheduler().cancelTask(taskID);
}
taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(HolographicDisplays.getInstance(),
this::runPeriodicUpdateTask, 1, timeUnit.toSeconds(updateInterval) * 20L);
taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(
plugin,
this::runPeriodicUpdateTask,
1,
timeUnit.toSeconds(updateInterval) * 20L);
}
public ServerInfo getCurrentServerInfo(@NotNull String serverName) {
@ -64,7 +68,7 @@ public class BungeeServerTracker {
removeUnusedServers();
if (Settings.pingerEnabled) {
Bukkit.getScheduler().runTaskAsynchronously(HolographicDisplays.getInstance(), () -> {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
for (TrackedServer trackedServer : trackedServers.values()) {
updateServerInfoWithPinger(trackedServer);
}

View File

@ -31,11 +31,9 @@ import me.filoghost.holographicdisplays.plugin.commands.subs.ReloadCommand;
import me.filoghost.holographicdisplays.plugin.commands.subs.RemovelineCommand;
import me.filoghost.holographicdisplays.plugin.commands.subs.SetlineCommand;
import me.filoghost.holographicdisplays.plugin.commands.subs.TeleportCommand;
import me.filoghost.holographicdisplays.plugin.disk.ConfigManager;
import me.filoghost.holographicdisplays.plugin.disk.Settings;
import me.filoghost.holographicdisplays.plugin.format.ColorScheme;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologram;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologramManager;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
@ -52,12 +50,13 @@ import java.util.logging.Level;
public class HologramCommandManager extends SubCommandManager {
private final HolographicDisplays holographicDisplays;
private final List<HologramSubCommand> subCommands;
private final HelpCommand helpCommand;
public HologramCommandManager(ConfigManager configManager, InternalHologramManager internalHologramManager, NMSManager nmsManager) {
public HologramCommandManager(HolographicDisplays holographicDisplays, InternalHologramEditor hologramEditor, NMSManager nmsManager) {
setName("holograms");
InternalHologramEditor hologramEditor = new InternalHologramEditor(internalHologramManager, configManager);
this.holographicDisplays = holographicDisplays;
this.helpCommand = new HelpCommand(this);
this.subCommands = new ArrayList<>();
@ -71,7 +70,7 @@ public class HologramCommandManager extends SubCommandManager {
subCommands.add(new MovehereCommand(hologramEditor));
subCommands.add(new AlignCommand(hologramEditor));
subCommands.add(new CopyCommand(hologramEditor));
subCommands.add(new ReloadCommand());
subCommands.add(new ReloadCommand(holographicDisplays));
subCommands.add(new RemovelineCommand(this, hologramEditor));
subCommands.add(new SetlineCommand(this, hologramEditor));
@ -148,7 +147,7 @@ public class HologramCommandManager extends SubCommandManager {
@Override
protected void sendNoArgsMessage(CommandContext context) {
CommandSender sender = context.getSender();
String version = HolographicDisplays.getInstance().getDescription().getVersion();
String version = holographicDisplays.getDescription().getVersion();
sender.sendMessage(ColorScheme.PRIMARY_DARKER + "Server is running " + ColorScheme.PRIMARY + "Holographic Displays "
+ ColorScheme.PRIMARY_DARKER + "v" + version + " by " + ColorScheme.PRIMARY + "filoghost");
if (helpCommand.hasPermission(sender)) {

View File

@ -17,15 +17,19 @@ import org.bukkit.command.ConsoleCommandSender;
public class ReloadCommand extends HologramSubCommand {
public ReloadCommand() {
private final HolographicDisplays holographicDisplays;
public ReloadCommand(HolographicDisplays holographicDisplays) {
super("reload");
setDescription("Reloads the holograms from the database.");
this.holographicDisplays = holographicDisplays;
}
@Override
public void execute(CommandSender sender, String[] args, SubCommandContext context) {
PrintableErrorCollector errorCollector = new PrintableErrorCollector();
HolographicDisplays.getInstance().load(false, errorCollector);
holographicDisplays.load(false, errorCollector);
if (!errorCollector.hasErrors()) {
sender.sendMessage(ColorScheme.PRIMARY + "Configuration reloaded successfully.");

View File

@ -7,7 +7,6 @@ package me.filoghost.holographicdisplays.plugin.listener;
import me.filoghost.holographicdisplays.common.nms.NMSManager;
import me.filoghost.holographicdisplays.common.nms.entity.NMSEntity;
import me.filoghost.holographicdisplays.plugin.HolographicDisplays;
import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramManager;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologramManager;
import org.bukkit.Bukkit;
@ -18,14 +17,21 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.plugin.Plugin;
public class ChunkListener implements Listener {
private final Plugin plugin;
private final NMSManager nmsManager;
private final InternalHologramManager internalHologramManager;
private final APIHologramManager apiHologramManager;
public ChunkListener(NMSManager nmsManager, InternalHologramManager internalHologramManager, APIHologramManager apiHologramManager) {
public ChunkListener(
Plugin plugin,
NMSManager nmsManager,
InternalHologramManager internalHologramManager,
APIHologramManager apiHologramManager) {
this.plugin = plugin;
this.nmsManager = nmsManager;
this.internalHologramManager = internalHologramManager;
this.apiHologramManager = apiHologramManager;
@ -57,7 +63,7 @@ public class ChunkListener implements Listener {
if (Bukkit.isPrimaryThread()) {
onChunkLoad(chunk);
} else {
Bukkit.getScheduler().runTask(HolographicDisplays.getInstance(), () -> onChunkLoad(chunk));
Bukkit.getScheduler().runTask(plugin, () -> onChunkLoad(chunk));
}
}

View File

@ -15,15 +15,16 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.jetbrains.annotations.Nullable;
public class UpdateNotificationListener implements Listener {
// The new version found by the updater, null if there is no new version
private String newVersion;
private @Nullable String newVersion;
public void runAsyncUpdateCheck() {
public void runAsyncUpdateCheck(HolographicDisplays holographicDisplays) {
if (Settings.updateNotification) {
UpdateChecker.run(HolographicDisplays.getInstance(), 75097, newVersion -> {
UpdateChecker.run(holographicDisplays, 75097, newVersion -> {
this.newVersion = newVersion;
Log.info("Found a new version available: " + newVersion);
Log.info("Download it on Bukkit Dev:");

View File

@ -6,13 +6,13 @@
package me.filoghost.holographicdisplays.plugin.placeholder.internal;
import me.filoghost.fcommons.collection.CollectionUtils;
import me.filoghost.holographicdisplays.plugin.HolographicDisplays;
import me.filoghost.holographicdisplays.plugin.bridge.bungeecord.BungeeServerTracker;
import me.filoghost.holographicdisplays.plugin.bridge.bungeecord.ServerInfo;
import me.filoghost.holographicdisplays.plugin.disk.Settings;
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;
import java.time.Instant;
import java.util.Arrays;
@ -24,10 +24,10 @@ public class DefaultPlaceholders {
private static final String NO_SERVER_SPECIFIED_ERROR = "[No server specified]";
public static void resetAndRegister(
Plugin plugin,
PlaceholderRegistry placeholderRegistry,
AnimationRegistry animationRegistry,
BungeeServerTracker bungeeServerTracker) {
HolographicDisplays plugin = HolographicDisplays.getInstance();
placeholderRegistry.unregisterAll(plugin);
placeholderRegistry.registerGlobalPlaceholderReplacer(plugin, "empty", Integer.MAX_VALUE, (argument) -> {