mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-26 06:53:20 +00:00
Add an annotation for @Config elements which will automatically create a slider control (#5026)
This commit is contained in:
1 parent
359d4fe2d5
commit
d1478ca529
11 files changed
+163
-13
No files matched your search
@@ -116,4 +116,12 @@ public @interface Config
|
||||
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||
@interface RequiresWorldRestart
|
||||
{}
|
||||
|
||||
/**
|
||||
* A field marked with this annotation (and {@link RangeInt} or {@link RangeDouble}) will have a slider control attached in the config UI
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface SlidingOption
|
||||
{}
|
||||
}
|
||||
@@ -366,7 +366,13 @@ public class ConfigElement implements IConfigElement
|
||||
{
|
||||
return isProperty ? prop.getMaxValue() : null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasSlidingControl() {
|
||||
return prop.hasSlidingControl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a ConfigElement derived from the annotation-based config system
|
||||
* @param configClass the class which contains the configuration
|
||||
|
||||
@@ -134,7 +134,7 @@ public class ConfigManager
|
||||
* does not exist, it will be created with default values derived from the mods config classes variable default values
|
||||
* and comments and ranges, as well as configuration names based on the appropriate annotations found in {@code @Config}.
|
||||
*
|
||||
* Note, that this method is being called by the {@link FMLModContaier}, so the mod needn't call it in init().
|
||||
* Note, that this method is being called by the {@link net.minecraftforge.fml.common.FMLModContainer}, so the mod needn't call it in init().
|
||||
*
|
||||
* If this method is called after the initial load, it will check whether the values in the Configuration object differ
|
||||
* from the values in the corresponding variables. If they differ, it will either overwrite the variables if the Configuration
|
||||
@@ -240,6 +240,7 @@ public class ConfigManager
|
||||
|
||||
boolean requiresMcRestart = f.isAnnotationPresent(Config.RequiresMcRestart.class);
|
||||
boolean requiresWorldRestart = f.isAnnotationPresent(Config.RequiresWorldRestart.class);
|
||||
boolean hasSlidingControl = f.isAnnotationPresent(Config.SlidingOption.class);
|
||||
|
||||
if (FieldWrapper.hasWrapperFor(f)) //Wrappers exist for primitives, enums, maps and arrays
|
||||
{
|
||||
@@ -259,7 +260,6 @@ public class ConfigManager
|
||||
if (!existed || loading) //Creates keys in category specified by the wrapper if new ones are programaticaly added
|
||||
{
|
||||
Property property = property(cfg, wrapper.getCategory(), suffix, propType, adapt.isArrayAdapter());
|
||||
|
||||
adapt.setDefaultValue(property, wrapper.getValue(key));
|
||||
if (!existed)
|
||||
adapt.setValue(property, wrapper.getValue(key));
|
||||
@@ -295,7 +295,7 @@ public class ConfigManager
|
||||
}
|
||||
|
||||
if (loading)
|
||||
wrapper.setupConfiguration(cfg, comment, langKey, requiresMcRestart, requiresWorldRestart);
|
||||
wrapper.setupConfiguration(cfg, comment, langKey, requiresMcRestart, requiresWorldRestart, hasSlidingControl);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -176,6 +176,12 @@ public abstract class FieldWrapper implements IFieldWrapper
|
||||
|
||||
@Override
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart)
|
||||
{
|
||||
this.setupConfiguration(cfg, desc, langKey, reqMCRestart, reqWorldRestart, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart, boolean hasSlidingControl)
|
||||
{
|
||||
ConfigCategory confCat = cfg.getCategory(getCategory());
|
||||
confCat.setComment(desc);
|
||||
@@ -251,11 +257,16 @@ public abstract class FieldWrapper implements IFieldWrapper
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart) {
|
||||
this.setupConfiguration(cfg, desc, langKey, reqMCRestart, reqWorldRestart,false);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Override
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart)
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart, boolean hasSlidingControl)
|
||||
{
|
||||
super.setupConfiguration(cfg, desc, langKey, reqMCRestart, reqWorldRestart);
|
||||
super.setupConfiguration(cfg, desc, langKey, reqMCRestart, reqWorldRestart, hasSlidingControl);
|
||||
|
||||
Property prop = cfg.getCategory(this.category).get(this.name); // Will be setup in general by ConfigManager
|
||||
|
||||
@@ -325,9 +336,14 @@ public abstract class FieldWrapper implements IFieldWrapper
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart)
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart) {
|
||||
this.setupConfiguration(cfg, desc, langKey, reqMCRestart, reqWorldRestart,false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart, boolean hasSlidingControl)
|
||||
{
|
||||
super.setupConfiguration(cfg, desc, langKey, reqMCRestart, reqWorldRestart);
|
||||
super.setupConfiguration(cfg, desc, langKey, reqMCRestart, reqWorldRestart, hasSlidingControl);
|
||||
|
||||
Property prop = cfg.getCategory(this.category).get(this.name);
|
||||
|
||||
@@ -340,6 +356,7 @@ public abstract class FieldWrapper implements IFieldWrapper
|
||||
prop.setComment(NEW_LINE.join(new String[] { desc, "Min: " + ia.min(), "Max: " + ia.max() }));
|
||||
else
|
||||
prop.setComment(NEW_LINE.join(new String[] { "Min: " + ia.min(), "Max: " + ia.max() }));
|
||||
prop.setHasSlidingControl(hasSlidingControl);
|
||||
}
|
||||
|
||||
RangeDouble da = field.getAnnotation(RangeDouble.class);
|
||||
@@ -351,6 +368,7 @@ public abstract class FieldWrapper implements IFieldWrapper
|
||||
prop.setComment(NEW_LINE.join(new String[] { desc, "Min: " + da.min(), "Max: " + da.max() }));
|
||||
else
|
||||
prop.setComment(NEW_LINE.join(new String[] { "Min: " + da.min(), "Max: " + da.max() }));
|
||||
prop.setHasSlidingControl(hasSlidingControl);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -381,7 +399,7 @@ public abstract class FieldWrapper implements IFieldWrapper
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart)
|
||||
public void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart, boolean hasSlidingControl)
|
||||
{
|
||||
Property prop = cfg.getCategory(this.category).get(this.name); // Will be setup in general by ConfigManager
|
||||
|
||||
@@ -389,6 +407,7 @@ public abstract class FieldWrapper implements IFieldWrapper
|
||||
prop.setLanguageKey(langKey);
|
||||
prop.setRequiresMcRestart(reqMCRestart);
|
||||
prop.setRequiresWorldRestart(reqWorldRestart);
|
||||
prop.setHasSlidingControl(hasSlidingControl);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -62,13 +62,25 @@ public interface IFieldWrapper
|
||||
boolean handlesKey(String key);
|
||||
|
||||
/**
|
||||
* @see IFieldWrapper#setupConfiguration(Configuration, String, String, boolean, boolean, boolean)
|
||||
*/
|
||||
@Deprecated
|
||||
void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param cfg The configuration object holding the properties
|
||||
* @param desc The properties description
|
||||
* @param langKey The languageKey of the property, used in GUI
|
||||
* @param reqMCRestart True, if a change in this property requires a restart of Minecraft
|
||||
* @param reqWorldRestart True, if the world needs to be reloaded after changes to this property
|
||||
* @param hasSlidingControl true if the property is going to have a slider control attached in the configuration UI;
|
||||
* works in conjunction with {@link Config.RangeInt} and {@link Config.RangeDouble}
|
||||
*/
|
||||
void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart);
|
||||
default void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart, boolean hasSlidingControl)
|
||||
{
|
||||
setupConfiguration(cfg, desc, langKey, reqMCRestart, reqWorldRestart);
|
||||
}
|
||||
|
||||
/**
|
||||
* i.e. general.map in the example above
|
||||
|
||||
@@ -82,6 +82,8 @@ public class Property
|
||||
private String langKey;
|
||||
private String minValue;
|
||||
private String maxValue;
|
||||
/** If true, will have a slider attached automatically in configuration UI */
|
||||
private boolean hasSlidingControl;
|
||||
|
||||
private Class<? extends IConfigEntry> configEntryClass = null;
|
||||
private Class<? extends IArrayEntry> arrayEntryClass = null;
|
||||
@@ -1260,4 +1262,14 @@ public class Property
|
||||
public void set(long value){ set(Long.toString(value)); }
|
||||
public void set(boolean value){ set(Boolean.toString(value)); }
|
||||
public void set(double value){ set(Double.toString(value)); }
|
||||
|
||||
public boolean hasSlidingControl()
|
||||
{
|
||||
return hasSlidingControl;
|
||||
}
|
||||
|
||||
public void setHasSlidingControl(boolean b)
|
||||
{
|
||||
hasSlidingControl=b;
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ public class GuiConfig extends GuiScreen
|
||||
/**
|
||||
* This constructor handles the {@code @Config} configuration classes
|
||||
* @param parentScreen the parent GuiScreen object
|
||||
* @param mod the mod for which to create a screen
|
||||
* @param modid the mod for which to create a screen
|
||||
*/
|
||||
public GuiConfig(GuiScreen parentScreen, String modid, String title)
|
||||
{
|
||||
|
||||
@@ -138,9 +138,19 @@ public class GuiConfigEntries extends GuiListExtended
|
||||
else if (configElement.getType() == ConfigGuiType.BOOLEAN)
|
||||
this.listEntries.add(new GuiConfigEntries.BooleanEntry(this.owningScreen, this, configElement));
|
||||
else if (configElement.getType() == ConfigGuiType.INTEGER)
|
||||
this.listEntries.add(new GuiConfigEntries.IntegerEntry(this.owningScreen, this, configElement));
|
||||
{
|
||||
if(configElement.hasSlidingControl())
|
||||
listEntries.add(new GuiConfigEntries.NumberSliderEntry(owningScreen,this,configElement));
|
||||
else
|
||||
this.listEntries.add(new GuiConfigEntries.IntegerEntry(this.owningScreen, this, configElement));
|
||||
}
|
||||
else if (configElement.getType() == ConfigGuiType.DOUBLE)
|
||||
this.listEntries.add(new GuiConfigEntries.DoubleEntry(this.owningScreen, this, configElement));
|
||||
{
|
||||
if(configElement.hasSlidingControl())
|
||||
listEntries.add(new NumberSliderEntry(owningScreen,this,configElement));
|
||||
else
|
||||
this.listEntries.add(new GuiConfigEntries.DoubleEntry(this.owningScreen, this, configElement));
|
||||
}
|
||||
else if (configElement.getType() == ConfigGuiType.COLOR)
|
||||
{
|
||||
if (configElement.getValidValues() != null && configElement.getValidValues().length > 0)
|
||||
|
||||
@@ -192,4 +192,12 @@ public interface IConfigElement
|
||||
* [Property] Gets a Pattern object used in String property input validation.
|
||||
*/
|
||||
Pattern getValidationPattern();
|
||||
|
||||
/**
|
||||
* @return true if this element is going to have a slider attached
|
||||
*/
|
||||
default boolean hasSlidingControl()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* 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.test;
|
||||
|
||||
import net.minecraftforge.common.config.Config;
|
||||
import net.minecraftforge.common.config.ConfigManager;
|
||||
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
|
||||
@Mod.EventBusSubscriber
|
||||
@Mod(modid = SliderConfigElementTest.ID,name = "Slider Config Element Test",version = "0.0")
|
||||
public class SliderConfigElementTest {
|
||||
|
||||
static final String ID="slider_config_element";
|
||||
|
||||
@Config(modid = ID)
|
||||
public static class Configuration
|
||||
{
|
||||
@Config.SlidingOption
|
||||
@Config.RangeDouble(min = 0,max = 1)
|
||||
public static double dble = 0.5;
|
||||
|
||||
@Config.SlidingOption
|
||||
@Config.RangeInt(min = -100,max = 50)
|
||||
public static int integer = 0;
|
||||
|
||||
@Config.SlidingOption
|
||||
@Config.RangeDouble(min = 15.6f,max = 344.78f)
|
||||
public static float f = 60.7f;
|
||||
|
||||
//this won't do anything
|
||||
@Config.SlidingOption
|
||||
public static boolean bool = false;
|
||||
|
||||
//this also won't do anything
|
||||
@Config.SlidingOption
|
||||
public static String string = "I don't slide";
|
||||
|
||||
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void optionsChanged(ConfigChangedEvent.OnConfigChangedEvent configChangedEvent)
|
||||
{
|
||||
if(configChangedEvent.getModID().equals(ID))
|
||||
{
|
||||
ConfigManager.sync(ID,Config.Type.INSTANCE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,5 +12,11 @@
|
||||
"logoFile": "",
|
||||
"screenshots": [],
|
||||
"dependencies": []
|
||||
},
|
||||
{
|
||||
"modid": "slider_config_element",
|
||||
"name": "Slider Config Element Test",
|
||||
"description": "",
|
||||
"version": "0.0"
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user