Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/com/extendedclip/deluxemenus/DeluxeMenus.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import org.bukkit.Bukkit;
import org.bukkit.Material;
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.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.stream.Collectors;
Expand All @@ -56,6 +60,10 @@ public class DeluxeMenus extends JavaPlugin {
private final GeneralConfig generalConfig = new GeneralConfig(this);
private DeluxeMenusConfig menuConfig;

// Our own copy of config.yml so that a reload can parse it on an async thread and swap it in on the main thread.
private FileConfiguration config;
private final AtomicBoolean reloading = new AtomicBoolean(false);

@Override
public void onEnable() {
this.generalConfig.load();
Expand Down Expand Up @@ -193,6 +201,41 @@ public void reload() {
this.generalConfig.reload();
}

@Override
public @NotNull FileConfiguration getConfig() {
if (this.config == null) {
reloadConfig();
}
return this.config;
}

@Override
public void reloadConfig() {
// Same as JavaPlugin#reloadConfig(). The jar has no config.yml, so there are no defaults to copy from it.
this.config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml"));
}

/**
* Replaces the in-memory config.yml with one that was already loaded, for example on an async thread.
* Must be called on the main thread.
*/
public void replaceConfig(final @NotNull FileConfiguration config) {
this.config = config;
}

/**
* Marks the start of a reload.
*
* @return false if another reload is still running
*/
public boolean tryStartReload() {
return this.reloading.compareAndSet(false, true);
}

public void finishReload() {
this.reloading.set(false);
}

public GeneralConfig getGeneralConfig() {
return generalConfig;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.extendedclip.deluxemenus.command.subcommand;

import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.config.DeluxeMenusConfig;
import com.extendedclip.deluxemenus.menu.Menu;
import com.extendedclip.deluxemenus.utils.Messages;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class ReloadCommand extends SubCommand {
Expand All @@ -31,45 +36,143 @@ public void execute(final @NotNull CommandSender sender, final @NotNull List<Str
return;
}

if (plugin.getConfiguration().checkConfig(null, "config.yml", false) == null) {
plugin.sms(sender, Messages.RELOAD_FAIL);
if (!arguments.isEmpty() && Menu.getMenuByName(arguments.get(0)).isEmpty()) {
plugin.sms(sender, Messages.INVALID_MENU.message().replaceText(MENU_REPLACER_BUILDER.replacement(arguments.get(0)).build()));
return;
}

if (!arguments.isEmpty()) {
if (Menu.getMenuByName(arguments.get(0)).isEmpty()) {
plugin.sms(sender, Messages.INVALID_MENU.message().replaceText(MENU_REPLACER_BUILDER.replacement(arguments.get(0)).build()));
return;
}

Menu.unload(plugin, arguments.get(0));
if (!plugin.tryStartReload()) {
plugin.sms(sender, Messages.RELOAD_IN_PROGRESS);
return;
}

if (plugin.getConfiguration().loadGUIMenu(arguments.get(0))) {
plugin.sms(sender, Messages.MENU_RELOADED.message().replaceText(MENU_REPLACER_BUILDER.replacement(arguments.get(0)).build()));
return;
try {
if (arguments.isEmpty()) {
reloadAll(sender);
} else {
reloadMenu(sender, arguments.get(0));
}
} catch (final Throwable throwable) {
plugin.finishReload();
throw throwable;
}
}

plugin.sms(sender, Messages.MENU_NOT_RELOADED.message().replaceText(MENU_REPLACER_BUILDER.replacement(arguments.get(0)).build()));
return;
/**
* Reloads config.yml and every menu. Reading and parsing the files happens on an async thread so the reload
* does not cause a lag spike. Only building the menus and swapping them in happens on the main thread.
*/
private void reloadAll(final @NotNull CommandSender sender) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
DeluxeMenusConfig.PreparedReload prepared;
try {
prepared = plugin.getConfiguration().prepareReload();
} catch (final Throwable throwable) {
plugin.getLogger().log(Level.SEVERE, "Something went wrong while reloading DeluxeMenus!", throwable);
prepared = null;
}

}
final DeluxeMenusConfig.PreparedReload result = prepared;
runOnMainThread(() -> {
if (result == null) {
plugin.sms(sender, Messages.RELOAD_FAIL);
return;
}

plugin.clearCaches();
plugin.replaceConfig(result.mainConfig());
plugin.getGeneralConfig().load();
Menu.unload(plugin);
plugin.getConfiguration().applyMenus(result.menus());
plugin.sms(sender, Messages.RELOAD_SUCCESS);

int gLoaded = Menu.getLoadedMenuSize();

if (gLoaded == 1) {
plugin.sms(sender, Messages.MENU_LOADED.message().replaceText(AMOUNT_REPLACER_BUILDER.replacement(String.valueOf(gLoaded)).build()));
return;
}

plugin.sms(sender, Messages.MENUS_LOADED.message().replaceText(AMOUNT_REPLACER_BUILDER.replacement(String.valueOf(gLoaded)).build()));
});
});
}

plugin.clearCaches();
plugin.reloadConfig();
plugin.saveConfig();
plugin.reload();
Menu.unload(plugin);
plugin.getConfiguration().loadGUIMenus();
plugin.sms(sender, Messages.RELOAD_SUCCESS);
/**
* Reloads a single menu. Reading and parsing its file happens on an async thread.
*/
private void reloadMenu(final @NotNull CommandSender sender, final @NotNull String menuName) {
// Read everything we need from the in-memory config.yml here, on the main thread.
final FileConfiguration config = plugin.getConfig();
final ConfigurationSection menusSection = config.getConfigurationSection("gui_menus");
final boolean listed = menusSection != null && menusSection.getKeys(false).contains(menuName);
final boolean hasFile = listed && config.contains("gui_menus." + menuName + ".file");
final String fileName = hasFile ? config.getString("gui_menus." + menuName + ".file") : null;

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
boolean configValid;
DeluxeMenusConfig.ParsedMenu parsed = null;
try {
configValid = plugin.getConfiguration().checkConfig(null, "config.yml", false) != null;
if (configValid && hasFile) {
parsed = plugin.getConfiguration().parseMenuFile(menuName, fileName);
}
} catch (final Throwable throwable) {
plugin.getLogger().log(Level.SEVERE, "Something went wrong while reloading menu: " + menuName, throwable);
configValid = false;
}

int gLoaded = Menu.getLoadedMenuSize();
final boolean valid = configValid;
final DeluxeMenusConfig.ParsedMenu parsedMenu = parsed;
runOnMainThread(() -> {
if (!valid) {
plugin.sms(sender, Messages.RELOAD_FAIL);
return;
}

Menu.unload(plugin, menuName);

boolean loaded = false;
if (listed && !hasFile) {
plugin.getConfiguration().loadMenu(plugin.getConfig(), menuName, true, "config");
loaded = true;
} else if (parsedMenu != null) {
plugin.getConfiguration().applyMenus(List.of(parsedMenu));
loaded = Menu.getMenuByName(menuName).isPresent();
}

if (loaded) {
plugin.sms(sender, Messages.MENU_RELOADED.message().replaceText(MENU_REPLACER_BUILDER.replacement(menuName).build()));
return;
}

plugin.sms(sender, Messages.MENU_NOT_RELOADED.message().replaceText(MENU_REPLACER_BUILDER.replacement(menuName).build()));
});
});
}

if (gLoaded == 1) {
plugin.sms(sender, Messages.MENU_LOADED.message().replaceText(AMOUNT_REPLACER_BUILDER.replacement(String.valueOf(gLoaded)).build()));
/**
* Runs the task on the main thread and marks the reload as finished afterwards, even if the task fails.
* If the plugin was disabled in the meantime, the task is skipped.
*/
private void runOnMainThread(final @NotNull Runnable task) {
if (!plugin.isEnabled()) {
plugin.finishReload();
return;
}

plugin.sms(sender, Messages.MENUS_LOADED.message().replaceText(AMOUNT_REPLACER_BUILDER.replacement(String.valueOf(gLoaded)).build()));
try {
Bukkit.getScheduler().runTask(plugin, () -> {
try {
task.run();
} finally {
plugin.finishReload();
}
});
} catch (final IllegalStateException exception) {
// The plugin was disabled between the check above and scheduling the task.
plugin.finishReload();
}
}

@Override
Expand Down
Loading