Search in sources :

Example 36 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project HuskyCrates-Sponge by codeHusky.

the class SpinnerCrateView method updateTick.

private void updateTick() {
    //revDampening = 1.15;
    waitCurrent++;
    //once clicks is greater than offset we stop the spinner
    if (waitCurrent == Math.round(updateMax) && clicks < offset && tickerState == 0) {
        //System.out.println(clicks + " : " + offset);
        waitCurrent = 0;
        updateMax *= dampening;
        updateInv(-1);
        ourplr.playSound(SoundTypes.UI_BUTTON_CLICK, ourplr.getLocation().getPosition(), 0.25);
        clicks++;
        trueclicks++;
    //HuskyCrates.instance.logger.info(maxClicks + " : " + trueclicks);
    } else if (clicks >= offset && updateMax != 100 && tickerState == 0) {
        ourplr.openInventory(disp, plugin.genericCause);
        tickerState = 1;
        ourplr.playSound(SoundTypes.ENTITY_FIREWORK_LAUNCH, ourplr.getLocation().getPosition(), 1);
        updateMax = 100;
        waitCurrent = 0;
    } else if (tickerState == 1) {
        if (waitCurrent == Math.round(updateMax)) {
            updater.cancel();
            ourplr.closeInventory(plugin.genericCause);
            if (giveToPlayer.getReward().getReward() instanceof String) {
                Sponge.getCommandManager().process(new CrateCommandSource(), giveToPlayer.getReward().getReward().toString().replace("%p", ourplr.getName()));
            } else {
                //System.out.println(giveToPlayer.getReward().treatAsSingle());
                ourplr.getInventory().offer((ItemStack) giveToPlayer.getReward().getReward());
            }
            boolean mult = false;
            if (!giveToPlayer.getReward().treatAsSingle() && giveToPlayer.getReward().getReward() instanceof ItemStack) {
                if (((ItemStack) giveToPlayer.getReward().getReward()).getQuantity() > 1) {
                    /*ourplr.sendMessage(Text.of("You won ", TextColors.YELLOW,
                                ((ItemStack) giveToPlayer.getReward().getReward()).getQuantity() + " ",
                                TextSerializers.FORMATTING_CODE.deserialize(giveToPlayer.getReward().getRewardName()), TextColors.RESET, " from a ",
                                TextSerializers.FORMATTING_CODE.deserialize(vc.displayName), TextColors.RESET, "!"));*/
                    ourplr.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(vc.langData.formatter(vc.langData.prefix + vc.langData.rewardMessage, ((ItemStack) giveToPlayer.getReward().getReward()).getQuantity() + "", ourplr, vc, giveToPlayer)));
                    mult = true;
                }
            }
            if (!mult) {
                String[] vowels = { "a", "e", "i", "o", "u" };
                if (Arrays.asList(vowels).contains(giveToPlayer.getReward().getRewardName().substring(0, 1).toLowerCase())) {
                    ourplr.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(vc.langData.formatter(vc.langData.prefix + vc.langData.rewardMessage, "an", ourplr, vc, giveToPlayer)));
                } else {
                    ourplr.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(vc.langData.formatter(vc.langData.prefix + vc.langData.rewardMessage, "a", ourplr, vc, giveToPlayer)));
                }
            }
            ourplr.playSound(SoundTypes.ENTITY_EXPERIENCE_ORB_PICKUP, ourplr.getLocation().getPosition(), 1);
        } else if (waitCurrent % 5 == 0) {
            updateInv(2);
        }
    }
}
Also used : CrateCommandSource(pw.codehusky.huskycrates.crate.CrateCommandSource) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 37 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project HuskyCrates-Sponge by codeHusky.

the class SpinnerCrateView method updateInv.

private void updateInv(int state) {
    ItemStack border = ItemStack.builder().itemType(ItemTypes.STAINED_GLASS_PANE).add(Keys.DYE_COLOR, DyeColors.BLACK).build();
    border.offer(Keys.DISPLAY_NAME, Text.of(TextStyles.RESET, "HuskyCrates"));
    //border.offer(Keys.ITEM_LORE,lore);
    ItemStack selector = ItemStack.of(ItemTypes.REDSTONE_TORCH, 1);
    selector.offer(Keys.DISPLAY_NAME, Text.of(TextStyles.RESET, "HuskyCrates"));
    //selector.offer(Keys.ITEM_LORE,lore);
    int slotnum = 0;
    for (Inventory e : disp.slots()) {
        if (state == 0 && (slotnum == 4 || slotnum == 22)) {
            e.set(selector);
        } else if (slotnum > 9 && slotnum < 17 && state != 2) {
            //int itemNum = items.size() - 1 - Math.abs(((slotnum - 10) + (clicks)) % items.size());
            int itemNum = Math.abs((clicks + (slotnum - 9) - 3) % items.size());
            //System.out.println(itemNum);
            e.set(((CrateRewardHolder) items.get(itemNum)[1]).getDisplayItem());
            if (slotnum == 13) {
                giveToPlayer = (CrateRewardHolder) items.get(itemNum)[1];
            }
        } else if (slotnum != 13) {
            if (state == 2) {
                e.set(confettiBorder());
            } else if (state == 0) {
                e.set(border);
            }
        } else if (slotnum == 13 && state == 2) {
            e.set(giveToPlayer.getDisplayItem());
        }
        slotnum++;
    }
    if (!ourplr.isViewingInventory()) {
        ourplr.openInventory(disp, plugin.genericCause);
    }
}
Also used : ItemStack(org.spongepowered.api.item.inventory.ItemStack) CrateRewardHolder(pw.codehusky.huskycrates.crate.config.CrateRewardHolder) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 38 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project HuskyCrates-Sponge by codeHusky.

the class TemplateCrateView method getInventory.

@Override
public Inventory getInventory() {
    Inventory woop = Inventory.builder().property(InventoryDimension.PROPERTY_NAME, InventoryDimension.of(9, 1)).property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(Text.of(TextColors.DARK_RED, "INVALID CRATE TYPE!"))).listener(ClickInventoryEvent.class, evt -> evt.setCancelled(true)).build(plugin);
    woop.offer(ItemStack.of(ItemTypes.BARRIER, 256 * 2 + 64));
    for (Inventory e : woop.slots()) {
        ItemStack b = e.peek().get();
        b.setQuantity(1);
        e.set(b);
    }
    return woop;
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) HuskyCrates(pw.codehusky.huskycrates.HuskyCrates) ItemTypes(org.spongepowered.api.item.ItemTypes) Random(java.util.Random) InventoryTitle(org.spongepowered.api.item.inventory.property.InventoryTitle) ArrayList(java.util.ArrayList) VirtualCrate(pw.codehusky.huskycrates.crate.VirtualCrate) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Text(org.spongepowered.api.text.Text) InventoryDimension(org.spongepowered.api.item.inventory.property.InventoryDimension) Player(org.spongepowered.api.entity.living.player.Player) ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) TextColors(org.spongepowered.api.text.format.TextColors) ClickInventoryEvent(org.spongepowered.api.event.item.inventory.ClickInventoryEvent) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 39 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack 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 40 with ItemStack

use of org.spongepowered.api.item.inventory.ItemStack in project HuskyCrates-Sponge by codeHusky.

the class Chest method execute.

@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
    if (commandContext.getOne("type").isPresent()) {
        String type = commandContext.<String>getOne("type").get();
        Optional<Player> player = commandContext.getOne("player");
        VirtualCrate virtualCrate = HuskyCrates.instance.getCrateUtilities().getVirtualCrate(type);
        int quantity = commandContext.getOne("quantity").isPresent() ? commandContext.<Integer>getOne("quantity").get() : 1;
        if (virtualCrate == null) {
            commandSource.sendMessage(Text.of("Invalid crate id: " + type + ". Please check your config."));
            return CommandResult.empty();
        }
        if (!player.isPresent()) {
            commandSource.sendMessage(Text.of("You need to either specify a player or be in game"));
            return CommandResult.empty();
        }
        ItemStack chestItemStack = virtualCrate.getCrateItem(quantity);
        InventoryTransactionResult.Type mainInventory = player.get().getInventory().offer(chestItemStack.copy()).getType();
        if (!mainInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
            InventoryTransactionResult.Type enderInventory = player.get().getEnderChestInventory().offer(chestItemStack.copy()).getType();
            if (!enderInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
                commandSource.sendMessage(Text.of("Couldn't give chest to " + player.get().getName() + " because of a full inventory and enderchest"));
                HuskyCrates.instance.logger.info("Couldn't give chest to " + player.get().getName() + " because of a full inventory and enderchest");
            } else {
                if (player.isPresent()) {
                    player.get().sendMessage(Text.of("You have been given 1 or more ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " crate(s), but some have been placed in your Ender Chest."));
                } else {
                    commandSource.sendMessage(Text.of("You have been given 1 or more ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " crate(s), but some have been placed in your Ender Chest."));
                }
            }
        }
    } else {
        commandSource.sendMessage(Text.of("Usage: /crate chest <id> [player]"));
    }
    return CommandResult.success();
}
Also used : VirtualCrate(pw.codehusky.huskycrates.crate.VirtualCrate) Player(org.spongepowered.api.entity.living.player.Player) ItemStack(org.spongepowered.api.item.inventory.ItemStack) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Aggregations

ItemStack (org.spongepowered.api.item.inventory.ItemStack)76 Player (org.spongepowered.api.entity.living.player.Player)30 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)29 Listener (org.spongepowered.api.event.Listener)17 ArrayList (java.util.ArrayList)12 Text (org.spongepowered.api.text.Text)10 ItemDropper (com.skelril.nitro.item.ItemDropper)9 MarketService (com.skelril.skree.service.MarketService)9 World (org.spongepowered.api.world.World)9 VirtualCrate (pw.codehusky.huskycrates.crate.VirtualCrate)9 BigDecimal (java.math.BigDecimal)8 Entity (org.spongepowered.api.entity.Entity)8 Vector3d (com.flowpowered.math.vector.Vector3d)5 Clause (com.skelril.nitro.Clause)5 InventoryTransactionResult (org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)5 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)4 Inventory (org.spongepowered.api.item.inventory.Inventory)4 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 EntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)3