From 4ceb54b56682282b99d00846322644a5ab4fc512 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sun, 22 May 2016 00:45:48 +1000 Subject: [PATCH] Add logger --- .../com/plotsquared/bukkit/BukkitMain.java | 12 ++++----- .../configuration/MemorySection.java | 2 +- .../intellectualcrafters/plot/IPlotMain.java | 3 ++- .../com/intellectualcrafters/plot/PS.java | 27 +++++++++++++++++-- .../plot/logger/DelegateLogger.java | 22 +++++++++++++++ .../plot/logger/ILogger.java | 5 ++++ .../plot/object/PlotArea.java | 2 +- .../com/plotsquared/sponge/SpongeMain.java | 16 +++++------ 8 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 Core/src/main/java/com/intellectualcrafters/plot/logger/DelegateLogger.java create mode 100644 Core/src/main/java/com/intellectualcrafters/plot/logger/ILogger.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 630a91534..5ebdef3c4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -185,7 +185,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain @Override public void runEntityTask() { - log(C.PREFIX + "KillAllEntities started."); + PS.log(C.PREFIX + "KillAllEntities started."); TaskManager.runTaskRepeat(new Runnable() { @Override public void run() { @@ -514,7 +514,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain Settings.OFFLINE_MODE = true; } if (!checkVersion) { - log(C.PREFIX + " &c[WARN] Titles are disabled - please update your version of Bukkit to support this feature."); + PS.log(C.PREFIX + " &c[WARN] Titles are disabled - please update your version of Bukkit to support this feature."); Settings.TITLES = false; } else { AbstractTitle.TITLE_CLASS = new DefaultTitle_19(); @@ -523,11 +523,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain } } if (Settings.OFFLINE_MODE) { - log(C.PREFIX + PS.log(C.PREFIX + " &6PlotSquared is using Offline Mode UUIDs either because of user preference, or because you are using an old version of " + "Bukkit"); } else { - log(C.PREFIX + " &6PlotSquared is using online UUIDs"); + PS.log(C.PREFIX + " &6PlotSquared is using online UUIDs"); } if (Settings.USE_SQLUUIDHANDLER) { return new SQLUUIDHandler(wrapper); @@ -575,7 +575,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain public void startMetrics() { Metrics metrics = new Metrics(this); metrics.start(); - log(C.PREFIX + "&6Metrics enabled."); + PS.log(C.PREFIX + "&6Metrics enabled."); } @Override @@ -599,7 +599,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain SetGenCB.setGenerator(BukkitUtil.getWorld(worldName)); } } catch (Exception ignored) { - log("Failed to reload world: " + world); + PS.log("Failed to reload world: " + world); Bukkit.getServer().unloadWorld(world, false); } } diff --git a/Core/src/main/java/com/intellectualcrafters/configuration/MemorySection.java b/Core/src/main/java/com/intellectualcrafters/configuration/MemorySection.java index 70db80141..d2d8b7798 100644 --- a/Core/src/main/java/com/intellectualcrafters/configuration/MemorySection.java +++ b/Core/src/main/java/com/intellectualcrafters/configuration/MemorySection.java @@ -539,7 +539,7 @@ public class MemorySection implements ConfigurationSection { List list = getList(path); if (list == null) { - PS.get().IMP.log(path + " is null"); + PS.get().log(path + " is null"); return new ArrayList<>(0); } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/IPlotMain.java b/Core/src/main/java/com/intellectualcrafters/plot/IPlotMain.java index ad44946a3..68901d2f1 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/IPlotMain.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/IPlotMain.java @@ -3,6 +3,7 @@ package com.intellectualcrafters.plot; import com.intellectualcrafters.plot.generator.GeneratorWrapper; import com.intellectualcrafters.plot.generator.HybridUtils; import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; +import com.intellectualcrafters.plot.logger.ILogger; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.AbstractTitle; import com.intellectualcrafters.plot.util.ChatManager; @@ -20,7 +21,7 @@ import com.intellectualcrafters.plot.util.WorldUtil; import java.io.File; import java.util.List; -public interface IPlotMain { +public interface IPlotMain extends ILogger { /** * Log a message to console. diff --git a/Core/src/main/java/com/intellectualcrafters/plot/PS.java b/Core/src/main/java/com/intellectualcrafters/plot/PS.java index 8239e578e..f80ed4429 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/PS.java @@ -16,6 +16,7 @@ import com.intellectualcrafters.plot.generator.GeneratorWrapper; import com.intellectualcrafters.plot.generator.HybridPlotWorld; import com.intellectualcrafters.plot.generator.HybridUtils; import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; +import com.intellectualcrafters.plot.logger.ILogger; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotAnalysis; @@ -48,7 +49,6 @@ import com.intellectualcrafters.plot.util.WorldUtil; import com.intellectualcrafters.plot.util.area.QuadMap; import com.plotsquared.listener.WESubscriber; import com.sk89q.worldedit.WorldEdit; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -86,6 +86,7 @@ public class PS { private static PS instance; public final IPlotMain IMP; + public ILogger LOGGER; private final HashSet plotAreaHashCheck = new HashSet<>(); /** * All plot areas mapped by world (quick world access). @@ -130,6 +131,7 @@ public class PS { PS.instance = this; this.thread = Thread.currentThread(); this.IMP = iPlotMain; + this.LOGGER = iPlotMain; this.platform = platform; this.version = this.IMP.getPluginVersion(); try { @@ -295,6 +297,27 @@ public class PS { return PS.instance; } + /** + * Set the logger + * @see com.intellectualcrafters.plot.logger.DelegateLogger + * @see #getLogger() + * @param logger + */ + public void setLogger(ILogger logger) { + if (logger == null) { + throw new IllegalArgumentException("Logger may not be null"); + } + LOGGER = logger; + } + + /** + * Get the current logger + * @return + */ + public ILogger getLogger() { + return LOGGER; + } + /** * Log a message to the IPlotMain logger. * @@ -302,7 +325,7 @@ public class PS { * @see IPlotMain#log(String) */ public static void log(Object message) { - PS.get().IMP.log(StringMan.getString(message)); + PS.get().LOGGER.log(StringMan.getString(message)); } /** diff --git a/Core/src/main/java/com/intellectualcrafters/plot/logger/DelegateLogger.java b/Core/src/main/java/com/intellectualcrafters/plot/logger/DelegateLogger.java new file mode 100644 index 000000000..481e21f63 --- /dev/null +++ b/Core/src/main/java/com/intellectualcrafters/plot/logger/DelegateLogger.java @@ -0,0 +1,22 @@ +package com.intellectualcrafters.plot.logger; + +public class DelegateLogger implements ILogger { + + private final ILogger parent; + + public DelegateLogger(ILogger parent) { + if (parent == null) { + throw new IllegalArgumentException("Parent cannot be null"); + } + this.parent = parent; + } + + public ILogger getParent() { + return parent; + } + + @Override + public void log(String message) { + parent.log(message); + } +} diff --git a/Core/src/main/java/com/intellectualcrafters/plot/logger/ILogger.java b/Core/src/main/java/com/intellectualcrafters/plot/logger/ILogger.java new file mode 100644 index 000000000..983fd544c --- /dev/null +++ b/Core/src/main/java/com/intellectualcrafters/plot/logger/ILogger.java @@ -0,0 +1,5 @@ +package com.intellectualcrafters.plot.logger; + +public interface ILogger { + public void log(String message); +} diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java index 128576106..23301ae74 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java @@ -344,7 +344,7 @@ public abstract class PlotArea { } for (Entry stringObjectEntry : options.entrySet()) { if (!config.contains(stringObjectEntry.getKey())) { - PS.get().IMP.log(stringObjectEntry.toString()); + PS.get().log(stringObjectEntry.toString()); config.set(stringObjectEntry.getKey(), stringObjectEntry.getValue()); } } diff --git a/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java b/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java index 8f1a86b1a..9721c3d5e 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/SpongeMain.java @@ -122,17 +122,17 @@ public class SpongeMain implements IPlotMain { @Listener public void init(GameInitializationEvent event) { - log("PlotSquared: Game init"); + PS.log("PlotSquared: Game init"); } @Listener public void onInit(GamePreInitializationEvent event) { - log("PlotSquared: Game pre init"); + PS.log("PlotSquared: Game pre init"); } @Listener public void onServerAboutToStart(GameAboutToStartServerEvent event) { - log("PlotSquared: Server init"); + PS.log("PlotSquared: Server init"); THIS = this; new PS(this, "Sponge"); this.server = this.game.getServer(); @@ -177,7 +177,7 @@ public class SpongeMain implements IPlotMain { @Override public int[] getServerVersion() { - log("Checking minecraft version: Sponge: "); + PS.log("Checking minecraft version: Sponge: "); String version = this.game.getPlatform().getMinecraftVersion().getName(); String[] split = version.split("\\."); return new int[]{Integer.parseInt(split[0]), Integer.parseInt(split[1]), split.length == 3 ? Integer.parseInt(split[2]) : 0}; @@ -248,13 +248,13 @@ public class SpongeMain implements IPlotMain { @Override public void registerPlotPlusEvents() { // TODO Auto-generated method stub - log("registerPlotPlusEvents is not implemented!"); + PS.log("registerPlotPlusEvents is not implemented!"); } @Override public void registerForceFieldEvents() { // TODO Auto-generated method stub - log("registerForceFieldEvents is not implemented!"); + PS.log("registerForceFieldEvents is not implemented!"); } @Override @@ -308,9 +308,9 @@ public class SpongeMain implements IPlotMain { try { SpongeMetrics metrics = new SpongeMetrics(this.game, this.plugin); metrics.start(); - log(C.PREFIX.s() + "&6Metrics enabled."); + PS.log(C.PREFIX.s() + "&6Metrics enabled."); } catch (IOException ignored) { - log(C.PREFIX.s() + "&cFailed to load up metrics."); + PS.log(C.PREFIX.s() + "&cFailed to load up metrics."); } }