Merge pull request #166 from SquidDev-CC/feature/config-gui

Add config GUI
This commit is contained in:
Daniel Ratcliffe 2017-05-02 13:46:01 +01:00 committed by GitHub
commit 3a360a50b0
6 changed files with 185 additions and 54 deletions

View File

@ -3,9 +3,10 @@
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft;
import com.google.common.base.CaseFormat;
import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount;
import dan200.computercraft.api.media.IMedia;
@ -82,7 +83,10 @@
// UNIVERSAL //
///////////////
@Mod( modid = "ComputerCraft", name = "ComputerCraft", version = "${version}" )
@Mod(
modid = "ComputerCraft", name = "ComputerCraft", version = "${version}",
guiFactory = "dan200.computercraft.client.gui.GuiConfigCC$Factory"
)
public class ComputerCraft
{
// GUI IDs
@ -158,6 +162,31 @@ public static class Upgrades
public static TurtleModem advancedModem;
}
public static class Config {
public static Configuration config;
public static Property http_enable;
public static Property http_whitelist;
public static Property disable_lua51_features;
public static Property default_computer_settings;
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 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;
}
// Registries
public static ClientComputerRegistry clientComputerRegistry = new ClientComputerRegistry();
public static ServerComputerRegistry serverComputerRegistry = new ServerComputerRegistry();
@ -192,76 +221,63 @@ public ComputerCraft()
public void preInit( FMLPreInitializationEvent event )
{
// Load config
Configuration config = new Configuration( event.getSuggestedConfigurationFile() );
config.load();
Config.config = new Configuration( event.getSuggestedConfigurationFile() );
Config.config.load();
// Setup general
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\" for more fine grained control than this)" );
Property prop = config.get(Configuration.CATEGORY_GENERAL, "http_enable", http_enable);
prop.setComment( "Enable the \"http\" API on Computers (see \"http_whitelist\" for more fine grained control than this)" );
http_enable = prop.getBoolean(http_enable);
Config.http_whitelist = Config.config.get( Configuration.CATEGORY_GENERAL, "http_whitelist", http_whitelist );
Config.http_whitelist.setComment( "A semicolon limited list of wildcards for domains that can be accessed through the \"http\" API on Computers. Set this to \"*\" to access to the entire internet. Example: \"*.pastebin.com;*.github.com;*.computercraft.info\" will restrict access to just those 3 domains." );
prop = config.get(Configuration.CATEGORY_GENERAL, "http_whitelist", http_whitelist );
prop.setComment( "A semicolon limited list of wildcards for domains that can be accessed through the \"http\" API on Computers. Set this to \"*\" to access to the entire internet. Example: \"*.pastebin.com;*.github.com;*.computercraft.info\" will restrict access to just those 3 domains." );
http_whitelist = prop.getString();
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." );
prop = config.get(Configuration.CATEGORY_GENERAL, "disable_lua51_features", disable_lua51_features );
prop.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." );
disable_lua51_features = prop.getBoolean( disable_lua51_features );
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" );
prop = config.get( Configuration.CATEGORY_GENERAL, "default_computer_settings", default_computer_settings );
prop.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" );
default_computer_settings = prop.getString();
Config.enableCommandBlock = Config.config.get( Configuration.CATEGORY_GENERAL, "enableCommandBlock", enableCommandBlock );
Config.enableCommandBlock.setComment( "Enable Command Block peripheral support" );
prop = config.get(Configuration.CATEGORY_GENERAL, "enableCommandBlock", enableCommandBlock);
prop.setComment( "Enable Command Block peripheral support" );
enableCommandBlock = prop.getBoolean(enableCommandBlock);
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" );
prop = config.get(Configuration.CATEGORY_GENERAL, "modem_range", modem_range);
prop.setComment( "The range of Wireless Modems at low altitude in clear weather, in meters" );
modem_range = Math.min( prop.getInt(), 100000 );
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" );
prop = config.get(Configuration.CATEGORY_GENERAL, "modem_highAltitudeRange", modem_highAltitudeRange);
prop.setComment( "The range of Wireless Modems at maximum altitude in clear weather, in meters" );
modem_highAltitudeRange = Math.min( prop.getInt(), 100000 );
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" );
prop = config.get(Configuration.CATEGORY_GENERAL, "modem_rangeDuringStorm", modem_rangeDuringStorm);
prop.setComment( "The range of Wireless Modems at low altitude in stormy weather, in meters" );
modem_rangeDuringStorm = Math.min( prop.getInt(), 100000 );
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" );
prop = config.get(Configuration.CATEGORY_GENERAL, "modem_highAltitudeRangeDuringStorm", modem_highAltitudeRangeDuringStorm);
prop.setComment( "The range of Wireless Modems at maximum altitude in stormy weather, in meters" );
modem_highAltitudeRangeDuringStorm = Math.min( prop.getInt(), 100000 );
Config.computerSpaceLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "computerSpaceLimit", computerSpaceLimit );
Config.computerSpaceLimit.setComment( "The disk space limit for computers and turtles, in bytes" );
prop = config.get(Configuration.CATEGORY_GENERAL, "computerSpaceLimit", computerSpaceLimit);
prop.setComment( "The disk space limit for computers and turtles, in bytes" );
computerSpaceLimit = prop.getInt();
Config.floppySpaceLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "floppySpaceLimit", floppySpaceLimit );
Config.floppySpaceLimit.setComment( "The disk space limit for floppy disks, in bytes" );
prop = config.get(Configuration.CATEGORY_GENERAL, "floppySpaceLimit", floppySpaceLimit);
prop.setComment( "The disk space limit for floppy disks, in bytes" );
floppySpaceLimit = prop.getInt();
Config.turtlesNeedFuel = Config.config.get( Configuration.CATEGORY_GENERAL, "turtlesNeedFuel", turtlesNeedFuel );
Config.turtlesNeedFuel.setComment( "Set whether Turtles require fuel to move" );
prop = config.get(Configuration.CATEGORY_GENERAL, "turtlesNeedFuel", turtlesNeedFuel);
prop.setComment( "Set whether Turtles require fuel to move" );
turtlesNeedFuel = prop.getBoolean( turtlesNeedFuel );
Config.turtleFuelLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "turtleFuelLimit", turtleFuelLimit );
Config.turtleFuelLimit.setComment( "The fuel limit for Turtles" );
prop = config.get(Configuration.CATEGORY_GENERAL, "turtleFuelLimit", turtleFuelLimit);
prop.setComment( "The fuel limit for Turtles" );
turtleFuelLimit = prop.getInt( turtleFuelLimit );
Config.advancedTurtleFuelLimit = Config.config.get( Configuration.CATEGORY_GENERAL, "advancedTurtleFuelLimit", advancedTurtleFuelLimit );
Config.advancedTurtleFuelLimit.setComment( "The fuel limit for Advanced Turtles" );
prop = config.get(Configuration.CATEGORY_GENERAL, "advancedTurtleFuelLimit", advancedTurtleFuelLimit);
prop.setComment( "The fuel limit for Advanced Turtles" );
advancedTurtleFuelLimit = prop.getInt(advancedTurtleFuelLimit);
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)" );
prop = config.get(Configuration.CATEGORY_GENERAL, "turtlesObeyBlockProtection", turtlesObeyBlockProtection);
prop.setComment( "If set to true, Turtles will be unable to build, dig, or enter protected areas (such as near the server spawn point)" );
turtlesObeyBlockProtection = prop.getBoolean( turtlesObeyBlockProtection );
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" );
prop = config.get(Configuration.CATEGORY_GENERAL, "turtlesCanPush", turtlesCanPush);
prop.setComment( "If set to true, Turtles will push entities out of the way instead of stopping if there is space to do so" );
turtlesCanPush = prop.getBoolean(turtlesCanPush);
for (Property property : Config.config.getCategory( Configuration.CATEGORY_GENERAL ).getOrderedValues())
{
property.setLanguageKey( "gui.computercraft:config." + CaseFormat.LOWER_CAMEL.to( CaseFormat.LOWER_UNDERSCORE, property.getName() ) );
}
config.save();
syncConfig();
// Setup network
networkEventChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel( "CC" );
@ -271,6 +287,32 @@ public void preInit( FMLPreInitializationEvent event )
turtleProxy.preInit();
}
public static void syncConfig() {
http_enable = Config.http_enable.getBoolean();
http_whitelist = Config.http_whitelist.getString();
disable_lua51_features = Config.disable_lua51_features.getBoolean();
default_computer_settings = Config.default_computer_settings.getString();
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();
turtlesNeedFuel = Config.turtlesNeedFuel.getBoolean();
turtleFuelLimit = Config.turtleFuelLimit.getInt();
advancedTurtleFuelLimit = Config.advancedTurtleFuelLimit.getInt();
turtlesObeyBlockProtection = Config.turtlesObeyBlockProtection.getBoolean();
turtlesCanPush = Config.turtlesCanPush.getBoolean();
Config.config.save();
}
@Mod.EventHandler
public void init( FMLInitializationEvent event )
{

View File

@ -0,0 +1,61 @@
package dan200.computercraft.client.gui;
import dan200.computercraft.ComputerCraft;
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", false, false, "ComputerCraft" );
}
private static List<IConfigElement> getConfigElements()
{
ArrayList<IConfigElement> elements = new ArrayList<IConfigElement>();
for (Property property : ComputerCraft.Config.config.getCategory( Configuration.CATEGORY_GENERAL ).getOrderedValues())
{
elements.add( new ConfigElement( property ) );
}
return elements;
}
public static class Factory
implements IModGuiFactory
{
@Override
public void initialize( Minecraft minecraft )
{
}
@Override
public Class<? extends GuiScreen> mainConfigGuiClass()
{
return GuiConfigCC.class;
}
@Override
public Set<RuntimeOptionCategoryElement> runtimeGuiCategories()
{
return null;
}
@Override
public RuntimeOptionGuiHandler getHandlerFor( RuntimeOptionCategoryElement runtimeOptionCategoryElement )
{
return null;
}
}
}

View File

@ -71,6 +71,7 @@
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
@ -656,5 +657,13 @@ public void onWorldLoad( WorldEvent.Load event )
public void onWorldUnload( WorldEvent.Unload event )
{
}
@SubscribeEvent
public void onConfigChanged( ConfigChangedEvent.OnConfigChangedEvent event) {
if( event.getModID().equals( "ComputerCraft" ) )
{
ComputerCraft.syncConfig();
}
}
}
}

View File

@ -39,3 +39,20 @@ upgrade.computercraft:advanced_modem.adjective=Ender
gui.computercraft:wired_modem.peripheral_connected=Peripheral "%s" connected to network
gui.computercraft:wired_modem.peripheral_disconnected=Peripheral "%s" disconnected from network
gui.computercraft:config.http_enable=Enable HTTP API
gui.computercraft:config.http_whitelist=HTTP whitelist
gui.computercraft:config.disable_lua51_features=Disable Lua 5.1 features
gui.computercraft:config.default_computer_settings=Default Computer settings
gui.computercraft:config.enable_command_block=Enable command blocks 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

View File

@ -2,6 +2,7 @@ New Features in ComputerCraft 1.80:
* Added .getResponseHeaders() to HTTP responses.
* Return a HTTP response when a HTTP error occurs.
* Added a GUI to change ComputerCraft config options
New Features in ComputerCraft 1.79:

View File

@ -2,5 +2,6 @@ New Features in ComputerCraft 1.80:
* Added .getResponseHeaders() to HTTP responses.
* Return a HTTP response when a HTTP error occurs.
* Added a GUI to change ComputerCraft config options
Type "help changelog" to see the full version history.