Search in sources :

Example 1 with ContentKey

use of com.ichorpowered.guardianapi.content.key.ContentKey in project guardian by ichorpowered.

the class AbstractDetectionContentLoader method acquireAll.

@Override
public void acquireAll(Set<ContentKey<?>> contentKeys) {
    if (this.contentContainer == null)
        return;
    contentKeys.forEach(key -> {
        final Optional<ConfigurationAssignment> assignment = key.getAssignments().stream().filter(contentAssignment -> contentAssignment.getClass().equals(ConfigurationAssignment.class)).map(contentAssignment -> (ConfigurationAssignment) contentAssignment).findFirst();
        if (!assignment.isPresent())
            return;
        final ConfigurationAssignment configurationAssignment = assignment.get();
        final CommentedConfigurationNode node = this.configurationFile.getNode(configurationAssignment.lookup().toArray());
        if (MapValue.class.isAssignableFrom(key.getDefaultValue().getClass())) {
            final Map<Object, Object> collect = Maps.newHashMap();
            if (node.hasMapChildren()) {
                for (final Map.Entry<Object, ? extends ConfigurationNode> entry : node.getChildrenMap().entrySet()) {
                    collect.put(entry.getKey(), entry.getValue().getValue());
                    this.contentContainer.attempt(key, GuardianMapValue.builder((Key) key).defaultElement(collect).element(collect).create());
                }
                return;
            }
        }
        if (Value.class.isAssignableFrom(key.getDefaultValue().getClass())) {
            try {
                Object value = node.getValue(key.getElementToken());
                this.contentContainer.attempt(key, GuardianValue.builder((Key) key).defaultElement(value).element(value).create());
            } catch (ObjectMappingException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : ConfigFile(tech.ferus.util.config.ConfigFile) DetectionContentLoader(com.ichorpowered.guardianapi.detection.DetectionContentLoader) ConfigurationAssignment(com.ichorpowered.guardian.content.assignment.ConfigurationAssignment) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Set(java.util.Set) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Key(com.ichorpowered.guardianapi.util.item.key.Key) GuardianValue(com.ichorpowered.guardian.util.item.mutable.GuardianValue) GuardianMapValue(com.ichorpowered.guardian.util.item.mutable.GuardianMapValue) ContentKey(com.ichorpowered.guardianapi.content.key.ContentKey) Detection(com.ichorpowered.guardianapi.detection.Detection) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Value(com.ichorpowered.guardianapi.util.item.value.mutable.Value) MapValue(com.ichorpowered.guardianapi.util.item.value.mutable.MapValue) Map(java.util.Map) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) ContentContainer(com.ichorpowered.guardianapi.content.ContentContainer) Optional(java.util.Optional) Path(java.nio.file.Path) ConfigurationAssignment(com.ichorpowered.guardian.content.assignment.ConfigurationAssignment) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Map(java.util.Map) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 2 with ContentKey

use of com.ichorpowered.guardianapi.content.key.ContentKey in project guardian by ichorpowered.

the class AbstractDetectionContentLoader method acquireSingle.

@Override
public void acquireSingle(ContentKey<?> key) {
    if (this.contentContainer == null)
        return;
    final Optional<ConfigurationAssignment> assignment = key.getAssignments().stream().filter(contentAssignment -> contentAssignment.getClass().equals(ConfigurationAssignment.class)).map(contentAssignment -> (ConfigurationAssignment) contentAssignment).findFirst();
    if (!assignment.isPresent())
        return;
    final ConfigurationAssignment configurationAssignment = assignment.get();
    final CommentedConfigurationNode node = this.configurationFile.getNode(configurationAssignment.lookup().toArray());
    if (MapValue.class.isAssignableFrom(key.getDefaultValue().getClass())) {
        final Map<Object, Object> collect = Maps.newHashMap();
        if (node.hasMapChildren()) {
            for (final Map.Entry<Object, ? extends ConfigurationNode> entry : node.getChildrenMap().entrySet()) {
                collect.put(entry.getKey(), entry.getValue().getValue());
                this.contentContainer.attempt(key, GuardianMapValue.builder((Key) key).defaultElement(collect).element(collect).create());
            }
            return;
        }
    }
    if (Value.class.isAssignableFrom(key.getDefaultValue().getClass())) {
        try {
            Object value = node.getValue(key.getElementToken());
            this.contentContainer.attempt(key, GuardianValue.builder((Key) key).defaultElement(value).element(value).create());
        } catch (ObjectMappingException e) {
            e.printStackTrace();
        }
    }
}
Also used : ConfigFile(tech.ferus.util.config.ConfigFile) DetectionContentLoader(com.ichorpowered.guardianapi.detection.DetectionContentLoader) ConfigurationAssignment(com.ichorpowered.guardian.content.assignment.ConfigurationAssignment) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Set(java.util.Set) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Key(com.ichorpowered.guardianapi.util.item.key.Key) GuardianValue(com.ichorpowered.guardian.util.item.mutable.GuardianValue) GuardianMapValue(com.ichorpowered.guardian.util.item.mutable.GuardianMapValue) ContentKey(com.ichorpowered.guardianapi.content.key.ContentKey) Detection(com.ichorpowered.guardianapi.detection.Detection) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Value(com.ichorpowered.guardianapi.util.item.value.mutable.Value) MapValue(com.ichorpowered.guardianapi.util.item.value.mutable.MapValue) Map(java.util.Map) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) ContentContainer(com.ichorpowered.guardianapi.content.ContentContainer) Optional(java.util.Optional) Path(java.nio.file.Path) ConfigurationAssignment(com.ichorpowered.guardian.content.assignment.ConfigurationAssignment) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Map(java.util.Map) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Aggregations

Maps (com.google.common.collect.Maps)2 ConfigurationAssignment (com.ichorpowered.guardian.content.assignment.ConfigurationAssignment)2 GuardianMapValue (com.ichorpowered.guardian.util.item.mutable.GuardianMapValue)2 GuardianValue (com.ichorpowered.guardian.util.item.mutable.GuardianValue)2 ContentContainer (com.ichorpowered.guardianapi.content.ContentContainer)2 ContentKey (com.ichorpowered.guardianapi.content.key.ContentKey)2 Detection (com.ichorpowered.guardianapi.detection.Detection)2 DetectionContentLoader (com.ichorpowered.guardianapi.detection.DetectionContentLoader)2 Key (com.ichorpowered.guardianapi.util.item.key.Key)2 MapValue (com.ichorpowered.guardianapi.util.item.value.mutable.MapValue)2 Value (com.ichorpowered.guardianapi.util.item.value.mutable.Value)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)2 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)2 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)2 ConfigFile (tech.ferus.util.config.ConfigFile)2