Search in sources :

Example 26 with ItemType

use of org.spongepowered.api.item.ItemType 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 27 with ItemType

use of org.spongepowered.api.item.ItemType in project SpongeCommon by SpongePowered.

the class SpongeItemStackBuilder method buildContent.

@Override
protected Optional<ItemStack> buildContent(DataView container) throws InvalidDataException {
    checkNotNull(container);
    if (!container.contains(DataQueries.ITEM_TYPE) || !container.contains(DataQueries.ITEM_COUNT) || !container.contains(DataQueries.ITEM_DAMAGE_VALUE)) {
        return Optional.empty();
    }
    final String itemTypeId = getData(container, DataQueries.ITEM_TYPE, String.class);
    final int count = getData(container, DataQueries.ITEM_COUNT, Integer.class);
    final ItemType itemType = SpongeImpl.getRegistry().getType(ItemType.class, itemTypeId).get();
    final int damage = getData(container, DataQueries.ITEM_DAMAGE_VALUE, Integer.class);
    final net.minecraft.item.ItemStack itemStack = new net.minecraft.item.ItemStack((Item) itemType, count, damage);
    if (container.contains(DataQueries.UNSAFE_NBT)) {
        final NBTTagCompound compound = NbtTranslator.getInstance().translateData(container.getView(DataQueries.UNSAFE_NBT).get());
        itemStack.setTagCompound(compound);
    }
    if (container.contains(DataQueries.DATA_MANIPULATORS)) {
        final List<DataView> views = container.getViewList(DataQueries.DATA_MANIPULATORS).get();
        final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(views);
        final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
        for (DataManipulator<?, ?> manipulator : manipulators) {
            ((IMixinCustomDataHolder) itemStack).offerCustom(manipulator, MergeFunction.IGNORE_ALL);
        }
        if (!transaction.failedData.isEmpty()) {
            ((IMixinCustomDataHolder) itemStack).addFailedData(transaction.failedData);
        }
    }
    return Optional.of((ItemStack) itemStack);
}
Also used : ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ItemType(org.spongepowered.api.item.ItemType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder) DataView(org.spongepowered.api.data.DataView) SerializedDataTransaction(org.spongepowered.common.data.persistence.SerializedDataTransaction) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 28 with ItemType

use of org.spongepowered.api.item.ItemType in project RedProtect by FabioZumbi12.

the class RPBlockListener method onBlockPlace.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockPlace(ChangeBlockEvent.Place e, @First Player p) {
    RedProtect.get().logger.debug("blocks", "BlockListener - Is BlockPlaceEvent event! Cancelled? " + e.isCancelled());
    BlockSnapshot b = e.getTransactions().get(0).getOriginal();
    Location<World> bloc = b.getLocation().get();
    World w = bloc.getExtent();
    ItemType m = ItemTypes.NONE;
    if (p.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        m = p.getItemInHand(HandTypes.MAIN_HAND).get().getItem();
    } else if (p.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
        m = p.getItemInHand(HandTypes.OFF_HAND).get().getItem();
    }
    Boolean antih = RedProtect.get().cfgs.getBool("region-settings.anti-hopper");
    Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get());
    if (r == null && RedProtect.get().cfgs.getGlobalFlagList(w.getName(), "if-build-false", "place-blocks").contains(b.getState().getType().getName())) {
        return;
    }
    if (r != null) {
        if (!r.canMinecart(p) && m.getName().contains("minecart")) {
            RPLang.sendMessage(p, "blocklistener.region.cantplace");
            e.setCancelled(true);
            return;
        }
        if (b.getState().getType().equals(BlockTypes.MOB_SPAWNER) && r.allowSpawner(p)) {
            return;
        }
        try {
            if (!r.canBuild(p) && !r.canPlace(b)) {
                RPLang.sendMessage(p, "blocklistener.region.cantbuild");
                e.setCancelled(true);
            } else {
                if (!RedProtect.get().ph.hasPerm(p, "redprotect.bypass") && antih && (m.equals(ItemTypes.HOPPER) || m.getName().contains("rail"))) {
                    int x = bloc.getBlockX();
                    int y = bloc.getBlockY();
                    int z = bloc.getBlockZ();
                    BlockSnapshot ib = w.createSnapshot(x, y + 1, z);
                    if (!cont.canBreak(p, ib) || !cont.canBreak(p, b)) {
                        RPLang.sendMessage(p, "blocklistener.container.chestinside");
                        e.setCancelled(true);
                    }
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
Also used : ItemType(org.spongepowered.api.item.ItemType) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Example 29 with ItemType

use of org.spongepowered.api.item.ItemType in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onInteractRight.

// listen right click
@Listener(order = Order.FIRST)
public void onInteractRight(InteractBlockEvent.Secondary event, @First Player p) {
    BlockSnapshot b = event.getTargetBlock();
    Location<World> l = null;
    RedProtect.get().logger.debug("player", "RPPlayerListener - Is InteractBlockEvent.Secondary event");
    if (!b.getState().getType().equals(BlockTypes.AIR)) {
        l = b.getLocation().get();
        RedProtect.get().logger.debug("player", "RPPlayerListener - Is InteractBlockEvent.Secondary event. The block is " + b.getState().getType().getName());
    } else {
        l = p.getLocation();
    }
    Region r = RedProtect.get().rm.getTopRegion(l);
    ItemType itemInHand = RPUtil.getItemHand(p);
    String claimmode = RedProtect.get().cfgs.getWorldClaimType(p.getWorld().getName());
    if (itemInHand.getId().equalsIgnoreCase(RedProtect.get().cfgs.getString("wands.adminWandID")) && ((claimmode.equalsIgnoreCase("WAND") || claimmode.equalsIgnoreCase("BOTH")) || RedProtect.get().ph.hasPerm(p, "RedProtect.get().admin.claim"))) {
        if (!RPUtil.canBuildNear(p, l)) {
            event.setCancelled(true);
            return;
        }
        RedProtect.get().secondLocationSelections.put(p, l);
        p.sendMessage(RPUtil.toText(RPLang.get("playerlistener.wand2") + RPLang.get("general.color") + " (&e" + l.getBlockX() + RPLang.get("general.color") + ", &e" + l.getBlockY() + RPLang.get("general.color") + ", &e" + l.getBlockZ() + RPLang.get("general.color") + ")."));
        event.setCancelled(true);
        // show preview border
        if (RedProtect.get().firstLocationSelections.containsKey(p) && RedProtect.get().secondLocationSelections.containsKey(p)) {
            Location<World> loc1 = RedProtect.get().firstLocationSelections.get(p);
            Location<World> loc2 = RedProtect.get().secondLocationSelections.get(p);
            if (loc1.getPosition().distanceSquared(loc2.getPosition()) > RedProtect.get().cfgs.getInt("region-settings.define-max-distance") && !RedProtect.get().ph.hasPerm(p, "redprotect.bypass.define-max-distance")) {
                Double dist = loc1.getPosition().distanceSquared(loc2.getPosition());
                RPLang.sendMessage(p, String.format(RPLang.get("regionbuilder.selection.maxdefine"), RedProtect.get().cfgs.getInt("region-settings.define-max-distance"), dist.intValue()));
            } else {
                RPUtil.addBorder(p, RPUtil.get4Points(loc1, loc2, p.getLocation().getBlockY()));
            }
        }
        return;
    }
    // other blocks and interactions
    if (r != null) {
        if ((itemInHand.equals(ItemTypes.ENDER_PEARL) || itemInHand.getName().equals("minecraft:chorus_fruit")) && !r.canTeleport(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantuse");
            event.setUseItemResult(Tristate.FALSE);
            event.setCancelled(true);
        } else if ((itemInHand.equals(ItemTypes.BOW) || itemInHand.equals(ItemTypes.SNOWBALL) || itemInHand.equals(ItemTypes.EGG)) && !r.canProtectiles(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantuse");
            event.setUseItemResult(Tristate.FALSE);
            event.setCancelled(true);
        } else if (itemInHand.equals(ItemTypes.POTION) && !r.usePotions(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantuse");
            event.setUseItemResult(Tristate.FALSE);
            event.setCancelled(true);
        } else if (itemInHand.equals(ItemTypes.MONSTER_EGG) && !r.canInteractPassives(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantuse");
            event.setUseItemResult(Tristate.FALSE);
            event.setCancelled(true);
        }
    }
}
Also used : ItemType(org.spongepowered.api.item.ItemType) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) World(org.spongepowered.api.world.World) Listener(org.spongepowered.api.event.Listener)

Example 30 with ItemType

use of org.spongepowered.api.item.ItemType in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onInteractLeft.

// listen left click
@Listener(order = Order.FIRST)
public void onInteractLeft(InteractBlockEvent.Primary event, @First Player p) {
    BlockSnapshot b = event.getTargetBlock();
    Location<World> l = null;
    RedProtect.get().logger.debug("player", "RPPlayerListener - Is InteractBlockEvent.Primary event");
    if (!b.getState().getType().equals(BlockTypes.AIR)) {
        l = b.getLocation().get();
        RedProtect.get().logger.debug("player", "RPPlayerListener - Is InteractBlockEvent.Primary event. The block is " + b.getState().getType().getName());
    } else {
        l = p.getLocation();
    }
    ItemType itemInHand = ItemTypes.NONE;
    if (p.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        itemInHand = p.getItemInHand(HandTypes.MAIN_HAND).get().getItem();
    } else if (p.getItemInHand(HandTypes.OFF_HAND).isPresent()) {
        itemInHand = p.getItemInHand(HandTypes.OFF_HAND).get().getItem();
    }
    String claimmode = RedProtect.get().cfgs.getWorldClaimType(p.getWorld().getName());
    if (itemInHand.getId().equalsIgnoreCase(RedProtect.get().cfgs.getString("wands.adminWandID")) && ((claimmode.equalsIgnoreCase("WAND") || claimmode.equalsIgnoreCase("BOTH")) || RedProtect.get().ph.hasPerm(p, "redprotect.admin.claim"))) {
        if (!RPUtil.canBuildNear(p, l)) {
            event.setCancelled(true);
            return;
        }
        RedProtect.get().firstLocationSelections.put(p, l);
        p.sendMessage(RPUtil.toText(RPLang.get("playerlistener.wand1") + RPLang.get("general.color") + " (&e" + l.getBlockX() + RPLang.get("general.color") + ", &e" + l.getBlockY() + RPLang.get("general.color") + ", &e" + l.getBlockZ() + RPLang.get("general.color") + ")."));
        event.setCancelled(true);
        // show preview border
        if (RedProtect.get().firstLocationSelections.containsKey(p) && RedProtect.get().secondLocationSelections.containsKey(p)) {
            Location<World> loc1 = RedProtect.get().firstLocationSelections.get(p);
            Location<World> loc2 = RedProtect.get().secondLocationSelections.get(p);
            if (loc1.getPosition().distanceSquared(loc2.getPosition()) > RedProtect.get().cfgs.getInt("region-settings.define-max-distance") && !p.hasPermission("redprotect.bypass.define-max-distance")) {
                Double dist = loc1.getPosition().distanceSquared(loc2.getPosition());
                RPLang.sendMessage(p, String.format(RPLang.get("regionbuilder.selection.maxdefine"), RedProtect.get().cfgs.getInt("region-settings.define-max-distance"), dist.intValue()));
            } else {
                RPUtil.addBorder(p, RPUtil.get4Points(loc1, loc2, p.getLocation().getBlockY()));
            }
        }
    }
}
Also used : ItemType(org.spongepowered.api.item.ItemType) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) 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