2019-01-29 03:42:37 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2019-02-10 22:57:03 +00:00
|
|
|
* Copyright (c) 2016-2019.
|
2019-01-29 03:42:37 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.minecraftforge.fml.config;
|
|
|
|
|
2019-02-16 02:45:28 +00:00
|
|
|
import com.electronwill.nightconfig.core.CommentedConfig;
|
2019-01-29 03:42:37 +00:00
|
|
|
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
|
|
|
import net.minecraftforge.common.ForgeConfigSpec;
|
|
|
|
import net.minecraftforge.eventbus.api.Event;
|
|
|
|
import net.minecraftforge.fml.ModContainer;
|
|
|
|
import net.minecraftforge.fml.loading.StringUtils;
|
|
|
|
|
2019-02-16 02:45:28 +00:00
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
2019-01-29 03:42:37 +00:00
|
|
|
public class ModConfig
|
|
|
|
{
|
|
|
|
private final Type type;
|
|
|
|
private final ForgeConfigSpec spec;
|
|
|
|
private final String fileName;
|
|
|
|
private final ModContainer container;
|
|
|
|
private final ConfigFileTypeHandler configHandler;
|
2019-02-16 02:45:28 +00:00
|
|
|
private CommentedConfig configData;
|
|
|
|
private Callable<Void> saveHandler;
|
2019-01-29 03:42:37 +00:00
|
|
|
|
|
|
|
public ModConfig(final Type type, final ForgeConfigSpec spec, final ModContainer container, final String fileName) {
|
|
|
|
this.type = type;
|
|
|
|
this.spec = spec;
|
|
|
|
this.fileName = fileName;
|
|
|
|
this.container = container;
|
|
|
|
this.configHandler = ConfigFileTypeHandler.TOML;
|
|
|
|
ConfigTracker.INSTANCE.trackConfig(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ModConfig(final Type type, final ForgeConfigSpec spec, final ModContainer activeContainer) {
|
|
|
|
this(type, spec, activeContainer, defaultConfigName(type, activeContainer.getModId()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String defaultConfigName(Type type, String modId) {
|
|
|
|
// config file name would be "forge-client.toml" and "forge-server.toml"
|
|
|
|
return String.format("%s-%s.toml", modId, type.extension());
|
|
|
|
}
|
|
|
|
public Type getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFileName() {
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ConfigFileTypeHandler getHandler() {
|
|
|
|
return configHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ForgeConfigSpec getSpec() {
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getModId() {
|
|
|
|
return container.getModId();
|
|
|
|
}
|
|
|
|
|
2019-02-16 02:45:28 +00:00
|
|
|
public CommentedConfig getConfigData() {
|
2019-01-29 03:42:37 +00:00
|
|
|
return this.configData;
|
|
|
|
}
|
|
|
|
|
2019-02-16 02:45:28 +00:00
|
|
|
void setConfigData(final CommentedConfig configData) {
|
2019-01-29 03:42:37 +00:00
|
|
|
this.configData = configData;
|
|
|
|
this.spec.setConfig(this.configData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fireEvent(final ModConfigEvent configEvent) {
|
|
|
|
this.container.dispatchConfigEvent(configEvent);
|
|
|
|
}
|
|
|
|
|
2019-02-16 02:45:28 +00:00
|
|
|
public void save() {
|
|
|
|
((CommentedFileConfig)this.configData).save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Path getFullPath() {
|
|
|
|
return ((CommentedFileConfig)this.configData).getNioPath();
|
|
|
|
}
|
|
|
|
|
2019-01-29 03:42:37 +00:00
|
|
|
public enum Type {
|
|
|
|
/**
|
2019-02-10 21:38:14 +00:00
|
|
|
* Common mod config for configuration that needs to be loaded on both environments.
|
|
|
|
* Loaded on both servers and clients.
|
|
|
|
* Stored in the global config directory.
|
|
|
|
* Not synced.
|
|
|
|
* Suffix is "-common" by default.
|
|
|
|
*/
|
|
|
|
COMMON,
|
|
|
|
/**
|
|
|
|
* Client config is for configuration affecting the ONLY client state such as graphical options.
|
|
|
|
* Only loaded on the client side.
|
|
|
|
* Stored in the global config directory.
|
|
|
|
* Not synced.
|
|
|
|
* Suffix is "-client" by default.
|
2019-01-29 03:42:37 +00:00
|
|
|
*/
|
|
|
|
CLIENT,
|
|
|
|
// /**
|
|
|
|
// * Player type config is configuration that is associated with a player.
|
|
|
|
// * Preferences around machine states, for example.
|
|
|
|
// */
|
|
|
|
// PLAYER,
|
|
|
|
/**
|
|
|
|
* Server type config is configuration that is associated with a server instance.
|
2019-02-10 21:38:14 +00:00
|
|
|
* Only loaded during server startup.
|
|
|
|
* Stored in a server/save specific "serverconfig" directory.
|
|
|
|
* Synced to clients during connection.
|
|
|
|
* Suffix is "-server" by default.
|
2019-01-29 03:42:37 +00:00
|
|
|
*/
|
|
|
|
SERVER;
|
|
|
|
|
|
|
|
public String extension() {
|
|
|
|
return StringUtils.toLowerCase(name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ModConfigEvent extends Event {
|
|
|
|
private final ModConfig config;
|
|
|
|
|
|
|
|
ModConfigEvent(final ModConfig config) {
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ModConfig getConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Loading extends ModConfigEvent {
|
|
|
|
Loading(final ModConfig config) {
|
|
|
|
super(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:09:58 +00:00
|
|
|
public static class Reloading extends ModConfigEvent {
|
|
|
|
Reloading(final ModConfig config) {
|
2019-01-29 03:42:37 +00:00
|
|
|
super(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|