Search in sources :

Example 21 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method updatePhysicalCrates.

public void updatePhysicalCrates() {
    if (updating)
        return;
    updating = true;
    try {
        CommentedConfigurationNode root = crateConfig.load();
        root.getNode("positions").setValue(null);
        for (Object ob : ((HashMap) crateUtilities.physicalCrates.clone()).keySet()) {
            Location<World> e = (Location<World>) ob;
            CommentedConfigurationNode node = root.getNode("positions").getAppendedNode();
            node.getNode("location").setValue(TypeToken.of(Location.class), e);
            //System.out.println("echo");
            try {
                node.getNode("crateID").setValue(crateUtilities.physicalCrates.get(e).vc.id);
            } catch (NullPointerException err) {
                System.out.println("removing a crate!");
                node.setValue(null);
                crateUtilities.physicalCrates.remove(ob);
                logger.warn("Invalid crate at (" + e.getPosition().getFloorX() + ", " + e.getPosition().getFloorY() + ", " + e.getPosition().getFloorZ() + ")!");
            }
        }
        crateConfig.save(root);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ObjectMappingException e) {
        e.printStackTrace();
    }
    crateUtilities.flag = false;
    updating = false;
}
Also used : CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) JSONObject(org.json.JSONObject) IOException(java.io.IOException) World(org.spongepowered.api.world.World) Location(org.spongepowered.api.world.Location) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 22 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method postGameStart.

@Listener(order = Order.POST)
public void postGameStart(GameStartedServerEvent event) {
    Sponge.getScheduler().createTaskBuilder().async().execute(new Consumer<Task>() {

        @Override
        public void accept(Task task) {
            try {
                JSONObject obj = JsonReader.readJsonFromUrl("https://api.github.com/repos/codehusky/HuskyCrates-Sponge/releases");
                String[] thisVersion = pC.getVersion().get().split("\\.");
                String[] remoteVersion = obj.getJSONArray("releases").getJSONObject(0).getString("tag_name").replace("v", "").split("\\.");
                for (int i = 0; i < Math.min(remoteVersion.length, thisVersion.length); i++) {
                    if (!thisVersion[i].equals(remoteVersion[i])) {
                        if (Integer.parseInt(thisVersion[i]) > Integer.parseInt(remoteVersion[i])) {
                            //we're ahead
                            logger.warn("----------------------------------------------------");
                            logger.warn("Running unreleased version. (Developer build?)");
                            logger.warn("----------------------------------------------------");
                        } else {
                            //we're behind
                            logger.warn("----------------------------------------------------");
                            logger.warn("Your version of HuskyCrates is out of date!");
                            logger.warn("Your version: v" + pC.getVersion().get());
                            logger.warn("Latest version: " + obj.getJSONArray("releases").getJSONObject(0).getString("tag_name"));
                            logger.warn("Update here: https://goo.gl/hgtPMR");
                            logger.warn("----------------------------------------------------");
                        }
                        return;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).submit(this);
    Sponge.getScheduler().createTaskBuilder().execute(new Consumer<Task>() {

        @Override
        public void accept(Task task) {
            logger.info("Deleting existing armor stands...");
            for (World bit : Sponge.getServer().getWorlds()) {
                for (Entity ent : bit.getEntities()) {
                    if (ent instanceof ArmorStand) {
                        ArmorStand arm = (ArmorStand) ent;
                        if (arm.getCreator().isPresent()) {
                            if (arm.getCreator().get().equals(UUID.fromString(armorStandIdentifier))) {
                                arm.remove();
                            }
                        }
                    }
                }
            }
            logger.info("Initalizing config...");
            if (!crateUtilities.hasInitalizedVirtualCrates) {
                crateUtilities.generateVirtualCrates(crateConfig);
            }
            // doublecheck
            crateUtilities.hasInitalizedVirtualCrates = true;
            logger.info("Done initalizing config.");
            logger.info("Populating physical crates...");
            CommentedConfigurationNode root = null;
            try {
                root = crateConfig.load();
                double max = root.getNode("positions").getChildrenList().size();
                double count = 0;
                for (CommentedConfigurationNode node : root.getNode("positions").getChildrenList()) {
                    count++;
                    Location<World> ee;
                    try {
                        ee = node.getNode("location").getValue(TypeToken.of(Location.class));
                    } catch (InvalidDataException err2) {
                        logger.warn("Bug sponge developers about world UUIDs!");
                        ee = new Location<World>(Sponge.getServer().getWorld(node.getNode("location", "WorldName").getString()).get(), node.getNode("location", "X").getDouble(), node.getNode("location", "Y").getDouble(), node.getNode("location", "Z").getDouble());
                    }
                    if (!crateUtilities.physicalCrates.containsKey(ee))
                        crateUtilities.physicalCrates.put(ee, new PhysicalCrate(ee, node.getNode("crateID").getString(), HuskyCrates.instance));
                    logger.info("PROGRESS: " + Math.round((count / max) * 100) + "%");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ObjectMappingException e) {
                e.printStackTrace();
            }
            crateUtilities.startParticleEffects();
            logger.info("Done populating physical crates.");
            logger.info("Initalization complete.");
        }
    }).delayTicks(1).submit(this);
}
Also used : Entity(org.spongepowered.api.entity.Entity) Task(org.spongepowered.api.scheduler.Task) PhysicalCrate(pw.codehusky.huskycrates.crate.PhysicalCrate) IOException(java.io.IOException) World(org.spongepowered.api.world.World) Consumer(java.util.function.Consumer) JSONObject(org.json.JSONObject) ArmorStand(org.spongepowered.api.entity.living.ArmorStand) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) Location(org.spongepowered.api.world.Location) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Listener(org.spongepowered.api.event.Listener)

Example 23 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project SpongeAPI by SpongePowered.

the class TextConfigSerializer method deserialize.

@Override
public Text deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
    StringWriter writer = new StringWriter();
    GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder().setIndent(0).setSink(() -> new BufferedWriter(writer)).setHeaderMode(HeaderMode.NONE).build();
    try {
        gsonLoader.save(value);
    } catch (IOException e) {
        throw new ObjectMappingException(e);
    }
    return Sponge.getDataManager().deserialize(Text.class, DataContainer.createNew().set(Queries.JSON, writer.toString())).get();
}
Also used : StringWriter(java.io.StringWriter) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) Text(org.spongepowered.api.text.Text) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 24 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project guardian by ichorpowered.

the class AbstractDetectionContentLoader method acquireAll.

@Override
public void acquireAll() {
    if (this.contentContainer == null || this.contentContainer.getPossibleKeys() == null)
        return;
    this.contentContainer.getPossibleKeys().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 25 with ObjectMappingException

use of ninja.leaping.configurate.objectmapping.ObjectMappingException in project AtherysCore by Atherys-Horizons.

the class AbstractConfigurateAdapter method serialize.

@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    GsonConfigurationLoader loader = GsonConfigurationLoader.builder().setLenient(true).setIndent(0).build();
    try {
        ConfigurationNode node = loader.createEmptyNode().setValue(TypeToken.of(clazz), src);
        StringWriter writer = new StringWriter();
        loader.saveInternal(node, writer);
        String json = writer.toString();
        return parser.parse(json);
    } catch (ObjectMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : StringWriter(java.io.StringWriter) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) GsonConfigurationLoader(ninja.leaping.configurate.gson.GsonConfigurationLoader) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Aggregations

ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)30 IOException (java.io.IOException)21 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)10 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)8 Map (java.util.Map)7 GsonConfigurationLoader (ninja.leaping.configurate.gson.GsonConfigurationLoader)6 Optional (java.util.Optional)5 Detection (com.ichorpowered.guardianapi.detection.Detection)4 Path (java.nio.file.Path)4 Set (java.util.Set)4 Text (org.spongepowered.api.text.Text)4 Maps (com.google.common.collect.Maps)3 ConfigurationAssignment (com.ichorpowered.guardian.content.assignment.ConfigurationAssignment)3 GuardianMapValue (com.ichorpowered.guardian.util.item.mutable.GuardianMapValue)3 GuardianValue (com.ichorpowered.guardian.util.item.mutable.GuardianValue)3 ContentContainer (com.ichorpowered.guardianapi.content.ContentContainer)3 ContentKey (com.ichorpowered.guardianapi.content.key.ContentKey)3 DetectionContentLoader (com.ichorpowered.guardianapi.detection.DetectionContentLoader)3 Key (com.ichorpowered.guardianapi.util.item.key.Key)3 MapValue (com.ichorpowered.guardianapi.util.item.value.mutable.MapValue)3