Skip to content

Commit

Permalink
Added sponge and javadocs (don't know if the current config path work…
Browse files Browse the repository at this point in the history
…s, we'll see ig)
  • Loading branch information
Abelkrijgtalles committed Dec 7, 2024
1 parent 3424179 commit b8f2af1
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

public interface LoaderInfo {

/**
* Returns the {@link Config}.
*
* @return The Mojang Maps {@link Config}
*/
Config getConfig();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

public interface Config {

/**
* Gets a value from the config.
*
* @param key The key to get the value from. Seperated with a dot when accessing in a group.
* @return The value of the key in the config. Returns null if it doesn't exist.
*/
String get(String key);

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Config getConfig() {
return config;
}

public String defaultConfig(String filename) {
private String defaultConfig(String filename) {

return YamlLikeConfigGenerator.Defaults.PURE_YAML.renderConfig(MojangMaps.getDefaultConfig());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* mojang_maps.sponge.main
* Copyright (C) 2024 Abel van Hulst/Abelkrijgtalles/Abelpro678
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

package nl.abelkrijgtalles.mojangmaps.sponge;

import java.nio.file.Path;
import nl.abelkrijgtalles.mojangmaps.common.MojangMaps;
import nl.abelkrijgtalles.mojangmaps.common.compatibility.LoaderInfo;
import nl.abelkrijgtalles.mojangmaps.common.compatibility.config.Config;
import nl.abelkrijgtalles.mojangmaps.common.compatibility.config.YamlLikeConfigGenerator;

public class LoaderInfoSponge implements LoaderInfo {

private final SpongeConfig.Wrapper config;

public LoaderInfoSponge(Path configPath) {

config = new SpongeConfig.Wrapper(configPath, getDefaultConfig());
}

@Override
public Config getConfig() {

return null;
}

private String getDefaultConfig() {

return YamlLikeConfigGenerator.Defaults.SPONGE.renderConfig(MojangMaps.getDefaultConfig());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* mojang_maps.sponge.main
* Copyright (C) 2024 Abel van Hulst/Abelkrijgtalles/Abelpro678
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

package nl.abelkrijgtalles.mojangmaps.sponge;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import nl.abelkrijgtalles.mojangmaps.common.MojangMaps;
import nl.abelkrijgtalles.mojangmaps.common.compatibility.config.Config;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.configurate.loader.ParsingException;
import org.spongepowered.configurate.serialize.SerializationException;

public class SpongeConfig {

private final HoconConfigurationLoader loader;
private final Path configPath;
private final String defaultConfig;
private CommentedConfigurationNode rootNode;

public SpongeConfig(Path configPath, String defaultConfig) {

this.configPath = configPath;
loader = HoconConfigurationLoader.builder()
.path(configPath)
.build();

this.defaultConfig = defaultConfig;
}

public void loadConfig() {

try {
if (Files.notExists(configPath)) {
saveDefaultConfig();
}

rootNode = loader.load(ConfigurationOptions.defaults());
} catch (ParsingException e) {
MojangMaps.LOGGER.error("Unable to load Sponge config for Mojang Maps");
throw new RuntimeException(e);
}

}

private void saveConfig() {

try {
loader.save(rootNode);
} catch (ConfigurateException e) {
MojangMaps.LOGGER.error("Unable to save Sponge configuration for Mojang Maps.");
throw new RuntimeException(e);
}

}

private void saveDefaultConfig() {

try {
Files.writeString(configPath, defaultConfig);
} catch (IOException e) {
MojangMaps.LOGGER.error("Unable to save default Sponge configuration for Mojang Maps.");
throw new RuntimeException(e);
}

}

public String getString(String key, String defaultValue) {

return rootNode.node((Object[]) key.split("\\.")).getString(defaultValue);
}

public void setString(String key, String value) {

try {
rootNode.node((Object[]) key.split("\\.")).set(value);
} catch (SerializationException e) {
MojangMaps.LOGGER.error("Unable to set key %s to %s for the Mojang Maps Sponge configuration.".formatted(key, value));
throw new RuntimeException(e);
}
}

public void setComment(String key, String comment) {

rootNode.node((Object[]) key.split("\\.")).comment(comment);
}

public static class Wrapper implements Config {

private final SpongeConfig config;

public Wrapper(Path configPath, String defaultConfig) {

config = new SpongeConfig(configPath, defaultConfig);

}

@Override
public String get(String key) {

return config.getString(key, null);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,32 @@

package nl.abelkrijgtalles.mojangmaps.sponge;

import com.google.inject.Inject;
import java.nio.file.Path;
import nl.abelkrijgtalles.mojangmaps.common.MojangMaps;
import org.spongepowered.api.Server;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.plugin.builtin.jvm.Plugin;

@Plugin(MojangMaps.MOD_ID)
public class SpongeMain {

@Inject
@DefaultConfig(sharedRoot = true)
private Path defaultConfig;

@Inject
@DefaultConfig(sharedRoot = true)
private ConfigurationLoader<CommentedConfigurationNode> configLoader;

@Listener
public void onServerStart(final StartedEngineEvent<Server> event) {

MojangMaps.init();
MojangMaps.init(new LoaderInfoSponge(defaultConfig));

}

Expand Down

0 comments on commit b8f2af1

Please sign in to comment.