more Logging and automatic config file creation

This commit is contained in:
Martin Olischläger 2024-09-20 22:35:59 +02:00
parent 36bbd5ef97
commit 52b37d1094
3 changed files with 29 additions and 6 deletions

View File

@ -30,5 +30,6 @@ tasks.register<Exec>("copyJarToTestServer") {
dependsOn(tasks.jar)
mustRunAfter(tasks.jar)
commandLine("cp", "build/libs/WorldMuseum-1.0-SNAPSHOT.jar", "/home/elias/Dokumente/mcTestServer/plugins/worldmuseum.jar")
commandLine("cp", "build/libs/WorldMuseum-1.0-SNAPSHOT.jar", "/Users/olischma/Desktop/paper/plugins")
}

View File

@ -45,12 +45,18 @@ public class Main extends JavaPlugin {
private void loadWorlds() {
File worldFolder = new File(getDataFolder().getAbsolutePath() + "/worlds");
if(!worldFolder.setReadOnly()) throw new RuntimeException("Could not set Worlds to readonly. Be careful!");
//if(!worldFolder.setReadOnly()) throw new RuntimeException("Could not set Worlds to readonly. Be careful!");
//noinspection ResultOfMethodCallIgnored
worldFolder.mkdirs();
Arrays.stream(Objects.requireNonNull(worldFolder.listFiles())).forEach(file -> this.worlds.add(new ViewableWorld(file)));
Arrays.stream(Objects.requireNonNull(worldFolder.listFiles())).forEach(file -> {
Bukkit.getLogger().info("[Worldmuseum] Loading world " + file.getName());
if (file.isDirectory()) this.worlds.add(new ViewableWorld(file));
else Bukkit.getLogger().info( "[Worldmuseum] " + file.getName() + " is not a valid world!");
});
this.worlds.forEach(ViewableWorld::loadWorld);
for(ViewableWorld viewableWorld : this.worlds) viewableWorld.getWorld().setAutoSave(false);
}
public static Main instance() {

View File

@ -3,7 +3,7 @@ package eu.mhsl.minecraft.WorldMuseum.viewableWorld;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.TriState;
import org.bukkit.*;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.generator.ChunkGenerator;
@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
public class ViewableWorld {
@ -23,13 +24,24 @@ public class ViewableWorld {
public ViewableWorld(File worldLocation) {
this.worldLocation = worldLocation;
this.name = "unnamed_" + worldLocation.getName();
this.name = "unnamed_" + this.worldLocation.toPath().getFileName().toString();
File configFile = new File(this.worldLocation.getAbsolutePath() + "/config.yml");
if (configFile.exists()) {
ConfigurationSection config = YamlConfiguration.loadConfiguration(configFile);
FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
this.enabled = config.getBoolean("enabled");
this.name = config.getString("name");
this.icon = Material.valueOf(config.getString("icon"));
} else {
System.out.println("[WorldMuseum] Could not find config.yml");
try {
configFile.createNewFile();
FileConfiguration data = YamlConfiguration.loadConfiguration(configFile);
data.set("enabled", false);
data.set("name", this.name);
data.set("icon", this.icon.name());
data.save(configFile);
} catch (IOException e) {e.printStackTrace();}
}
}
@ -63,4 +75,8 @@ public class ViewableWorld {
public boolean isEnabled() {
return enabled;
}
public World getWorld() {
return world;
}
}