Last active
December 23, 2015 05:02
-
-
Save zml2008/e3548cfed7f5525667dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Plugin(...) | |
class ExamplePlugin { | |
@Inject InheritableRoot worldConfig; | |
ObjectMappedInheritableRoot<MyConfig> mapped; | |
@Listener | |
public void enable(GameInitializationEvent event) { | |
mapped = worldConfig.mappedTo(TypeToken.of(MyConfig.class)); | |
} | |
public void moveSettingToGlobal(World world, Object... path) throws IOException { | |
ConfigurationLoader<INheritableNode> loader = worldConfig.getInherited(world); | |
InheritableNode node = loader.load(); | |
InheritableNode child = node.getNode(path); | |
Object val = child.getValue(); | |
if (val != null) { | |
child.setValue(null); | |
child.global().setValue(val); | |
} | |
loader.save(node); | |
mapped.clearCache(); | |
} | |
@Listener | |
public void blockBreak(BlockBreakEVent.Pre event) { // I don't actually know how events work | |
if (worldConfig.get(event.getTargetBlock().getLocation().getExtent()).noBreak) { | |
event.setCancelled(true); | |
} | |
} | |
} | |
@ConfigSerializable | |
class MyConfig { | |
@Setting("no-break") public boolean noBreak = true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ConfigManager { | |
InheritableRoot getRoot(Object pluginContainer); | |
// [snip] | |
} | |
interface InheritableRoot { | |
InheritableNode getInherited(World world); | |
void save(InheritableNode); | |
ObjectMappedInheritableRoot<T> mappedTo(TypeToken<T>); | |
ConfigurationLoader<CommentedConfigurationNode> world(World world); | |
ConfigurationLoader<CommentedConfigurationNode> dimension(Dimension dim); | |
ConfigurationLoader<CommentedConfigurationNode> global(); | |
} | |
interface ObjectMappedInheritableRoot<MappedType> { // caching, loads on demand | |
MappedType get(World world); | |
void clearCache(); // or reload? | |
void save(World world, MappedType); // TODO: Make this load-only -- assume object mapper sets all useful defaults? | |
} | |
class ObjectMappedImpl<MappedType> implements ObjectMappedInheritableRoot<MappedType> { | |
private TypeToken<MappedType> mapperType; | |
private final ObjectMapper<MappedType> mapper; | |
public ObjectMappedImpl(InheritableRoot root, TypeToken<MappedType> mapperType) { | |
this.whatever | |
this.mapper = root.global().getDefaultOptions().getDefaultObjectMapperFactory().newMapper(mapperType); | |
} | |
@Override | |
public MappedType get(World world) throws ObjectMappingException { | |
ConfigurationNode node = getConfig(world); | |
return this.mapper.bindToNew().populate(node); // TODO: Keep track of these and update them (mapper.bind(instance)).populate(node) in save(instance, world)) | |
} | |
public void save(MappedType instance, World world) throws ObjectMappingException, IOException { | |
ConfigurationNode node = getConfig(world); | |
this.mapper.bind(instance).serialize(node); | |
root.getInherited(world).save(node); // update (see in load) | |
} | |
private InheritableNode getConfig(World world) { | |
return new InheritableNode(worldFromCache(world), dimensionFromCache(world.getDimension()), globalFromCache()); | |
} | |
private ConfigurationNode worldFromCache(World world) { | |
// load from root.world(world), unless cached (trigger on save) | |
} | |
// and so on | |
} | |
class InheritableNode implements CommentedConfigurationNode { | |
private final ConfigurationNode world,dimension,global; // These would have to be cached somehow so that all world configs share a dimension and all share a global -- config reference | |
@Override | |
public Object getValue(Object def) { | |
Object ret = world.getValue(); | |
if (ret == null) { | |
ret = dimension.getValue(); | |
} | |
if (ret == null) { | |
ret = global.getValue(def); | |
} | |
/*if (ret == null) { | |
ret = def; // todo check out calculateDef flag? (insert into global configuration) | |
}*/ | |
return ret; | |
} | |
@Override | |
public void setValue(Object val) { | |
if (val == null) { | |
world.setValue(null); | |
dimension.setValue(null); | |
global.setValue(null); | |
} else { | |
if (!world.isVirtual()) { | |
world.setValue(val); | |
} else if (!dimension.isVirtual()) { | |
dimension.setValue(val); | |
} else { | |
global.setValue(val); | |
} | |
} | |
} | |
public CommentedConfigurationNode world() { | |
return world; | |
} | |
public CommentedConfigurationNode dimension() { | |
return dimension; | |
} | |
public CommentedConfigurationNode global() { | |
return global; | |
} | |
@Override | |
public InheritableNode getNode(Object... path) { | |
return new InheritableNode(world.getNode(path), dim.getNode(path), global.getNode(path)); | |
} | |
// TODO: OVerride other relevant getter methods to return the appropriate node type | |
// [snip delegating methods] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment