Skip to content

Commit

Permalink
Refactor ConfigurationManager and Main class for better logging
Browse files Browse the repository at this point in the history
The refactoring includes better directory handling and error logging in ConfigurationManager. Also, the Main class has been refactored to initialize the ConfigurationManager in a more efficient way. The console will now display informative messages upon successful or unsuccessful creation of configuration directories.
  • Loading branch information
SmartGecko44 committed Dec 3, 2023
1 parent 8cb8051 commit 1e932f2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 44 deletions.
78 changes: 40 additions & 38 deletions src/main/java/org/gecko/wauh/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,46 @@ public final class Main extends JavaPlugin {
private TNTListener tntListener;
private CreeperListener creeperListener;
private ScaleReverse scaleReverse;
private final Main plugin = Main.getPlugin(Main.class);
private ConfigurationManager configManager = new ConfigurationManager(plugin);
private FileConfiguration config = configManager.getConfig();
private ConfigurationManager configManager;
private FileConfiguration config;

@Override
public void onEnable() {
// Plugin startup logic
Bukkit.getConsoleSender().sendMessage("");
Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "Yay");

// Create instances of the listeners
bucketListener = new BucketListener();
barrierListener = new BarrierListener();
bedrockListener = new BedrockListener();
waterBucketListener = new WaterBucketListener();
tntListener = new TNTListener();
creeperListener = new CreeperListener();
configManager = new ConfigurationManager(Main.getPlugin(Main.class));
config = configManager.getConfig();

// Register the listeners
getServer().getPluginManager().registerEvents(bucketListener, this);
getServer().getPluginManager().registerEvents(barrierListener, this);
getServer().getPluginManager().registerEvents(bedrockListener, this);
getServer().getPluginManager().registerEvents(waterBucketListener, this);
getServer().getPluginManager().registerEvents(tntListener, this);
getServer().getPluginManager().registerEvents(creeperListener, this);

// Register the StopWauh command with the listeners as arguments
this.getCommand("stopwauh").setExecutor(new StopWauh(bucketListener, barrierListener, bedrockListener, waterBucketListener));
this.getCommand("setradiuslimit").setExecutor(new SetRadiusLimitCommand(this));
this.getCommand("setradiuslimit").setTabCompleter(new SetRadiusLimitCommand(this));
this.getCommand("toggleremovalview").setExecutor(new ToggleRemovalView(this));
}

@Override
public void onDisable() {
// Plugin shutdown logic
Bukkit.getConsoleSender().sendMessage("");
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "kys");
}

public int getRadiusLimit() {
return playerRadiusLimit + 2;
Expand Down Expand Up @@ -86,39 +123,4 @@ public ScaleReverse getScaleReverse() {
return scaleReverse;
}

@Override
public void onEnable() {
// Plugin startup logic
Bukkit.getConsoleSender().sendMessage("");
Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "Yay");

// Create instances of the listeners
bucketListener = new BucketListener();
barrierListener = new BarrierListener();
bedrockListener = new BedrockListener();
waterBucketListener = new WaterBucketListener();
tntListener = new TNTListener();
creeperListener = new CreeperListener();

// Register the listeners
getServer().getPluginManager().registerEvents(bucketListener, this);
getServer().getPluginManager().registerEvents(barrierListener, this);
getServer().getPluginManager().registerEvents(bedrockListener, this);
getServer().getPluginManager().registerEvents(waterBucketListener, this);
getServer().getPluginManager().registerEvents(tntListener, this);
getServer().getPluginManager().registerEvents(creeperListener, this);

// Register the StopWauh command with the listeners as arguments
this.getCommand("stopwauh").setExecutor(new StopWauh(bucketListener, barrierListener, bedrockListener, waterBucketListener));
this.getCommand("setradiuslimit").setExecutor(new SetRadiusLimitCommand(this));
this.getCommand("setradiuslimit").setTabCompleter(new SetRadiusLimitCommand(this));
this.getCommand("toggleremovalview").setExecutor(new ToggleRemovalView(this));
}

@Override
public void onDisable() {
// Plugin shutdown logic
Bukkit.getConsoleSender().sendMessage("");
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "kys");
}
}
21 changes: 15 additions & 6 deletions src/main/java/org/gecko/wauh/data/ConfigurationManager.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package org.gecko.wauh.data;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.FileConfiguration;
import org.gecko.wauh.Main;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConfigurationManager {
private final Main plugin;
private final File configFile;
private FileConfiguration config;

private final FileConfiguration config;
private final Logger logger = Logger.getLogger(Main.class.getName());
public ConfigurationManager(Main plugin) {
this.plugin = plugin;
this.configFile = new File(plugin.getDataFolder(), "data.yml");

// Create the data.yml file if it doesn't exist
if (!configFile.exists()) {
plugin.saveResource("data.yml", false);
File dir = new File("Wauh");
boolean dirCreated = dir.mkdirs();
if (!dirCreated) {
logger.log(Level.SEVERE, "Config folder could not be created");
} else {
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Config folder created!");
}
plugin.saveResource("Wauh/data.yml", false);
}

// Load the config
Expand All @@ -33,7 +42,7 @@ public void saveConfig() {
try {
config.save(configFile);
} catch (IOException e) {
e.printStackTrace();
logger.log(Level.SEVERE, "Unable to save config", e);
}
}

Expand Down

0 comments on commit 1e932f2

Please sign in to comment.