Add config GUI

This allows you to modify various settings in-game.
This commit is contained in:
SquidDev 2017-05-01 22:13:25 +01:00
parent 1b562ae837
commit 230b578a98
4 changed files with 111 additions and 11 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
@ -95,6 +99,8 @@ public class ComputerCraft
public static final int pocketComputerGUIID = 106;
// Configuration options
public static Configuration config;
public static boolean http_enable = true;
public static String http_whitelist = "*";
public static boolean disable_lua51_features = false;
@ -192,11 +198,20 @@ public ComputerCraft()
public void preInit( FMLPreInitializationEvent event )
{
// Load config
Configuration config = new Configuration( event.getSuggestedConfigurationFile() );
config = new Configuration( event.getSuggestedConfigurationFile() );
config.load();
// Setup general
syncConfig();
// Setup network
networkEventChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel( "CC" );
networkEventChannel.register( new PacketHandler() );
proxy.preInit();
turtleProxy.preInit();
}
public static void syncConfig() {
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);
@ -261,14 +276,12 @@ public void preInit( FMLPreInitializationEvent event )
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.getCategory( Configuration.CATEGORY_GENERAL ).getOrderedValues())
{
property.setLanguageKey( "gui.computercraft:config." + CaseFormat.LOWER_CAMEL.to( CaseFormat.LOWER_UNDERSCORE, property.getName() ) );
}
config.save();
// Setup network
networkEventChannel = NetworkRegistry.INSTANCE.newEventDrivenChannel( "CC" );
networkEventChannel.register( new PacketHandler() );
proxy.preInit();
turtleProxy.preInit();
}
@Mod.EventHandler

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.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=HTTP enable
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 block
gui.computercraft:config.modem_range=Modem range
gui.computercraft:config.modem_high_altitude_range=Modem range (high-altitude)
gui.computercraft:config.modem_range_during_storm=Modem range (storm)
gui.computercraft:config.modem_high_altitude_range_during_storm=Modem range (high-altitude, storm)
gui.computercraft:config.computer_space_limit=Computer space limit
gui.computercraft:config.floppy_space_limit=Floppy space limit
gui.computercraft:config.turtles_need_fuel=Turtles need 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