mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-19 14:59:32 +00:00
604 lines
28 KiB
Diff
604 lines
28 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: violetc <58360096+s-yh-china@users.noreply.github.com>
|
|
Date: Fri, 29 Oct 2021 16:52:57 +0800
|
|
Subject: [PATCH] Leaves Server Config And Command
|
|
|
|
|
|
diff --git a/src/main/java/co/aikar/timings/TimingsExport.java b/src/main/java/co/aikar/timings/TimingsExport.java
|
|
index c07eb451a576811a39021f6f97103c77488fd001..5752f97787a22f182a316a21ee9a06baad39a827 100644
|
|
--- a/src/main/java/co/aikar/timings/TimingsExport.java
|
|
+++ b/src/main/java/co/aikar/timings/TimingsExport.java
|
|
@@ -242,7 +242,8 @@ public class TimingsExport extends Thread {
|
|
parent.put("config", createObject(
|
|
pair("spigot", mapAsJSON(Bukkit.spigot().getSpigotConfig(), null)),
|
|
pair("bukkit", mapAsJSON(Bukkit.spigot().getBukkitConfig(), null)),
|
|
- pair("paper", mapAsJSON(Bukkit.spigot().getPaperConfig(), null))
|
|
+ pair("paper", mapAsJSON(Bukkit.spigot().getPaperConfig(), null)), // Leaves - add config to timings report
|
|
+ pair("leaves", mapAsJSON(Bukkit.spigot().getLeavesConfig(), null)) // Leaves - add config to timings report
|
|
));
|
|
|
|
new TimingsExport(listeners, parent, history).start();
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index e5d4c3eb2f6bfe446d6520564a263dbc6fecc1fa..3095f4ef8aea1d47d0a8880fcfdaf9d124d83773 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -1121,6 +1121,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
LOGGER.info("Done ({})! For help, type \"help\"", doneTime);
|
|
// Paper end
|
|
|
|
+ top.leavesmc.leaves.LeavesConfig.createWorldSections = false; // Leaves - dont let plugin create worlds fill our config
|
|
org.spigotmc.WatchdogThread.tick(); // Paper
|
|
org.spigotmc.WatchdogThread.hasStarted = true; // Paper
|
|
Arrays.fill( recentTps, 20 );
|
|
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
index a7e133f3495e9132a5fdae2c24f225e7b026295a..cd0f05aa0f2720d450ee1b975648e73dc22ac9a5 100644
|
|
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
@@ -221,6 +221,9 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
|
|
com.destroystokyo.paper.VersionHistoryManager.INSTANCE.getClass(); // load version history now
|
|
io.papermc.paper.brigadier.PaperBrigadierProviderImpl.INSTANCE.getClass(); // init PaperBrigadierProvider
|
|
// Paper end
|
|
+
|
|
+ top.leavesmc.leaves.LeavesConfig.init((java.io.File) options.valueOf("leaves-settings")); // Leaves - Server Config
|
|
+ top.leavesmc.leaves.LeavesConfig.registerCommands(); // Leaves - Server Command
|
|
|
|
this.setPvpAllowed(dedicatedserverproperties.pvp);
|
|
this.setFlightAllowed(dedicatedserverproperties.allowFlight);
|
|
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
|
|
index 973ecd50f9cb6b86c353586e84d15dcb118ccb60..31f0034115a88a42b18e0b838eab8b801473bbef 100644
|
|
--- a/src/main/java/net/minecraft/world/level/Level.java
|
|
+++ b/src/main/java/net/minecraft/world/level/Level.java
|
|
@@ -177,6 +177,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
|
|
// Paper end
|
|
|
|
public final com.destroystokyo.paper.antixray.ChunkPacketBlockController chunkPacketBlockController; // Paper - Anti-Xray
|
|
+ public final top.leavesmc.leaves.LeavesConfig.WorldConfig leavesConfig; // Leaves - World Config
|
|
public final co.aikar.timings.WorldTimingsHandler timings; // Paper
|
|
public static BlockPos lastPhysicsProblem; // Spigot
|
|
private org.spigotmc.TickLimiter entityLimiter;
|
|
@@ -277,6 +278,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
|
|
protected Level(WritableLevelData worlddatamutable, ResourceKey<Level> resourcekey, RegistryAccess iregistrycustom, Holder<DimensionType> holder, Supplier<ProfilerFiller> supplier, boolean flag, boolean flag1, long i, int j, org.bukkit.generator.ChunkGenerator gen, org.bukkit.generator.BiomeProvider biomeProvider, org.bukkit.World.Environment env, java.util.function.Function<org.spigotmc.SpigotWorldConfig, io.papermc.paper.configuration.WorldConfiguration> paperWorldConfigCreator, java.util.concurrent.Executor executor) { // Paper - Async-Anti-Xray - Pass executor
|
|
this.spigotConfig = new org.spigotmc.SpigotWorldConfig(((net.minecraft.world.level.storage.PrimaryLevelData) worlddatamutable).getLevelName()); // Spigot
|
|
this.paperConfig = paperWorldConfigCreator.apply(this.spigotConfig); // Paper
|
|
+ this.leavesConfig = new top.leavesmc.leaves.LeavesConfig.WorldConfig(((net.minecraft.world.level.storage.PrimaryLevelData)worlddatamutable).getLevelName()); // Leaves - World Config
|
|
this.generator = gen;
|
|
this.world = new CraftWorld((ServerLevel) this, gen, biomeProvider, env);
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
index 0d9f5e8b880bff78c122e863c42a23e47c455b9c..4cc3d03b30db2057b6e03a3b3e92459f8ad81eae 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
@@ -957,6 +957,7 @@ public final class CraftServer implements Server {
|
|
|
|
org.spigotmc.SpigotConfig.init((File) console.options.valueOf("spigot-settings")); // Spigot
|
|
this.console.paperConfigurations.reloadConfigs(this.console);
|
|
+ top.leavesmc.leaves.LeavesConfig.init((File) console.options.valueOf("leaves-settings")); // Leaves - Server Config
|
|
for (ServerLevel world : this.console.getAllLevels()) {
|
|
// world.serverLevelData.setDifficulty(config.difficulty); // Paper - per level difficulty
|
|
world.setSpawnSettings(world.serverLevelData.getDifficulty() != Difficulty.PEACEFUL && config.spawnMonsters, config.spawnAnimals); // Paper - per level difficulty (from MinecraftServer#setDifficulty(ServerLevel, Difficulty, boolean))
|
|
@@ -972,6 +973,7 @@ public final class CraftServer implements Server {
|
|
}
|
|
}
|
|
world.spigotConfig.init(); // Spigot
|
|
+ world.leavesConfig.init(); // Leaves - World Config
|
|
}
|
|
|
|
Plugin[] pluginClone = pluginManager.getPlugins().clone(); // Paper
|
|
@@ -987,6 +989,7 @@ public final class CraftServer implements Server {
|
|
this.reloadData();
|
|
org.spigotmc.SpigotConfig.registerCommands(); // Spigot
|
|
io.papermc.paper.command.PaperCommands.registerCommands(this.console); // Paper
|
|
+ top.leavesmc.leaves.LeavesConfig.registerCommands(); // Leaves - Server Command
|
|
this.overrideAllCommandBlockCommands = this.commandsConfiguration.getStringList("command-block-overrides").contains("*");
|
|
this.ignoreVanillaPermissions = this.commandsConfiguration.getBoolean("ignore-vanilla-permissions");
|
|
|
|
@@ -2740,6 +2743,14 @@ public final class CraftServer implements Server {
|
|
{
|
|
return CraftServer.this.console.paperConfigurations.createLegacyObject(CraftServer.this.console);
|
|
}
|
|
+
|
|
+ // Leaves start - add config to timings report
|
|
+ @Override
|
|
+ public YamlConfiguration getLeavesConfig()
|
|
+ {
|
|
+ return top.leavesmc.leaves.LeavesConfig.config;
|
|
+ }
|
|
+ /// Leaves end - add config to timings report
|
|
|
|
@Override
|
|
public void restart() {
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java
|
|
index ca4925d43413600ba092581c71086a7f5e357853..62e21fff47afa7b63c779e7271446dc3c92924e2 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/Main.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/Main.java
|
|
@@ -165,6 +165,14 @@ public class Main {
|
|
.ofType(File.class)
|
|
.defaultsTo(new File("paper.yml"))
|
|
.describedAs("Yml file");
|
|
+
|
|
+ // Leaves start - Server Config
|
|
+ acceptsAll(asList("leaves", "leaves-settings"), "File for leaves settings")
|
|
+ .withRequiredArg()
|
|
+ .ofType(File.class)
|
|
+ .defaultsTo(new File("leaves.yml"))
|
|
+ .describedAs("Yml file");
|
|
+ // Leaves end - Server Config
|
|
|
|
acceptsAll(asList("add-plugin", "add-extra-plugin-jar"), "Specify paths to extra plugin jars to be loaded in addition to those in the plugins folder. This argument can be specified multiple times, once for each extra plugin jar path.")
|
|
.withRequiredArg()
|
|
diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..4c290eb2d4abf9e9b923b8d0878f319328b7cf8c
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java
|
|
@@ -0,0 +1,309 @@
|
|
+package top.leavesmc.leaves;
|
|
+
|
|
+import com.destroystokyo.paper.util.SneakyThrow;
|
|
+import com.google.common.base.Throwables;
|
|
+import net.minecraft.server.MinecraftServer;
|
|
+import org.bukkit.Bukkit;
|
|
+import org.bukkit.command.Command;
|
|
+import org.bukkit.configuration.ConfigurationSection;
|
|
+import org.bukkit.configuration.file.YamlConfiguration;
|
|
+
|
|
+import java.io.File;
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
+import java.lang.reflect.Method;
|
|
+import java.lang.reflect.Modifier;
|
|
+import java.util.HashMap;
|
|
+import java.util.List;
|
|
+import java.util.Map;
|
|
+import java.util.logging.Level;
|
|
+
|
|
+// Powered by Tuinity(https://github.com/Tuinity/Tuinity)
|
|
+
|
|
+public final class LeavesConfig {
|
|
+
|
|
+ public static final String CONFIG_HEADER = "Configuration file for Leaves.";
|
|
+ public static final int CURRENT_CONFIG_VERSION = 2;
|
|
+
|
|
+ private static final Object[] EMPTY = new Object[0];
|
|
+
|
|
+ private static File configFile;
|
|
+ public static YamlConfiguration config;
|
|
+ private static int configVersion;
|
|
+ public static boolean createWorldSections = true;
|
|
+ static Map<String, Command> commands;
|
|
+
|
|
+ public static void init(final File file) {
|
|
+ LeavesConfig.configFile = file;
|
|
+ config = new YamlConfiguration();
|
|
+ config.options().header(CONFIG_HEADER);
|
|
+ config.options().copyDefaults(true);
|
|
+
|
|
+ if (!file.exists()) {
|
|
+ try {
|
|
+ file.createNewFile();
|
|
+ } catch (final Exception ex) {
|
|
+ Bukkit.getLogger().log(Level.SEVERE, "Failure to create leaves config", ex);
|
|
+ }
|
|
+ } else {
|
|
+ try {
|
|
+ config.load(file);
|
|
+ } catch (final Exception ex) {
|
|
+ Bukkit.getLogger().log(Level.SEVERE, "Failure to load leaves config", ex);
|
|
+ SneakyThrow.sneaky(ex); /* Rethrow, this is critical */
|
|
+ throw new RuntimeException(ex); // unreachable
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (config.contains("config-version-please-do-not-modify-me")) {
|
|
+ LeavesConfig.set("config-version-please-do-not-modify-me", null);
|
|
+ }
|
|
+ LeavesConfig.configVersion = LeavesConfig.getInt("config-version", CURRENT_CONFIG_VERSION);
|
|
+ LeavesConfig.set("config-version", CURRENT_CONFIG_VERSION);
|
|
+
|
|
+ updateConfigVersion(config);
|
|
+
|
|
+ LeavesConfig.load(config);
|
|
+
|
|
+ commands = new HashMap<>();
|
|
+ }
|
|
+
|
|
+ public static void load(final YamlConfiguration config) {
|
|
+ for (Method method : LeavesConfig.class.getDeclaredMethods()) {
|
|
+ if (Modifier.isPrivate(method.getModifiers())) {
|
|
+ if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
|
|
+ try {
|
|
+ method.setAccessible(true);
|
|
+ method.invoke(null);
|
|
+ } catch (InvocationTargetException ex) {
|
|
+ throw Throwables.propagate(ex.getCause());
|
|
+ } catch (Exception ex) {
|
|
+ Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ /* We re-save to add new options */
|
|
+ try {
|
|
+ config.save(LeavesConfig.configFile);
|
|
+ } catch (final Exception ex) {
|
|
+ Bukkit.getLogger().log(Level.SEVERE, "Unable to save leaves config", ex);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private static void updateConfigVersion(final YamlConfiguration config) {
|
|
+ if (configVersion < CURRENT_CONFIG_VERSION) {
|
|
+
|
|
+ }
|
|
+ }
|
|
+
|
|
+ static void set(final String path, final Object value) {
|
|
+ LeavesConfig.config.set(path, value);
|
|
+ }
|
|
+
|
|
+ public static void registerCommands() {
|
|
+ for (Map.Entry<String, Command> entry : commands.entrySet()) {
|
|
+ MinecraftServer.getServer().server.getCommandMap().register(entry.getKey(), "Leaves", entry.getValue());
|
|
+ }
|
|
+ }
|
|
+
|
|
+ static boolean getBoolean(final String path, final boolean dfl) {
|
|
+ LeavesConfig.config.addDefault(path, Boolean.valueOf(dfl));
|
|
+ return LeavesConfig.config.getBoolean(path, dfl);
|
|
+ }
|
|
+
|
|
+ static int getInt(final String path, final int dfl) {
|
|
+ LeavesConfig.config.addDefault(path, Integer.valueOf(dfl));
|
|
+ return LeavesConfig.config.getInt(path, dfl);
|
|
+ }
|
|
+
|
|
+ static long getLong(final String path, final long dfl) {
|
|
+ LeavesConfig.config.addDefault(path, Long.valueOf(dfl));
|
|
+ return LeavesConfig.config.getLong(path, dfl);
|
|
+ }
|
|
+
|
|
+ static double getDouble(final String path, final double dfl) {
|
|
+ LeavesConfig.config.addDefault(path, Double.valueOf(dfl));
|
|
+ return LeavesConfig.config.getDouble(path, dfl);
|
|
+ }
|
|
+
|
|
+ static String getString(final String path, final String dfl) {
|
|
+ LeavesConfig.config.addDefault(path, dfl);
|
|
+ return LeavesConfig.config.getString(path, dfl);
|
|
+ }
|
|
+
|
|
+ public static final class WorldConfig {
|
|
+
|
|
+ public final String worldName;
|
|
+ public String configPath;
|
|
+ ConfigurationSection worldDefaults;
|
|
+
|
|
+ public WorldConfig(final String worldName) {
|
|
+ this.worldName = worldName;
|
|
+ this.init();
|
|
+ }
|
|
+
|
|
+ public void init() {
|
|
+ this.worldDefaults = LeavesConfig.config.getConfigurationSection("world-settings.default");
|
|
+ if (this.worldDefaults == null) {
|
|
+ this.worldDefaults = LeavesConfig.config.createSection("world-settings.default");
|
|
+ }
|
|
+
|
|
+ String worldSectionPath = LeavesConfig.configVersion < CURRENT_CONFIG_VERSION ? this.worldName : "world-settings.".concat(this.worldName);
|
|
+ ConfigurationSection section = LeavesConfig.config.getConfigurationSection(worldSectionPath);
|
|
+ this.configPath = worldSectionPath;
|
|
+ if (LeavesConfig.createWorldSections) {
|
|
+ if (section == null) {
|
|
+ section = LeavesConfig.config.createSection(worldSectionPath);
|
|
+ }
|
|
+ LeavesConfig.config.set(worldSectionPath, section);
|
|
+ }
|
|
+
|
|
+ this.load();
|
|
+ }
|
|
+
|
|
+ public void load() {
|
|
+ for (final Method method : LeavesConfig.WorldConfig.class.getDeclaredMethods()) {
|
|
+ if (method.getReturnType() != void.class || method.getParameterCount() != 0 ||
|
|
+ !Modifier.isPrivate(method.getModifiers()) || Modifier.isStatic(method.getModifiers())) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ try {
|
|
+ method.setAccessible(true);
|
|
+ method.invoke(this, EMPTY);
|
|
+ } catch (final Exception ex) {
|
|
+ SneakyThrow.sneaky(ex); /* Rethrow, this is critical */
|
|
+ throw new RuntimeException(ex); // unreachable
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ ConfigurationSection oldSection = LeavesConfig.config.getConfigurationSection(this.worldName);
|
|
+ LeavesConfig.config.set("world-settings.".concat(this.worldName), oldSection);
|
|
+ LeavesConfig.config.set(this.worldName, null);
|
|
+ }
|
|
+
|
|
+ /* We re-save to add new options */
|
|
+ try {
|
|
+ LeavesConfig.config.save(LeavesConfig.configFile);
|
|
+ } catch (final Exception ex) {
|
|
+ Bukkit.getLogger().log(Level.SEVERE, "Unable to save leaves config", ex);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ void set(final String path, final Object val) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ this.worldDefaults.set(path, val);
|
|
+ if (config != null && config.get(path) != null) {
|
|
+ config.set(path, val);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ boolean getBoolean(final String path, final boolean dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ this.worldDefaults.addDefault(path, Boolean.valueOf(dfl));
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ if (config != null && config.getBoolean(path) == dfl) {
|
|
+ config.set(path, null);
|
|
+ }
|
|
+ }
|
|
+ return config == null ? this.worldDefaults.getBoolean(path) : config.getBoolean(path, this.worldDefaults.getBoolean(path));
|
|
+ }
|
|
+
|
|
+ boolean getBooleanRaw(final String path, final boolean dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ if (config != null && config.getBoolean(path) == dfl) {
|
|
+ config.set(path, null);
|
|
+ }
|
|
+ }
|
|
+ return config == null ? this.worldDefaults.getBoolean(path, dfl) : config.getBoolean(path, this.worldDefaults.getBoolean(path, dfl));
|
|
+ }
|
|
+
|
|
+ int getInt(final String path, final int dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ this.worldDefaults.addDefault(path, Integer.valueOf(dfl));
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ if (config != null && config.getInt(path) == dfl) {
|
|
+ config.set(path, null);
|
|
+ }
|
|
+ }
|
|
+ return config == null ? this.worldDefaults.getInt(path) : config.getInt(path, this.worldDefaults.getInt(path));
|
|
+ }
|
|
+
|
|
+ int getIntRaw(final String path, final int dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ if (config != null && config.getInt(path) == dfl) {
|
|
+ config.set(path, null);
|
|
+ }
|
|
+ }
|
|
+ return config == null ? this.worldDefaults.getInt(path, dfl) : config.getInt(path, this.worldDefaults.getInt(path, dfl));
|
|
+ }
|
|
+
|
|
+ long getLong(final String path, final long dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ this.worldDefaults.addDefault(path, Long.valueOf(dfl));
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ if (config != null && config.getLong(path) == dfl) {
|
|
+ config.set(path, null);
|
|
+ }
|
|
+ }
|
|
+ return config == null ? this.worldDefaults.getLong(path) : config.getLong(path, this.worldDefaults.getLong(path));
|
|
+ }
|
|
+
|
|
+ long getLongRaw(final String path, final long dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ if (config != null && config.getLong(path) == dfl) {
|
|
+ config.set(path, null);
|
|
+ }
|
|
+ }
|
|
+ return config == null ? this.worldDefaults.getLong(path, dfl) : config.getLong(path, this.worldDefaults.getLong(path, dfl));
|
|
+ }
|
|
+
|
|
+ double getDouble(final String path, final double dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ this.worldDefaults.addDefault(path, Double.valueOf(dfl));
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ if (config != null && config.getDouble(path) == dfl) {
|
|
+ config.set(path, null);
|
|
+ }
|
|
+ }
|
|
+ return config == null ? this.worldDefaults.getDouble(path) : config.getDouble(path, this.worldDefaults.getDouble(path));
|
|
+ }
|
|
+
|
|
+ double getDoubleRaw(final String path, final double dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) {
|
|
+ if (config != null && config.getDouble(path) == dfl) {
|
|
+ config.set(path, null);
|
|
+ }
|
|
+ }
|
|
+ return config == null ? this.worldDefaults.getDouble(path, dfl) : config.getDouble(path, this.worldDefaults.getDouble(path, dfl));
|
|
+ }
|
|
+
|
|
+ String getString(final String path, final String dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ this.worldDefaults.addDefault(path, dfl);
|
|
+ return config == null ? this.worldDefaults.getString(path) : config.getString(path, this.worldDefaults.getString(path));
|
|
+ }
|
|
+
|
|
+ String getStringRaw(final String path, final String dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ return config == null ? this.worldDefaults.getString(path, dfl) : config.getString(path, this.worldDefaults.getString(path, dfl));
|
|
+ }
|
|
+
|
|
+ List getList(final String path, final List dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ this.worldDefaults.addDefault(path, dfl);
|
|
+ return config == null ? this.worldDefaults.getList(path) : config.getList(path, this.worldDefaults.getList(path));
|
|
+ }
|
|
+
|
|
+ List getListRaw(final String path, final List dfl) {
|
|
+ final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath);
|
|
+ return config == null ? this.worldDefaults.getList(path, dfl) : config.getList(path, this.worldDefaults.getList(path, dfl));
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/command/CommandArgument.java b/src/main/java/top/leavesmc/leaves/command/CommandArgument.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..eadc6d168fb13299348b0c275ae352ee2f1e1ea2
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/command/CommandArgument.java
|
|
@@ -0,0 +1,43 @@
|
|
+package top.leavesmc.leaves.command;
|
|
+
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import java.util.ArrayList;
|
|
+import java.util.Arrays;
|
|
+import java.util.List;
|
|
+
|
|
+public class CommandArgument {
|
|
+
|
|
+ private final List<CommandArgumentType<?>> argumentTypes;
|
|
+ private final List<List<String>> tabComplete;
|
|
+
|
|
+ public CommandArgument(CommandArgumentType<?>... argumentTypes) {
|
|
+ this.argumentTypes = List.of(argumentTypes);
|
|
+ this.tabComplete = new ArrayList<>();
|
|
+ for (int i = 0; i < argumentTypes.length; i++) {
|
|
+ tabComplete.add(new ArrayList<>());
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public List<String> tabComplete(int n) {
|
|
+ if (tabComplete.size() > n) {
|
|
+ return tabComplete.get(n);
|
|
+ } else {
|
|
+ return List.of();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public CommandArgument setTabComplete(int index, List<String> list) {
|
|
+ tabComplete.set(index, list);
|
|
+ return this;
|
|
+ }
|
|
+
|
|
+ public CommandArgumentResult parse(int index, String @NotNull [] args) {
|
|
+ Object[] result = new Object[argumentTypes.size()];
|
|
+ Arrays.fill(result, null);
|
|
+ for (int i = index, j = 0; i < args.length && j < result.length; i++, j++) {
|
|
+ result[j] = argumentTypes.get(j).pasre(args[i]);
|
|
+ }
|
|
+ return new CommandArgumentResult(new ArrayList<>(Arrays.asList(result)));
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/command/CommandArgumentResult.java b/src/main/java/top/leavesmc/leaves/command/CommandArgumentResult.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..340eaca64c96180b895a075ce9e44402cd104eed
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/command/CommandArgumentResult.java
|
|
@@ -0,0 +1,62 @@
|
|
+package top.leavesmc.leaves.command;
|
|
+
|
|
+import net.minecraft.core.BlockPos;
|
|
+import org.bukkit.util.Vector;
|
|
+
|
|
+import java.util.List;
|
|
+import java.util.Objects;
|
|
+
|
|
+public class CommandArgumentResult {
|
|
+
|
|
+ private final List<Object> result;
|
|
+
|
|
+ public CommandArgumentResult(List<Object> result) {
|
|
+ this.result = result;
|
|
+ }
|
|
+
|
|
+ public Integer readInt(int def) {
|
|
+ return Objects.requireNonNullElse(read(Integer.class), def);
|
|
+ }
|
|
+
|
|
+ public Double readDouble(double def) {
|
|
+ return Objects.requireNonNullElse(read(Double.class), def);
|
|
+ }
|
|
+
|
|
+ public String readString(String def) {
|
|
+ return Objects.requireNonNullElse(read(String.class), def);
|
|
+ }
|
|
+
|
|
+ public BlockPos readPos() {
|
|
+ Integer[] pos = {read(Integer.class), read(Integer.class), read(Integer.class)};
|
|
+ for (Integer po : pos) {
|
|
+ if (po == null) {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+ return new BlockPos(pos[0], pos[1], pos[2]);
|
|
+ }
|
|
+
|
|
+ public Vector readVector() {
|
|
+ Double[] pos = {read(Double.class), read(Double.class), read(Double.class)};
|
|
+ for (Double po : pos) {
|
|
+ if (po == null) {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+ return new Vector(pos[0], pos[1], pos[2]);
|
|
+ }
|
|
+
|
|
+ public <T> T read(Class<T> tClass) {
|
|
+ if (result.isEmpty()) {
|
|
+ return null;
|
|
+ }
|
|
+
|
|
+ Object obj = result.remove(0);
|
|
+ if (tClass.isInstance(obj)) {
|
|
+ return tClass.cast(obj);
|
|
+ } else {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/command/CommandArgumentType.java b/src/main/java/top/leavesmc/leaves/command/CommandArgumentType.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..edf12195c7224ca2fb5d3c2ac3fcf485d3049d07
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/command/CommandArgumentType.java
|
|
@@ -0,0 +1,37 @@
|
|
+package top.leavesmc.leaves.command;
|
|
+
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public abstract class CommandArgumentType<E> {
|
|
+
|
|
+ public static final CommandArgumentType<Integer> INTEGER = new CommandArgumentType<>() {
|
|
+ @Override
|
|
+ public Integer pasre(@NotNull String arg) {
|
|
+ try {
|
|
+ return Integer.parseInt(arg);
|
|
+ } catch (NumberFormatException e) {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+ };
|
|
+
|
|
+ public static final CommandArgumentType<Double> DOUBLE = new CommandArgumentType<>() {
|
|
+ @Override
|
|
+ public Double pasre(@NotNull String arg) {
|
|
+ try {
|
|
+ return Double.parseDouble(arg);
|
|
+ } catch (NumberFormatException e) {
|
|
+ return null;
|
|
+ }
|
|
+ }
|
|
+ };
|
|
+
|
|
+ public static final CommandArgumentType<String> STRING = new CommandArgumentType<>() {
|
|
+ @Override
|
|
+ public String pasre(@NotNull String arg) {
|
|
+ return arg;
|
|
+ }
|
|
+ };
|
|
+
|
|
+ public abstract E pasre(@NotNull String arg);
|
|
+}
|