Skip to content

Commit

Permalink
Remove standard logger usage and switch to ExtendedFancyLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Oct 5, 2024
1 parent d810131 commit 14adc08
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
import java.util.logging.Logger;

public interface FancyNpcsPlugin {

Expand All @@ -27,8 +26,6 @@ static FancyNpcsPlugin get() {

JavaPlugin getPlugin();

Logger getLogger();

ExtendedFancyLogger getFancyLogger();

ScheduledExecutorService getNpcThread();
Expand Down
12 changes: 6 additions & 6 deletions api/src/main/java/de/oliver/fancynpcs/api/utils/SkinFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public static CompletableFuture<SkinData> fetchSkinByUUID(String uuid) {
FancyNpcsPlugin.get().getSkinCache().upsert(new SkinCacheData(skinData, System.currentTimeMillis(), 1000L * 60 * 60 * 24 + randomFromTo(15, 30))); // 15-30 days
return skinData;
} catch (IOException e) {
FancyNpcsPlugin.get().getPlugin().getLogger().warning("Failed to fetch skin data for UUID " + uuid);
FancyNpcsPlugin.get().getPlugin().getLogger().warning(e.getMessage());
FancyNpcsPlugin.get().getFancyLogger().warn("Failed to fetch skin data for UUID " + uuid);
FancyNpcsPlugin.get().getFancyLogger().warn(e.getMessage());
return null;
}
});
Expand Down Expand Up @@ -183,8 +183,8 @@ public static CompletableFuture<SkinData> fetchSkinByURL(String skinURL) {
skinCache.put(skinURL, skinData);
return skinData;
} catch (IOException e) {
FancyNpcsPlugin.get().getPlugin().getLogger().warning("Failed to fetch skin data for URL " + skinURL);
FancyNpcsPlugin.get().getPlugin().getLogger().warning(e.getMessage());
FancyNpcsPlugin.get().getFancyLogger().warn("Failed to fetch skin data for URL " + skinURL);
FancyNpcsPlugin.get().getFancyLogger().warn(e.getMessage());
return null;
}
});
Expand Down Expand Up @@ -227,7 +227,7 @@ public String value() {
SkinData skinData = fetchSkin(identifier).join();
return skinData == null ? null : skinData.value();
} catch (Exception e) {
FancyNpcsPlugin.get().getPlugin().getLogger().warning("Failed to fetch skin data for " + identifier);
FancyNpcsPlugin.get().getFancyLogger().warn("Failed to fetch skin data for " + identifier);
}
}

Expand All @@ -246,7 +246,7 @@ public String signature() {
SkinData skinData = fetchSkin(identifier).join();
return skinData == null ? null : skinData.signature();
} catch (Exception e) {
FancyNpcsPlugin.get().getPlugin().getLogger().warning("Failed to fetch skin data for " + identifier);
FancyNpcsPlugin.get().getFancyLogger().warn("Failed to fetch skin data for " + identifier);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public FancyNpcs() {

Appender consoleAppender = new ConsoleAppender("[{loggerName}] ({threadName}) {logLevel}: {message}");
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
File logsFile = new File("plugins/FancyNpcs/logs/FN-logs-" + date + ".json");
File logsFile = new File("plugins/FancyNpcs/logs/FN-logs-" + date + ".txt");
if (!logsFile.exists()) {
try {
logsFile.getParentFile().mkdirs();
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.oliver.fancynpcs;

import de.oliver.fancyanalytics.logger.ExtendedFancyLogger;
import de.oliver.fancynpcs.api.*;
import de.oliver.fancynpcs.api.actions.ActionTrigger;
import de.oliver.fancynpcs.api.actions.NpcAction;
Expand Down Expand Up @@ -28,13 +29,15 @@
public class NpcManagerImpl implements NpcManager {

private final JavaPlugin plugin;
private final ExtendedFancyLogger logger;
private final Function<NpcData, Npc> npcAdapter;
private final File npcConfigFile;
private final Map<String, Npc> npcs; // npc id -> npc
private boolean isLoaded;

public NpcManagerImpl(JavaPlugin plugin, Function<NpcData, Npc> npcAdapter) {
this.plugin = plugin;
this.logger = FancyNpcs.getInstance().getFancyLogger();
this.npcAdapter = npcAdapter;
npcs = new ConcurrentHashMap<>();
npcConfigFile = new File("plugins" + File.separator + "FancyNpcs" + File.separator + "npcs.yml");
Expand All @@ -57,7 +60,8 @@ public void removeNpc(Npc npc) {
try {
npcConfig.save(npcConfigFile);
} catch (IOException e) {
e.printStackTrace();
logger.error("Could not save npc config file");
logger.error(e);
}
}

Expand Down Expand Up @@ -119,7 +123,8 @@ public void saveNpcs(boolean force) {
try {
npcConfigFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
logger.error("Could not create npc config file");
logger.error(e);
return;
}
}
Expand Down Expand Up @@ -232,19 +237,20 @@ public void loadNpcs() {
try {
location = npcConfig.getLocation("npcs." + id + ".location");
} catch (Exception ignored) {
logger.warn("Could not load location for npc '" + id + "'");
}

if (location == null) {
String worldName = npcConfig.getString("npcs." + id + ".location.world");
World world = Bukkit.getWorld(worldName);

if (world == null) {
plugin.getLogger().info("Trying to load the world: '" + worldName + "'");
logger.info("Trying to load the world: '" + worldName + "'");
world = new WorldCreator(worldName).createWorld();
}

if (world == null) {
plugin.getLogger().info("Could not load npc '" + id + "', because the world '" + worldName + "' is not loaded");
logger.info("Could not load npc '" + id + "', because the world '" + worldName + "' is not loaded");
continue;
}

Expand Down Expand Up @@ -310,7 +316,7 @@ public void loadNpcs() {
if (sendMessagesRandomly && !messages.isEmpty()) {
migrateActionList.add(new NpcAction.NpcActionData(++actionOrder, FancyNpcs.getInstance().getActionManager().getActionByName("execute_random_action"), ""));
}

for (String message : messages) {
migrateActionList.add(new NpcAction.NpcActionData(++actionOrder, FancyNpcs.getInstance().getActionManager().getActionByName("message"), message));
}
Expand All @@ -325,7 +331,7 @@ public void loadNpcs() {
actiontriggerSection.getKeys(false).forEach(trigger -> {
ActionTrigger actionTrigger = ActionTrigger.getByName(trigger);
if (actionTrigger == null) {
System.out.println("Could not find action trigger: " + trigger);
logger.warn("Could not find action trigger: " + trigger);
return;
}

Expand All @@ -337,14 +343,14 @@ public void loadNpcs() {
String value = npcConfig.getString("npcs." + id + ".actions." + trigger + "." + order + ".value");
NpcAction action = FancyNpcs.getInstance().getActionManager().getActionByName(actionName);
if (action == null) {
System.out.println("Could not find action: " + actionName);
logger.warn("Could not find action: " + actionName);
return;
}

try {
actionList.add(new NpcAction.NpcActionData(Integer.parseInt(order), action, value));
} catch (NumberFormatException e) {
System.out.println("Could not parse order: " + order);
logger.warn("Could not parse order: " + order);
}
});

Expand All @@ -363,11 +369,13 @@ public void loadNpcs() {
for (String attrName : npcConfig.getConfigurationSection("npcs." + id + ".attributes").getKeys(false)) {
NpcAttribute attribute = FancyNpcs.getInstance().getAttributeManager().getAttributeByName(type, attrName);
if (attribute == null) {
logger.warn("Could not find attribute: " + attrName);
continue;
}

String value = npcConfig.getString("npcs." + id + ".attributes." + attrName);
if (!attribute.isValidValue(value)) {
logger.warn("Invalid value for attribute: " + attrName);
continue;
}

Expand Down Expand Up @@ -443,13 +451,13 @@ private void takeBackup(YamlConfiguration npcConfig) {
try {
backupFile.createNewFile();
} catch (IOException e) {
FancyNpcs.getInstance().getLogger().severe("Could not create backup file for NPCs");
logger.error("Could not create backup file for NPCs");
}

try {
npcConfig.save(backupFile);
} catch (IOException e) {
FancyNpcs.getInstance().getLogger().severe("Could not save backup file for NPCs");
logger.error("Could not save backup file for NPCs");
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/oliver/fancynpcs/utils/SkinCacheYaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void upsert(SkinFetcher.SkinCacheData skinCacheData, boolean onlyIfExists
try {
yaml.save(file);
} catch (Exception e) {
FancyNpcs.getInstance().getLogger().warning("Failed to save skin cache");
FancyNpcs.getInstance().getFancyLogger().error("Failed to save skin cache");
}
}

Expand Down

0 comments on commit 14adc08

Please sign in to comment.