|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 GeyserMC. http://geysermc.org |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + * @author GeyserMC |
| 23 | + * @link https://github.com/GeyserMC/Geyser |
| 24 | + */ |
| 25 | + |
| 26 | +package org.geysermc.geyser.configuration; |
| 27 | + |
| 28 | +import org.spongepowered.configurate.ConfigurationNode; |
| 29 | +import org.spongepowered.configurate.NodePath; |
| 30 | +import org.spongepowered.configurate.interfaces.InterfaceDefaultOptions; |
| 31 | +import org.spongepowered.configurate.transformation.ConfigurationTransformation; |
| 32 | +import org.spongepowered.configurate.yaml.YamlConfigurationLoader; |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.io.IOException; |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
| 38 | + |
| 39 | +public class ConfigLoaderTemp { |
| 40 | + private static final String HEADER = """ |
| 41 | + -------------------------------- |
| 42 | + Geyser Configuration File |
| 43 | +
|
| 44 | + A bridge between Minecraft: Bedrock Edition and Minecraft: Java Edition. |
| 45 | +
|
| 46 | + GitHub: https://github.com/GeyserMC/Geyser |
| 47 | + Discord: https://discord.gg/geysermc |
| 48 | + Wiki: https://wiki.geysermc.org/ |
| 49 | +
|
| 50 | + NOTICE: See https://wiki.geysermc.org/geyser/setup/ for the setup guide. Many video tutorials are outdated. |
| 51 | + In most cases, especially with server hosting providers, further hosting-specific configuration is required. |
| 52 | + --------------------------------"""; |
| 53 | + |
| 54 | + public static <T extends GeyserConfig> T load(Class<T> configClass) throws IOException { |
| 55 | + var loader = YamlConfigurationLoader.builder() |
| 56 | + .file(new File("newconfig.yml")) |
| 57 | + .defaultOptions(InterfaceDefaultOptions.get() |
| 58 | + .header(HEADER)) |
| 59 | + .build(); |
| 60 | + ConfigurationNode node = loader.load(); |
| 61 | + // temp fix for node.virtual() being broken |
| 62 | + var virtual = !Files.exists(Path.of("newconfig.yml")); |
| 63 | + |
| 64 | + // TODO needed or else Configurate breaks |
| 65 | + var migrations = ConfigurationTransformation.versionedBuilder() |
| 66 | + // Pre-Configurate |
| 67 | + .addVersion(5, ConfigurationTransformation.builder() |
| 68 | + .addAction(NodePath.path("legacyPingPassthrough"), (path, value) -> { |
| 69 | + // Invert value |
| 70 | + value.set(Boolean.FALSE.equals(value.get(boolean.class))); |
| 71 | + return new Object[]{"integratedPingPassthrough"}; |
| 72 | + }) |
| 73 | + .addAction(NodePath.path("remote"), (path, value) -> |
| 74 | + new Object[]{"java"}) |
| 75 | + .build()) |
| 76 | + .build(); |
| 77 | + |
| 78 | + migrations.apply(node); |
| 79 | + |
| 80 | + T config = node.get(configClass); |
| 81 | + System.out.println(config); |
| 82 | + |
| 83 | + loader.save(node); |
| 84 | + |
| 85 | + return config; |
| 86 | + } |
| 87 | +} |
0 commit comments