Search in sources :

Example 1 with InvalidDataException

use of org.spongepowered.api.data.persistence.InvalidDataException in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method gameReloaded.

@Listener
public void gameReloaded(GameReloadEvent event) {
    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();
                    }
                }
            }
        }
    }
    langData = new SharedLangData("", "You won %a %R&rfrom a %C&r!", "&e%p just won %a %R&r&e from a %C&r!", "You need a %K&r to open this crate.");
    CommentedConfigurationNode conf = null;
    try {
        conf = crateConfig.load();
        if (!conf.getNode("lang").isVirtual())
            langData = new SharedLangData(conf.getNode("lang"));
        else
            logger.info("Using default lang settings.");
    } catch (IOException e) {
        e.printStackTrace();
        logger.warn("Lang load failed, using defaults.");
    }
    crateUtilities.generateVirtualCrates(crateConfig);
    CommentedConfigurationNode root = null;
    try {
        root = crateConfig.load();
        for (CommentedConfigurationNode node : root.getNode("positions").getChildrenList()) {
            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));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ObjectMappingException e) {
        e.printStackTrace();
    }
    crateUtilities.startParticleEffects();
}
Also used : Entity(org.spongepowered.api.entity.Entity) ArmorStand(org.spongepowered.api.entity.living.ArmorStand) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) SharedLangData(pw.codehusky.huskycrates.lang.SharedLangData) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) PhysicalCrate(pw.codehusky.huskycrates.crate.PhysicalCrate) IOException(java.io.IOException) World(org.spongepowered.api.world.World) Location(org.spongepowered.api.world.Location) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Listener(org.spongepowered.api.event.Listener)

Example 2 with InvalidDataException

use of org.spongepowered.api.data.persistence.InvalidDataException 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)

Aggregations

IOException (java.io.IOException)2 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)2 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)2 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)2 Entity (org.spongepowered.api.entity.Entity)2 ArmorStand (org.spongepowered.api.entity.living.ArmorStand)2 Listener (org.spongepowered.api.event.Listener)2 Location (org.spongepowered.api.world.Location)2 World (org.spongepowered.api.world.World)2 PhysicalCrate (pw.codehusky.huskycrates.crate.PhysicalCrate)2 Consumer (java.util.function.Consumer)1 JSONObject (org.json.JSONObject)1 Task (org.spongepowered.api.scheduler.Task)1 SharedLangData (pw.codehusky.huskycrates.lang.SharedLangData)1