Search in sources :

Example 71 with ItemStack

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

the class VirtualCrate method getCrateWand.

/***
     * Retrieve the crate item
     * @since 1.2.1
     * @return the ItemStack with the keys.
     */
public ItemStack getCrateWand() {
    ItemStack key = ItemStack.builder().itemType(ItemTypes.BLAZE_ROD).add(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(displayName + " Wand")).build();
    ArrayList<Text> itemLore = new ArrayList<>();
    itemLore.add(Text.of(TextColors.WHITE, "A wand for a ", TextSerializers.FORMATTING_CODE.deserialize(displayName), TextColors.WHITE, "."));
    itemLore.add(Text.of(TextColors.DARK_GRAY, "HuskyCrates"));
    key.offer(Keys.ITEM_LORE, itemLore);
    if (keyDamage != null) {
        key = ItemStack.builder().fromContainer(key.toContainer().set(DataQuery.of("UnsafeDamage"), keyDamage)).build();
    }
    //
    return ItemStack.builder().fromContainer(key.toContainer().set(DataQuery.of("UnsafeData", "crateID"), id)).build();
}
Also used : ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 72 with ItemStack

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

the class CrateRewardHolderParser method itemFromNode.

private static ItemStack itemFromNode(ConfigurationNode itemRoot) {
    try {
        ItemStack item = ItemStack.builder().itemType(itemRoot.getNode("id").getValue(TypeToken.of(ItemType.class))).quantity(itemRoot.getNode("count").getInt(1)).add(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(itemRoot.getNode("name").getString())).build();
        if (!itemRoot.getNode("variant").isVirtual()) {
        //if(Sponge.getRegistry().getType(TreeType.class,itemRoot.getNode("variant").getString()).isPresent()) {
        //System.out.println(item.offer(Keys.TREE_TYPE,getTreeType(itemRoot.getNode("variant").getString("oak"))));
        //System.out.println(itemRoot.getNode("variant").getValue());
        //}
        }
        if (!itemRoot.getNode("lore").isVirtual()) {
            ArrayList<Text> lore = new ArrayList<>();
            for (String ll : itemRoot.getNode("lore").getList(TypeToken.of(String.class))) {
                lore.add(TextSerializers.FORMATTING_CODE.deserialize(ll));
            }
            item.offer(Keys.ITEM_LORE, lore);
        }
        if (!itemRoot.getNode("name").isVirtual()) {
            item.offer(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(itemRoot.getNode("name").getString()));
        }
        if (!itemRoot.getNode("enchants").isVirtual()) {
            ArrayList<ItemEnchantment> enchantments = new ArrayList<>();
            for (Object key : itemRoot.getNode("enchants").getChildrenMap().keySet()) {
                int level = itemRoot.getNode("enchants").getChildrenMap().get(key).getInt();
                String enchantID = (String) key;
                // STRINGS ONLY!
                Enchantment enc = getEnchantment(enchantID);
                ItemEnchantment itemEnchantment = new ItemEnchantment(enc, level);
                enchantments.add(itemEnchantment);
            }
            item.offer(Keys.ITEM_ENCHANTMENTS, enchantments);
        }
        if (!itemRoot.getNode("damage").isVirtual()) {
            //HuskyCrates.instance.logger.info("damage override called");
            item = ItemStack.builder().fromContainer(//OVERRIDE DAMAGE VAL! :)
            item.toContainer().set(DataQuery.of("UnsafeDamage"), itemRoot.getNode("damage").getInt(0))).build();
        }
        if (!itemRoot.getNode("nbt").isVirtual()) {
            //nbt overrrides
            LinkedHashMap items = (LinkedHashMap) itemRoot.getNode("nbt").getValue();
            if (item.toContainer().get(DataQuery.of("UnsafeData")).isPresent()) {
                BiMap real = ((BiMap) item.toContainer().getMap(DataQuery.of("UnsafeData")).get());
                items.putAll(real);
            }
            //System.out.println(item.toContainer().get(DataQuery.of("UnsafeData")).get().getClass());
            item = ItemStack.builder().fromContainer(item.toContainer().set(DataQuery.of("UnsafeData"), items)).build();
        }
        //item.offer(Keys.PICKUP_DELAY,itemRoot.getNode("pickupdelay").getInt())
        return item;
    } catch (ObjectMappingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ItemType(org.spongepowered.api.item.ItemType) BiMap(com.google.common.collect.BiMap) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) LinkedHashMap(java.util.LinkedHashMap) ItemEnchantment(org.spongepowered.api.data.meta.ItemEnchantment) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemEnchantment(org.spongepowered.api.data.meta.ItemEnchantment) Enchantment(org.spongepowered.api.item.Enchantment) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 73 with ItemStack

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

the class CrateRewardHolderParser method toConfig.

public static ConfigurationNode toConfig(CrateRewardHolder holder) {
    //pretty much just for conversion
    ConfigurationNode toOverwrite = HoconConfigurationLoader.builder().build().createEmptyNode();
    toOverwrite.setValue(itemToNode(holder.getDisplayItem()));
    ConfigurationNode hd = toOverwrite.getNode("huskydata");
    if (holder.getReward().getReward() instanceof String) {
        hd.getNode("reward", "overrideRewardName").setValue(holder.getReward().getRewardName());
        hd.getNode("reward", "command").setValue(holder.getReward().getReward());
        hd.getNode("reward", "type").setValue("command");
    } else {
        if (holder.isDispRewardSimilar()) {
            if (holder.getDisplayItem().getQuantity() != ((ItemStack) holder.getReward().getReward()).getQuantity()) {
                // we are overriding the count, but not the item! :)
                hd.getNode("reward", "overrideCount").setValue(((ItemStack) holder.getReward().getReward()).getQuantity());
            }
        } else {
            hd.getNode("reward", "overrideItem").setValue(itemToNode((ItemStack) holder.getReward().getReward()));
        }
        String checkAgainst;
        if (holder.getDisplayItem().get(Keys.DISPLAY_NAME).isPresent()) {
            checkAgainst = holder.getDisplayItem().get(Keys.DISPLAY_NAME).get().toPlain();
        } else {
            checkAgainst = holder.getDisplayItem().getItem().getName();
        }
        if (!checkAgainst.equals(holder.getReward().getRewardName())) {
            hd.getNode("reward", "overrideRewardName").setValue(holder.getReward().getRewardName());
        }
        hd.getNode("reward", "type").setValue("item");
    }
    hd.getNode("weight").setValue(holder.getChance());
    return toOverwrite;
}
Also used : ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 74 with ItemStack

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

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