1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 03:00:30 +00:00

Clean up our handling of configuration files

- Move configuration loading into a separate file, just so it doesn't
   clutter up ComputerCraft.java.
 - Normalise property names, so they're all snake_case.
 - Split properties into separate categories (http, turtle, peripheral),
   so the main one is less cluttered.
 - Define an explicit ordering of each category.
This commit is contained in:
SquidDev 2019-01-05 20:12:02 +00:00
parent 744bba300e
commit 0d6787641a
6 changed files with 394 additions and 256 deletions

View File

@ -6,8 +6,6 @@
package dan200.computercraft;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount;
import dan200.computercraft.api.lua.ILuaAPIFactory;
@ -71,9 +69,6 @@ import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.SidedProxy;
@ -119,8 +114,8 @@ public class ComputerCraft
public static final int viewComputerGUIID = 110;
// Configuration options
private static final String[] DEFAULT_HTTP_WHITELIST = new String[] { "*" };
private static final String[] DEFAULT_HTTP_BLACKLIST = new String[] {
public static final String[] DEFAULT_HTTP_WHITELIST = new String[] { "*" };
public static final String[] DEFAULT_HTTP_BLACKLIST = new String[] {
"127.0.0.0/8",
"10.0.0.0/8",
"172.16.0.0/12",
@ -128,17 +123,27 @@ public class ComputerCraft
"fd00::/8",
};
public static int computerSpaceLimit = 1000 * 1000;
public static int floppySpaceLimit = 125 * 1000;
public static int maximumFilesOpen = 128;
public static boolean disable_lua51_features = false;
public static String default_computer_settings = "";
public static boolean debug_enable = true;
public static int computer_threads = 1;
public static boolean logPeripheralErrors = false;
public static boolean http_enable = true;
public static boolean http_websocket_enable = true;
public static AddressPredicate http_whitelist = new AddressPredicate( DEFAULT_HTTP_WHITELIST );
public static AddressPredicate http_blacklist = new AddressPredicate( DEFAULT_HTTP_BLACKLIST );
public static boolean disable_lua51_features = false;
public static String default_computer_settings = "";
public static boolean debug_enable = false;
public static int computer_threads = 1;
public static boolean logPeripheralErrors = false;
public static boolean enableCommandBlock = false;
public static int modem_range = 64;
public static int modem_highAltitudeRange = 384;
public static int modem_rangeDuringStorm = 64;
public static int modem_highAltitudeRangeDuringStorm = 384;
public static int maxNotesPerTick = 8;
public static boolean turtlesNeedFuel = true;
public static int turtleFuelLimit = 20000;
public static int advancedTurtleFuelLimit = 100000;
@ -155,17 +160,6 @@ public class ComputerCraft
public static final int terminalWidth_pocketComputer = 26;
public static final int terminalHeight_pocketComputer = 20;
public static int modem_range = 64;
public static int modem_highAltitudeRange = 384;
public static int modem_rangeDuringStorm = 64;
public static int modem_highAltitudeRangeDuringStorm = 384;
public static int computerSpaceLimit = 1000 * 1000;
public static int floppySpaceLimit = 125 * 1000;
public static int maximumFilesOpen = 128;
public static int maxNotesPerTick = 8;
// Blocks and Items
public static class Blocks
{
@ -209,40 +203,6 @@ public class ComputerCraft
public static PocketSpeaker pocketSpeaker;
}
public static class Config
{
public static Configuration config;
public static Property http_enable;
public static Property http_websocket_enable;
public static Property http_whitelist;
public static Property http_blacklist;
public static Property disable_lua51_features;
public static Property default_computer_settings;
public static Property debug_enable;
public static Property computer_threads;
public static Property logPeripheralErrors;
public static Property enableCommandBlock;
public static Property turtlesNeedFuel;
public static Property turtleFuelLimit;
public static Property advancedTurtleFuelLimit;
public static Property turtlesObeyBlockProtection;
public static Property turtlesCanPush;
public static Property turtleDisabledActions;
public static Property modem_range;
public static Property modem_highAltitudeRange;
public static Property modem_rangeDuringStorm;
public static Property modem_highAltitudeRangeDuringStorm;
public static Property computerSpaceLimit;
public static Property floppySpaceLimit;
public static Property maximumFilesOpen;
public static Property maxNotesPerTick;
}
// Registries
public static ClientComputerRegistry clientComputerRegistry = new ClientComputerRegistry();
public static ServerComputerRegistry serverComputerRegistry = new ServerComputerRegistry();
@ -275,8 +235,7 @@ public class ComputerCraft
log = event.getModLog();
// Load config
Config.config = new Configuration( event.getSuggestedConfigurationFile() );
loadConfig();
Config.load( event.getSuggestedConfigurationFile() );
// Setup network
networkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel( ComputerCraft.MOD_ID );
@ -285,159 +244,6 @@ public class ComputerCraft
turtleProxy.preInit();
}
public static void loadConfig()
{
Config.config.load();
Config.http_enable = Config.config.get( Configuration.CATEGORY_GENERAL, "http_enable", http_enable );
Config.http_enable.setComment( "Enable the \"http\" API on Computers (see \"http_whitelist\" and \"http_blacklist\" for more fine grained control than this)" );
Config.http_websocket_enable = Config.config.get( Configuration.CATEGORY_GENERAL, "http_websocket_enable", http_websocket_enable );
Config.http_websocket_enable.setComment( "Enable use of http websockets. This requires the \"http_enable\" option to also be true." );
{
ConfigCategory category = Config.config.getCategory( Configuration.CATEGORY_GENERAL );
Property currentProperty = category.get( "http_whitelist" );
if( currentProperty != null && !currentProperty.isList() ) category.remove( "http_whitelist" );
Config.http_whitelist = Config.config.get( Configuration.CATEGORY_GENERAL, "http_whitelist", DEFAULT_HTTP_WHITELIST );
if( currentProperty != null && !currentProperty.isList() )
{
Config.http_whitelist.setValues( currentProperty.getString().split( ";" ) );
}
}
Config.http_whitelist.setComment( "A list of wildcards for domains or IP ranges that can be accessed through the \"http\" API on Computers.\n" +
"Set this to \"*\" to access to the entire internet. Example: \"*.pastebin.com\" will restrict access to just subdomains of pastebin.com.\n" +
"You can use domain names (\"pastebin.com\"), wilcards (\"*.pastebin.com\") or CIDR notation (\"127.0.0.0/8\")." );
Config.http_blacklist = Config.config.get( Configuration.CATEGORY_GENERAL, "http_blacklist", DEFAULT_HTTP_BLACKLIST );
Config.http_blacklist.setComment( "A list of wildcards for domains or IP ranges that cannot be accessed through the \"http\" API on Computers.\n" +
"If this is empty then all whitelisted domains will be accessible. Example: \"*.github.com\" will block access to all subdomains of github.com.\n" +
"You can use domain names (\"pastebin.com\"), wilcards (\"*.pastebin.com\") or CIDR notation (\"127.0.0.0/8\")." );
Config.disable_lua51_features = Config.config.get( Configuration.CATEGORY_GENERAL, "disable_lua51_features", disable_lua51_features );
Config.disable_lua51_features.setComment( "Set this to true to disable Lua 5.1 functions that will be removed in a future update. Useful for ensuring forward compatibility of your programs now." );
Config.default_computer_settings = Config.config.get( Configuration.CATEGORY_GENERAL, "default_computer_settings", default_computer_settings );
Config.default_computer_settings.setComment( "A comma seperated list of default system settings to set on new computers. Example: \"shell.autocomplete=false,lua.autocomplete=false,edit.autocomplete=false\" will disable all autocompletion" );
Config.debug_enable = Config.config.get( Configuration.CATEGORY_GENERAL, "debug_enable", debug_enable );
Config.debug_enable.setComment( "Enable Lua's debug library. Whilst this should be safe for general use, it may allow players to interact with other computers. Enable at your own risk." );
Config.computer_threads = Config.config.get( Configuration.CATEGORY_GENERAL, "computer_threads", computer_threads );
Config.computer_threads
.setMinValue( 1 )
.setRequiresWorldRestart( true )
.setComment( "Set the number of threads computers can run on. A higher number means more computers can run at once, but may induce lag.\n" +
"Please note that some mods may not work with a thread count higher than 1. Use with caution." );
Config.logPeripheralErrors = Config.config.get( Configuration.CATEGORY_GENERAL, "logPeripheralErrors", logPeripheralErrors );
Config.logPeripheralErrors.setComment( "Log exceptions thrown by peripherals and other Lua objects.\n" +
"This makes it easier for mod authors to debug problems, but may result in log spam should people use buggy methods." );
Config.enableCommandBlock = Config.config.get( Configuration.CATEGORY_GENERAL, "enableCommandBlock", enableCommandBlock );
Config.enableCommandBlock.setComment( "Enable Command Block peripheral support" );
Config.modem_range = Config.config.get( Configuration.CATEGORY_GENERAL, "modem_range", modem_range );
Config.modem_range.setComment( "The range of Wireless Modems at low altitude in clear weather, in meters" );
Config.modem_highAltitudeRange = Config.config.get( Configuration.CATEGORY_GENERAL, "modem_highAltitudeRange", modem_highAltitudeRange );
Config.modem_highAltitudeRange.setComment( "The range of Wireless Modems at maximum altitude in clear weather, in meters" );
Config.modem_rangeDuringStorm = Config.config.get( Configuration.CATEGORY_GENERAL, "modem_rangeDuringStorm", modem_rangeDuringStorm );
Config.modem_rangeDuringStorm.setComment( "The range of Wireless Modems at low altitude in stormy weather, in meters" );
Config.modem_highAltitudeRangeDuringStorm = Config.config.get( Configuration.CATEGORY_GENERAL, "modem_highAltitudeRangeDuringStorm", modem_highAltitudeRangeDuringStorm );
Config.modem_highAltitudeRangeDuringStorm.setComment( "The range of Wireless Modems at maximum altitude in stormy weather, in meters" );
Config.computerSpaceLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "computerSpaceLimit", computerSpaceLimit );
Config.computerSpaceLimit.setComment( "The disk space limit for computers and turtles, in bytes" );
Config.floppySpaceLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "floppySpaceLimit", floppySpaceLimit );
Config.floppySpaceLimit.setComment( "The disk space limit for floppy disks, in bytes" );
Config.turtlesNeedFuel = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesNeedFuel", turtlesNeedFuel );
Config.turtlesNeedFuel.setComment( "Set whether Turtles require fuel to move" );
Config.maximumFilesOpen = Config.config.get( Configuration.CATEGORY_GENERAL, "maximumFilesOpen", maximumFilesOpen );
Config.maximumFilesOpen.setComment( "Set how many files a computer can have open at the same time. Set to 0 for unlimited." );
Config.turtleFuelLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "turtleFuelLimit", turtleFuelLimit );
Config.turtleFuelLimit.setComment( "The fuel limit for Turtles" );
Config.advancedTurtleFuelLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "advancedTurtleFuelLimit", advancedTurtleFuelLimit );
Config.advancedTurtleFuelLimit.setComment( "The fuel limit for Advanced Turtles" );
Config.turtlesObeyBlockProtection = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesObeyBlockProtection", turtlesObeyBlockProtection );
Config.turtlesObeyBlockProtection.setComment( "If set to true, Turtles will be unable to build, dig, or enter protected areas (such as near the server spawn point)" );
Config.turtlesCanPush = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesCanPush", turtlesCanPush );
Config.turtlesCanPush.setComment( "If set to true, Turtles will push entities out of the way instead of stopping if there is space to do so" );
Config.turtleDisabledActions = Config.config.get( Configuration.CATEGORY_GENERAL, "turtle_disabled_actions", new String[0] );
Config.turtleDisabledActions.setComment( "A list of turtle actions which are disabled." );
Config.maxNotesPerTick = Config.config.get( Configuration.CATEGORY_GENERAL, "maxNotesPerTick", maxNotesPerTick );
Config.maxNotesPerTick.setComment( "Maximum amount of notes a speaker can play at once" );
for( Property property : Config.config.getCategory( Configuration.CATEGORY_GENERAL ).getOrderedValues() )
{
property.setLanguageKey( "gui.computercraft:config." + CaseFormat.LOWER_CAMEL.to( CaseFormat.LOWER_UNDERSCORE, property.getName() ) );
}
syncConfig();
}
public static void syncConfig()
{
http_enable = Config.http_enable.getBoolean();
http_websocket_enable = Config.http_websocket_enable.getBoolean();
http_whitelist = new AddressPredicate( Config.http_whitelist.getStringList() );
http_blacklist = new AddressPredicate( Config.http_blacklist.getStringList() );
disable_lua51_features = Config.disable_lua51_features.getBoolean();
default_computer_settings = Config.default_computer_settings.getString();
debug_enable = Config.debug_enable.getBoolean();
computer_threads = Config.computer_threads.getInt();
logPeripheralErrors = Config.logPeripheralErrors.getBoolean();
enableCommandBlock = Config.enableCommandBlock.getBoolean();
modem_range = Math.min( Config.modem_range.getInt(), 100000 );
modem_highAltitudeRange = Math.min( Config.modem_highAltitudeRange.getInt(), 100000 );
modem_rangeDuringStorm = Math.min( Config.modem_rangeDuringStorm.getInt(), 100000 );
modem_highAltitudeRangeDuringStorm = Math.min( Config.modem_highAltitudeRangeDuringStorm.getInt(), 100000 );
computerSpaceLimit = Config.computerSpaceLimit.getInt();
floppySpaceLimit = Config.floppySpaceLimit.getInt();
maximumFilesOpen = Math.max( 0, Config.maximumFilesOpen.getInt() );
turtlesNeedFuel = Config.turtlesNeedFuel.getBoolean();
turtleFuelLimit = Config.turtleFuelLimit.getInt();
advancedTurtleFuelLimit = Config.advancedTurtleFuelLimit.getInt();
turtlesObeyBlockProtection = Config.turtlesObeyBlockProtection.getBoolean();
turtlesCanPush = Config.turtlesCanPush.getBoolean();
turtleDisabledActions.clear();
Converter<String, String> converter = CaseFormat.LOWER_CAMEL.converterTo( CaseFormat.UPPER_UNDERSCORE );
for( String value : Config.turtleDisabledActions.getStringList() )
{
try
{
turtleDisabledActions.add( TurtleAction.valueOf( converter.convert( value ) ) );
}
catch( IllegalArgumentException e )
{
ComputerCraft.log.error( "Unknown turtle action " + value );
}
}
maxNotesPerTick = Math.max( 1, Config.maxNotesPerTick.getInt() );
Config.config.save();
}
@Mod.EventHandler
public void init( FMLInitializationEvent event )
{

View File

@ -7,34 +7,19 @@
package dan200.computercraft.client.gui;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.Config;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import net.minecraftforge.fml.client.IModGuiFactory;
import net.minecraftforge.fml.client.config.GuiConfig;
import net.minecraftforge.fml.client.config.IConfigElement;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class GuiConfigCC extends GuiConfig
{
public GuiConfigCC( GuiScreen parentScreen )
{
super( parentScreen, getConfigElements(), ComputerCraft.MOD_ID, false, false, "CC: Tweaked" );
}
private static List<IConfigElement> getConfigElements()
{
ArrayList<IConfigElement> elements = new ArrayList<>();
for( Property property : ComputerCraft.Config.config.getCategory( Configuration.CATEGORY_GENERAL ).getOrderedValues() )
{
elements.add( new ConfigElement( property ) );
}
return elements;
super( parentScreen, Config.getConfigElements(), ComputerCraft.MOD_ID, false, false, "CC: Tweaked" );
}
public static class Factory

View File

@ -0,0 +1,342 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.core.apis.AddressPredicate;
import net.minecraftforge.common.config.ConfigCategory;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import net.minecraftforge.fml.client.config.IConfigElement;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static dan200.computercraft.ComputerCraft.DEFAULT_HTTP_BLACKLIST;
import static dan200.computercraft.ComputerCraft.DEFAULT_HTTP_WHITELIST;
public class Config
{
private static final int MODEM_MAX_RANGE = 100000;
private static final String CATEGORY_GENERAL = "general";
private static final String CATEGORY_HTTP = "http";
private static final String CATEGORY_PERIPHERAL = "peripheral";
private static final String CATEGORY_TURTLE = "turtle";
private static Configuration config;
private static Property httpEnable;
private static Property httpWebsocketEnable;
private static Property httpWhitelist;
private static Property httpBlacklist;
private static Property computerSpaceLimit;
private static Property floppySpaceLimit;
private static Property maximumFilesOpen;
private static Property disableLua51Features;
private static Property defaultComputerSettings;
private static Property debugEnabled;
private static Property computerThreads;
private static Property logComputerErrors;
private static Property turtlesNeedFuel;
private static Property turtleFuelLimit;
private static Property advancedTurtleFuelLimit;
private static Property turtlesObeyBlockProtection;
private static Property turtlesCanPush;
private static Property turtleDisabledActions;
private static Property commandBlockEnabled;
private static Property modemRange;
private static Property modemHighAltitudeRange;
private static Property modemRangeDuringStorm;
private static Property modemHighAltitudeRangeDuringStorm;
private static Property maxNotesPerTick;
public static void load( File configFile )
{
config = new Configuration( configFile );
config.load();
{ // General computers
renameProperty( CATEGORY_GENERAL, "computerSpaceLimit", CATEGORY_GENERAL, "computer_space_limit" );
renameProperty( CATEGORY_GENERAL, "floppySpaceLimit", CATEGORY_GENERAL, "floppy_space_limit" );
renameProperty( CATEGORY_GENERAL, "maximumFilesOpen", CATEGORY_GENERAL, "maximum_open_files" );
renameProperty( CATEGORY_GENERAL, "debug_enable", CATEGORY_GENERAL, "debug_enabled" );
renameProperty( CATEGORY_GENERAL, "logPeripheralErrors", CATEGORY_GENERAL, "log_computer_errors" );
computerSpaceLimit = config.get( CATEGORY_GENERAL, "computer_space_limit", ComputerCraft.computerSpaceLimit );
computerSpaceLimit.setComment( "The disk space limit for computers and turtles, in bytes" );
floppySpaceLimit = config.get( CATEGORY_GENERAL, "floppy_space_limit", ComputerCraft.floppySpaceLimit );
floppySpaceLimit.setComment( "The disk space limit for floppy disks, in bytes" );
maximumFilesOpen = config.get( CATEGORY_GENERAL, "maximum_open_files", ComputerCraft.maximumFilesOpen );
maximumFilesOpen.setComment( "Set how many files a computer can have open at the same time. Set to 0 for unlimited." );
maximumFilesOpen.setMinValue( 0 );
disableLua51Features = config.get( CATEGORY_GENERAL, "disable_lua51_features", ComputerCraft.disable_lua51_features );
disableLua51Features.setComment( "Set this to true to disable Lua 5.1 functions that will be removed in a future update. Useful for ensuring forward compatibility of your programs now." );
defaultComputerSettings = config.get( CATEGORY_GENERAL, "default_computer_settings", ComputerCraft.default_computer_settings );
defaultComputerSettings.setComment( "A comma seperated list of default system settings to set on new computers. Example: \"shell.autocomplete=false,lua.autocomplete=false,edit.autocomplete=false\" will disable all autocompletion" );
debugEnabled = config.get( CATEGORY_GENERAL, "debug_enabled", ComputerCraft.debug_enable );
debugEnabled.setComment( "Enable Lua's debug library. Whilst this should be safe for general use, it may allow players to interact with other computers. Enable at your own risk." );
computerThreads = config.get( CATEGORY_GENERAL, "computer_threads", ComputerCraft.computer_threads );
computerThreads
.setMinValue( 1 )
.setRequiresWorldRestart( true )
.setComment( "Set the number of threads computers can run on. A higher number means more computers can run at once, but may induce lag.\n" +
"Please note that some mods may not work with a thread count higher than 1. Use with caution." );
logComputerErrors = config.get( CATEGORY_GENERAL, "log_computer_errors", ComputerCraft.logPeripheralErrors );
logComputerErrors.setComment( "Log exceptions thrown by peripherals and other Lua objects.\n" +
"This makes it easier for mod authors to debug problems, but may result in log spam should people use buggy methods." );
setOrder(
CATEGORY_GENERAL,
computerSpaceLimit, floppySpaceLimit, maximumFilesOpen,
disableLua51Features, defaultComputerSettings, debugEnabled, computerThreads, logComputerErrors
);
}
{ // HTTP
renameProperty( CATEGORY_GENERAL, "http_enable", CATEGORY_HTTP, "enabled" );
renameProperty( CATEGORY_GENERAL, "http_websocket_enable", CATEGORY_HTTP, "websocket_enabled" );
renameProperty( CATEGORY_GENERAL, "http_whitelist", CATEGORY_HTTP, "whitelist" );
renameProperty( CATEGORY_GENERAL, "http_blacklist", CATEGORY_HTTP, "blacklist" );
httpEnable = config.get( CATEGORY_HTTP, "enabled", ComputerCraft.http_enable );
httpEnable.setComment( "Enable the \"http\" API on Computers (see \"http_whitelist\" and \"http_blacklist\" for more fine grained control than this)" );
httpWebsocketEnable = config.get( CATEGORY_HTTP, "websocket_enabled", ComputerCraft.http_websocket_enable );
httpWebsocketEnable.setComment( "Enable use of http websockets. This requires the \"http_enable\" option to also be true." );
httpWhitelist = config.get( CATEGORY_HTTP, "whitelist", DEFAULT_HTTP_WHITELIST );
httpWhitelist.setComment( "A list of wildcards for domains or IP ranges that can be accessed through the \"http\" API on Computers.\n" +
"Set this to \"*\" to access to the entire internet. Example: \"*.pastebin.com\" will restrict access to just subdomains of pastebin.com.\n" +
"You can use domain names (\"pastebin.com\"), wilcards (\"*.pastebin.com\") or CIDR notation (\"127.0.0.0/8\")." );
httpBlacklist = config.get( CATEGORY_HTTP, "blacklist", DEFAULT_HTTP_BLACKLIST );
httpBlacklist.setComment( "A list of wildcards for domains or IP ranges that cannot be accessed through the \"http\" API on Computers.\n" +
"If this is empty then all whitelisted domains will be accessible. Example: \"*.github.com\" will block access to all subdomains of github.com.\n" +
"You can use domain names (\"pastebin.com\"), wilcards (\"*.pastebin.com\") or CIDR notation (\"127.0.0.0/8\")." );
setOrder(
CATEGORY_HTTP,
httpEnable, httpWebsocketEnable, httpWhitelist, httpBlacklist
);
}
{ // Peripherals
renameProperty( CATEGORY_GENERAL, "enableCommandBlock", CATEGORY_PERIPHERAL, "command_block_enabled" );
renameProperty( CATEGORY_GENERAL, "modem_range", CATEGORY_PERIPHERAL, "modem_range" );
renameProperty( CATEGORY_GENERAL, "modem_highAltitudeRange", CATEGORY_PERIPHERAL, "modem_high_altitude_range" );
renameProperty( CATEGORY_GENERAL, "modem_rangeDuringStorm", CATEGORY_PERIPHERAL, "modem_range_during_storm" );
renameProperty( CATEGORY_GENERAL, "modem_highAltitudeRangeDuringStorm", CATEGORY_PERIPHERAL, "modem_high_altitude_range_during_storm" );
renameProperty( CATEGORY_GENERAL, "maxNotesPerTick", CATEGORY_PERIPHERAL, "max_notes_per_tick" );
commandBlockEnabled = config.get( CATEGORY_PERIPHERAL, "command_block_enabled", ComputerCraft.enableCommandBlock );
commandBlockEnabled.setComment( "Enable Command Block peripheral support" );
modemRange = config.get( CATEGORY_PERIPHERAL, "modem_range", ComputerCraft.modem_range );
modemRange.setComment( "The range of Wireless Modems at low altitude in clear weather, in meters" );
modemRange.setMinValue( 0 );
modemRange.setMaxValue( MODEM_MAX_RANGE );
modemHighAltitudeRange = config.get( CATEGORY_PERIPHERAL, "modem_high_altitude_range", ComputerCraft.modem_highAltitudeRange );
modemHighAltitudeRange.setComment( "The range of Wireless Modems at maximum altitude in clear weather, in meters" );
modemHighAltitudeRange.setMinValue( 0 );
modemHighAltitudeRange.setMaxValue( MODEM_MAX_RANGE );
modemRangeDuringStorm = config.get( CATEGORY_PERIPHERAL, "modem_range_during_storm", ComputerCraft.modem_rangeDuringStorm );
modemRangeDuringStorm.setComment( "The range of Wireless Modems at low altitude in stormy weather, in meters" );
modemRangeDuringStorm.setMinValue( 0 );
modemRangeDuringStorm.setMaxValue( MODEM_MAX_RANGE );
modemHighAltitudeRangeDuringStorm = config.get( CATEGORY_PERIPHERAL, "modem_high_altitude_range_during_storm", ComputerCraft.modem_highAltitudeRangeDuringStorm );
modemHighAltitudeRangeDuringStorm.setComment( "The range of Wireless Modems at maximum altitude in stormy weather, in meters" );
modemHighAltitudeRangeDuringStorm.setMinValue( 0 );
modemHighAltitudeRangeDuringStorm.setMaxValue( MODEM_MAX_RANGE );
maxNotesPerTick = config.get( CATEGORY_PERIPHERAL, "max_notes_per_tick", ComputerCraft.maxNotesPerTick );
maxNotesPerTick.setComment( "Maximum amount of notes a speaker can play at once" );
maxNotesPerTick.setMinValue( 1 );
setOrder(
CATEGORY_PERIPHERAL,
commandBlockEnabled, modemRange, modemHighAltitudeRange, modemRangeDuringStorm, modemHighAltitudeRangeDuringStorm, maxNotesPerTick
);
}
{ // Turtles
renameProperty( CATEGORY_GENERAL, "turtlesNeedFuel", CATEGORY_TURTLE, "need_fuel" );
renameProperty( CATEGORY_GENERAL, "turtleFuelLimit", CATEGORY_TURTLE, "normal_fuel_limit" );
renameProperty( CATEGORY_GENERAL, "advancedTurtleFuelLimit", CATEGORY_TURTLE, "advanced_fuel_limit" );
renameProperty( CATEGORY_GENERAL, "turtlesObeyBlockProtection", CATEGORY_TURTLE, "obey_block_protection" );
renameProperty( CATEGORY_GENERAL, "turtlesCanPush", CATEGORY_TURTLE, "can_push" );
renameProperty( CATEGORY_GENERAL, "turtle_disabled_actions", CATEGORY_TURTLE, "disabled_actions" );
turtlesNeedFuel = config.get( CATEGORY_TURTLE, "need_fuel", ComputerCraft.turtlesNeedFuel );
turtlesNeedFuel.setComment( "Set whether Turtles require fuel to move" );
turtleFuelLimit = config.get( CATEGORY_TURTLE, "normal_fuel_limit", ComputerCraft.turtleFuelLimit );
turtleFuelLimit.setComment( "The fuel limit for Turtles" );
turtleFuelLimit.setMinValue( 0 );
advancedTurtleFuelLimit = config.get( CATEGORY_TURTLE, "advanced_fuel_limit", ComputerCraft.advancedTurtleFuelLimit );
advancedTurtleFuelLimit.setComment( "The fuel limit for Advanced Turtles" );
advancedTurtleFuelLimit.setMinValue( 0 );
turtlesObeyBlockProtection = config.get( CATEGORY_TURTLE, "obey_block_protection", ComputerCraft.turtlesObeyBlockProtection );
turtlesObeyBlockProtection.setComment( "If set to true, Turtles will be unable to build, dig, or enter protected areas (such as near the server spawn point)" );
turtlesCanPush = config.get( CATEGORY_TURTLE, "can_push", ComputerCraft.turtlesCanPush );
turtlesCanPush.setComment( "If set to true, Turtles will push entities out of the way instead of stopping if there is space to do so" );
turtleDisabledActions = config.get( CATEGORY_TURTLE, "disabled_actions", new String[0] );
turtleDisabledActions.setComment( "A list of turtle actions which are disabled." );
setOrder(
CATEGORY_TURTLE,
turtlesNeedFuel, turtleFuelLimit, advancedTurtleFuelLimit, turtlesObeyBlockProtection, turtlesCanPush, turtleDisabledActions
);
}
setupLanguage( config.getCategory( CATEGORY_GENERAL ), "gui.computercraft:config" );
for( String child : config.getCategoryNames() )
{
if( child.equals( CATEGORY_GENERAL ) ) continue;
setupLanguage( config.getCategory( child ), "gui.computercraft:config." + child );
}
sync();
}
private static void setOrder( String category, Property... properties )
{
List<String> names = new ArrayList<>( properties.length );
for( Property property : properties ) names.add( property.getName() );
config.getCategory( category ).setPropertyOrder( names );
}
private static void renameProperty( String oldCat, String oldProp, String newCat, String newProp )
{
if( !config.hasCategory( oldCat ) ) return;
ConfigCategory cat = config.getCategory( oldCat );
if( !cat.containsKey( oldProp ) ) return;
Property prop = cat.remove( oldProp );
prop.setName( newProp );
config.getCategory( newCat ).put( newProp, prop );
// Clean up old categories
if( cat.isEmpty() ) config.removeCategory( cat );
}
private static void setupLanguage( ConfigCategory category, String key )
{
category.setLanguageKey( key );
for( Property property : category.getOrderedValues() )
{
property.setLanguageKey( key + "." + CaseFormat.LOWER_CAMEL.to( CaseFormat.LOWER_UNDERSCORE, property.getName() ) );
}
for( ConfigCategory child : category.getChildren() )
{
setupLanguage( child, key + "." + child.getName() );
}
}
public static void reload()
{
config.load();
sync();
}
public static void sync()
{
// General
ComputerCraft.computerSpaceLimit = computerSpaceLimit.getInt();
ComputerCraft.floppySpaceLimit = floppySpaceLimit.getInt();
ComputerCraft.maximumFilesOpen = Math.max( 0, maximumFilesOpen.getInt() );
ComputerCraft.disable_lua51_features = disableLua51Features.getBoolean();
ComputerCraft.default_computer_settings = defaultComputerSettings.getString();
ComputerCraft.debug_enable = debugEnabled.getBoolean();
ComputerCraft.computer_threads = computerThreads.getInt();
ComputerCraft.logPeripheralErrors = logComputerErrors.getBoolean();
// HTTP
ComputerCraft.http_enable = httpEnable.getBoolean();
ComputerCraft.http_websocket_enable = httpWebsocketEnable.getBoolean();
ComputerCraft.http_whitelist = new AddressPredicate( httpWhitelist.getStringList() );
ComputerCraft.http_blacklist = new AddressPredicate( httpBlacklist.getStringList() );
// Peripheral
ComputerCraft.enableCommandBlock = commandBlockEnabled.getBoolean();
ComputerCraft.maxNotesPerTick = Math.max( 1, maxNotesPerTick.getInt() );
ComputerCraft.modem_range = Math.min( modemRange.getInt(), MODEM_MAX_RANGE );
ComputerCraft.modem_highAltitudeRange = Math.min( modemHighAltitudeRange.getInt(), MODEM_MAX_RANGE );
ComputerCraft.modem_rangeDuringStorm = Math.min( modemRangeDuringStorm.getInt(), MODEM_MAX_RANGE );
ComputerCraft.modem_highAltitudeRangeDuringStorm = Math.min( modemHighAltitudeRangeDuringStorm.getInt(), MODEM_MAX_RANGE );
// Turtles
ComputerCraft.turtlesNeedFuel = turtlesNeedFuel.getBoolean();
ComputerCraft.turtleFuelLimit = turtleFuelLimit.getInt();
ComputerCraft.advancedTurtleFuelLimit = advancedTurtleFuelLimit.getInt();
ComputerCraft.turtlesObeyBlockProtection = turtlesObeyBlockProtection.getBoolean();
ComputerCraft.turtlesCanPush = turtlesCanPush.getBoolean();
ComputerCraft.turtleDisabledActions.clear();
Converter<String, String> converter = CaseFormat.LOWER_CAMEL.converterTo( CaseFormat.UPPER_UNDERSCORE );
for( String value : turtleDisabledActions.getStringList() )
{
try
{
ComputerCraft.turtleDisabledActions.add( TurtleAction.valueOf( converter.convert( value ) ) );
}
catch( IllegalArgumentException e )
{
ComputerCraft.log.error( "Unknown turtle action " + value );
}
}
config.save();
}
public static List<IConfigElement> getConfigElements()
{
ArrayList<IConfigElement> elements = new ArrayList<>();
// Add all child categories
for( String categoryName : config.getCategoryNames() )
{
if( categoryName.equals( CATEGORY_GENERAL ) ) continue;
ConfigCategory category = config.getCategory( categoryName );
elements.add( new ConfigElement( category ) );
}
// Add the general category
for( Property property : config.getCategory( CATEGORY_GENERAL ).getOrderedValues() )
{
elements.add( new ConfigElement( property ) );
}
return elements;
}
}

View File

@ -14,6 +14,7 @@ import dan200.computercraft.core.tracking.ComputerTracker;
import dan200.computercraft.core.tracking.Tracking;
import dan200.computercraft.core.tracking.TrackingContext;
import dan200.computercraft.core.tracking.TrackingField;
import dan200.computercraft.shared.Config;
import dan200.computercraft.shared.command.framework.*;
import dan200.computercraft.shared.command.text.TableBuilder;
import dan200.computercraft.shared.computer.core.ComputerFamily;
@ -381,8 +382,7 @@ public final class CommandComputerCraft extends CommandDelegate
@Override
public void execute( @Nonnull CommandContext context, @Nonnull List<String> arguments )
{
ComputerCraft.loadConfig();
ComputerCraft.syncConfig();
Config.reload();
context.getSender().sendMessage( new TextComponentString( "Reloaded config" ) );
}
} );

View File

@ -11,6 +11,7 @@ import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.client.gui.*;
import dan200.computercraft.core.computer.MainThread;
import dan200.computercraft.shared.Config;
import dan200.computercraft.shared.PocketUpgrades;
import dan200.computercraft.shared.command.CommandComputerCraft;
import dan200.computercraft.shared.command.ContainerViewComputer;
@ -607,10 +608,7 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
@SubscribeEvent
public void onConfigChanged( ConfigChangedEvent.OnConfigChangedEvent event )
{
if( event.getModID().equals( ComputerCraft.MOD_ID ) )
{
ComputerCraft.syncConfig();
}
if( event.getModID().equals( ComputerCraft.MOD_ID ) ) Config.sync();
}
@SubscribeEvent

View File

@ -44,26 +44,33 @@ gui.computercraft:wired_modem.peripheral_disconnected=Peripheral "%s" disconnect
gui.computercraft:tooltip.copy=Copy to clipboard
gui.computercraft:config.http_enable=Enable HTTP API
gui.computercraft:config.http_websocket_enable=Enable HTTP websockets
gui.computercraft:config.http_whitelist=HTTP whitelist
gui.computercraft:config.http_blacklist=HTTP blacklist
gui.computercraft:config.disable_lua51_features=Disable Lua 5.1 features
gui.computercraft:config.default_computer_settings=Default Computer settings
gui.computercraft:config.debug_enable=Enable debug library
gui.computercraft:config.log_peripheral_errors=Log peripheral errors
gui.computercraft:config.enable_command_block=Enable command block peripheral
gui.computercraft:config.modem_range=Modem range (default)
gui.computercraft:config.modem_high_altitude_range=Modem range (high-altitude)
gui.computercraft:config.modem_range_during_storm=Modem range (bad weather)
gui.computercraft:config.modem_high_altitude_range_during_storm=Modem range (high-altitude, bad weather)
gui.computercraft:config.computer_space_limit=Computer space limit (bytes)
gui.computercraft:config.floppy_space_limit=Floppy Disk space limit (bytes)
gui.computercraft:config.turtles_need_fuel=Enable fuel
gui.computercraft:config.turtle_fuel_limit=Turtle fuel limit
gui.computercraft:config.advanced_turtle_fuel_limit=Advanced Turtle fuel limit
gui.computercraft:config.turtles_obey_block_protection=Turtles obey block protection
gui.computercraft:config.turtles_can_push=Turtles can push entities
gui.computercraft:config.turtle_disabled_actions=Disabled turtle actions
gui.computercraft:config.maximum_files_open=Maximum files open per computer
gui.computercraft:config.max_notes_per_tick=Maximum notes that a computer can play at once
gui.computercraft:config.maximum_open_files=Maximum files open per computer
gui.computercraft:config.disable_lua51_features=Disable Lua 5.1 features
gui.computercraft:config.default_computer_settings=Default Computer settings
gui.computercraft:config.debug_enabled=Enable debug library
gui.computercraft:config.computer_threads=Computer threads
gui.computercraft:config.log_computer_errors=Log computer errors
gui.computercraft:config.http=HTTP
gui.computercraft:config.http.enabled=Enable the HTTP API
gui.computercraft:config.http.websocket_enabled=Enable websockets
gui.computercraft:config.http.whitelist=HTTP whitelist
gui.computercraft:config.http.blacklist=HTTP blacklist
gui.computercraft:config.peripheral=Peripherals
gui.computercraft:config.peripheral.command_block_enabled=Enable command block peripheral
gui.computercraft:config.peripheral.modem_range=Modem range (default)
gui.computercraft:config.peripheral.modem_high_altitude_range=Modem range (high-altitude)
gui.computercraft:config.peripheral.modem_range_during_storm=Modem range (bad weather)
gui.computercraft:config.peripheral.modem_high_altitude_range_during_storm=Modem range (high-altitude, bad weather)
gui.computercraft:config.peripheral.max_notes_per_tick=Maximum notes that a computer can play at once
gui.computercraft:config.turtle=Turtles
gui.computercraft:config.turtle.need_fuel=Enable fuel
gui.computercraft:config.turtle.normal_fuel_limit=Turtle fuel limit
gui.computercraft:config.turtle.advanced_fuel_limit=Advanced Turtle fuel limit
gui.computercraft:config.turtle.obey_block_protection=Turtles obey block protection
gui.computercraft:config.turtle.can_push=Turtles can push entities
gui.computercraft:config.turtle.disabled_actions=Disabled turtle actions