Search in sources :

Example 1 with CommentedConfigurationNode

use of ninja.leaping.configurate.commented.CommentedConfigurationNode in project HuskyCrates-Sponge by codeHusky.

the class CrateUtilities method generateVirtualCrates.

public void generateVirtualCrates(ConfigurationLoader<CommentedConfigurationNode> config) {
    toCheck = new ArrayList<>();
    physicalCrates = new HashMap<>();
    //System.out.println("GEN VC CALLED");
    try {
        CommentedConfigurationNode configRoot = config.load();
        crateTypes = new HashMap<>();
        Map<Object, ? extends CommentedConfigurationNode> b = configRoot.getNode("crates").getChildrenMap();
        for (Object prekey : b.keySet()) {
            String key = (String) prekey;
            crateTypes.put(key, new VirtualCrate(key, config, configRoot.getNode("crates", key)));
        }
        config.save(configRoot);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        CommentedConfigurationNode root = plugin.crateConfig.load();
        List<? extends CommentedConfigurationNode> cacher = root.getNode("cachedCrates").getChildrenList();
        for (CommentedConfigurationNode i : cacher) {
            try {
                toCheck.add(i.getValue(TypeToken.of(Location.class)));
            } catch (ObjectMappingException e) {
                e.printStackTrace();
                i.setValue(null);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    hasInitalizedVirtualCrates = true;
}
Also used : CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 2 with CommentedConfigurationNode

use of ninja.leaping.configurate.commented.CommentedConfigurationNode 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 3 with CommentedConfigurationNode

use of ninja.leaping.configurate.commented.CommentedConfigurationNode in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method gameInit.

@Listener
public void gameInit(GamePreInitializationEvent event) {
    logger = LoggerFactory.getLogger(pC.getName());
    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.");
    }
    logger.info("Let's not init VCrates here anymore. ://)");
    instance = this;
}
Also used : CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) SharedLangData(pw.codehusky.huskycrates.lang.SharedLangData) IOException(java.io.IOException) Listener(org.spongepowered.api.event.Listener)

Example 4 with CommentedConfigurationNode

use of ninja.leaping.configurate.commented.CommentedConfigurationNode 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 5 with CommentedConfigurationNode

use of ninja.leaping.configurate.commented.CommentedConfigurationNode 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)5 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)5 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)4 Listener (org.spongepowered.api.event.Listener)3 Location (org.spongepowered.api.world.Location)3 World (org.spongepowered.api.world.World)3 JSONObject (org.json.JSONObject)2 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)2 Entity (org.spongepowered.api.entity.Entity)2 ArmorStand (org.spongepowered.api.entity.living.ArmorStand)2 PhysicalCrate (pw.codehusky.huskycrates.crate.PhysicalCrate)2 SharedLangData (pw.codehusky.huskycrates.lang.SharedLangData)2 Consumer (java.util.function.Consumer)1 Task (org.spongepowered.api.scheduler.Task)1