From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Mon, 29 Feb 2016 21:02:09 -0600 Subject: [PATCH] Paper config files Loads each yml file for early init too so it can be used for early options diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..80a3d5890aab91e6a48d5734140187851106bde3 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java @@ -0,0 +1,188 @@ +package com.destroystokyo.paper; + +import com.google.common.base.Throwables; + +import java.io.File; +import java.io.IOException; +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.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.regex.Pattern; + +import net.minecraft.server.MinecraftServer; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; + +public class PaperConfig { + + private static File CONFIG_FILE; + private static final String HEADER = "This is the main configuration file for Paper.\n" + + "As you can see, there's tons to configure. Some options may impact gameplay, so use\n" + + "with caution, and make sure you know what each option does before configuring.\n" + + "\n" + + "If you need help with the configuration or have any questions related to Paper,\n" + + "join us in our Discord or IRC channel.\n" + + "\n" + + "Discord: https://discord.gg/papermc\n" + + "IRC: #paper @ irc.esper.net ( https://webchat.esper.net/?channels=paper ) \n" + + "Website: https://papermc.io/ \n" + + "Docs: https://docs.papermc.io/ \n"; + /*========================================================================*/ + public static YamlConfiguration config; + static int version; + static Map commands; + private static boolean verbose; + private static boolean fatalError; + /*========================================================================*/ + + public static void init(File configFile) { + CONFIG_FILE = configFile; + config = new YamlConfiguration(); + try { + config.load(CONFIG_FILE); + } catch (IOException ex) { + } catch (InvalidConfigurationException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Could not load paper.yml, please correct your syntax errors", ex); + throw Throwables.propagate(ex); + } + config.options().header(HEADER); + config.options().copyDefaults(true); + verbose = getBoolean("verbose", false); + + commands = new HashMap(); + commands.put("paper", new PaperCommand("paper")); + + version = getInt("config-version", 27); + set("config-version", 27); + readConfig(PaperConfig.class, null); + } + + protected static void logError(String s) { + Bukkit.getLogger().severe(s); + } + + protected static void fatal(String s) { + fatalError = true; + throw new RuntimeException("Fatal paper.yml config error: " + s); + } + + protected static void log(String s) { + if (verbose) { + Bukkit.getLogger().info(s); + } + } + + public static void registerCommands() { + for (Map.Entry entry : commands.entrySet()) { + MinecraftServer.getServer().server.getCommandMap().register(entry.getKey(), "Paper", entry.getValue()); + } + } + + static void readConfig(Class clazz, Object instance) { + for (Method method : clazz.getDeclaredMethods()) { + if (Modifier.isPrivate(method.getModifiers())) { + if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) { + try { + method.setAccessible(true); + method.invoke(instance); + } catch (InvocationTargetException ex) { + throw Throwables.propagate(ex.getCause()); + } catch (Exception ex) { + Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex); + } + } + } + } + saveConfig(); + } + + static void saveConfig() { + try { + config.save(CONFIG_FILE); + } catch (IOException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Could not save " + CONFIG_FILE, ex); + } + } + + private static final Pattern SPACE = Pattern.compile(" "); + private static final Pattern NOT_NUMERIC = Pattern.compile("[^-\\d.]"); + public static int getSeconds(String str) { + str = SPACE.matcher(str).replaceAll(""); + final char unit = str.charAt(str.length() - 1); + str = NOT_NUMERIC.matcher(str).replaceAll(""); + double num; + try { + num = Double.parseDouble(str); + } catch (Exception e) { + num = 0D; + } + switch (unit) { + case 'd': num *= (double) 60*60*24; break; + case 'h': num *= (double) 60*60; break; + case 'm': num *= (double) 60; break; + default: case 's': break; + } + return (int) num; + } + + protected static String timeSummary(int seconds) { + String time = ""; + + if (seconds > 60 * 60 * 24) { + time += TimeUnit.SECONDS.toDays(seconds) + "d"; + seconds %= 60 * 60 * 24; + } + + if (seconds > 60 * 60) { + time += TimeUnit.SECONDS.toHours(seconds) + "h"; + seconds %= 60 * 60; + } + + if (seconds > 0) { + time += TimeUnit.SECONDS.toMinutes(seconds) + "m"; + } + return time; + } + + private static void set(String path, Object val) { + config.set(path, val); + } + + private static boolean getBoolean(String path, boolean def) { + config.addDefault(path, def); + return config.getBoolean(path, config.getBoolean(path)); + } + + private static double getDouble(String path, double def) { + config.addDefault(path, def); + return config.getDouble(path, config.getDouble(path)); + } + + private static float getFloat(String path, float def) { + // TODO: Figure out why getFloat() always returns the default value. + return (float) getDouble(path, (double) def); + } + + private static int getInt(String path, int def) { + config.addDefault(path, def); + return config.getInt(path, config.getInt(path)); + } + + private static List getList(String path, T def) { + config.addDefault(path, def); + return (List) config.getList(path, config.getList(path)); + } + + private static String getString(String path, String def) { + config.addDefault(path, def); + return config.getString(path, config.getString(path)); + } +} diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..1dc56bee6827f6552cfa93a92fe8d0b5f94ab596 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java @@ -0,0 +1,99 @@ +package com.destroystokyo.paper; + +import java.util.List; + +import java.util.stream.Collectors; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.YamlConfiguration; +import org.spigotmc.SpigotWorldConfig; + +import static com.destroystokyo.paper.PaperConfig.log; +import static com.destroystokyo.paper.PaperConfig.logError; +import static com.destroystokyo.paper.PaperConfig.saveConfig; + +public class PaperWorldConfig { + + private final String worldName; + private final SpigotWorldConfig spigotConfig; + private YamlConfiguration config; + private boolean verbose; + + public PaperWorldConfig(String worldName, SpigotWorldConfig spigotConfig) { + this.worldName = worldName; + this.spigotConfig = spigotConfig; + this.config = PaperConfig.config; + init(); + } + + public void init() { + this.config = PaperConfig.config; // grab updated reference + log("-------- World Settings For [" + worldName + "] --------"); + PaperConfig.readConfig(PaperWorldConfig.class, this); + } + + private void set(String path, Object val) { + config.set("world-settings.default." + path, val); + if (config.get("world-settings." + worldName + "." + path) != null) { + config.set("world-settings." + worldName + "." + path, val); + } + } + + private void remove(String path) { + config.addDefault("world-settings.default." + path, null); + set(path, null); + } + + public void removeOldValues() { + boolean needsSave = false; + + if (needsSave) { + saveConfig(); + } + } + + private boolean getBoolean(String path, boolean def) { + config.addDefault("world-settings.default." + path, def); + return config.getBoolean("world-settings." + worldName + "." + path, config.getBoolean("world-settings.default." + path)); + } + + private double getDouble(String path, double def) { + config.addDefault("world-settings.default." + path, def); + return config.getDouble("world-settings." + worldName + "." + path, config.getDouble("world-settings.default." + path)); + } + + private int getInt(String path, int def) { + return getInt(path, def, true); + } + + private int getInt(String path, int def, boolean setDefault) { + if (setDefault) { + config.addDefault("world-settings.default." + path, def); + } + return config.getInt("world-settings." + worldName + "." + path, config.getInt("world-settings.default." + path, def)); + } + + private long getLong(String path, long def) { + config.addDefault("world-settings.default." + path, def); + return config.getLong("world-settings." + worldName + "." + path, config.getLong("world-settings.default." + path)); + } + + private float getFloat(String path, float def) { + // TODO: Figure out why getFloat() always returns the default value. + return (float) getDouble(path, (double) def); + } + + private List getList(String path, List def) { + config.addDefault("world-settings.default." + path, def); + return (List) config.getList("world-settings." + worldName + "." + path, config.getList("world-settings.default." + path)); + } + + private String getString(String path, String def) { + config.addDefault("world-settings.default." + path, def); + return config.getString("world-settings." + worldName + "." + path, config.getString("world-settings.default." + path)); + } + + private > List getEnumList(String path, List def, Class type) { + config.addDefault("world-settings.default." + path, def.stream().map(Enum::name).collect(Collectors.toList())); + return ((List) (config.getList("world-settings." + worldName + "." + path, config.getList("world-settings.default." + path)))).stream().map(s -> Enum.valueOf(type, s)).collect(Collectors.toList()); + } +} diff --git a/src/main/java/net/minecraft/server/Main.java b/src/main/java/net/minecraft/server/Main.java index 9e4cca119722c2edc478fdd1a34af4370649aa5c..b49528d8a2c577def4f74ee694ffd53b481acb32 100644 --- a/src/main/java/net/minecraft/server/Main.java +++ b/src/main/java/net/minecraft/server/Main.java @@ -109,6 +109,12 @@ public class Main { DedicatedServerSettings dedicatedserversettings = new DedicatedServerSettings(optionset); // CraftBukkit - CLI argument support dedicatedserversettings.forceSave(); + // Paper start - load config files for access below if needed + org.bukkit.configuration.file.YamlConfiguration bukkitConfiguration = loadConfigFile((File) optionset.valueOf("bukkit-settings")); + org.bukkit.configuration.file.YamlConfiguration spigotConfiguration = loadConfigFile((File) optionset.valueOf("spigot-settings")); + org.bukkit.configuration.file.YamlConfiguration paperConfiguration = loadConfigFile((File) optionset.valueOf("paper-settings")); + // Paper end + Path path1 = Paths.get("eula.txt"); Eula eula = new Eula(path1); @@ -284,6 +290,20 @@ public class Main { } + // Paper start - load config files + private static org.bukkit.configuration.file.YamlConfiguration loadConfigFile(File configFile) throws Exception { + org.bukkit.configuration.file.YamlConfiguration config = new org.bukkit.configuration.file.YamlConfiguration(); + if (configFile.exists()) { + try { + config.load(configFile); + } catch (Exception ex) { + throw new Exception("Failed to load configuration file: " + configFile.getName(), ex); + } + } + return config; + } + // Paper end + public static void forceUpgrade(LevelStorageSource.LevelStorageAccess session, DataFixer dataFixer, boolean eraseCache, BooleanSupplier continueCheck, WorldGenSettings generatorOptions) { Main.LOGGER.info("Forcing world upgrade! {}", session.getLevelId()); // CraftBukkit WorldUpgrader worldupgrader = new WorldUpgrader(session, dataFixer, generatorOptions, eraseCache); diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 6016216921232bedba84acc1f6a87a3cfbb75850..ef128de5fe17231b39edb12a7014291d03cf79ec 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -601,6 +601,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop> 4, net.minecraft.util.Mth.floor(entity.getZ()) >> 4)); + } + // Paper end + public boolean isPositionTicking(long pos) { ChunkHolder playerchunk = this.getVisibleChunkIfPresent(pos); diff --git a/src/main/java/net/minecraft/world/entity/EntityType.java b/src/main/java/net/minecraft/world/entity/EntityType.java index ec57266d48998ed0c656006834c82342236ea4c9..d790c37753f019b682fe00bea392359ba18bf204 100644 --- a/src/main/java/net/minecraft/world/entity/EntityType.java +++ b/src/main/java/net/minecraft/world/entity/EntityType.java @@ -674,4 +674,10 @@ public class EntityType implements EntityTypeTest { T create(EntityType type, Level world); } + + // Paper start + public static java.util.Set getEntityNameList() { + return Registry.ENTITY_TYPE.keySet(); + } + // Paper end } diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java index 3dce5044332ee4a8f685ab1cde02cd4c72a85b63..6ded806628f2f3fd5b357eed41990abd7e06a372 100644 --- a/src/main/java/net/minecraft/world/level/Level.java +++ b/src/main/java/net/minecraft/world/level/Level.java @@ -144,6 +144,8 @@ public abstract class Level implements LevelAccessor, AutoCloseable { public boolean populating; public final org.spigotmc.SpigotWorldConfig spigotConfig; // Spigot + public final com.destroystokyo.paper.PaperWorldConfig paperConfig; // Paper + public final SpigotTimings.WorldTimingsHandler timings; // Spigot public static BlockPos lastPhysicsProblem; // Spigot private org.spigotmc.TickLimiter entityLimiter; @@ -162,6 +164,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable { protected Level(WritableLevelData worlddatamutable, ResourceKey resourcekey, Holder holder, Supplier supplier, boolean flag, boolean flag1, long i, org.bukkit.generator.ChunkGenerator gen, org.bukkit.generator.BiomeProvider biomeProvider, org.bukkit.World.Environment env) { this.spigotConfig = new org.spigotmc.SpigotWorldConfig(((net.minecraft.world.level.storage.PrimaryLevelData) worlddatamutable).getLevelName()); // Spigot + this.paperConfig = new com.destroystokyo.paper.PaperWorldConfig(((net.minecraft.world.level.storage.PrimaryLevelData) worlddatamutable).getLevelName(), this.spigotConfig); // Paper 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 6657edb762d234ab388b0bf7dd52f910e0e0af8b..d29c6d0536619fab5a48fbb52115dac09e7d7ca3 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -866,6 +866,7 @@ public final class CraftServer implements Server { } org.spigotmc.SpigotConfig.init((File) console.options.valueOf("spigot-settings")); // Spigot + com.destroystokyo.paper.PaperConfig.init((File) console.options.valueOf("paper-settings")); // Paper for (ServerLevel world : this.console.getAllLevels()) { world.serverLevelData.setDifficulty(config.difficulty); world.setSpawnSettings(config.spawnMonsters, config.spawnAnimals); @@ -881,12 +882,14 @@ public final class CraftServer implements Server { } } world.spigotConfig.init(); // Spigot + world.paperConfig.init(); // Paper } this.pluginManager.clearPlugins(); this.commandMap.clearCommands(); this.reloadData(); org.spigotmc.SpigotConfig.registerCommands(); // Spigot + com.destroystokyo.paper.PaperConfig.registerCommands(); // Paper this.overrideAllCommandBlockCommands = this.commandsConfiguration.getStringList("command-block-overrides").contains("*"); this.ignoreVanillaPermissions = this.commandsConfiguration.getBoolean("ignore-vanilla-permissions"); @@ -2331,4 +2334,35 @@ public final class CraftServer implements Server { return this.spigot; } // Spigot end + + // Paper start + @SuppressWarnings({"rawtypes", "unchecked"}) + public static java.nio.file.Path dumpHeap(java.nio.file.Path dir, String name) { + try { + java.nio.file.Files.createDirectories(dir); + + javax.management.MBeanServer server = java.lang.management.ManagementFactory.getPlatformMBeanServer(); + java.nio.file.Path file; + + try { + Class clazz = Class.forName("openj9.lang.management.OpenJ9DiagnosticsMXBean"); + Object openj9Mbean = java.lang.management.ManagementFactory.newPlatformMXBeanProxy(server, "openj9.lang.management:type=OpenJ9Diagnostics", clazz); + java.lang.reflect.Method m = clazz.getMethod("triggerDumpToFile", String.class, String.class); + file = dir.resolve(name + ".phd"); + m.invoke(openj9Mbean, "heap", file.toString()); + } catch (ClassNotFoundException e) { + Class clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean"); + Object hotspotMBean = java.lang.management.ManagementFactory.newPlatformMXBeanProxy(server, "com.sun.management:type=HotSpotDiagnostic", clazz); + java.lang.reflect.Method m = clazz.getMethod("dumpHeap", String.class, boolean.class); + file = dir.resolve(name + ".hprof"); + m.invoke(hotspotMBean, file.toString(), true); + } + + return file; + } catch (Throwable t) { + Bukkit.getLogger().log(Level.SEVERE, "Could not write heap", t); + return null; + } + } + // Paper end } diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java index e75ff70c52b097af447b7a91ef2c20c08a904d92..fae37b7b353cb7b0f8d4a79407fcc219a3c45b64 100644 --- a/src/main/java/org/bukkit/craftbukkit/Main.java +++ b/src/main/java/org/bukkit/craftbukkit/Main.java @@ -129,6 +129,14 @@ public class Main { .defaultsTo(new File("spigot.yml")) .describedAs("Yml file"); // Spigot End + + // Paper Start + acceptsAll(asList("paper", "paper-settings"), "File for paper settings") + .withRequiredArg() + .ofType(File.class) + .defaultsTo(new File("paper.yml")) + .describedAs("Yml file"); + // Paper end } }; diff --git a/src/main/java/org/spigotmc/SpigotWorldConfig.java b/src/main/java/org/spigotmc/SpigotWorldConfig.java index a04da0a7d690fe3fcf10810b4e8c92a8ae027b86..feef74e3a6d50344245c4a61ece5b2194af1072f 100644 --- a/src/main/java/org/spigotmc/SpigotWorldConfig.java +++ b/src/main/java/org/spigotmc/SpigotWorldConfig.java @@ -58,8 +58,14 @@ public class SpigotWorldConfig public int getInt(String path, int def) { - this.config.addDefault( "world-settings.default." + path, def ); - return this.config.getInt( "world-settings." + this.worldName + "." + path, this.config.getInt( "world-settings.default." + path ) ); + // Paper start - get int without setting default + return this.getInt(path, def, true); + } + public int getInt(String path, int def, boolean setDef) + { + if (setDef) this.config.addDefault( "world-settings.default." + path, def ); + return this.config.getInt( "world-settings." + this.worldName + "." + path, this.config.getInt( "world-settings.default." + path, def ) ); + // Paper end } public List getList(String path, T def)