Search in sources :

Example 51 with ItemType

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

the class CrateConfigParser method itemFromNode.

private static ItemStack itemFromNode(ConfigurationNode itemRoot) {
    try {
        if (itemRoot.getNode("id").isVirtual()) {
            HuskyCrates.instance.logger.error("NO ITEM ID: " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
            return ItemStack.of(ItemTypes.NONE, 1);
        }
        ItemType type;
        Integer dmg = null;
        try {
            type = itemRoot.getNode("id").getValue(TypeToken.of(ItemType.class));
        } catch (Exception e) {
            String id = itemRoot.getNode("id").getString();
            String[] parts = id.split(":");
            try {
                if (parts.length == 3) {
                    id = parts[0] + ":" + parts[1];
                    dmg = Integer.parseInt(parts[2]);
                } else if (parts.length == 2) {
                    id = parts[0];
                    dmg = Integer.parseInt(parts[1]);
                }
            } catch (Exception ee) {
                HuskyCrates.instance.logger.error("INVALID ITEM ID: \"" + itemRoot.getNode("id").getString("NOT A STRING") + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
                return ItemStack.of(ItemTypes.NONE, 1);
            }
            Optional<ItemType> optType = Sponge.getRegistry().getType(ItemType.class, id);
            if (optType.isPresent()) {
                type = optType.get();
            } else {
                HuskyCrates.instance.logger.error("INVALID ITEM ID: \"" + itemRoot.getNode("id").getString("NOT A STRING") + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
                return ItemStack.of(ItemTypes.NONE, 1);
            }
        }
        ItemStack item = ItemStack.builder().itemType(type).quantity(itemRoot.getNode("count").getInt(1)).build();
        if (!itemRoot.getNode("name").isVirtual()) {
            item.offer(Keys.DISPLAY_NAME, TextSerializers.FORMATTING_CODE.deserialize(itemRoot.getNode("name").getString()));
        }
        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<Enchantment> enchantments = new ArrayList<>();
            for (Object key : itemRoot.getNode("enchants").getChildrenMap().keySet()) {
                int level = itemRoot.getNode("enchants").getChildrenMap().get(key).getInt();
                String enchantID = (String) key;
                Optional<EnchantmentType> pEnchantType = Sponge.getRegistry().getType(EnchantmentType.class, enchantID);
                if (!pEnchantType.isPresent()) {
                    HuskyCrates.instance.logger.error("INVALID ENCHANT ID: \"" + key + "\" || " + itemRoot.getNode("name").getString("(no name)") + " (item #" + itemRoot.getKey() + ") || " + itemRoot.getParent().getParent().getKey());
                    return ItemStack.of(ItemTypes.NONE, 1);
                }
                Enchantment pEnchant = Enchantment.of(pEnchantType.get(), level);
                enchantments.add(pEnchant);
            }
            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();
        } else if (dmg != null) {
            item = ItemStack.builder().fromContainer(// OVERRIDE DAMAGE VAL! :)
            item.toContainer().set(DataQuery.of("UnsafeDamage"), dmg)).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 : Optional(java.util.Optional) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) ItemType(org.spongepowered.api.item.ItemType) BiMap(com.google.common.collect.BiMap) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) LinkedHashMap(java.util.LinkedHashMap) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 52 with ItemType

use of org.spongepowered.api.item.ItemType in project Skree by Skelril.

the class CursedMineInstance method sweepFloor.

public void sweepFloor() {
    for (Item item : getContained(Item.class)) {
        if (!contains(item)) {
            continue;
        }
        ItemType id = item.getItemType();
        for (ItemType aItem : ITEMS) {
            if (aItem == id) {
                ItemStackSnapshot snapshot = item.get(Keys.REPRESENTED_ITEM).get();
                int newAmt = (int) (snapshot.getQuantity() * .8);
                if (newAmt < 1) {
                    item.remove();
                } else {
                    ItemStack newStack = snapshot.createStack();
                    newStack.setQuantity(newAmt);
                    item.offer(Keys.REPRESENTED_ITEM, newStack.createSnapshot());
                }
                break;
            }
        }
    }
}
Also used : Item(org.spongepowered.api.entity.Item) ItemType(org.spongepowered.api.item.ItemType) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack)

Example 53 with ItemType

use of org.spongepowered.api.item.ItemType in project Skree by Skelril.

the class FocusTeleporter method onRightClick.

@Listener
public void onRightClick(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
    Optional<org.spongepowered.api.item.inventory.ItemStack> optHeldItem = player.getItemInHand(HandTypes.MAIN_HAND);
    if (!optHeldItem.isPresent()) {
        return;
    }
    org.spongepowered.api.item.inventory.ItemStack held = optHeldItem.get();
    if (held.getItem() != this) {
        return;
    }
    Optional<Location<World>> optDestination = getDestination(held);
    if (!optDestination.isPresent()) {
        return;
    }
    Inventory result = player.getInventory().query((ItemType) Sponge.getRegistry().getType(ItemType.class, "skree:ender_focus").get());
    if (result.size() > 0) {
        Task.builder().execute(() -> {
            result.poll(1);
            player.setLocation(optDestination.get());
        }).delayTicks(1).submit(SkreePlugin.inst());
        event.setUseBlockResult(Tristate.FALSE);
    }
}
Also used : ItemType(org.spongepowered.api.item.ItemType) ItemStack(net.minecraft.item.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) Location(org.spongepowered.api.world.Location) Listener(org.spongepowered.api.event.Listener)

Aggregations

ItemType (org.spongepowered.api.item.ItemType)53 ItemStack (org.spongepowered.api.item.inventory.ItemStack)19 BlockState (org.spongepowered.api.block.BlockState)12 Listener (org.spongepowered.api.event.Listener)12 World (org.spongepowered.api.world.World)11 Optional (java.util.Optional)9 Text (org.spongepowered.api.text.Text)8 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)7 ArrayList (java.util.ArrayList)7 Sponge (org.spongepowered.api.Sponge)7 BlockType (org.spongepowered.api.block.BlockType)7 List (java.util.List)6 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)6 Player (org.spongepowered.api.entity.living.player.Player)6 Vector3d (com.flowpowered.math.vector.Vector3d)5 CommandResult (org.spongepowered.api.command.CommandResult)5 Collectors (java.util.stream.Collectors)4 IBlockState (net.minecraft.block.state.IBlockState)4 Inventory (org.spongepowered.api.item.inventory.Inventory)4 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)4