소스 검색

Adding new classes

RedstoneFuture 5 달 전
부모
커밋
e66a25483f

+ 57 - 0
missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/game/modules/LobbyConfig.java

@@ -0,0 +1,57 @@
+/*
+ * This file is part of MissileWars (https://github.com/Butzlabben/missilewars).
+ * Copyright (c) 2018-2021 Daniel Nägele.
+ *
+ * MissileWars is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * MissileWars is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MissileWars.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package de.butzlabben.missilewars.configuration.game.modules;
+
+import com.google.gson.annotations.SerializedName;
+import de.butzlabben.missilewars.Logger;
+import de.butzlabben.missilewars.configuration.Config;
+import de.butzlabben.missilewars.configuration.arena.modules.AreaConfig;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+import lombok.ToString;
+import org.bukkit.Bukkit;
+import org.bukkit.Location;
+import org.bukkit.World;
+
+@Getter
+@ToString
+@RequiredArgsConstructor
+public class LobbyConfig {
+    
+    private String name = "arena0";
+    @SerializedName("world") private String worldName = getBukkitDefaultWorld().getName();
+    @SerializedName("lobby_time") private int lobbyTime = 60;
+    @Setter @SerializedName("spawn_point") private Location spawnPoint = Config.getFallbackSpawn().add(40, 0, 0);
+    @Setter @SerializedName("after_game_spawn") private Location afterGameSpawn = Config.getFallbackSpawn();
+    @Setter @SerializedName("area") private AreaConfig areaConfig = AreaConfig.aroundLocation(spawnPoint, 20);
+    
+    private World getBukkitDefaultWorld() {
+        return Bukkit.getWorlds().get(0);
+    }
+    
+    public World getBukkitWorld() {
+        World world = Bukkit.getWorld(worldName);
+        if (world == null) {
+            Logger.ERROR.log("Could not find any world with the name: " + worldName);
+            Logger.ERROR.log("Please correct this in the configuration of lobby \"" + name + "\"");
+        }
+        return world;
+    }
+}

+ 48 - 0
missilewars-plugin/src/main/java/de/butzlabben/missilewars/initialization/ConfigLoader.java

@@ -0,0 +1,48 @@
+package de.butzlabben.missilewars.initialization;
+
+import de.butzlabben.missilewars.Logger;
+import de.butzlabben.missilewars.configuration.Config;
+import de.butzlabben.missilewars.configuration.PluginMessages;
+import org.bukkit.configuration.file.YamlConfiguration;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+
+public class ConfigLoader {
+    
+    public static void loadConfigs() {
+        
+        Config.load();
+        PluginMessages.load();
+        
+    }
+    
+    public static YamlConfiguration loadConfigFile(File file) {
+        
+        // check if the directory and the file exists or create it new
+        FileManager.createNewConfig(file);
+        
+        // try to load the config
+        YamlConfiguration cfg = getLoadedConfig(file);
+        
+        // copy the config input
+        cfg.options().copyDefaults(true);
+        
+        return cfg;
+    }
+    
+    public static YamlConfiguration getLoadedConfig(File file) {
+        String fileName = file.getName();
+        YamlConfiguration cfg;
+
+        try {
+            cfg = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
+        } catch (FileNotFoundException e) {
+            Logger.ERROR.log("Couldn't load " + fileName + "!");
+            e.printStackTrace();
+            return null;
+        }
+        return cfg;
+    }
+    
+}

+ 181 - 0
missilewars-plugin/src/main/java/de/butzlabben/missilewars/initialization/FileManager.java

@@ -0,0 +1,181 @@
+package de.butzlabben.missilewars.initialization;
+
+import de.butzlabben.missilewars.Logger;
+import de.butzlabben.missilewars.MissileWars;
+import de.butzlabben.missilewars.configuration.Config;
+import de.butzlabben.missilewars.player.PlayerData;
+import org.apache.commons.io.FileUtils;
+import org.bukkit.Bukkit;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.configuration.serialization.ConfigurationSerialization;
+import org.bukkit.plugin.java.JavaPlugin;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+public class FileManager {
+    
+    public static void setupRoutine() {
+        
+        deleteTempWorlds();
+        
+        ConfigLoader.loadConfigs();
+        
+        saveDefaultResource(Config.getMissilesFolder(), "missiles.zip", MissileWars.getInstance());
+        saveDefaultResource(Config.getShieldsFolder(), "shields.zip", MissileWars.getInstance());
+        
+        ConfigurationSerialization.registerClass(PlayerData.class);
+    }
+    
+    public static void shotDownRoutine() {
+        
+        deleteTempWorlds();
+        
+    }
+    
+    /**
+     * This methode deletes the old MissileWars (temporary) arena world from the last server session, if still exists.
+     */
+    private static void deleteTempWorlds() {
+        File[] dirs = Bukkit.getWorldContainer().listFiles();
+        if (dirs == null) return;
+
+        for (File dir : dirs) {
+            if (dir.getName().startsWith("mw-")) {
+                try {
+                    FileUtils.deleteDirectory(dir);
+                } catch (Exception ignored) {
+                }
+            }
+        }
+    }
+    
+    /**
+     * Extracts a ZIP file from the plugin's resource folder and saves the contents
+     * into the specified targetPath.
+     * 
+     * @param targetPath The directory where the extracted files should be saved.
+     * @param defaultArchiveFile The name of the ZIP file in the plugin's resource folder.
+     */
+    public static void saveDefaultResource(String targetPath, String defaultArchiveFile, JavaPlugin plugin) {
+        // Ensure the target directory exists
+        File targetDir = new File(targetPath);
+        
+        // Check if the target directory already exists; if so, skip extraction
+        if (targetDir.exists()) {
+            Logger.NORMAL.log("Directory '" + targetDir.getPath() + "' already exists. Skipping extraction.");
+            return;
+        }
+        
+        // Create the target directory if it does not exist
+        if (!targetDir.mkdirs()) {
+            Logger.ERROR.log("Failed to create directory '" + targetDir.getPath() + "'");
+            return;
+        }
+        
+        // Get the resource as an InputStream
+        try (InputStream in = plugin.getResource(defaultArchiveFile)) {
+            if (in == null) {
+                Logger.ERROR.log("Unable to find resource '" + defaultArchiveFile + "'!");
+                return;
+            }
+    
+            // Unzip the resource to the target directory
+            Logger.NORMAL.log("Unzipping resource '" + defaultArchiveFile + "' to directory: " + targetDir.getPath());
+            unzip(in, targetDir);
+        } catch (IOException e) {
+            Logger.ERROR.log("Failed to unzip resource '" + defaultArchiveFile + "'!");
+            e.printStackTrace();
+        }
+    }
+    
+    private static void unzip(InputStream in, File targetDir) throws IOException {
+        try (ZipInputStream zis = new ZipInputStream(in)) {
+            ZipEntry entry;
+            while ((entry = zis.getNextEntry()) != null) {
+                File newFile = new File(targetDir, entry.getName());
+    
+                if (entry.isDirectory()) {
+                    // Handle directory entries
+                    if (!newFile.isDirectory() && !newFile.mkdirs()) {
+                        throw new IOException("Failed to create directory " + newFile);
+                    }
+                } else {
+                    // Check if file already exists and decide whether to overwrite or not
+                    if (newFile.exists()) {
+                        Logger.WARN.log("File " + newFile.getName() + " already exists. Skipping.");
+                        continue; // Skip if the file exists
+                    }
+    
+                    // Ensure parent directory exists
+                    File parent = newFile.getParentFile();
+                    if (!parent.isDirectory() && !parent.mkdirs()) {
+                        throw new IOException("Failed to create directory " + parent);
+                    }
+    
+                    // Write file content
+                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
+                        byte[] buffer = new byte[1024];
+                        int len;
+                        while ((len = zis.read(buffer)) > 0) {
+                            fos.write(buffer, 0, len);
+                        }
+                    }
+                }
+                zis.closeEntry();
+            }
+        }
+    }
+    
+    public static void safeFile(File file, YamlConfiguration cfg) {
+        String fileName = file.getName();
+
+        try {
+            cfg.save(file);
+        } catch (IOException e) {
+            Logger.ERROR.log("Could not save " + fileName + "!");
+            e.printStackTrace();
+        }
+    }
+    
+    /**
+     * Creates a new configuration file in the specified directory if it does not already exist.
+     * 
+     * @param file The File object representing the configuration file to be created.
+     */
+    public static void createNewConfig(File file) {
+        String fileName = file.getName();
+        File dir = file.getParentFile(); // Get the parent directory of the file
+
+        // Check if the directory exists; if not, create it
+        if (dir != null && !dir.exists()) {
+            if (dir.mkdirs()) {
+                Logger.NORMAL.log("Directory '" + dir.getPath() + "' created.");
+            } else {
+                Logger.ERROR.log("Failed to create directory '" + dir.getPath() + "'.");
+                return;
+            }
+        }
+
+        // Check if the config file exists; if not, create it
+        if (!file.exists()) {
+            try {
+                if (file.createNewFile()) {
+                    Logger.NORMAL.log("Configuration file '" + fileName + "' created successfully.");
+                } else {
+                    Logger.ERROR.log("Failed to create configuration file '" + fileName + "'.");
+                }
+            } catch (IOException e) {
+                Logger.ERROR.log("Could not create " + fileName + " due to an IOException!");
+                e.printStackTrace();
+            }
+        } else {
+            Logger.NORMAL.log("Configuration file '" + fileName + "' already exists. Skipping creation.");
+        }
+    }
+    
+}

+ 29 - 0
missilewars-plugin/src/main/java/de/butzlabben/missilewars/initialization/GamesInitialization.java

@@ -0,0 +1,29 @@
+package de.butzlabben.missilewars.initialization;
+
+import de.butzlabben.missilewars.MissileWars;
+import de.butzlabben.missilewars.game.Arenas;
+import de.butzlabben.missilewars.game.GameManager;
+import de.butzlabben.missilewars.game.signs.SignUpdateRunnable;
+import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
+
+public class GamesInitialization {
+    
+    public static void initialize() {
+        
+        // TODO Ref
+        Arenas.load();
+        GameManager.getInstance().loadGamesOnStartup();
+        
+        GameManager.getInstance().getGames().values().forEach(game -> {
+            for (Player player : Bukkit.getOnlinePlayers()) {
+                if (!game.isIn(player.getLocation())) continue;
+                game.teleportToLobbySpawn(player);
+            }
+        });
+        
+        Bukkit.getScheduler().runTaskTimerAsynchronously(MissileWars.getInstance(), new SignUpdateRunnable(), 20, 20 * 10);
+        
+    }
+    
+}