Skip to content

Instantly share code, notes, and snippets.

@SocraticPhoenix
Created June 17, 2016 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SocraticPhoenix/02432a44525aaf278b7a93c16b4944d1 to your computer and use it in GitHub Desktop.
Save SocraticPhoenix/02432a44525aaf278b7a93c16b4944d1 to your computer and use it in GitHub Desktop.
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 socraticphoenix@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @author Socratic_Phoenix (socraticphoenix@gmail.com)
*/
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.util.CollectionUtils;
import org.spongepowered.api.util.ResettableBuilder;
import java.util.HashMap;
import java.util.Map;
public class ConfigurationTranslator<T> {
private static HoconConfigurationLoader generator;
static {
generator = HoconConfigurationLoader.builder().build();
}
private Map<String, String> keyTranslations; //Original -> new key | mappings
private Class<T> target;
private int content;
public ConfigurationTranslator(Map<String, String> keyTranslations, Class<T> target, int content) {
this.keyTranslations = keyTranslations;
this.target = target;
this.content = content;
}
public static <V> Builder<V> builder(Class<V> target, int content) {
return new Builder<>(target, content);
}
public static ConfigurationTranslator<ItemStack> itemStackTranslator() {
return ConfigurationTranslator.builder(ItemStack.class, 1)
.transfer("UnsafeData", "tag")
.transfer("ItemType", "id")
.transfer("UnsafeDamage", "damage")
.transfer("Count", "count")
.build();
}
public ConfigurationNode convertObject(T in) throws ObjectMappingException {
ConfigurationNode node = ConfigurationTranslator.generator.createEmptyNode();
node.setValue(TypeToken.of(this.target), in);
ConfigurationNode out = ConfigurationTranslator.generator.createEmptyNode();
this.keyTranslations.entrySet().forEach(e -> out.getNode(e.getValue()).setValue(node.getNode(e.getKey())));
return out;
}
public T convertNode(ConfigurationNode in) throws ObjectMappingException {
ConfigurationNode out = ConfigurationTranslator.generator.createEmptyNode();
this.keyTranslations.entrySet().forEach(e -> out.getNode(e.getKey()).setValue(in.getNode(e.getValue())));
out.getNode("ContentVersion").setValue(this.content);
return out.getValue(TypeToken.of(this.target));
}
public static class Builder<U> implements ResettableBuilder<ConfigurationTranslator<U>, Builder<U>> {
private Map<String, String> keyTranslations;
private Class<U> target;
private int content;
public Builder(Class<U> target, int content) {
this.target = target;
this.keyTranslations = new HashMap<>();
this.content = content;
}
public Builder transfer(String originalKey, String newKey) {
this.keyTranslations.put(originalKey, newKey);
return this;
}
public ConfigurationTranslator<U> build() {
//copy the map so that the build can potentially continue being used after this build
return new ConfigurationTranslator<>(CollectionUtils.copyMap(this.keyTranslations), this.target, this.content);
}
@Override
public Builder<U> from(ConfigurationTranslator<U> value) {
if (value.target.equals(this.target)) {
this.keyTranslations = value.keyTranslations;
return this;
} else {
throw new IllegalArgumentException("Builder and value do not have compatible target classes.");
}
}
@Override
public Builder<U> reset() {
this.keyTranslations.clear();
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment