Skip to content

Commit

Permalink
EcoPlugin#afterLoad now runs 2 ticks after, with a preliminary reload…
Browse files Browse the repository at this point in the history
… 1 tick after startup to fix load order bugs
  • Loading branch information
WillFP committed May 13, 2023
1 parent 3fe1c2c commit 4aed337
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
8 changes: 8 additions & 0 deletions eco-api/src/main/java/com/willfp/eco/core/Eco.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ public interface Eco {
@NotNull
Logger createLogger(@NotNull EcoPlugin plugin);

/**
* Get NOOP logger.
*
* @return The logger.
*/
@NotNull
Logger getNOOPLogger();

/**
* Create a PAPI integration.
*
Expand Down
35 changes: 23 additions & 12 deletions eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public abstract class EcoPlugin extends JavaPlugin implements PluginLike, Regist
/**
* The logger for the plugin.
*/
private final Logger logger;
private Logger logger;

/**
* If the server is running an outdated version of the plugin.
Expand Down Expand Up @@ -425,7 +425,18 @@ public final void onEnable() {

this.loadPluginCommands().forEach(PluginCommand::register);

this.getScheduler().runLater(this::afterLoad, 1);
// Run preliminary reload to resolve load order issues
this.getScheduler().runLater(() -> {
Logger before = this.getLogger();
// Temporary silence logger.
this.logger = Eco.get().getNOOPLogger();

this.reload(false);

this.logger = before;
}, 1);

this.getScheduler().runLater(this::afterLoad, 2);

if (this.isSupportingExtensions()) {
this.getExtensionLoader().loadExtensions();
Expand Down Expand Up @@ -601,9 +612,18 @@ public final void afterLoad(@NotNull final LifecyclePosition position,
* Reload the plugin.
*/
public final void reload() {
this.reload(true);
}

/**
* Reload the plugin.
*
* @param cancelTasks If tasks should be cancelled.
*/
public final void reload(final boolean cancelTasks) {
this.getConfigHandler().updateConfigs();

if (this.cancelsTasksOnReload()) {
if (cancelTasks) {
this.getScheduler().cancelAll();
}

Expand Down Expand Up @@ -1163,15 +1183,6 @@ public boolean isUsingLocalStorage() {
return this.configYml.isUsingLocalStorage();
}

/**
* Get if all tasks should be cancelled on reload.
*
* @return If cancelling tasks on reload.
*/
public boolean cancelsTasksOnReload() {
return true;
}

@Override
@NotNull
public final String getID() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.willfp.eco.core.items;

import com.willfp.eco.core.Eco;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -55,7 +54,7 @@ public CustomItem(@NotNull final NamespacedKey key,
*/
Eco.get().getEcoPlugin().getScheduler().runLater(() -> {
if (!matches(getItem())) {
Bukkit.getLogger().severe("Item with key " + key + " is invalid!");
Eco.get().getEcoPlugin().getLogger().severe("Item with key " + key + " is invalid!");
}
}, 1);
}
Expand Down
5 changes: 2 additions & 3 deletions eco-api/src/main/java/com/willfp/eco/core/recipe/Recipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.willfp.eco.core.recipe.recipes.CraftingRecipe;
import com.willfp.eco.core.recipe.recipes.ShapedCraftingRecipe;
import com.willfp.eco.util.NamespacedKeyUtils;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -127,8 +126,8 @@ public static CraftingRecipe createAndRegisterRecipe(@NotNull final EcoPlugin pl
}

if (builder.isAir()) {
Bukkit.getLogger().warning("Crafting recipe " + plugin.getID() + ":" + key + " consists only");
Bukkit.getLogger().warning("of air or invalid items! It will not be registered.");
plugin.getLogger().warning("Crafting recipe " + plugin.getID() + ":" + key + " consists only");
plugin.getLogger().warning("of air or invalid items! It will not be registered.");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.willfp.eco.internal.logging

import java.util.logging.LogRecord
import java.util.logging.Logger

object NOOPLogger : Logger("eco_noop", null as String?) {
override fun log(record: LogRecord?) {
return
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.willfp.eco.internal.gui.menu.renderedInventory
import com.willfp.eco.internal.gui.slot.EcoSlotBuilder
import com.willfp.eco.internal.integrations.PAPIExpansion
import com.willfp.eco.internal.logging.EcoLogger
import com.willfp.eco.internal.logging.NOOPLogger
import com.willfp.eco.internal.placeholder.PlaceholderParser
import com.willfp.eco.internal.proxy.EcoProxyFactory
import com.willfp.eco.internal.scheduling.EcoScheduler
Expand Down Expand Up @@ -125,6 +126,9 @@ class EcoImpl : EcoSpigotPlugin(), Eco {
override fun createLogger(plugin: EcoPlugin) =
EcoLogger(plugin)

override fun getNOOPLogger() =
NOOPLogger

override fun createPAPIIntegration(plugin: EcoPlugin) {
PAPIExpansion(plugin)
}
Expand Down

0 comments on commit 4aed337

Please sign in to comment.