Search in sources :

Example 1 with LanternEquipmentType

use of org.lanternpowered.server.inventory.equipment.LanternEquipmentType 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 LanternEquipmentType

use of org.lanternpowered.server.inventory.equipment.LanternEquipmentType in project LanternServer by LanternPowered.

the class EquipmentTypeRegistryModule method registerDefaults.

@Override
public void registerDefaults() {
    register(new LanternEquipmentType("minecraft", "all", type -> true));
    register(new LanternEquipmentType("minecraft", "equipped", type -> type instanceof LanternHeldEquipmentType || type instanceof LanternWornEquipmentType));
    register(new LanternHeldEquipmentType("minecraft", "held", type -> type instanceof LanternHeldEquipmentType));
    register(new LanternHeldEquipmentType("minecraft", "main_hand"));
    register(new LanternHeldEquipmentType("minecraft", "off_hand"));
    register(new LanternWornEquipmentType("minecraft", "worn", type -> type instanceof LanternWornEquipmentType));
    register(new LanternWornEquipmentType("minecraft", "boots"));
    register(new LanternWornEquipmentType("minecraft", "chestplate"));
    register(new LanternWornEquipmentType("minecraft", "headwear"));
    register(new LanternWornEquipmentType("minecraft", "leggings"));
}
Also used : LanternEquipmentType(org.lanternpowered.server.inventory.equipment.LanternEquipmentType) LanternWornEquipmentType(org.lanternpowered.server.inventory.equipment.LanternWornEquipmentType) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LanternHeldEquipmentType(org.lanternpowered.server.inventory.equipment.LanternHeldEquipmentType) HandTypes(org.spongepowered.api.data.type.HandTypes) HandType(org.spongepowered.api.data.type.HandType) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) AdditionalPluginCatalogRegistryModule(org.lanternpowered.server.game.registry.AdditionalPluginCatalogRegistryModule) LanternEquipmentType(org.lanternpowered.server.inventory.equipment.LanternEquipmentType) LanternHeldEquipmentType(org.lanternpowered.server.inventory.equipment.LanternHeldEquipmentType) LanternWornEquipmentType(org.lanternpowered.server.inventory.equipment.LanternWornEquipmentType)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 LanternEquipmentType (org.lanternpowered.server.inventory.equipment.LanternEquipmentType)2 EquipmentType (org.spongepowered.api.item.inventory.equipment.EquipmentType)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Optional (java.util.Optional)1 Predicate (java.util.function.Predicate)1 AdditionalPluginCatalogRegistryModule (org.lanternpowered.server.game.registry.AdditionalPluginCatalogRegistryModule)1 LanternHeldEquipmentType (org.lanternpowered.server.inventory.equipment.LanternHeldEquipmentType)1 LanternWornEquipmentType (org.lanternpowered.server.inventory.equipment.LanternWornEquipmentType)1 Property (org.spongepowered.api.data.Property)1 EquipmentProperty (org.spongepowered.api.data.property.item.EquipmentProperty)1 HandType (org.spongepowered.api.data.type.HandType)1 HandTypes (org.spongepowered.api.data.type.HandTypes)1 ItemType (org.spongepowered.api.item.ItemType)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 EquipmentTypes (org.spongepowered.api.item.inventory.equipment.EquipmentTypes)1 EquipmentSlotType (org.spongepowered.api.item.inventory.property.EquipmentSlotType)1