Simplify hologram database

This commit is contained in:
filoghost 2021-03-06 00:26:33 +01:00
parent cc0ef3bf7b
commit a8a44bec90
15 changed files with 70 additions and 143 deletions

View File

@ -13,14 +13,11 @@ import me.filoghost.holographicdisplays.api.internal.BackendAPI;
import me.filoghost.holographicdisplays.bridge.bungeecord.BungeeServerTracker;
import me.filoghost.holographicdisplays.bridge.protocollib.ProtocolLibHook;
import me.filoghost.holographicdisplays.commands.HologramCommandManager;
import me.filoghost.holographicdisplays.commands.Messages;
import me.filoghost.holographicdisplays.core.Utils;
import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.ProtocolPacketSettings;
import me.filoghost.holographicdisplays.disk.ConfigManager;
import me.filoghost.holographicdisplays.disk.Configuration;
import me.filoghost.holographicdisplays.disk.HologramConfig;
import me.filoghost.holographicdisplays.disk.HologramLoadException;
import me.filoghost.holographicdisplays.disk.HologramDatabase;
import me.filoghost.holographicdisplays.disk.upgrade.LegacySymbolsUpgrader;
import me.filoghost.holographicdisplays.listener.ChunkListener;
import me.filoghost.holographicdisplays.listener.InteractListener;
@ -127,49 +124,27 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
updateNotificationListener.runAsyncUpdateCheck();
}
public void load(CommandSender sender, boolean deferHologramLoad) {
public void load(CommandSender sender, boolean deferHologramsCreation) {
PlaceholdersManager.untrackAll();
internalHologramManager.clearAll();
BungeeServerTracker.resetTrackedServers();
configManager.reloadCustomPlaceholders();
configManager.reloadMainConfig();
configManager.reloadHologramDatabase();
HologramDatabase hologramDatabase = configManager.loadHologramDatabase();
try {
AnimationsRegistry.loadAnimations(configManager);
} catch (Exception e) {
Log.warning("Failed to load animation files!", e);
}
BungeeServerTracker.resetTrackedServers();
BungeeServerTracker.restartTask(Configuration.bungeeRefreshSeconds);
PlaceholdersManager.untrackAll();
internalHologramManager.clearAll();
if (deferHologramLoad) {
if (deferHologramsCreation) {
// For the initial load: holograms are loaded later, when the worlds are ready
Bukkit.getScheduler().runTask(this, () -> createHolograms(sender));
Bukkit.getScheduler().runTask(this, () -> hologramDatabase.createHolograms(sender, internalHologramManager));
} else {
createHolograms(sender);
}
}
private void createHolograms(CommandSender sender) {
// Create all the holograms
for (HologramConfig hologramConfig : configManager.getHologramDatabase().getHolograms()) {
try {
hologramConfig.createHologram(internalHologramManager);
} catch (HologramLoadException e) {
if (sender != null) {
Messages.sendWarning(sender, Utils.formatExceptionMessage(e));
} else {
Log.warning(Utils.formatExceptionMessage(e));
}
} catch (Exception e) {
Log.warning("Unexpected exception while loading the hologram \"" + hologramConfig.getName() + "\".", e);
}
}
// Then trigger a refresh for all of them
for (InternalHologram hologram : internalHologramManager.getHolograms()) {
hologram.refresh();
hologramDatabase.createHolograms(sender, internalHologramManager);
}
}

View File

@ -45,8 +45,7 @@ public class AddlineCommand extends LineEditingCommand implements QuickEditComma
hologram.getLinesUnsafe().add(line);
hologram.refresh();
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram));
sender.sendMessage(Colors.PRIMARY + "Line added.");

View File

@ -57,8 +57,7 @@ public class AlignCommand extends HologramSubCommand {
hologram.teleport(loc);
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
sender.sendMessage(Colors.PRIMARY + "Hologram \"" + hologram.getName() + "\" aligned to the hologram \"" + referenceHologram.getName() + "\" on the " + axis.toUpperCase() + " axis.");
}

View File

@ -44,8 +44,7 @@ public class CopyCommand extends HologramSubCommand {
toHologram.refresh();
configManager.getHologramDatabase().addOrUpdate(toHologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
sender.sendMessage(Colors.PRIMARY + "Hologram \"" + fromHologram.getName() + "\" copied into hologram \"" + toHologram.getName() + "\".");
}

View File

@ -76,8 +76,7 @@ public class CreateCommand extends HologramSubCommand {
hologram.getLinesUnsafe().add(line);
hologram.refresh();
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
Location look = player.getLocation();
look.setPitch(90);
player.teleport(look, TeleportCause.PLUGIN);

View File

@ -36,8 +36,7 @@ public class DeleteCommand extends HologramSubCommand {
internalHologramManager.deleteHologram(hologram);
configManager.getHologramDatabase().removeHologram(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
sender.sendMessage(Colors.PRIMARY + "You deleted the hologram '" + hologram.getName() + "'.");
}

View File

@ -55,8 +55,7 @@ public class InsertlineCommand extends LineEditingCommand implements QuickEditCo
hologram.getLinesUnsafe().add(insertAfter, line);
hologram.refresh();
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram));

View File

@ -41,8 +41,7 @@ public class MovehereCommand extends HologramSubCommand {
hologram.teleport(player.getLocation());
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
Location to = player.getLocation();
to.setPitch(90);
player.teleport(to, TeleportCause.PLUGIN);

View File

@ -123,8 +123,7 @@ public class ReadimageCommand extends LineEditingCommand {
Messages.sendTip(sender, "The image has a very low height. You can increase it by increasing the width, it will scale automatically.");
}
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
if (append) {
sender.sendMessage(Colors.PRIMARY + "The image was appended int the end of the hologram.");

View File

@ -87,8 +87,7 @@ public class ReadtextCommand extends LineEditingCommand {
hologram.getLinesUnsafe().addAll(linesToAdd);
hologram.refresh();
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
if (isImageExtension(FileUtils.getExtension(fileName))) {
Messages.sendWarning(sender, "The read file has an image's extension. If it is an image, you should use /" + context.getRootLabel() + " readimage.");

View File

@ -49,8 +49,7 @@ public class RemovelineCommand extends LineEditingCommand implements QuickEditCo
hologram.removeLine(index);
hologram.refresh();
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram));
sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " removed.");

View File

@ -52,8 +52,7 @@ public class SetlineCommand extends LineEditingCommand implements QuickEditComma
prevLine.despawn();
hologram.refresh();
configManager.getHologramDatabase().addOrUpdate(hologram);
configManager.saveHologramDatabase();
configManager.saveHologramDatabase(internalHologramManager);
Bukkit.getPluginManager().callEvent(new InternalHologramEditEvent(hologram));
sender.sendMessage(Colors.PRIMARY + "Line " + lineNumber + " changed.");

View File

@ -13,6 +13,7 @@ import me.filoghost.fcommons.config.FileConfig;
import me.filoghost.fcommons.config.exception.ConfigException;
import me.filoghost.fcommons.config.mapped.MappedConfigLoader;
import me.filoghost.fcommons.logging.Log;
import me.filoghost.holographicdisplays.object.internal.InternalHologramManager;
import java.nio.file.Path;
@ -21,14 +22,12 @@ public class ConfigManager extends BaseConfigManager {
private final MappedConfigLoader<MainConfigModel> mainConfigLoader;
private final ConfigLoader databaseConfigLoader;
private final ConfigLoader placeholdersConfigLoader;
private final HologramDatabase hologramDatabase;
public ConfigManager(Path rootDataFolder) {
super(rootDataFolder);
this.mainConfigLoader = getMappedConfigLoader("config.yml", MainConfigModel.class);
this.databaseConfigLoader = getConfigLoader("database.yml");
this.placeholdersConfigLoader = getConfigLoader("custom-placeholders.yml");
this.hologramDatabase = new HologramDatabase();
}
public void reloadMainConfig() {
@ -44,7 +43,7 @@ public class ConfigManager extends BaseConfigManager {
Configuration.load(mainConfig);
}
public void reloadHologramDatabase() {
public HologramDatabase loadHologramDatabase() {
Config databaseConfig;
try {
@ -54,21 +53,19 @@ public class ConfigManager extends BaseConfigManager {
databaseConfig = new Config(); // Fallback: empty config
}
hologramDatabase.reloadFromConfig(databaseConfig);
HologramDatabase hologramDatabase = new HologramDatabase();
hologramDatabase.loadFromConfig(databaseConfig);
return hologramDatabase;
}
public void saveHologramDatabase() {
public void saveHologramDatabase(InternalHologramManager hologramManager) {
try {
databaseConfigLoader.save(hologramDatabase.saveToConfig());
databaseConfigLoader.save(HologramDatabase.exportToConfig(hologramManager));
} catch (ConfigException e) {
logConfigSaveException(databaseConfigLoader.getFile(), e);
}
}
public HologramDatabase getHologramDatabase() {
return hologramDatabase;
}
public void reloadCustomPlaceholders() {
FileConfig placeholdersConfig;

View File

@ -7,22 +7,22 @@ package me.filoghost.holographicdisplays.disk;
import me.filoghost.fcommons.config.Config;
import me.filoghost.fcommons.config.ConfigSection;
import me.filoghost.fcommons.logging.Log;
import me.filoghost.holographicdisplays.commands.Messages;
import me.filoghost.holographicdisplays.core.Utils;
import me.filoghost.holographicdisplays.object.internal.InternalHologram;
import me.filoghost.holographicdisplays.object.internal.InternalHologramManager;
import org.bukkit.command.CommandSender;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.List;
public class HologramDatabase {
private final LinkedHashMap<String, HologramConfig> hologramConfigs;
private static List<HologramConfig> hologramConfigs;
public HologramDatabase() {
this.hologramConfigs = new LinkedHashMap<>();
}
public void reloadFromConfig(Config config) {
hologramConfigs.clear();
public void loadFromConfig(Config config) {
hologramConfigs = new ArrayList<>();
for (String hologramName : config.getKeys()) {
ConfigSection hologramSection = config.getConfigSection(hologramName);
@ -31,15 +31,37 @@ public class HologramDatabase {
}
HologramConfig hologramConfig = new HologramConfig(hologramName, hologramSection);
hologramConfigs.put(hologramConfig.getName(), hologramConfig);
hologramConfigs.add(hologramConfig);
}
}
public Config saveToConfig() {
public void createHolograms(CommandSender sender, InternalHologramManager internalHologramManager) {
// Create all the holograms
for (HologramConfig hologramConfig : hologramConfigs) {
try {
hologramConfig.createHologram(internalHologramManager);
} catch (HologramLoadException e) {
if (sender != null) {
Messages.sendWarning(sender, Utils.formatExceptionMessage(e));
} else {
Log.warning(Utils.formatExceptionMessage(e));
}
} catch (Exception e) {
Log.warning("Unexpected exception while loading the hologram \"" + hologramConfig.getName() + "\".", e);
}
}
// Then trigger a refresh for all of them
for (InternalHologram hologram : internalHologramManager.getHolograms()) {
hologram.refresh();
}
}
public static Config exportToConfig(InternalHologramManager hologramManager) {
Config config = new Config();
for (Entry<String, HologramConfig> entry : hologramConfigs.entrySet()) {
config.setConfigSection(entry.getKey(), entry.getValue().toConfigSection());
for (InternalHologram hologram : hologramManager.getHolograms()) {
config.setConfigSection(hologram.getName(), new HologramConfig(hologram).toConfigSection());
}
config.setHeader(
@ -51,18 +73,4 @@ public class HologramDatabase {
return config;
}
public Collection<HologramConfig> getHolograms() {
return hologramConfigs.values();
}
public void addOrUpdate(InternalHologram hologram) {
HologramConfig hologramConfig = new HologramConfig(hologram);
hologramConfigs.put(hologram.getName(), hologramConfig);
}
public void removeHologram(InternalHologram hologram) {
hologramConfigs.remove(hologram.getName());
}
}

View File

@ -1,42 +0,0 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.task;
import me.filoghost.fcommons.logging.Log;
import me.filoghost.holographicdisplays.core.Utils;
import me.filoghost.holographicdisplays.disk.HologramConfig;
import me.filoghost.holographicdisplays.disk.HologramLoadException;
import me.filoghost.holographicdisplays.object.internal.InternalHologram;
import me.filoghost.holographicdisplays.object.internal.InternalHologramManager;
import java.util.Collection;
public class StartupLoadHologramsTask implements Runnable {
private final InternalHologramManager internalHologramManager;
private final Collection<HologramConfig> hologramConfigsToLoad;
public StartupLoadHologramsTask(InternalHologramManager internalHologramManager, Collection<HologramConfig> hologramConfigsToLoad) {
this.internalHologramManager = internalHologramManager;
this.hologramConfigsToLoad = hologramConfigsToLoad;
}
@Override
public void run() {
for (HologramConfig hologramConfig : hologramConfigsToLoad) {
try {
InternalHologram hologram = hologramConfig.createHologram(internalHologramManager);
hologram.refresh();
} catch (HologramLoadException e) {
Log.warning(Utils.formatExceptionMessage(e));
} catch (Exception e) {
Log.warning("Unexpected exception while loading the hologram \"" + hologramConfig.getName() + "\"."
+ " Please contact the developer.", e);
}
}
}
}