diff --git a/src/main/java/de/steev/bm/BetterMinecraft.java b/src/main/java/de/steev/bm/BetterMinecraft.java index 7a92030..c84d3c3 100644 --- a/src/main/java/de/steev/bm/BetterMinecraft.java +++ b/src/main/java/de/steev/bm/BetterMinecraft.java @@ -5,89 +5,81 @@ import de.steev.bm.Listener.BedListener; import de.steev.bm.Listener.InteractListener; import de.steev.bm.Listener.KillListener; import de.steev.bm.Manager.GameManager; - +import de.steev.bm.utils.Config; +import de.steev.bm.utils.exceptions.ConfigEntryExceptions; import org.bukkit.ChatColor; import org.bukkit.World; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.java.JavaPlugin; -import java.io.File; import java.io.IOException; +import java.util.Objects; import java.util.UUID; +import static de.steev.bm.utils.Constants.*; public class BetterMinecraft extends JavaPlugin implements Listener { // Global Variables that might come helpfull later public int playerInBed = 0; public World world; - String version = "1.2"; // Custom Playerdata File - private File playerdata; - private FileConfiguration playerDataConfig; - private final String playerdatafilename = "playerdata.yml"; - private static String prefix = ChatColor.GRAY + "[" + ChatColor.AQUA + "BetterMinecraft" + ChatColor.GRAY + "]"; - + private Config playerData = new Config(playerDataFilename, this); private GameManager gameManager; // Handles initialisation public void onEnable() { // Messaging - this.getLogger().info(prefix + ChatColor.WHITE + "Plugin Startet"); + this.getLogger().info(prefix + ChatColor.WHITE + "Plugin Startup"); this.getLogger().info(prefix + ChatColor.WHITE + "Initializing Gamemanager"); gameManager = new GameManager(this); // Registers Events - this.getLogger().info(prefix + ChatColor.WHITE + "Lade Events"); + this.getLogger().info(prefix + ChatColor.WHITE + "Load Events"); this.getServer().getPluginManager().registerEvents(new BedListener(this.gameManager), this); this.getServer().getPluginManager().registerEvents(new KillListener(this.gameManager), this); this.getServer().getPluginManager().registerEvents(new InteractListener(this.gameManager), this); this.getServer().getPluginManager().registerEvents(this, this); - this.getCommand("back").setExecutor(new back(this.gameManager)); + Objects.requireNonNull(this.getCommand("back")).setExecutor(new back(this.gameManager)); // PlayerData - playerdata = new File(this.getDataFolder(), playerdatafilename); - playerDataConfig = YamlConfiguration.loadConfiguration(playerdata); - saveplayerdata(); + try { + this.playerData.saveConfig(playerDataFilename); + } catch (IOException e) { + throw new RuntimeException(e); + } - // More Messaging - this.getLogger().info(ChatColor.AQUA + "Ich bin Fertig :D"); + // More Messaging + this.getLogger().info(ChatColor.AQUA + "Startup complete :D"); } @EventHandler void onPlayerJoin(PlayerJoinEvent e) { Player p = (Player) e.getPlayer(); UUID uuid = p.getUniqueId(); - // prevent this allways resetting the job count - if (this.playerDataConfig.get(uuid + "." + version + ".read") == null) { - p.sendMessage("================= Better Minecraft " + version + " ================="); - p.sendMessage("Command: /back eingefügt"); - p.sendMessage("====================================================="); - this.playerDataConfig.set(uuid + ".name", p.getName()); - this.playerDataConfig.set(uuid + "." + version + ".read", "true"); - saveplayerdata(); - } - } - - void saveplayerdata() { try { - playerDataConfig.save(playerdata); - } catch (IOException e) { - this.getLogger().warning("Unable to save " + playerdatafilename); // shouldn't really happen, but save - // throws the exception + // prevent this allways resetting the job count + if (this.playerData.readBooleanFromConfig(uuid + "." + Version + ".read")) { + p.sendMessage("================= Better Minecraft " + Version + " ================="); + p.sendMessage("Command: /back eingefügt"); + p.sendMessage("====================================================="); + + this.playerData.writeStringToConfig(uuid + ".name", p.getName()); + this.playerData.writeStringToConfig(uuid + "." + Version + ".read", "true"); + this.playerData.saveConfig(playerDataFilename); + } + } catch (ConfigEntryExceptions cee) { + this.getLogger().warning("reading config failed with error: " + cee.getMessage()); + } catch (IOException ioe) { + throw new RuntimeException(ioe); } } - public FileConfiguration getPlayerDataConfig(){ - return this.playerDataConfig; - } public static String LoggerPrefix(){ return prefix; } diff --git a/src/main/java/de/steev/bm/utils/Config.java b/src/main/java/de/steev/bm/utils/Config.java new file mode 100644 index 0000000..e05a69a --- /dev/null +++ b/src/main/java/de/steev/bm/utils/Config.java @@ -0,0 +1,72 @@ +package de.steev.bm.utils; + +import de.steev.bm.BetterMinecraft; +import de.steev.bm.utils.exceptions.ConfigEntryExceptions; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; + +import static de.steev.bm.utils.Constants.playerDataFilename; + +public class Config { + private String name; + private File configFile; + private FileConfiguration fileConfiguration; + private BetterMinecraft plugin; + + public Config(String name, BetterMinecraft plugin) { + this.name = name; + this.plugin = plugin; + configFile = new File(this.plugin.getDataFolder(), playerDataFilename); + fileConfiguration = YamlConfiguration.loadConfiguration(configFile); + } + + public void saveConfig(String name) throws IOException { + try { + fileConfiguration.save(configFile); + } catch (IOException e) { + this.plugin.getLogger().warning("Unable to save " + playerDataFilename); // shouldn't really happen, but save + // throws the exception + throw e; + } + } + + public void writeStringToConfig(String path, String value) { + this.fileConfiguration.set(path, value); + } + + public String readStringFromConfig(String path) throws ConfigEntryExceptions { + + if (this.fileConfiguration.get(path) == null) { + throw new ConfigEntryExceptions("entry at: " + path + " found null"); + } + + return this.fileConfiguration.getString(path); + } + + public int readIntFromConfig(String path) throws ConfigEntryExceptions { + if (this.fileConfiguration.get(path) == null) { + throw new ConfigEntryExceptions("entry at: " + path + " found null"); + } + + return this.fileConfiguration.getInt(path); + } + + public boolean readBooleanFromConfig(String path) throws ConfigEntryExceptions { + if (this.fileConfiguration.get(path) == null) { + throw new ConfigEntryExceptions("entry at: " + path + " found null"); + } + + return this.fileConfiguration.getBoolean(path); + } + + public FileConfiguration getConfig() { + return this.fileConfiguration; + } + + public String getName() { + return this.name; + } +} diff --git a/src/main/java/de/steev/bm/utils/Constants.java b/src/main/java/de/steev/bm/utils/Constants.java new file mode 100644 index 0000000..c8ccc92 --- /dev/null +++ b/src/main/java/de/steev/bm/utils/Constants.java @@ -0,0 +1,9 @@ +package de.steev.bm.utils; + +import org.bukkit.ChatColor; + +public class Constants { + public static final String prefix = ChatColor.GRAY + "[" + ChatColor.AQUA + "BetterMinecraft" + ChatColor.GRAY + "]"; + public static final String playerDataFilename = "playerdata.yml"; + public static final String Version = "1.2.2"; +} diff --git a/src/main/java/de/steev/bm/utils/exceptions/ConfigEntryExceptions.java b/src/main/java/de/steev/bm/utils/exceptions/ConfigEntryExceptions.java new file mode 100644 index 0000000..928c12d --- /dev/null +++ b/src/main/java/de/steev/bm/utils/exceptions/ConfigEntryExceptions.java @@ -0,0 +1,9 @@ +package de.steev.bm.utils.exceptions; + +public class ConfigEntryExceptions extends Exception { + public ConfigEntryExceptions() {} + + public ConfigEntryExceptions(String message) { + super(message); + } +}