diff --git a/Essentials/build.gradle b/Essentials/build.gradle index d04d2800dca..b410742cd23 100644 --- a/Essentials/build.gradle +++ b/Essentials/build.gradle @@ -7,6 +7,7 @@ dependencies { exclude group: "org.bukkit", module: "bukkit" } compileOnly 'net.luckperms:api:5.3' + compileOnly 'org.apache.logging.log4j:log4j-core:2.17.1' api 'io.papermc:paperlib:1.0.6' diff --git a/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler.java b/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler.java index 01f7fa3caf0..dab484339c0 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler.java +++ b/Essentials/src/main/java/com/earth2me/essentials/AlternativeCommandsHandler.java @@ -1,6 +1,6 @@ package com.earth2me.essentials; -import java.util.stream.Collectors; +import com.earth2me.essentials.utils.DebugLogUtil; import net.ess3.provider.KnownCommandsProvider; import org.bukkit.command.Command; import org.bukkit.command.PluginIdentifiableCommand; @@ -13,7 +13,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.logging.Level; +import java.util.stream.Collectors; public class AlternativeCommandsHandler { private final transient Map>> altCommands = new HashMap<>(); @@ -45,9 +45,7 @@ public final void addPlugin(final Plugin plugin) { while (pluginCmdIterator.hasNext()) { final Command cmd = pluginCmdIterator.next().get(); if (cmd == null) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Essentials: Alternative command for " + commandName + " removed due to garbage collection"); - } + DebugLogUtil.debugLog("Essentials: Alternative command for " + commandName + " removed due to garbage collection"); pluginCmdIterator.remove(); continue; @@ -136,9 +134,7 @@ public Command getAlternative(final String label) { public void executed(final String label, final Command pc) { if (pc instanceof PluginIdentifiableCommand) { final String altString = ((PluginIdentifiableCommand) pc).getPlugin().getName() + ":" + pc.getName(); - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Essentials: Alternative command " + label + " found, using " + altString); - } + DebugLogUtil.debugLog("Essentials: Alternative command " + label + " found, using " + altString); disabledList.put(label, altString); } } diff --git a/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java index fc98e97e175..02f5e1ab01a 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java @@ -3,6 +3,7 @@ import com.earth2me.essentials.api.IAsyncTeleport; import com.earth2me.essentials.commands.WarpNotFoundException; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.LocationUtil; import io.papermc.lib.PaperLib; import net.ess3.api.IEssentials; @@ -398,9 +399,7 @@ void respawnNow(final IUser teleportee, final TeleportCause cause, final Complet if (location != null) { nowAsync(teleportee, new LocationTarget(location), cause, future); } else { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Could not find bed spawn, forcing respawn event."); - } + DebugLogUtil.debugLog("Could not find bed spawn, forcing respawn event."); final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, player.getWorld().getSpawnLocation(), false); ess.getServer().getPluginManager().callEvent(pre); nowAsync(teleportee, new LocationTarget(pre.getRespawnLocation()), cause, future); diff --git a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java index 39c7a596612..0aef3a44139 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java @@ -41,6 +41,7 @@ import com.earth2me.essentials.updatecheck.UpdateChecker; import com.earth2me.essentials.userstorage.ModernUserMap; import com.earth2me.essentials.utils.AdventureUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.VersionUtil; import io.papermc.lib.PaperLib; @@ -206,6 +207,7 @@ public ISettings getSettings() { public void setupForTesting(final Server server) throws IOException, InvalidDescriptionException { TESTING = true; LOGGER = new BaseLoggerProvider(this, BUKKIT_LOGGER); + DebugLogUtil.setEssentials(this); final File dataFolder = File.createTempFile("essentialstest", ""); if (!dataFolder.delete()) { throw new IOException(); @@ -437,9 +439,7 @@ public void onEnable() { ((Cancellable) event).setCancelled(true); } }); - if (getSettings().isDebug()) { - LOGGER.log(Level.INFO, "Registered Paper Recipe Book Event Listener"); - } + DebugLogUtil.debugLog("Registered Paper Recipe Book Event Listener"); } catch (final ClassNotFoundException ignored) { } } @@ -458,6 +458,7 @@ public void onEnable() { scheduleSyncRepeatingTask(timer, 1000, 50); Economy.setEss(this); + DebugLogUtil.setEssentials(this); execTimer.mark("RegHandler"); // Register /hat and /back default permissions @@ -477,9 +478,7 @@ public void onEnable() { execTimer.mark("Init(External)"); final String timeroutput = execTimer.end(); - if (getSettings().isDebug()) { - LOGGER.log(Level.INFO, "Essentials load " + timeroutput); - } + DebugLogUtil.debugLog("Essentials load " + timeroutput); } catch (final NumberFormatException ex) { handleCrash(ex); } catch (final Error ex) { @@ -506,9 +505,7 @@ public void saveConfig() { private void registerListeners(final PluginManager pm) { HandlerList.unregisterAll(this); - if (getSettings().isDebug()) { - LOGGER.log(Level.INFO, "Registering Listeners"); - } + DebugLogUtil.debugLog("Registering Listeners"); final EssentialsPluginListener pluginListener = new EssentialsPluginListener(this); pm.registerEvents(pluginListener, this); @@ -589,6 +586,7 @@ public void onDisable() { this.getPermissionsHandler().unregisterContexts(); Economy.setEss(null); + DebugLogUtil.setEssentials(null); Trade.closeLog(); getUsers().shutdown(); @@ -729,9 +727,7 @@ public boolean onCommand(final CommandSender sender, final Command command, fina public boolean onCommandEssentials(final CommandSender cSender, final Command command, final String commandLabel, final String[] args, final ClassLoader classLoader, final String commandPath, final String permissionPrefix, final IEssentialsModule module) { // Allow plugins to override the command via onCommand if (!getSettings().isCommandOverridden(command.getName()) && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) { - if (getSettings().isDebug()) { - LOGGER.log(Level.INFO, "Searching for alternative to: " + commandLabel); - } + DebugLogUtil.debugLog("Searching for alternative to: " + commandLabel); final Command pc = alternativeCommandsHandler.getAlternative(commandLabel); if (pc != null) { alternativeCommandsHandler.executed(commandLabel, pc); @@ -893,9 +889,7 @@ public void showError(final CommandSource sender, final Throwable exception, fin } else { sender.sendTl("errorWithMessage", exception.getMessage()); } - if (getSettings().isDebug()) { - LOGGER.log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("errorCallingCommand", commandLabel)), exception); - } + DebugLogUtil.debugLog(AdventureUtil.miniToLegacy(tlLiteral("errorCallingCommand", commandLabel)), exception); } @Override diff --git a/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java index 8b6b2d51f16..93f4d2aa713 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java +++ b/Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java @@ -9,6 +9,7 @@ import com.earth2me.essentials.utils.AdventureUtil; import com.earth2me.essentials.utils.CommonPlaceholders; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.LocationUtil; import com.earth2me.essentials.utils.MaterialUtil; @@ -203,11 +204,8 @@ public void onPlayerChat(final AsyncPlayerChatEvent event) { } } } catch (final UnsupportedOperationException ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Ignore could not block chat due to custom chat plugin event.", ex); - } else { - ess.getLogger().info("Ignore could not block chat due to custom chat plugin event."); - } + DebugLogUtil.debugLog("Ignore could not block chat due to custom chat plugin event.", ex); + ess.getLogger().info("Ignore could not block chat due to custom chat plugin event."); } user.updateActivityOnChat(true); @@ -383,9 +381,7 @@ public void run() { final Player toVanish = ess.getServer().getPlayerExact(p); if (toVanish != null && toVanish.isOnline()) { user.getBase().hidePlayer(toVanish); - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Hiding vanished player: " + p); - } + DebugLogUtil.debugLog("Hiding vanished player: " + p); } } } @@ -501,11 +497,8 @@ public void run() { try { tempInput = new TextInput(user.getSource(), "motd", true, ess); } catch (final IOException ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.WARNING, ex.getMessage(), ex); - } else { - ess.getLogger().log(Level.WARNING, ex.getMessage()); - } + DebugLogUtil.debugLog(ex.getMessage(), ex); + ess.getLogger().log(Level.WARNING, ex.getMessage()); } } @@ -741,9 +734,7 @@ public void handlePlayerCommandPreprocess(final PlayerCommandPreprocessEvent eve final Entry cooldownEntry = ess.getSettings().getCommandCooldownEntry(fullCommand); if (cooldownEntry != null) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Applying " + cooldownEntry.getValue() + "ms cooldown on /" + fullCommand + " for" + user.getName() + "."); - } + DebugLogUtil.debugLog("Applying " + cooldownEntry.getValue() + "ms cooldown on /" + fullCommand + " for" + user.getName() + "."); final Date expiry = new Date(System.currentTimeMillis() + cooldownEntry.getValue()); user.addCommandCooldown(cooldownEntry.getKey(), expiry, ess.getSettings().isCommandCooldownPersistent(fullCommand)); } @@ -881,9 +872,7 @@ public void run() { ess.scheduleSyncDelayedTask(new DelayedClickJumpTask()); } catch (final Exception ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.WARNING, ex.getMessage(), ex); - } + DebugLogUtil.debugLog(ex.getMessage(), ex); } } diff --git a/Essentials/src/main/java/com/earth2me/essentials/Jails.java b/Essentials/src/main/java/com/earth2me/essentials/Jails.java index f4e31f09692..3e229a94f54 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Jails.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Jails.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.config.EssentialsConfiguration; import com.earth2me.essentials.config.entities.LazyLocation; import com.earth2me.essentials.utils.AdventureUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import net.ess3.api.IEssentials; import net.ess3.api.IUser; import net.ess3.api.TranslatableException; @@ -75,9 +76,7 @@ private void registerListeners() { final PluginManager pluginManager = ess.getServer().getPluginManager(); final JailListener blockListener = new JailListener(); pluginManager.registerEvents(blockListener, ess); - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Registering Jail listener"); - } + DebugLogUtil.debugLog("Registering Jail listener"); } public void resetListener() { @@ -272,11 +271,8 @@ public void onJailPlayerRespawn(final PlayerRespawnEvent event) { try { event.setRespawnLocation(getJail(user.getJail())); } catch (final Exception ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage())), ex); - } else { - ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()))); - } + DebugLogUtil.debugLog(AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage())), ex); + ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()))); } } @@ -294,11 +290,8 @@ public void onJailPlayerTeleport(final PlayerTeleportEvent event) { try { event.setTo(getJail(user.getJail())); } catch (final Exception ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage())), ex); - } else { - ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()))); - } + DebugLogUtil.debugLog(AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage())), ex); + ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()))); } user.sendTl("jailMessage"); } @@ -318,11 +311,8 @@ public void onJailPlayerJoin(final PlayerJoinEvent event) { final CompletableFuture future = new CompletableFuture<>(); future.exceptionally(ex -> { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage())), ex); - } else { - ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()))); - } + DebugLogUtil.debugLog(AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage())), ex); + ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("returnPlayerToJailError", user.getName(), ex.getLocalizedMessage()))); return false; }); future.thenAccept(success -> user.sendTl("jailMessage")); diff --git a/Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java b/Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java index 8347c19a391..97de5918cdf 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java +++ b/Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java @@ -3,6 +3,7 @@ import com.earth2me.essentials.textreader.BookInput; import com.earth2me.essentials.textreader.BookPager; import com.earth2me.essentials.textreader.IText; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.MaterialUtil; import com.earth2me.essentials.utils.NumberUtil; @@ -41,7 +42,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.logging.Level; import java.util.regex.Pattern; public class MetaItemStack { @@ -138,9 +138,7 @@ public boolean canSpawn(final IEssentials ess) { } catch (final NoSuchMethodError nsme) { return true; } catch (final Throwable npe) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Itemstack is invalid", npe); - } + DebugLogUtil.debugLog("Itemstack is invalid", npe); return false; } } @@ -158,9 +156,7 @@ public void parseStringMeta(final CommandSource sender, final boolean allowUnsaf try { stack = ess.getServer().getUnsafe().modifyItemStack(stack, Joiner.on(' ').join(Arrays.asList(string).subList(fromArg, string.length))); } catch (final NullPointerException npe) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Itemstack is invalid", npe); - } + DebugLogUtil.debugLog("Itemstack is invalid", npe); } catch (final NoSuchMethodError nsme) { throw new TranslatableException(nsme, "noMetaJson"); } catch (final Throwable throwable) { @@ -176,9 +172,7 @@ public void parseStringMeta(final CommandSource sender, final boolean allowUnsaf // modifyItemStack requires that the item namespaced key is prepended to the components for some reason stack = ess.getServer().getUnsafe().modifyItemStack(stack, stack.getType().getKey() + components); } catch (final NullPointerException npe) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Itemstack is invalid", npe); - } + DebugLogUtil.debugLog("Itemstack is invalid", npe); } catch (final Throwable throwable) { throw new Exception(throwable.getMessage(), throwable); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/ProviderFactory.java b/Essentials/src/main/java/com/earth2me/essentials/ProviderFactory.java index 8cf458a7404..726201289a0 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/ProviderFactory.java +++ b/Essentials/src/main/java/com/earth2me/essentials/ProviderFactory.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import com.earth2me.essentials.utils.DebugLogUtil; import io.papermc.lib.PaperLib; import net.ess3.provider.Provider; import net.essentialsx.providers.NullableProvider; @@ -46,9 +47,7 @@ public final void registerProvider(final Class... toRegister if (Provider.class.isAssignableFrom(superclass)) { //noinspection unchecked registeredProviders.computeIfAbsent((Class) superclass, k -> new ArrayList<>()).add(provider); - if (essentials.getSettings().isDebug()) { - essentials.getLogger().info("Registered provider " + provider.getSimpleName() + " for " + superclass.getSimpleName()); - } + DebugLogUtil.debugLog("Registered provider " + provider.getSimpleName() + " for " + superclass.getSimpleName()); } } } diff --git a/Essentials/src/main/java/com/earth2me/essentials/Settings.java b/Essentials/src/main/java/com/earth2me/essentials/Settings.java index 8980aa97b3f..b4e288716d9 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Settings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Settings.java @@ -9,6 +9,7 @@ import com.earth2me.essentials.textreader.IText; import com.earth2me.essentials.textreader.SimpleTextInput; import com.earth2me.essentials.utils.AdventureUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.LocationUtil; @@ -610,9 +611,7 @@ public String getChatFormat(final String group) { @Override public String getChatFormat(final String group, final ChatType chatType) { final String mFormat = chatFormats.getFormat(group, chatType, new ChatFormatConfigSupplier(group, chatType)); - if (isDebug()) { - ess.getLogger().info(String.format("Found format '%s' for group '%s'", mFormat, group)); - } + DebugLogUtil.debugLog(String.format("Found format '%s' for group '%s'", mFormat, group)); return mFormat; } @@ -824,9 +823,7 @@ public void reloadConfig() { if (knownCommandsProvider != null) { boolean mapModified = false; if (!disabledBukkitCommands.isEmpty()) { - if (isDebug()) { - ess.getLogger().log(Level.INFO, "Re-adding " + disabledBukkitCommands.size() + " disabled commands!"); - } + DebugLogUtil.debugLog("Re-adding " + disabledBukkitCommands.size() + " disabled commands!"); knownCommandsProvider.getKnownCommands().putAll(disabledBukkitCommands); disabledBukkitCommands.clear(); mapModified = true; @@ -848,14 +845,10 @@ public void reloadConfig() { final String effectiveAlias = command.toLowerCase(Locale.ENGLISH); final Command toDisable = ess.getPluginCommand(effectiveAlias); if (toDisable != null) { - if (isDebug()) { - ess.getLogger().log(Level.INFO, "Attempting removal of " + effectiveAlias); - } + DebugLogUtil.debugLog("Attempting removal of " + effectiveAlias); final Command removed = knownCommandsProvider.getKnownCommands().remove(effectiveAlias); if (removed != null) { - if (isDebug()) { - ess.getLogger().log(Level.INFO, "Adding command " + effectiveAlias + " to disabled map!"); - } + DebugLogUtil.debugLog("Adding command " + effectiveAlias + " to disabled map!"); disabledBukkitCommands.put(effectiveAlias, removed); } @@ -872,9 +865,7 @@ public void reloadConfig() { final SyncCommandsProvider syncCommandsProvider = ess.provider(SyncCommandsProvider.class); if (mapModified) { - if (isDebug()) { - ess.getLogger().log(Level.INFO, "Syncing commands"); - } + DebugLogUtil.debugLog("Syncing commands"); if (reloadCount.get() < 2) { ess.scheduleSyncDelayedTask(syncCommandsProvider::syncCommands); } else { @@ -1804,9 +1795,7 @@ public Entry getCommandCooldownEntry(final String label) { for (final Entry entry : this.commandCooldowns.entrySet()) { // Check if label matches current pattern (command-cooldown in config) final boolean matches = entry.getKey().matcher(label).matches(); - if (isDebug()) { - ess.getLogger().info(String.format("Checking command '%s' against cooldown '%s': %s", label, entry.getKey(), matches)); - } + DebugLogUtil.debugLog(String.format("Checking command '%s' against cooldown '%s': %s", label, entry.getKey(), matches)); if (matches) { return entry; diff --git a/Essentials/src/main/java/com/earth2me/essentials/Trade.java b/Essentials/src/main/java/com/earth2me/essentials/Trade.java index 5168e2d76ca..121b71d881b 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Trade.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Trade.java @@ -3,6 +3,7 @@ import com.earth2me.essentials.craftbukkit.Inventories; import com.earth2me.essentials.craftbukkit.SetExpFix; import com.earth2me.essentials.utils.AdventureUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.VersionUtil; import net.ess3.api.IEssentials; @@ -187,9 +188,7 @@ public void isAffordableFor(final IUser user) throws ChargeException { } public void isAffordableFor(final IUser user, final CompletableFuture future) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "checking if " + user.getName() + " can afford charge."); - } + DebugLogUtil.debugLog("checking if " + user.getName() + " can afford charge."); if (getMoney() != null && getMoney().signum() > 0 && !user.canAfford(getMoney())) { future.completeExceptionally(new ChargeException("notEnoughMoney", AdventureUtil.parsed(NumberUtil.displayCurrency(getMoney(), ess)))); @@ -218,16 +217,12 @@ public boolean pay(final IUser user) throws MaxMoneyException { public Map pay(final IUser user, final OverflowType type) throws MaxMoneyException { if (getMoney() != null && getMoney().signum() > 0) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "paying user " + user.getName() + " via trade " + getMoney().toPlainString()); - } + DebugLogUtil.debugLog("paying user " + user.getName() + " via trade " + getMoney().toPlainString()); user.giveMoney(getMoney()); } if (getItemStack() != null) { if (type == OverflowType.ABORT && !Inventories.hasSpace(user.getBase(), 0, false, getItemStack())) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "abort paying " + user.getName() + " itemstack " + getItemStack().toString() + " due to lack of inventory space "); - } + DebugLogUtil.debugLog("abort paying " + user.getName() + " itemstack " + getItemStack().toString() + " due to lack of inventory space "); return Collections.singletonMap(0, getItemStack()); } @@ -235,9 +230,7 @@ public Map pay(final IUser user, final OverflowType type) th user.getBase().updateInventory(); if (!leftover.isEmpty()) { if (type == OverflowType.RETURN) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "paying " + user.getName() + " partial itemstack " + getItemStack().toString() + " with overflow " + leftover.get(0).toString()); - } + DebugLogUtil.debugLog("paying " + user.getName() + " partial itemstack " + getItemStack().toString() + " with overflow " + leftover.get(0).toString()); return leftover; } else { for (final ItemStack itemStack : leftover.values()) { @@ -248,12 +241,10 @@ public Map pay(final IUser user, final OverflowType type) th spillAmount -= itemStack.getAmount(); } } - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "paying " + user.getName() + " partial itemstack " + getItemStack().toString() + " and dropping overflow " + leftover.get(0).toString()); - } + DebugLogUtil.debugLog("paying " + user.getName() + " partial itemstack " + getItemStack().toString() + " and dropping overflow " + leftover.get(0).toString()); } - } else if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "paying " + user.getName() + " itemstack " + getItemStack().toString()); + } else { + DebugLogUtil.debugLog("paying " + user.getName() + " itemstack " + getItemStack().toString()); } user.getBase().updateInventory(); } @@ -278,13 +269,9 @@ public void charge(final IUser user) throws ChargeException { } public void charge(final IUser user, final CompletableFuture future) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "attempting to charge user " + user.getName()); - } + DebugLogUtil.debugLog("attempting to charge user " + user.getName()); if (getMoney() != null) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "charging user " + user.getName() + " money " + getMoney().toPlainString()); - } + DebugLogUtil.debugLog("charging user " + user.getName() + " money " + getMoney().toPlainString()); if (!user.canAfford(getMoney()) && getMoney().signum() > 0) { future.completeExceptionally(new ChargeException("notEnoughMoney", AdventureUtil.parsed(NumberUtil.displayCurrency(getMoney(), ess)))); return; @@ -292,9 +279,7 @@ public void charge(final IUser user, final CompletableFuture future) { user.takeMoney(getMoney()); } if (getItemStack() != null) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "charging user " + user.getName() + " itemstack " + getItemStack().toString()); - } + DebugLogUtil.debugLog("charging user " + user.getName() + " itemstack " + getItemStack().toString()); if (!Inventories.containsAtLeast(user.getBase(), getItemStack(), getItemStack().getAmount())) { future.completeExceptionally(new ChargeException("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " "))); return; @@ -311,9 +296,7 @@ public void charge(final IUser user, final CompletableFuture future) { user.takeMoney(cost); } if (getExperience() != null) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "charging user " + user.getName() + " exp " + getExperience()); - } + DebugLogUtil.debugLog("charging user " + user.getName() + " exp " + getExperience()); final int experience = SetExpFix.getTotalExperience(user.getBase()); if (experience < getExperience() && getExperience() > 0) { future.completeExceptionally(new ChargeException("notEnoughExperience")); @@ -321,9 +304,7 @@ public void charge(final IUser user, final CompletableFuture future) { } SetExpFix.setTotalExperience(user.getBase(), experience - getExperience()); } - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "charge user " + user.getName() + " completed"); - } + DebugLogUtil.debugLog("charge user " + user.getName() + " completed"); } public BigDecimal getMoney() { @@ -358,9 +339,7 @@ public BigDecimal getCommandCost(final IUser user) { cost = fallbackTrade.getCommandCost(user); } - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "calculated command (" + command + ") cost for " + user.getName() + " as " + cost); - } + DebugLogUtil.debugLog("calculated command (" + command + ") cost for " + user.getName() + " as " + cost); } if (cost.signum() != 0 && (user.isAuthorized("essentials.nocommandcost.all") || user.isAuthorized("essentials.nocommandcost." + command))) { return BigDecimal.ZERO; diff --git a/Essentials/src/main/java/com/earth2me/essentials/User.java b/Essentials/src/main/java/com/earth2me/essentials/User.java index 72f6feaad6c..fa86ca75382 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/User.java +++ b/Essentials/src/main/java/com/earth2me/essentials/User.java @@ -8,6 +8,7 @@ import com.earth2me.essentials.messaging.SimpleMessageRecipient; import com.earth2me.essentials.utils.AdventureUtil; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.NumberUtil; @@ -137,18 +138,14 @@ public boolean isAuthorized(final IEssentialsCommand cmd, final String permissio @Override public boolean isAuthorized(final String node) { final boolean result = isAuthorizedCheck(node); - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "checking if " + base.getName() + " has " + node + " - " + result); - } + DebugLogUtil.debugLog("checking if " + base.getName() + " has " + node + " - " + result); return result; } @Override public boolean isPermissionSet(final String node) { final boolean result = isPermSetCheck(node); - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "checking if " + base.getName() + " has " + node + " (set-explicit) - " + result); - } + DebugLogUtil.debugLog("checking if " + base.getName() + " has " + node + " (set-explicit) - " + result); return result; } @@ -168,11 +165,8 @@ private boolean isAuthorizedCheck(final String node) { try { return ess.getPermissionsHandler().hasPermission(base, node); } catch (final Exception ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage(), ex); - } else { - ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage()); - } + DebugLogUtil.debugLog("Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage(), ex); + ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage()); return false; } @@ -186,11 +180,8 @@ private boolean isPermSetCheck(final String node) { try { return ess.getPermissionsHandler().isPermissionSet(base, node); } catch (final Exception ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage(), ex); - } else { - ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage()); - } + DebugLogUtil.debugLog("Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage(), ex); + ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage()); return false; } @@ -204,11 +195,8 @@ private TriState isAuthorizedExactCheck(final String node) { try { return ess.getPermissionsHandler().isPermissionSetExact(base, node); } catch (final Exception ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage(), ex); - } else { - ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage()); - } + DebugLogUtil.debugLog("Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage(), ex); + ess.getLogger().log(Level.SEVERE, "Permission System Error: " + ess.getPermissionsHandler().getName() + " returned: " + ex.getMessage()); return TriState.UNSET; } @@ -528,9 +516,7 @@ public void setDisplayNick() { try { this.getBase().setPlayerListName(name); } catch (final IllegalArgumentException e) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Playerlist for " + name + " was not updated. Name clashed with another online player."); - } + DebugLogUtil.debugLog("Playerlist for " + name + " was not updated. Name clashed with another online player."); } } } @@ -582,9 +568,7 @@ public void setMoney(final BigDecimal value) throws MaxMoneyException { private BigDecimal _getMoney() { if (ess.getSettings().isEcoDisabled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Internal economy functions disabled, aborting balance check."); - } + DebugLogUtil.debugLog("Internal economy functions disabled, aborting balance check."); return BigDecimal.ZERO; } final EconomyLayer layer = EconomyLayers.getSelectedLayer(); @@ -596,9 +580,7 @@ private BigDecimal _getMoney() { public void setMoney(final BigDecimal value, final UserBalanceUpdateEvent.Cause cause) throws MaxMoneyException { if (ess.getSettings().isEcoDisabled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Internal economy functions disabled, aborting balance change."); - } + DebugLogUtil.debugLog("Internal economy functions disabled, aborting balance change."); return; } final BigDecimal oldBalance = _getMoney(); @@ -882,18 +864,14 @@ public boolean isGodModeEnabledRaw() { @Override public String getGroup() { final String result = ess.getPermissionsHandler().getGroup(base); - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "looking up groupname of " + base.getName() + " - " + result); - } + DebugLogUtil.debugLog("looking up groupname of " + base.getName() + " - " + result); return result; } @Override public boolean inGroup(final String group) { final boolean result = ess.getPermissionsHandler().inGroup(base, group); - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "checking if " + base.getName() + " is in group " + group + " - " + result); - } + DebugLogUtil.debugLog("checking if " + base.getName() + " is in group " + group + " - " + result); return result; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbalancetop.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbalancetop.java index f5915467fdc..efeee5d2e75 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbalancetop.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandbalancetop.java @@ -5,6 +5,7 @@ import com.earth2me.essentials.textreader.SimpleTextInput; import com.earth2me.essentials.textreader.TextPager; import com.earth2me.essentials.utils.AdventureUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.VersionUtil; @@ -104,9 +105,7 @@ private class Viewer implements Runnable { @Override public void run() { if (ess.getSettings().isEcoDisabled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Internal economy functions disabled, aborting baltop."); - } + DebugLogUtil.debugLog("Internal economy functions disabled, aborting baltop."); return; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandcreatekit.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandcreatekit.java index 7325a75b770..f78a82200e1 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandcreatekit.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandcreatekit.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.User; import com.earth2me.essentials.craftbukkit.Inventories; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.PasteUtil; import net.ess3.api.TranslatableException; import net.ess3.provider.SerializationProvider; @@ -94,9 +95,7 @@ private void uploadPaste(final CommandSource sender, final String kitName, final sender.sendMessage(separator); sender.sendTl("createKitSuccess", kitName, delayFormat, result.getPasteUrl()); sender.sendMessage(separator); - if (ess.getSettings().isDebug()) { - ess.getLogger().info(sender.getSender().getName() + " created a kit: " + result.getPasteUrl()); - } + DebugLogUtil.debugLog(sender.getSender().getName() + " created a kit: " + result.getPasteUrl()); } }); future.exceptionally(throwable -> { diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelhome.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelhome.java index 20500ef45d2..7b14b817e17 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelhome.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelhome.java @@ -3,6 +3,7 @@ import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.IUser; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import net.ess3.api.TranslatableException; import net.essentialsx.api.v2.events.HomeModifyEvent; import org.bukkit.Bukkit; @@ -22,9 +23,7 @@ private void deleteHome(CommandSource sender, User user, String home) { final HomeModifyEvent event = new HomeModifyEvent(sender.getUser(), user, home, user.getHome(home), false); Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("HomeModifyEvent canceled for /delhome execution by " + sender.getDisplayName()); - } + DebugLogUtil.debugLog("HomeModifyEvent canceled for /delhome execution by " + sender.getDisplayName()); return; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandeditsign.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandeditsign.java index b6908bdae56..493f8e0fd0f 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandeditsign.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandeditsign.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.VersionUtil; @@ -133,9 +134,7 @@ private boolean callSignEvent(final ModifiableSign sign, final Player player, fi Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("SignChangeEvent canceled for /editsign execution by " + player.getName()); - } + DebugLogUtil.debugLog("SignChangeEvent canceled for /editsign execution by " + player.getName()); return true; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java index fa97ff2ff9f..25f12d2fee8 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java @@ -10,6 +10,7 @@ import com.earth2me.essentials.utils.AdventureUtil; import com.earth2me.essentials.utils.CommandMapUtil; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FloatUtil; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.PasteUtil; @@ -578,9 +579,7 @@ private void runCleanup(final Server server, final CommandSource sender, final S continue; } - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Deleting user: " + user.getName() + " Money: " + moneyCount + " Homes: " + homeCount + " Last seen: " + DateUtil.formatDateDiff(lastLog)); - } + DebugLogUtil.debugLog("Deleting user: " + user.getName() + " Money: " + moneyCount + " Homes: " + homeCount + " Last seen: " + DateUtil.formatDateDiff(lastLog)); user.reset(); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandkit.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandkit.java index 1394a3c5e86..48c862756cd 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandkit.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandkit.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.Kit; import com.earth2me.essentials.User; import com.earth2me.essentials.utils.AdventureUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.StringUtil; import net.ess3.api.TranslatableException; import org.bukkit.Server; @@ -12,7 +13,6 @@ import java.util.Collections; import java.util.List; import java.util.Locale; -import java.util.logging.Level; public class Commandkit extends EssentialsCommand { public Commandkit() { @@ -86,9 +86,7 @@ private void giveKits(final User userTo, final User userFrom, final String kitNa userTo.sendTl("kitReceive", kit.getName()); } catch (final NoChargeException ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Soft kit error, abort spawning " + kit.getName(), ex); - } + DebugLogUtil.debugLog("Soft kit error, abort spawning " + kit.getName(), ex); } catch (final Exception ex) { ess.showError(userFrom.getSource(), ex, "\\ kit: " + kit.getName()); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrenamehome.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrenamehome.java index 4bf8e3cbc45..e343da59a08 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrenamehome.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandrenamehome.java @@ -2,6 +2,7 @@ import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.NumberUtil; import net.ess3.api.IUser; import net.ess3.api.TranslatableException; @@ -64,9 +65,7 @@ public void run(final Server server, final User user, final String commandLabel, final HomeModifyEvent event = new HomeModifyEvent(user, usersHome, oldName, newName, usersHome.getHome(oldName)); Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("HomeModifyEvent canceled for /renamehome execution by " + user.getDisplayName()); - } + DebugLogUtil.debugLog("HomeModifyEvent canceled for /renamehome execution by " + user.getDisplayName()); return; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsethome.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsethome.java index a3f446bdd80..7358a914974 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsethome.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandsethome.java @@ -2,6 +2,7 @@ import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.LocationUtil; import com.earth2me.essentials.utils.NumberUtil; import net.ess3.api.TranslatableException; @@ -74,9 +75,7 @@ public void run(final Server server, final User user, final String commandLabel, Bukkit.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("HomeModifyEvent canceled for /sethome execution by " + user.getDisplayName()); - } + DebugLogUtil.debugLog("HomeModifyEvent canceled for /sethome execution by " + user.getDisplayName()); return; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpaccept.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpaccept.java index 43398bdb799..1e6239a6da0 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpaccept.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpaccept.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.IUser; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import net.ess3.api.TranslatableException; import net.essentialsx.api.v2.events.TeleportRequestResponseEvent; import org.bukkit.Bukkit; @@ -97,9 +98,7 @@ private void handleTeleport(final User user, final IUser.TpaRequest request, Str final TeleportRequestResponseEvent event = new TeleportRequestResponseEvent(user, requester, request, true); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("TPA accept cancelled by API for " + user.getName() + " (requested by " + requester.getName() + ")"); - } + DebugLogUtil.debugLog("TPA accept cancelled by API for " + user.getName() + " (requested by " + requester.getName() + ")"); return; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpdeny.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpdeny.java index 869eb7da20c..eb3e336fab4 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpdeny.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandtpdeny.java @@ -2,6 +2,7 @@ import com.earth2me.essentials.IUser; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import net.ess3.api.TranslatableException; import net.essentialsx.api.v2.events.TeleportRequestResponseEvent; import org.bukkit.Bukkit; @@ -82,8 +83,8 @@ private boolean sendEvent(User user, User player, IUser.TpaRequest request) { final TeleportRequestResponseEvent event = new TeleportRequestResponseEvent(user, player, request, false); Bukkit.getPluginManager().callEvent(event); final boolean cancelled = event.isCancelled(); - if (cancelled && ess.getSettings().isDebug()) { - ess.getLogger().info("TPA deny cancelled by API for " + user.getName() + " (requested by " + player.getName() + ")"); + if (cancelled) { + DebugLogUtil.debugLog("TPA deny cancelled by API for " + user.getName() + " (requested by " + player.getName() + ")"); } return event.isCancelled(); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/EssentialsCommand.java b/Essentials/src/main/java/com/earth2me/essentials/commands/EssentialsCommand.java index 897c3d98101..22baa88640b 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/EssentialsCommand.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/EssentialsCommand.java @@ -5,6 +5,7 @@ import com.earth2me.essentials.IEssentialsModule; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -314,17 +315,13 @@ protected final List tabCompleteCommand(final CommandSource sender, fina } final int numArgs = args.length - index - 1; - if (ess.getSettings().isDebug()) { - ess.getLogger().info(numArgs + " " + index + " " + Arrays.toString(args)); - } + DebugLogUtil.debugLog(numArgs + " " + index + " " + Arrays.toString(args)); String[] effectiveArgs = new String[numArgs]; System.arraycopy(args, index, effectiveArgs, 0, numArgs); if (effectiveArgs.length == 0) { effectiveArgs = new String[] {""}; } - if (ess.getSettings().isDebug()) { - ess.getLogger().info(command + " -- " + Arrays.toString(effectiveArgs)); - } + DebugLogUtil.debugLog(command + " -- " + Arrays.toString(effectiveArgs)); return command.tabComplete(sender.getSender(), label, effectiveArgs); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/economy/vault/VaultEconomyProvider.java b/Essentials/src/main/java/com/earth2me/essentials/economy/vault/VaultEconomyProvider.java index 091c9a2f320..5e8b2a3f165 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/economy/vault/VaultEconomyProvider.java +++ b/Essentials/src/main/java/com/earth2me/essentials/economy/vault/VaultEconomyProvider.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.api.UserDoesNotExistException; import com.earth2me.essentials.config.EssentialsUserConfiguration; import com.earth2me.essentials.utils.AdventureUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.StringUtil; import com.google.common.base.Charsets; @@ -318,9 +319,7 @@ public boolean createPlayerAccount(OfflinePlayer player) { } // Loading a v4 UUID that we somehow didn't track, mark it as a normal player and hope for the best, vault sucks :/ - if (ess.getSettings().isDebug()) { - ess.getLogger().info("Vault requested a player account creation for a v4 UUID: " + player); - } + DebugLogUtil.debugLog("Vault requested a player account creation for a v4 UUID: " + player); final Player userPlayer; if (player instanceof Player) { diff --git a/Essentials/src/main/java/com/earth2me/essentials/items/AbstractItemDb.java b/Essentials/src/main/java/com/earth2me/essentials/items/AbstractItemDb.java index a3bcd932fdc..b212eac6c3b 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/items/AbstractItemDb.java +++ b/Essentials/src/main/java/com/earth2me/essentials/items/AbstractItemDb.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.IConf; import com.earth2me.essentials.User; import com.earth2me.essentials.craftbukkit.Inventories; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.MaterialUtil; import com.earth2me.essentials.utils.VersionUtil; @@ -104,9 +105,7 @@ public ItemStack get(final String id) throws Exception { ItemStack tryResolverDeserialize(final String id) { for (final PluginKey key : resolverMap.keySet()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info(String.format("Trying to deserialize item '%s' with resolver '%s'...", id, key)); - } + DebugLogUtil.debugLog(String.format("Trying to deserialize item '%s' with resolver '%s'...", id, key)); final Function resolver = resolverMap.get(key); final ItemStack stack = resolver.apply(id); @@ -121,9 +120,7 @@ ItemStack tryResolverDeserialize(final String id) { String tryResolverSerialize(final ItemStack stack) { for (final PluginKey key : resolverMap.keySet()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info(String.format("Trying to serialize '%s' with resolver '%s'...", stack.toString(), key)); - } + DebugLogUtil.debugLog(String.format("Trying to serialize '%s' with resolver '%s'...", stack.toString(), key)); final ItemResolver resolver = resolverMap.get(key); final String serialized = resolver.serialize(stack); diff --git a/Essentials/src/main/java/com/earth2me/essentials/signs/EssentialsSign.java b/Essentials/src/main/java/com/earth2me/essentials/signs/EssentialsSign.java index 7e6e1bc3cd0..8e1405628ea 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/signs/EssentialsSign.java +++ b/Essentials/src/main/java/com/earth2me/essentials/signs/EssentialsSign.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.utils.AdventureUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.MaterialUtil; import com.earth2me.essentials.utils.NumberUtil; @@ -111,9 +112,7 @@ protected final boolean onSignCreate(final SignChangeEvent event, final IEssenti final SignCreateEvent signEvent = new SignCreateEvent(sign, this, user); ess.getServer().getPluginManager().callEvent(signEvent); if (signEvent.isCancelled()) { - if (ess.getSettings().isDebug()) { - ess.getLogger().info("SignCreateEvent cancelled for sign " + signEvent.getEssentialsSign().getName()); - } + DebugLogUtil.debugLog("SignCreateEvent cancelled for sign " + signEvent.getEssentialsSign().getName()); return false; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/signs/SignBlockListener.java b/Essentials/src/main/java/com/earth2me/essentials/signs/SignBlockListener.java index 39966f3784f..7034d64ca5a 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/signs/SignBlockListener.java +++ b/Essentials/src/main/java/com/earth2me/essentials/signs/SignBlockListener.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.signs; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.MaterialUtil; import net.ess3.api.IEssentials; @@ -48,9 +49,7 @@ public void onSignBlockBreak(final BlockBreakEvent event) { public boolean protectSignsAndBlocks(final Block block, final Player player) throws MaxMoneyException { // prevent any signs be broken by destroying the block they are attached to if (EssentialsSign.checkIfBlockBreaksSigns(block)) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Prevented that a block was broken next to a sign."); - } + DebugLogUtil.debugLog("Prevented that a block was broken next to a sign."); return true; } diff --git a/Essentials/src/main/java/com/earth2me/essentials/signs/SignPlayerListener.java b/Essentials/src/main/java/com/earth2me/essentials/signs/SignPlayerListener.java index ff2638c7d3f..63432154ae0 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/signs/SignPlayerListener.java +++ b/Essentials/src/main/java/com/earth2me/essentials/signs/SignPlayerListener.java @@ -1,5 +1,6 @@ package com.earth2me.essentials.signs; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.MaterialUtil; import net.ess3.api.IEssentials; import org.bukkit.Material; @@ -11,8 +12,6 @@ import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import java.util.logging.Level; - public class SignPlayerListener implements Listener { private final transient IEssentials ess; @@ -39,9 +38,7 @@ public void onSignPlayerInteract(final PlayerInteractEvent event) { try { targetBlock = ess.getUser(event.getPlayer()).getTargetBlock(5); } catch (final IllegalStateException ex) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.WARNING, ex.getMessage(), ex); - } + DebugLogUtil.debugLog(ex.getMessage(), ex); } block = targetBlock; } else { diff --git a/Essentials/src/main/java/com/earth2me/essentials/textreader/KeywordReplacer.java b/Essentials/src/main/java/com/earth2me/essentials/textreader/KeywordReplacer.java index e61c5ced543..6d364ab6121 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/textreader/KeywordReplacer.java +++ b/Essentials/src/main/java/com/earth2me/essentials/textreader/KeywordReplacer.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.User; import com.earth2me.essentials.utils.AdventureUtil; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.DescParseTickFormat; import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.FormatUtil; @@ -27,7 +28,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -159,9 +159,7 @@ private void replaceKeywords(final CommandSource sender) { execTimer.mark("Text Replace"); final String timeroutput = execTimer.end(); - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Keyword Replacer " + timeroutput); - } + DebugLogUtil.debugLog("Keyword Replacer " + timeroutput); } private String replaceLine(String line, final String fullMatch, final String[] matchTokens, final User user) { diff --git a/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUUIDCache.java b/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUUIDCache.java index 8c0a88bbd9b..f30ebe0d172 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUUIDCache.java +++ b/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUUIDCache.java @@ -1,5 +1,6 @@ package com.earth2me.essentials.userstorage; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.StringUtil; import com.google.common.io.Files; import net.ess3.api.IEssentials; @@ -93,9 +94,7 @@ protected void updateCache(final UUID uuid, final String name) { final String sanitizedName = getSanitizedName(name); final UUID replacedUuid = nameToUuidMap.put(sanitizedName, uuid); if (!uuid.equals(replacedUuid)) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.WARNING, "Replaced UUID during cache update for " + sanitizedName + ": " + replacedUuid + " -> " + uuid); - } + DebugLogUtil.debugLog("Replaced UUID during cache update for " + sanitizedName + ": " + replacedUuid + " -> " + uuid); pendingNameWrite.set(true); } } @@ -127,8 +126,6 @@ protected void removeCache(final UUID uuid) { } private void loadCache() { - final boolean debug = ess.getSettings().isDebug(); - try { if (!nameToUuidFile.exists()) { if (!nameToUuidFile.createNewFile()) { @@ -137,9 +134,7 @@ private void loadCache() { return; } - if (debug) { - ess.getLogger().log(Level.INFO, "Loading Name->UUID cache from disk..."); - } + DebugLogUtil.debugLog("Loading Name->UUID cache from disk..."); nameToUuidMap.clear(); @@ -148,8 +143,8 @@ private void loadCache() { final String username = dis.readUTF(); final UUID uuid = new UUID(dis.readLong(), dis.readLong()); final UUID previous = nameToUuidMap.put(username, uuid); - if (previous != null && debug) { - ess.getLogger().log(Level.WARNING, "Replaced UUID during cache load for " + username + ": " + previous + " -> " + uuid); + if (previous != null) { + DebugLogUtil.debugLog("Replaced UUID during cache load for " + username + ": " + previous + " -> " + uuid); } } } @@ -165,17 +160,15 @@ private void loadCache() { return; } - if (debug) { - ess.getLogger().log(Level.INFO, "Loading UUID cache from disk..."); - } + DebugLogUtil.debugLog("Loading UUID cache from disk..."); uuidCache.clear(); try (final DataInputStream dis = new DataInputStream(new FileInputStream(uuidCacheFile))) { while (dis.available() > 0) { final UUID uuid = new UUID(dis.readLong(), dis.readLong()); - if (uuidCache.contains(uuid) && debug) { - ess.getLogger().log(Level.WARNING, "UUID " + uuid + " duplicated in cache"); + if (uuidCache.contains(uuid)) { + DebugLogUtil.debugLog("UUID " + uuid + " duplicated in cache"); } uuidCache.add(uuid); } @@ -186,9 +179,7 @@ private void loadCache() { } private void saveUuidCache() { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Saving UUID cache to disk..."); - } + DebugLogUtil.debugLog("Saving UUID cache to disk..."); try { final File tmpMap = File.createTempFile("uuids", ".tmp.bin", ess.getDataFolder()); @@ -202,9 +193,7 @@ private void saveUuidCache() { } private void saveNameToUuidCache() { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Saving Name->UUID cache to disk..."); - } + DebugLogUtil.debugLog("Saving Name->UUID cache to disk..."); try { final File tmpMap = File.createTempFile("usermap", ".tmp.bin", ess.getDataFolder()); diff --git a/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java b/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java index 6c9351c0d06..8443f9aae3f 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java +++ b/Essentials/src/main/java/com/earth2me/essentials/userstorage/ModernUserMap.java @@ -2,6 +2,7 @@ import com.earth2me.essentials.OfflinePlayerStub; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.NumberUtil; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -76,9 +77,7 @@ public User getUser(final UUID uuid) { try { return userCache.get(uuid); } catch (ExecutionException e) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.WARNING, "Exception while getting user for " + uuid, e); - } + DebugLogUtil.debugLog("Exception while getting user for " + uuid, e); return null; } } @@ -139,9 +138,7 @@ public User loadUncachedUser(final Player base) { debugLogUncachedNonPlayer(base); user = new User(base, ess); } else if (!base.equals(user.getBase())) { - if (ess.getSettings().isDebug()) { - ess.getLogger().log(Level.INFO, "Essentials updated the underlying Player object for " + user.getUUID()); - } + DebugLogUtil.debugLog("Essentials updated the underlying Player object for " + user.getUUID()); user.update(base); } uuidCache.updateCache(user.getUUID(), user.getName()); diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/DebugLogUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/DebugLogUtil.java new file mode 100644 index 00000000000..af3d27bf1b7 --- /dev/null +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/DebugLogUtil.java @@ -0,0 +1,83 @@ +package com.earth2me.essentials.utils; + +import com.earth2me.essentials.Essentials; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.message.SimpleMessage; + +public abstract class DebugLogUtil { + private static Essentials essentials; + private static final DebugLogUtil INSTANCE; + + static { + if (Essentials.TESTING) { + INSTANCE = new DebugLogUtil() { + @Override + protected void debugLogInternal(String message, Throwable throwable) { + essentials.getLogger().log(java.util.logging.Level.INFO, message, throwable); + } + }; + } else { + INSTANCE = new DebugLogUtilImpl(); + } + } + + private static final class DebugLogUtilImpl extends DebugLogUtil { + private final Appender APPENDER; + + private DebugLogUtilImpl() { + final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + final Configuration cfg = ctx.getConfiguration(); + + // if this ever breaks, we can do cfg.getAppenders() and look for instanceof RollingRandomAccessFileAppender + APPENDER = cfg.getAppender("File"); + + if (APPENDER == null) { + System.out.println("Unable to find latest.log appender. Please report this to the EssX team!"); + } + } + + @Override + protected void debugLogInternal(String message, Throwable throwable) { + if (APPENDER == null && essentials == null) { + System.out.println(message); + return; + } + + if (APPENDER == null || essentials != null && essentials.getSettings().isDebug()) { + essentials.getLogger().log(java.util.logging.Level.INFO, message); + return; + } + + final Log4jLogEvent evt = Log4jLogEvent.newBuilder() + .setLoggerName("EssentialsDebug") + .setLevel(Level.INFO) + .setMessage(new SimpleMessage(message)) + .setThrown(throwable) + .build(); + APPENDER.append(evt); + } + } + + private DebugLogUtil() { + } + + public static void setEssentials(Essentials essentials) { + DebugLogUtil.essentials = essentials; + } + + public static void debugLog(final String message) { + debugLog(message, null); + } + + protected abstract void debugLogInternal(final String message, final Throwable throwable); + + public static void debugLog(final String message, final Throwable throwable) { + INSTANCE.debugLogInternal(message, throwable); + } + +} diff --git a/EssentialsAntiBuild/src/main/java/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java b/EssentialsAntiBuild/src/main/java/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java index 6949ded7dda..e0e47b571e2 100644 --- a/EssentialsAntiBuild/src/main/java/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java +++ b/EssentialsAntiBuild/src/main/java/com/earth2me/essentials/antibuild/EssentialsAntiBuildListener.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.antibuild; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.VersionUtil; import net.ess3.api.IEssentials; @@ -33,8 +34,6 @@ import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.inventory.ItemStack; -import java.util.logging.Level; - public class EssentialsAntiBuildListener implements Listener { final private transient IAntiBuild prot; final private transient IEssentials ess; @@ -61,9 +60,7 @@ private static boolean isEntityPickupEvent() { private boolean metaPermCheck(final User user, final String action, final Block block) { if (block == null) { - if (ess.getSettings().isDebug()) { - prot.getLogger().log(Level.INFO, "AntiBuild permission check failed, invalid block."); - } + DebugLogUtil.debugLog("AntiBuild permission check failed, invalid block."); return false; } if (VersionUtil.PRE_FLATTENING) { @@ -74,9 +71,7 @@ private boolean metaPermCheck(final User user, final String action, final Block private boolean metaPermCheck(final User user, final String action, final ItemStack item) { if (item == null) { - if (ess.getSettings().isDebug()) { - prot.getLogger().log(Level.INFO, "AntiBuild permission check failed, invalid item."); - } + DebugLogUtil.debugLog("AntiBuild permission check failed, invalid item."); return false; } if (VersionUtil.PRE_FLATTENING) { @@ -98,9 +93,7 @@ private boolean metaPermCheck(final User user, final String action, final Materi if (user.getBase().isPermissionSet(dataPerm)) { return user.isAuthorized(dataPerm); } else { - if (ess.getSettings().isDebug()) { - prot.getLogger().log(Level.INFO, "DataValue perm on " + user.getName() + " is not directly set: " + dataPerm); - } + DebugLogUtil.debugLog("DataValue perm on " + user.getName() + " is not directly set: " + dataPerm); } } diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java index 96a8300a9a0..176fe5872fd 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java @@ -5,6 +5,7 @@ import club.minnced.discord.webhook.send.WebhookMessageBuilder; import com.earth2me.essentials.IEssentialsModule; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.VersionUtil; @@ -108,14 +109,10 @@ public TextChannel getChannel(String key, boolean primaryFallback) { public TextChannel getDefinedChannel(String key, boolean primaryFallback) { final long resolvedId = getSettings().getChannelId(key); - if (isDebug()) { - logger.log(Level.INFO, "Channel definition " + key + " resolved as " + resolvedId); - } + DebugLogUtil.debugLog("Channel definition " + key + " resolved as " + resolvedId); TextChannel channel = guild.getTextChannelById(resolvedId); if (channel == null && primaryFallback) { - if (isDebug()) { - logger.log(Level.WARNING, "Resolved channel id " + resolvedId + " was not found! Falling back to primary channel."); - } + DebugLogUtil.debugLog("Resolved channel id " + resolvedId + " was not found! Falling back to primary channel."); channel = primaryChannel; } return channel; diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/interactions/InteractionControllerImpl.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/interactions/InteractionControllerImpl.java index 26581597214..b9462121fc9 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/interactions/InteractionControllerImpl.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/interactions/InteractionControllerImpl.java @@ -1,5 +1,6 @@ package net.essentialsx.discord.interactions; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.StringUtil; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.exceptions.ErrorResponseException; @@ -94,16 +95,12 @@ public void processBatchRegistration() { for (final Command command : success) { commandMap.put(command.getName(), batchRegistrationQueue.get(command.getName())); batchRegistrationQueue.remove(command.getName()); - if (jda.isDebug()) { - logger.info("Registered guild command " + command.getName() + " with id " + command.getId()); - } + DebugLogUtil.debugLog("Registered guild command " + command.getName() + " with id " + command.getId()); } if (!batchRegistrationQueue.isEmpty()) { logger.warning(batchRegistrationQueue.size() + " Discord commands were lost during command registration!"); - if (jda.isDebug()) { - logger.warning("Lost commands: " + batchRegistrationQueue.keySet()); - } + DebugLogUtil.debugLog("Lost commands: " + batchRegistrationQueue.keySet()); batchRegistrationQueue.clear(); } }, failure -> { @@ -127,9 +124,7 @@ public void registerCommand(InteractionCommand command) throws InteractionExcept } if (!initialBatchRegistration) { - if (jda.isDebug()) { - logger.info("Marked guild command for batch registration: " + command.getName()); - } + DebugLogUtil.debugLog("Marked guild command for batch registration: " + command.getName()); batchRegistrationQueue.put(command.getName(), command); return; } @@ -143,9 +138,7 @@ public void registerCommand(InteractionCommand command) throws InteractionExcept jda.getGuild().upsertCommand(data).queue(success -> { commandMap.put(command.getName(), command); - if (jda.isDebug()) { - logger.info("Registered guild command " + success.getName() + " with id " + success.getId()); - } + DebugLogUtil.debugLog("Registered guild command " + success.getName() + " with id " + success.getId()); }, failure -> { if (failure instanceof ErrorResponseException && ((ErrorResponseException) failure).getErrorResponse() == ErrorResponse.MISSING_ACCESS) { logger.severe(tlLiteral("discordErrorCommand")); diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordListener.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordListener.java index ea2b0ddb34b..188d4797143 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordListener.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordListener.java @@ -1,5 +1,6 @@ package net.essentialsx.discord.listeners; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.StringUtil; import com.vdurmont.emoji.EmojiParser; @@ -23,7 +24,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; @@ -53,9 +53,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { // Get list of channel names that have this channel id mapped final List keys = plugin.getPlugin().getSettings().getKeysFromChannelId(event.getChannel().getIdLong()); if (keys == null || keys.size() == 0) { - if (plugin.isDebug()) { - logger.log(Level.INFO, "Skipping message due to no channel keys for id " + event.getChannel().getIdLong() + "!"); - } + DebugLogUtil.debugLog("Skipping message due to no channel keys for id " + event.getChannel().getIdLong() + "!"); return; } @@ -67,9 +65,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { if (!plugin.getSettings().getDiscordFilters().isEmpty()) { for (final Pattern pattern : plugin.getSettings().getDiscordFilters()) { if (pattern.matcher(message.getContentDisplay()).find()) { - if (plugin.isDebug()) { - logger.log(Level.INFO, "Skipping message " + message.getId() + " with content, \"" + message.getContentDisplay() + "\" as it matched the filter!"); - } + DebugLogUtil.debugLog("Skipping message " + message.getId() + " with content, \"" + message.getContentDisplay() + "\" as it matched the filter!"); return; } } @@ -93,10 +89,8 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { FormatUtil.replaceFormat(strippedMessage) : FormatUtil.stripFormat(strippedMessage); // Don't send blank messages - if (finalMessage.trim().length() == 0) { - if (plugin.isDebug()) { - logger.log(Level.INFO, "Skipping finalized empty message " + message.getId()); - } + if (finalMessage.trim().isEmpty()) { + DebugLogUtil.debugLog("Skipping finalized empty message " + message.getId()); return; } diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/ConsoleInjector.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/ConsoleInjector.java index 52ea96c64a8..2c8f23e3b16 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/ConsoleInjector.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/util/ConsoleInjector.java @@ -1,5 +1,6 @@ package net.essentialsx.discord.util; +import com.earth2me.essentials.utils.DebugLogUtil; import com.earth2me.essentials.utils.FormatUtil; import com.google.common.base.Splitter; import net.dv8tion.jda.api.entities.Message; @@ -55,9 +56,7 @@ public ConsoleInjector(JDADiscordService jda) { } final int backoff = recentRateLimit.incrementAndGet(); - if (jda.isDebug()) { - logger.warning("EssXBackoff: Webhook backoff in progress, skipping queue processing. Resuming in " + Math.abs(backoff) + " cycles."); - } + DebugLogUtil.debugLog("EssXBackoff: Webhook backoff in progress, skipping queue processing. Resuming in " + Math.abs(backoff) + " cycles."); return; } @@ -115,10 +114,8 @@ public void append(LogEvent event) { } } else if (recentRateLimit.get() >= 2) { // Start the webhook backoff, defaulting to 20s, which should reset our bucket. - if (jda.isDebug()) { - totalBackoffEvents.getAndIncrement(); - logger.warning("EssXBackoff: Beginning Webhook Backoff"); - } + totalBackoffEvents.getAndIncrement(); + DebugLogUtil.debugLog("EssXBackoff: Beginning Webhook Backoff"); recentRateLimit.set(-20); } return; diff --git a/EssentialsDiscordLink/src/main/java/net/essentialsx/discordlink/AccountStorage.java b/EssentialsDiscordLink/src/main/java/net/essentialsx/discordlink/AccountStorage.java index f7d95b582be..c43840c7bc0 100644 --- a/EssentialsDiscordLink/src/main/java/net/essentialsx/discordlink/AccountStorage.java +++ b/EssentialsDiscordLink/src/main/java/net/essentialsx/discordlink/AccountStorage.java @@ -1,5 +1,6 @@ package net.essentialsx.discordlink; +import com.earth2me.essentials.utils.DebugLogUtil; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.common.collect.Maps; @@ -48,9 +49,7 @@ public AccountStorage(final EssentialsDiscordLink plugin) throws IOException { return; } - if (plugin.getEss().getSettings().isDebug()) { - plugin.getLogger().log(Level.INFO, "Saving linked discord accounts to disk..."); - } + DebugLogUtil.debugLog("Saving linked discord accounts to disk..."); final Map clone; clone = new HashMap<>(uuidToDiscordIdMap);