Search in sources :

Example 1 with PhysicalCrate

use of pw.codehusky.huskycrates.crate.PhysicalCrate in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method entityInteract.

@Listener
public void entityInteract(InteractEntityEvent.Secondary.MainHand event) {
    //System.out.println(event.getTargetEntity().toContainer().get(DataQuery.of("UnsafeData","crateID")));
    if (event.getCause().root() instanceof Player) {
        Player plr = (Player) event.getCause().root();
        if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent() && plr.hasPermission("huskycrates.wand")) {
            ItemStack hand = plr.getItemInHand(HandTypes.MAIN_HAND).get();
            if (hand.getItem() == ItemTypes.BLAZE_ROD) {
                if (hand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).isPresent()) {
                    if (!crateUtilities.physicalCrates.containsKey(event.getTargetEntity().getLocation())) {
                        //System.out.println(event.getTargetEntity().getLocation().getBlockPosition());
                        event.getTargetEntity().offer(Keys.AI_ENABLED, false);
                        event.getTargetEntity().offer(Keys.IS_SILENT, true);
                        crateUtilities.physicalCrates.put(event.getTargetEntity().getLocation(), new PhysicalCrate(event.getTargetEntity().getLocation(), hand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).get().toString(), this));
                        crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).createHologram();
                        updatePhysicalCrates();
                    } else {
                        event.getTargetEntity().offer(Keys.AI_ENABLED, true);
                        event.getTargetEntity().offer(Keys.IS_SILENT, false);
                        crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).as.remove();
                        crateUtilities.physicalCrates.remove(event.getTargetEntity().getLocation());
                        updatePhysicalCrates();
                    }
                    event.setCancelled(true);
                    return;
                }
            }
        }
        if (crateUtilities.physicalCrates.containsKey(event.getTargetEntity().getLocation())) {
            String crateType = crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).vc.id;
            VirtualCrate vc = crateUtilities.getVirtualCrate(crateType);
            crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).createHologram();
            //crateUtilities.recognizeChest(te.getLocation());
            event.setCancelled(true);
            if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
                ItemStack inhand = plr.getItemInHand(HandTypes.MAIN_HAND).get();
                if (inhand.getItem() == vc.getKeyType()) {
                    if (inhand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).isPresent()) {
                        String id = inhand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).get().toString();
                        if (id.equals(crateType)) {
                            if (!plr.hasPermission("huskycrates.tester")) {
                                if (inhand.getQuantity() == 1)
                                    plr.setItemInHand(HandTypes.MAIN_HAND, null);
                                else {
                                    ItemStack tobe = inhand.copy();
                                    tobe.setQuantity(tobe.getQuantity() - 1);
                                    plr.setItemInHand(HandTypes.MAIN_HAND, tobe);
                                }
                            }
                            Task.Builder upcoming = scheduler.createTaskBuilder();
                            upcoming.execute(() -> {
                                crateUtilities.launchCrateForPlayer(crateType, plr, this);
                            }).delayTicks(1).submit(this);
                            return;
                        }
                    }
                }
            }
            plr.playSound(SoundTypes.BLOCK_ANVIL_LAND, event.getTargetEntity().getLocation().getPosition(), 0.3);
            try {
                plr.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(vc.langData.formatter(vc.langData.prefix + vc.langData.noKeyMessage, null, plr, vc, null)));
            } catch (Exception e) {
                plr.sendMessage(Text.of(TextColors.RED, "Critical crate failure, contact the administrator. (Admins, check console!)"));
                e.printStackTrace();
            }
        }
    }
}
Also used : VirtualCrate(pw.codehusky.huskycrates.crate.VirtualCrate) Player(org.spongepowered.api.entity.living.player.Player) Task(org.spongepowered.api.scheduler.Task) PhysicalCrate(pw.codehusky.huskycrates.crate.PhysicalCrate) ItemStack(org.spongepowered.api.item.inventory.ItemStack) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Listener(org.spongepowered.api.event.Listener)

Example 2 with PhysicalCrate

use of pw.codehusky.huskycrates.crate.PhysicalCrate in project HuskyCrates-Sponge by codeHusky.

the class HuskyCrates method placeBlock.

@Listener
public void placeBlock(ChangeBlockEvent event) {
    if (event.getCause().root() instanceof Player) {
        Player plr = (Player) event.getCause().root();
        if (event instanceof ChangeBlockEvent.Place || event instanceof ChangeBlockEvent.Break) {
            BlockType t = event.getTransactions().get(0).getOriginal().getLocation().get().getBlock().getType();
            Location<World> location = event.getTransactions().get(0).getOriginal().getLocation().get();
            location.getBlock().toContainer().set(DataQuery.of("rock"), 1);
            //location.getBlock().with()
            if (validCrateBlocks.contains(t)) {
                //crateUtilities.recognizeChest(event.getTransactions().get(0).getOriginal().getLocation().get());
                if (event instanceof ChangeBlockEvent.Place) {
                    if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
                        Optional<Object> tt = plr.getItemInHand(HandTypes.MAIN_HAND).get().toContainer().get(DataQuery.of("UnsafeData", "crateID"));
                        if (tt.isPresent()) {
                            String crateID = tt.get().toString();
                            if (!plr.hasPermission("huskycrates.tester")) {
                                event.setCancelled(true);
                                return;
                            }
                            if (!crateUtilities.physicalCrates.containsKey(location))
                                crateUtilities.physicalCrates.put(location, new PhysicalCrate(location, crateID, this));
                            crateUtilities.physicalCrates.get(location).createHologram();
                            updatePhysicalCrates();
                        }
                    }
                }
            } else {
                if (crateUtilities.physicalCrates.containsKey(location)) {
                    if (!plr.hasPermission("huskycrates.tester")) {
                        event.setCancelled(true);
                        return;
                    }
                    crateUtilities.flag = true;
                    crateUtilities.physicalCrates.get(location).as.remove();
                    crateUtilities.physicalCrates.remove(location);
                    updatePhysicalCrates();
                }
            }
        }
    }
}
Also used : ChangeBlockEvent(org.spongepowered.api.event.block.ChangeBlockEvent) Player(org.spongepowered.api.entity.living.player.Player) BlockType(org.spongepowered.api.block.BlockType) JSONObject(org.json.JSONObject) PhysicalCrate(pw.codehusky.huskycrates.crate.PhysicalCrate) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Example 3 with PhysicalCrate

use of pw.codehusky.huskycrates.crate.PhysicalCrate 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 4 with PhysicalCrate

use of pw.codehusky.huskycrates.crate.PhysicalCrate 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

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