Search in sources :

Example 6 with VirtualCrate

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

the class HuskyCrates method crateInteract.

@Listener
public void crateInteract(InteractBlockEvent.Secondary.MainHand event) {
    //pp.getInventory().offer(ItemStack.builder().fromContainer(ss.toContainer().set(DataQuery.of("UnsafeDamage"),3)).build());*/
    if (!event.getTargetBlock().getLocation().isPresent())
        return;
    Location<World> blk = event.getTargetBlock().getLocation().get();
    //System.out.println(blk.getBlock().getType());
    if (validCrateBlocks.contains(blk.getBlockType())) {
        Player plr = (Player) event.getCause().root();
        if (crateUtilities.physicalCrates.containsKey(blk)) {
            String crateType = crateUtilities.physicalCrates.get(blk).vc.id;
            VirtualCrate vc = crateUtilities.getVirtualCrate(crateType);
            crateUtilities.physicalCrates.get(blk).createHologram();
            if (vc.crateBlockType == blk.getBlockType()) {
                event.setCancelled(true);
            } else {
                return;
            }
            //crateUtilities.recognizeChest(te.getLocation());
            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, blk.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) World(org.spongepowered.api.world.World) 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 7 with VirtualCrate

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

the class Crate method getCrateKey.

public ItemStack getCrateKey(String id, int quantity) {
    VirtualCrate vc = plugin.crateUtilities.getVirtualCrate(id);
    if (vc != null) {
        ItemStack key = ItemStack.builder().itemType(ItemTypes.NETHER_STAR).quantity(quantity).add(Keys.DISPLAY_NAME, TextSerializers.LEGACY_FORMATTING_CODE.deserialize(vc.displayName + " Key")).build();
        ArrayList<Text> bb = new ArrayList<>();
        bb.add(Text.of(TextColors.WHITE, "A key for a ", TextSerializers.LEGACY_FORMATTING_CODE.deserialize(vc.displayName), TextColors.WHITE, "."));
        bb.add(Text.of(TextColors.WHITE, "crate_" + id));
        key.offer(Keys.ITEM_LORE, bb);
        return key;
    }
    return null;
}
Also used : VirtualCrate(pw.codehusky.huskycrates.crate.VirtualCrate) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

ItemStack (org.spongepowered.api.item.inventory.ItemStack)7 VirtualCrate (pw.codehusky.huskycrates.crate.VirtualCrate)7 Player (org.spongepowered.api.entity.living.player.Player)6 InventoryTransactionResult (org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)4 IOException (java.io.IOException)2 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)2 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)2 Listener (org.spongepowered.api.event.Listener)2 Task (org.spongepowered.api.scheduler.Task)2 ArrayList (java.util.ArrayList)1 Text (org.spongepowered.api.text.Text)1 World (org.spongepowered.api.world.World)1 PhysicalCrate (pw.codehusky.huskycrates.crate.PhysicalCrate)1