Search in sources :

Example 1 with Property

use of org.spongepowered.api.data.Property 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 2 with Property

use of org.spongepowered.api.data.Property in project Nucleus by NucleusPowered.

the class BlockInfoCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
    BlockRay<World> bl = BlockRay.from(player).distanceLimit(10).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).build();
    Optional<BlockRayHit<World>> ob = bl.end();
    // If the last block is not air...
    if (ob.isPresent() && ob.get().getLocation().getBlockType() != BlockTypes.AIR) {
        BlockRayHit<World> brh = ob.get();
        // get the information.
        BlockState b = brh.getLocation().getBlock();
        BlockType it = b.getType();
        List<Text> lt = new ArrayList<>();
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.id", it.getId(), it.getTranslation().get()));
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.extendedid", b.getId()));
        if (args.hasAny("e") || args.hasAny("extended")) {
            Collection<Property<?, ?>> cp = b.getApplicableProperties();
            if (!cp.isEmpty()) {
                cp.forEach(x -> {
                    if (x.getValue() != null) {
                        DataScanner.getText(player, "command.blockinfo.property.item", x.getKey().toString(), x.getValue()).ifPresent(lt::add);
                    }
                });
            }
            Collection<BlockTrait<?>> cb = b.getTraits();
            if (!cb.isEmpty()) {
                cb.forEach(x -> b.getTraitValue(x).ifPresent(v -> DataScanner.getText(player, "command.blockinfo.traits.item", x.getName(), v).ifPresent(lt::add)));
            }
        }
        Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.list.header", String.valueOf(brh.getBlockX()), String.valueOf(brh.getBlockY()), String.valueOf(brh.getBlockZ()))).sendTo(player);
        return CommandResult.success();
    }
    player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.none"));
    return CommandResult.empty();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) BlockRay(org.spongepowered.api.util.blockray.BlockRay) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ArrayList(java.util.ArrayList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) CommandResult(org.spongepowered.api.command.CommandResult) BlockTypes(org.spongepowered.api.block.BlockTypes) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) Property(org.spongepowered.api.data.Property) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Property(org.spongepowered.api.data.Property) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Aggregations

Optional (java.util.Optional)2 Property (org.spongepowered.api.data.Property)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 DataScanner (io.github.nucleuspowered.nucleus.internal.DataScanner)1 RunAsync (io.github.nucleuspowered.nucleus.internal.annotations.RunAsync)1 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)1 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)1 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)1 PermissionInformation (io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation)1 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 LanternEquipmentType (org.lanternpowered.server.inventory.equipment.LanternEquipmentType)1 Sponge (org.spongepowered.api.Sponge)1 BlockState (org.spongepowered.api.block.BlockState)1