Search in sources :

Example 41 with ItemType

use of org.spongepowered.api.item.ItemType in project LanternServer by LanternPowered.

the class LanternIngredientBuilder method with.

@Override
public IIngredient.Builder with(ItemType... types) {
    checkNotNull(types, "types");
    for (ItemType type : types) {
        checkNotNull(type, "type");
        this.matchers.add(type::matches);
    }
    return withDisplay(types);
}
Also used : ItemType(org.spongepowered.api.item.ItemType)

Example 42 with ItemType

use of org.spongepowered.api.item.ItemType in project LanternServer by LanternPowered.

the class EquipmentItemFilter method of.

/**
 * Constructs a {@link ItemFilter} for the provided
 * {@link EquipmentSlotType} property.
 *
 * @param equipmentSlotType The equipment slot type property
 * @return The equipment item filter
 */
static EquipmentItemFilter of(EquipmentSlotType equipmentSlotType) {
    checkNotNull(equipmentSlotType, "equipmentSlotType");
    final EquipmentType slotEquipmentType = equipmentSlotType.getValue();
    checkNotNull(slotEquipmentType, "value");
    final Property.Operator operator = equipmentSlotType.getOperator();
    checkArgument(operator == Property.Operator.EQUAL || operator == Property.Operator.NOTEQUAL, "Only the operators EQUAL and NOTEQUAL are supported, %s is not.", operator);
    return new EquipmentItemFilter() {

        @Override
        public boolean isValid(EquipmentType equipmentType) {
            final boolean result = ((LanternEquipmentType) slotEquipmentType).isChild(equipmentType);
            return (operator == Property.Operator.EQUAL) == result;
        }

        private boolean isValid(Optional<EquipmentProperty> optEquipmentProperty) {
            return optEquipmentProperty.map(property -> {
                final EquipmentType equipmentType = property.getValue();
                final boolean result = ((LanternEquipmentType) slotEquipmentType).isChild(equipmentType);
                return (operator == Property.Operator.EQUAL) == result;
            }).orElse(false);
        }

        @Override
        public boolean isValid(ItemStack stack) {
            return isValid(stack.getProperty(EquipmentProperty.class));
        }

        @Override
        public boolean isValid(ItemStackSnapshot stack) {
            return isValid(stack.getProperty(EquipmentProperty.class));
        }

        @Override
        public boolean isValid(ItemType type) {
            return isValid(type.getDefaultProperty(EquipmentProperty.class));
        }
    };
}
Also used : LanternEquipmentType(org.lanternpowered.server.inventory.equipment.LanternEquipmentType) EquipmentProperty(org.spongepowered.api.data.property.item.EquipmentProperty) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Predicate(java.util.function.Predicate) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Property(org.spongepowered.api.data.Property) Optional(java.util.Optional) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemType(org.spongepowered.api.item.ItemType) EquipmentSlotType(org.spongepowered.api.item.inventory.property.EquipmentSlotType) LanternEquipmentType(org.lanternpowered.server.inventory.equipment.LanternEquipmentType) Optional(java.util.Optional) EquipmentProperty(org.spongepowered.api.data.property.item.EquipmentProperty) ItemType(org.spongepowered.api.item.ItemType) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternEquipmentType(org.lanternpowered.server.inventory.equipment.LanternEquipmentType) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) EquipmentProperty(org.spongepowered.api.data.property.item.EquipmentProperty) Property(org.spongepowered.api.data.Property)

Example 43 with ItemType

use of org.spongepowered.api.item.ItemType in project LanternServer by LanternPowered.

the class ItemStackStore method deserialize.

@Override
public LanternItemStack deserialize(DataView dataView) throws InvalidDataException {
    final String identifier = dataView.getString(IDENTIFIER).get();
    final ItemType itemType = ItemRegistryModule.get().getById(identifier).orElseThrow(() -> new InvalidDataException("There is no item type with the id: " + identifier));
    final LanternItemStack itemStack = new LanternItemStack(itemType);
    deserialize(itemStack, dataView);
    return itemStack;
}
Also used : ItemType(org.spongepowered.api.item.ItemType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 44 with ItemType

use of org.spongepowered.api.item.ItemType in project LanternServer by LanternPowered.

the class CommandParticleEffect method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(GenericArguments.catalogedElement(Text.of("type"), ParticleType.class), GenericArguments.vector3d(Text.of("position")), GenericArguments.optional(GenericArguments.world(Text.of("world"))), // TODO: Tab complaining is currently throwing errors, but it's a small bug in SpongeAPI
    GenericArguments.flags().valueFlag(GenericArguments.integer(Text.of("quantity")), "-quantity", "q").valueFlag(GenericArguments.vector3d(Text.of("offset")), "-offset", "o").valueFlag(GenericArguments.vector3d(Text.of("velocity")), "-velocity", "v").valueFlag(GenericArguments.vector3d(Text.of("color")), "-color", "c").valueFlag(GenericArguments.doubleNum(Text.of("scale")), "-scale", "s").valueFlag(GenericArguments.catalogedElement(Text.of("note"), NotePitch.class), "-note", "n").valueFlag(GenericArguments.catalogedElement(Text.of("block"), BlockState.class), "-block", "b").valueFlag(GenericArguments.catalogedElement(Text.of("item"), ItemType.class), "-item", "i").valueFlag(GenericArguments.catalogedElement(Text.of("potion"), PotionEffectType.class), "-potion", "p").buildWith(GenericArguments.none())).executor((src, args) -> {
        final ParticleType particleType = args.<ParticleType>getOne("type").get();
        final Vector3d position = args.<Vector3d>getOne("position").get();
        final World world = args.<WorldProperties>getOne("world").map(props -> Sponge.getServer().getWorld(props.getUniqueId()).get()).orElseGet(((Locatable) src)::getWorld);
        final ParticleEffect.Builder builder = ParticleEffect.builder().type(particleType);
        args.<Integer>getOne("quantity").ifPresent(builder::quantity);
        args.<Vector3d>getOne("offset").ifPresent(builder::offset);
        args.<Vector3d>getOne("velocity").ifPresent(builder::velocity);
        args.<Vector3d>getOne("color").ifPresent(color -> builder.option(ParticleOptions.COLOR, Color.of(color.toInt())));
        args.<NotePitch>getOne("note").ifPresent(note -> builder.option(ParticleOptions.NOTE, note));
        args.<Double>getOne("scale").ifPresent(scale -> builder.option(ParticleOptions.SCALE, scale));
        args.<BlockState>getOne("block").ifPresent(blockState -> builder.option(ParticleOptions.BLOCK_STATE, blockState));
        args.<ItemType>getOne("item").ifPresent(item -> builder.option(ParticleOptions.ITEM_STACK_SNAPSHOT, new LanternItemStack(item).createSnapshot()));
        args.<PotionEffectType>getOne("potion").ifPresent(type -> builder.option(ParticleOptions.POTION_EFFECT_TYPE, type));
        world.spawnParticles(builder.build(), position);
        src.sendMessage(t("Successfully spawned the particle %s", particleType.getName()));
        return CommandResult.success();
    });
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) PotionEffectType(org.spongepowered.api.effect.potion.PotionEffectType) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) ParticleOptions(org.spongepowered.api.effect.particle.ParticleOptions) BlockTypes(org.spongepowered.api.block.BlockTypes) Sponge(org.spongepowered.api.Sponge) Vector3d(com.flowpowered.math.vector.Vector3d) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) BlockState(org.spongepowered.api.block.BlockState) ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) ParticleType(org.spongepowered.api.effect.particle.ParticleType) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Text(org.spongepowered.api.text.Text) Locatable(org.spongepowered.api.world.Locatable) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) NotePitch(org.spongepowered.api.data.type.NotePitch) Color(org.spongepowered.api.util.Color) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) ItemType(org.spongepowered.api.item.ItemType) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ParticleEffect(org.spongepowered.api.effect.particle.ParticleEffect) Vector3d(com.flowpowered.math.vector.Vector3d) ItemType(org.spongepowered.api.item.ItemType) ParticleType(org.spongepowered.api.effect.particle.ParticleType) World(org.spongepowered.api.world.World) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) NotePitch(org.spongepowered.api.data.type.NotePitch)

Example 45 with ItemType

use of org.spongepowered.api.item.ItemType in project LanternServer by LanternPowered.

the class BlockTypeBuilderImpl method build.

@Override
public LanternBlockType build(String pluginId, String id) {
    MutableBehaviorPipeline<Behavior> behaviorPipeline = this.behaviorPipeline;
    if (behaviorPipeline == null) {
        behaviorPipeline = new MutableBehaviorPipelineImpl<>(Behavior.class, new ArrayList<>());
    } else {
        behaviorPipeline = new MutableBehaviorPipelineImpl<>(Behavior.class, new ArrayList<>(behaviorPipeline.getBehaviors()));
    }
    TranslationProvider translationProvider = this.translationProvider;
    if (translationProvider == null) {
        String path = "tile." + id + ".name";
        if (!pluginId.equals("minecraft")) {
            path = pluginId + '.' + path;
        }
        translationProvider = TranslationProvider.of(tr(path));
    }
    PropertyProviderCollection.Builder properties;
    if (this.propertiesBuilder != null) {
        properties = this.propertiesBuilder;
    } else {
        properties = PropertyProviderCollections.DEFAULT.toBuilder();
    }
    ExtendedBlockStateProvider extendedBlockStateProvider = this.extendedBlockStateProvider;
    if (extendedBlockStateProvider == null) {
        extendedBlockStateProvider = new ExtendedBlockStateProvider() {

            @Override
            public BlockState get(BlockState blockState, @Nullable Location<World> location, @Nullable Direction face) {
                return blockState;
            }

            @Override
            public BlockState remove(BlockState blockState) {
                return blockState;
            }
        };
    }
    final LanternBlockType blockType = new LanternBlockType(pluginId, id, this.traits, translationProvider, behaviorPipeline, this.tileEntityProvider, extendedBlockStateProvider);
    // Override the default solid cube property provider if necessary
    final PropertyProvider<SolidCubeProperty> provider = properties.build().get(SolidCubeProperty.class).orElse(null);
    ObjectProvider<AABB> boundingBoxProvider = this.boundingBoxProvider;
    if (boundingBoxProvider instanceof SimpleObjectProvider) {
        // noinspection unchecked
        boundingBoxProvider = new CachedSimpleObjectProvider(blockType, ((SimpleObjectProvider) boundingBoxProvider).getProvider());
    }
    // noinspection ConstantConditions
    if (provider instanceof ConstantObjectProvider && provider.get(null, null, null).getValue()) {
        if (boundingBoxProvider instanceof ConstantObjectProvider) {
            // noinspection ConstantConditions
            final AABB aabb = boundingBoxProvider.get(null, null, null);
            final boolean isSolid = isSolid(aabb);
            if (isSolid) {
                properties.add(solidCube(true));
                properties.add(solidSide(true));
            } else {
                properties.add(solidCube(false));
                final BitSet solidSides = compileSidePropertyBitSet(aabb);
                // Check if all the direction bits are set
                final byte[] bytes = solidSides.toByteArray();
                if (bytes.length == 0 || bytes[0] != (1 << DIRECTION_INDEXES) - 1) {
                    properties.add(solidSide((blockState, location, face) -> {
                        final int index = getDirectionIndex(face);
                        return index != -1 && solidSides.get(index);
                    }));
                } else {
                    properties.add(solidSide(false));
                }
            }
        } else if (boundingBoxProvider instanceof CachedSimpleObjectProvider) {
            final List<AABB> values = ((CachedSimpleObjectProvider<AABB>) boundingBoxProvider).getValues();
            final BitSet bitSet = new BitSet();
            int count = 0;
            for (int i = 0; i < values.size(); i++) {
                if (isSolid(values.get(i))) {
                    bitSet.set(i);
                    count++;
                }
            }
            final boolean flag1 = count == values.size();
            final boolean flag2 = count == 0;
            // Use the best possible solid cube property
            if (flag1) {
                properties.add(solidCube(true));
                properties.add(solidSide(false));
            } else if (flag2) {
                properties.add(solidCube(false));
            } else {
                properties.add(solidCube(((blockState, location, face) -> bitSet.get(((LanternBlockState) blockState).getInternalId()))));
            }
            if (!flag1) {
                final BitSet[] solidSides = new BitSet[values.size()];
                int solidCount = 0;
                for (int i = 0; i < values.size(); i++) {
                    solidSides[i] = compileSidePropertyBitSet(values.get(i));
                    // Check if all the direction bits are set
                    final byte[] bytes = solidSides[i].toByteArray();
                    if (bytes.length != 0 && bytes[0] == (1 << DIRECTION_INDEXES) - 1) {
                        solidCount++;
                    }
                }
                if (solidCount == 0) {
                    properties.add(solidSide(false));
                } else {
                    properties.add(solidSide((blockState, location, face) -> {
                        final int index = getDirectionIndex(face);
                        if (index == -1) {
                            return false;
                        }
                        final int state = ((LanternBlockState) blockState).getInternalId();
                        return solidSides[state].get(index);
                    }));
                }
            }
        } else {
            final ObjectProvider<AABB> boundingBoxProvider1 = boundingBoxProvider;
            properties.add(solidCube(((blockState, location, face) -> isSolid(boundingBoxProvider1.get(blockState, location, face)))));
            properties.add(solidSide(((blockState, location, face) -> isSideSolid(boundingBoxProvider1.get(blockState, location, face), face))));
        }
    }
    blockType.setBoundingBoxProvider(boundingBoxProvider);
    blockType.setPropertyProviderCollection(properties.build());
    if (this.defaultStateProvider != null) {
        blockType.setDefaultBlockState(this.defaultStateProvider.apply(blockType.getDefaultState()));
    }
    if (this.itemTypeBuilder != null) {
        final ItemType itemType = this.itemTypeBuilder.blockType(blockType).behaviors(pipeline -> {
            // Only add the default behavior if there isn't any interaction behavior present
            if (pipeline.pipeline(InteractWithItemBehavior.class).getBehaviors().isEmpty()) {
                pipeline.add(new InteractWithBlockItemBehavior());
            }
        }).build(blockType.getPluginId(), blockType.getName());
        blockType.setItemType(itemType);
    }
    return blockType;
}
Also used : PropertyProviders.solidSide(org.lanternpowered.server.block.provider.property.PropertyProviders.solidSide) BehaviorPipeline(org.lanternpowered.server.behavior.pipeline.BehaviorPipeline) Vector3d(com.flowpowered.math.vector.Vector3d) AABB(org.spongepowered.api.util.AABB) Behavior(org.lanternpowered.server.behavior.Behavior) InteractWithBlockItemBehavior(org.lanternpowered.server.item.behavior.simple.InteractWithBlockItemBehavior) Function(java.util.function.Function) Supplier(java.util.function.Supplier) BoundingBoxes(org.lanternpowered.server.block.aabb.BoundingBoxes) ArrayList(java.util.ArrayList) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) ObjectProvider(org.lanternpowered.server.block.provider.ObjectProvider) PropertyProviderCollection(org.lanternpowered.server.block.provider.property.PropertyProviderCollection) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) ItemTypeBuilderImpl(org.lanternpowered.server.item.ItemTypeBuilderImpl) SolidCubeProperty(org.spongepowered.api.data.property.block.SolidCubeProperty) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) PropertyProvider(org.lanternpowered.server.block.provider.property.PropertyProvider) ConstantObjectProvider(org.lanternpowered.server.block.provider.ConstantObjectProvider) Nullable(javax.annotation.Nullable) Translation(org.spongepowered.api.text.translation.Translation) MutableBehaviorPipeline(org.lanternpowered.server.behavior.pipeline.MutableBehaviorPipeline) Location(org.spongepowered.api.world.Location) TranslationHelper.tr(org.lanternpowered.server.text.translation.TranslationHelper.tr) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PropertyProviders.solidCube(org.lanternpowered.server.block.provider.property.PropertyProviders.solidCube) MutableBehaviorPipelineImpl(org.lanternpowered.server.behavior.pipeline.impl.MutableBehaviorPipelineImpl) PropertyProviderCollections(org.lanternpowered.server.block.provider.property.PropertyProviderCollections) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) LanternTileEntityType(org.lanternpowered.server.block.tile.LanternTileEntityType) BlockState(org.spongepowered.api.block.BlockState) Consumer(java.util.function.Consumer) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) ItemTypeBuilder(org.lanternpowered.server.item.ItemTypeBuilder) Direction(org.spongepowered.api.util.Direction) TileEntityType(org.spongepowered.api.block.tileentity.TileEntityType) List(java.util.List) SimpleObjectProvider(org.lanternpowered.server.block.provider.SimpleObjectProvider) World(org.spongepowered.api.world.World) BitSet(java.util.BitSet) ItemType(org.spongepowered.api.item.ItemType) InteractWithBlockItemBehavior(org.lanternpowered.server.item.behavior.simple.InteractWithBlockItemBehavior) PropertyProviderCollection(org.lanternpowered.server.block.provider.property.PropertyProviderCollection) SolidCubeProperty(org.spongepowered.api.data.property.block.SolidCubeProperty) ItemType(org.spongepowered.api.item.ItemType) ArrayList(java.util.ArrayList) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) ArrayList(java.util.ArrayList) List(java.util.List) Behavior(org.lanternpowered.server.behavior.Behavior) InteractWithBlockItemBehavior(org.lanternpowered.server.item.behavior.simple.InteractWithBlockItemBehavior) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) BitSet(java.util.BitSet) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) SimpleObjectProvider(org.lanternpowered.server.block.provider.SimpleObjectProvider) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) BlockState(org.spongepowered.api.block.BlockState) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) ConstantObjectProvider(org.lanternpowered.server.block.provider.ConstantObjectProvider) ObjectProvider(org.lanternpowered.server.block.provider.ObjectProvider) ConstantObjectProvider(org.lanternpowered.server.block.provider.ConstantObjectProvider) CachedSimpleObjectProvider(org.lanternpowered.server.block.provider.CachedSimpleObjectProvider) SimpleObjectProvider(org.lanternpowered.server.block.provider.SimpleObjectProvider) AABB(org.spongepowered.api.util.AABB)

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