Search in sources :

Example 91 with CommandSource

use of org.spongepowered.api.command.CommandSource in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method postGameStart.

@Listener(order = Order.POST)
public void postGameStart(GameStartedServerEvent event) {
    checkVersion();
    if (forceStop) {
        // logger.error("Since a blacklisted mod is loaded, HuskyCrates will not start. Please check higher in your logs for the reasoning.");
        return;
    }
    Sponge.getScheduler().createTaskBuilder().execute(new Consumer<Task>() {

        @Override
        public void accept(Task task) {
            logger.info("Deleting existing armor stands...");
            removeArmorstands();
            logger.info("Initalizing config...");
            if (!crateUtilities.hasInitalizedVirtualCrates) {
                crateUtilities.generateVirtualCrates(crateConfig);
            }
            // doublecheck
            crateUtilities.hasInitalizedVirtualCrates = true;
            logger.info("Done initalizing config.");
            logger.info("Attempting legacy Physical Crates method (this will be removed in a later version)");
            CommentedConfigurationNode root = null;
            boolean convertFired = false;
            try {
                root = crateConfig.load();
                double max = root.getNode("positions").getChildrenList().size();
                double count = 0;
                for (VirtualCrate vc : crateUtilities.crateTypes.values()) {
                    if (vc.pendingKeys.size() > 0) {
                        logger.warn("legacy keys loaded! warn warn warn.");
                        if (!root.getNode("keys").isVirtual()) {
                            root.removeChild("keys");
                        }
                        convertFired = true;
                    }
                }
                if (!root.getNode("positions").isVirtual()) {
                    convertFired = true;
                    logger.warn("Legacy position data detected. Will convert.");
                    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, node.getNode("location", "BlockType").getString().equals("minecraft:air")));
                        logger.info("(LEGACY) PROGRESS: " + Math.round((count / max) * 100) + "%");
                    }
                    root.removeChild("positions");
                }
                if (!root.getNode("users").isVirtual()) {
                    for (Object uuidPre : root.getNode("users").getChildrenMap().keySet()) {
                        for (Object crateIDPre : root.getNode("users", uuidPre, "keys").getChildrenMap().keySet()) {
                            String uuid = uuidPre.toString();
                            String crateID = crateIDPre.toString();
                            int amount = root.getNode("users", uuid, "keys", crateID).getInt(0);
                            HuskyCrates.instance.crateUtilities.crateTypes.get(crateID).virtualBalances.put(uuid, amount);
                        }
                    }
                    root.removeChild("users");
                }
                crateConfig.save(root);
            } catch (Exception e) {
                crateUtilities.exceptionHandler(e);
                if (event.getCause().root() instanceof Player) {
                    CommandSource cs = (CommandSource) event.getCause().root();
                    cs.sendMessage(Text.of(TextColors.GOLD, "HuskyCrates", TextColors.WHITE, ":", TextColors.RED, " An error has occured. Please check the console for more information."));
                }
                return;
            }
            logger.info("Done with legacy loading technique");
            logger.info("Running DB routine.");
            try {
                DBReader.dbInitCheck();
                if (convertFired) {
                    logger.info("Saving data.");
                    DBReader.saveHuskyData();
                    logger.info("Done saving data.");
                } else {
                    logger.info("Loading data.");
                    DBReader.loadHuskyData();
                    logger.info("Done loading data.");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            logger.info("DB Data routine finished.");
            crateUtilities.startParticleEffects();
            logger.info("Initalization complete.");
            Sponge.getScheduler().createTaskBuilder().execute(() -> {
                try {
                    DBReader.dbInitCheck();
                    DBReader.saveHuskyData();
                    logger.info("Updated Database.");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }).interval(15, TimeUnit.MINUTES).delay(15, TimeUnit.MINUTES).async().submit(HuskyCrates.instance);
        }
    }).delayTicks(1).submit(this);
}
Also used : Task(org.spongepowered.api.scheduler.Task) Player(org.spongepowered.api.entity.living.player.Player) SQLException(java.sql.SQLException) PhysicalCrate(com.codehusky.huskycrates.crate.PhysicalCrate) World(org.spongepowered.api.world.World) CommandSource(org.spongepowered.api.command.CommandSource) SQLException(java.sql.SQLException) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) IOException(java.io.IOException) VirtualCrate(com.codehusky.huskycrates.crate.VirtualCrate) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) JSONObject(org.json.JSONObject) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Aggregations

CommandSource (org.spongepowered.api.command.CommandSource)91 Text (org.spongepowered.api.text.Text)60 CommandResult (org.spongepowered.api.command.CommandResult)48 List (java.util.List)47 CommandContext (org.spongepowered.api.command.args.CommandContext)45 Collectors (java.util.stream.Collectors)37 Sponge (org.spongepowered.api.Sponge)37 Player (org.spongepowered.api.entity.living.player.Player)36 Optional (java.util.Optional)35 TextColors (org.spongepowered.api.text.format.TextColors)30 CommandElement (org.spongepowered.api.command.args.CommandElement)27 TextActions (org.spongepowered.api.text.action.TextActions)27 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)25 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)24 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)23 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)23 GenericArguments (org.spongepowered.api.command.args.GenericArguments)23 Util (io.github.nucleuspowered.nucleus.Util)20 Nullable (javax.annotation.Nullable)20 Map (java.util.Map)19