Skip to content

Commit

Permalink
Changed settings handler to never be null, similar to the other handles
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Sep 15, 2023
1 parent bc437eb commit 784634e
Show file tree
Hide file tree
Showing 18 changed files with 619 additions and 324 deletions.
Expand Up @@ -98,8 +98,8 @@
public class SuperiorSkyblockPlugin extends JavaPlugin implements SuperiorSkyblock {

private static SuperiorSkyblockPlugin plugin;
private final Updater updater = new Updater(this, "superiorskyblock2");

/* Managers */
private final DataManager dataHandler = new DataManager(this);
private final FactoriesManagerImpl factoriesHandler = new FactoriesManagerImpl();
private final GridManagerImpl gridHandler = new GridManagerImpl(this,
Expand All @@ -126,15 +126,17 @@ public class SuperiorSkyblockPlugin extends JavaPlugin implements SuperiorSkyblo
private final ModulesManagerImpl modulesHandler = new ModulesManagerImpl(this,
new DefaultModulesContainer(this));
private final ServicesHandler servicesHandler = new ServicesHandler(this);
// The only handler that is initialized is this one, therefore it's not final.
// This is to prevent it's fields to be non-finals.
private SettingsManagerImpl settingsHandler = null;
private IScriptEngine scriptEngine = EnginesFactory.createDefaultEngine();
private final SettingsManagerImpl settingsHandler = new SettingsManagerImpl(this);

/* Global handlers */
private final Updater updater = new Updater(this, "superiorskyblock2");
private final EventsBus eventsBus = new EventsBus(this);

private final BukkitListeners bukkitListeners = new BukkitListeners(this);
private IScriptEngine scriptEngine = EnginesFactory.createDefaultEngine();
@Nullable
private ChunkGenerator worldGenerator = null;

/* NMS */
private String nmsPackageVersion;
private NMSAlgorithms nmsAlgorithms;
private NMSChunks nmsChunks;
Expand All @@ -145,18 +147,8 @@ public class SuperiorSkyblockPlugin extends JavaPlugin implements SuperiorSkyblo
private NMSTags nmsTags;
private NMSWorld nmsWorld;

private ChunkGenerator worldGenerator = null;

private boolean shouldEnable = true;

// public static void log(String message) {
// message = Formatters.COLOR_FORMATTER.format(message);
// if (message.contains(ChatColor.COLOR_CHAR + ""))
// Bukkit.getConsoleSender().sendMessage(ChatColor.getLastColors(message.substring(0, 2)) + "[" + plugin.getDescription().getName() + "] " + message);
// else
// plugin.getLogger().info(message);
// }

public static SuperiorSkyblockPlugin getPlugin() {
return plugin;
}
Expand Down Expand Up @@ -223,7 +215,7 @@ public void onEnable() {
GlowEnchantment.registerGlowEnchantment();

try {
settingsHandler = new SettingsManagerImpl(this);
settingsHandler.loadData();
} catch (ManagerLoadException ex) {
if (!ManagerLoadException.handle(ex)) {
shouldEnable = false;
Expand Down Expand Up @@ -519,7 +511,7 @@ public void reloadPlugin(boolean loadGrid) throws ManagerLoadException {

if (!loadGrid) {
modulesHandler.reloadModules(ModuleLoadTime.BEFORE_WORLD_CREATION);
settingsHandler = new SettingsManagerImpl(this);
settingsHandler.loadData();
modulesHandler.reloadModules(ModuleLoadTime.NORMAL);
} else {
commandsHandler.loadData();
Expand Down Expand Up @@ -686,10 +678,6 @@ public ServicesHandler getServices() {
return servicesHandler;
}

public void setSettings(SettingsManagerImpl settingsHandler) {
this.settingsHandler = settingsHandler;
}

public NMSAlgorithms getNMSAlgorithms() {
return nmsAlgorithms;
}
Expand Down
Expand Up @@ -379,7 +379,7 @@ public SettingsContainer(SuperiorSkyblockPlugin plugin, YamlConfiguration config
World.Environment environment = World.Environment.valueOf(env.toUpperCase(Locale.ENGLISH));
loadGenerator(config, "default-values.generator." + env, environment.ordinal());
} catch (Exception error) {
Log.error(error, "An unexpected error occurred while loading default generator values for ", env + ":");
Log.errorFromFile(error, "config.yml", "An unexpected error occurred while loading default generator values for ", env + ":");
}
}
} else {
Expand Down Expand Up @@ -430,11 +430,11 @@ public SettingsContainer(SuperiorSkyblockPlugin plugin, YamlConfiguration config

items.addTag(itemCompound);
} catch (Exception error) {
Log.error(error, "An unexpected error occurred while loading container item for ", slot + ":");
Log.errorFromFile(error, "config.yml", "An unexpected error occurred while loading container item for ", slot + ":");
}
}
} catch (IllegalArgumentException ex) {
Log.warn("Invalid container type ", container + ", skipping...");
Log.warnFromFile("config.yml", "Invalid container type ", container + ", skipping...");
}
}
}
Expand Down Expand Up @@ -490,7 +490,7 @@ public SettingsContainer(SuperiorSkyblockPlugin plugin, YamlConfiguration config
islandPreviewLocations.put(schematic.toLowerCase(Locale.ENGLISH), Serializers.LOCATION_SERIALIZER
.deserialize(config.getString("preview-islands." + schematic)));
} catch (Exception error) {
Log.warn("Cannot deserialize island preview for ", schematic, ", skipping...");
Log.warnFromFile("config.yml", "Cannot deserialize island preview for ", schematic, ", skipping...");
}
}
}
Expand All @@ -514,7 +514,7 @@ public SettingsContainer(SuperiorSkyblockPlugin plugin, YamlConfiguration config
try {
playerRespawnActions.add(RespawnAction.getByName(respawnAction));
} catch (NullPointerException error) {
Log.warn("Invalid respawn action ", respawnAction + ", skipping...");
Log.warnFromFile("config.yml", "Invalid respawn action ", respawnAction + ", skipping...");
}
});
blockCountsSaveThreshold = BigInteger.valueOf(config.getInt("block-counts-save-threshold", 100));
Expand Down Expand Up @@ -553,7 +553,7 @@ private KeySet loadSafeBlocks(SuperiorSkyblockPlugin plugin) {
cfg.set("safe-blocks", safeBlocks);
cfg.save(file);
} catch (IOException error) {
Log.error(error, "An unexpected error occurred while saving safe blocks into file:");
Log.errorFromFile(error, "config.yml", "An unexpected error occurred while saving safe blocks into file:");
}
}

Expand Down
@@ -0,0 +1,21 @@
package com.bgsoftware.superiorskyblock.config;

public abstract class SettingsContainerHolder {

private SettingsContainer container;

protected SettingsContainerHolder() {

}

public void setContainer(SettingsContainer container) {
this.container = container;
}

protected SettingsContainer getContainer() {
if (this.container == null)
throw new IllegalStateException("Cannot access SettingsManager before initialization");
return this.container;
}

}

0 comments on commit 784634e

Please sign in to comment.