Search in sources :

Example 6 with EquipmentType

use of org.spongepowered.api.item.inventory.equipment.EquipmentType in project SpongeCommon by SpongePowered.

the class LivingEntityMixin_EquipmentFabric_Inventory method lensGeneratorBridge$generateLens.

@Override
public Lens lensGeneratorBridge$generateLens(SlotLensProvider slotLensProvider) {
    Map<EquipmentType, SlotLens> equipmentLenses = new LinkedHashMap<>();
    for (int i = 0, slotsLength = EquipmentSlot.values().length; i < slotsLength; i++) {
        EquipmentSlot slot = EquipmentSlot.values()[i];
        equipmentLenses.put((EquipmentType) (Object) slot, slotLensProvider.getSlotLens(i));
    }
    return new EquipmentInventoryLens(equipmentLenses);
}
Also used : EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) EquipmentInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.EquipmentInventoryLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with EquipmentType

use of org.spongepowered.api.item.inventory.equipment.EquipmentType in project SpongeCommon by SpongePowered.

the class DamageEventHandler method createArmorModifiers.

public static Optional<List<DamageFunction>> createArmorModifiers(EntityLivingBase entityLivingBase, DamageSource damageSource, double damage) {
    if (!damageSource.isUnblockable()) {
        damage *= 25;
        net.minecraft.item.ItemStack[] inventory = Iterables.toArray(entityLivingBase.getArmorInventoryList(), net.minecraft.item.ItemStack.class);
        List<DamageFunction> modifiers = new ArrayList<>();
        List<DamageObject> damageObjects = new ArrayList<>();
        for (int index = 0; index < inventory.length; index++) {
            net.minecraft.item.ItemStack itemStack = inventory[index];
            if (itemStack.isEmpty()) {
                continue;
            }
            Item item = itemStack.getItem();
            if (item instanceof ItemArmor) {
                ItemArmor armor = (ItemArmor) item;
                double reduction = armor.damageReduceAmount / 25D;
                DamageObject object = new DamageObject();
                object.slot = index;
                object.ratio = reduction;
                damageObjects.add(object);
            }
        }
        boolean first = true;
        double ratio = 0;
        for (DamageObject prop : damageObjects) {
            EquipmentType type = resolveEquipment(prop.slot);
            final DamageObject object = new DamageObject();
            object.ratio = ratio;
            if (first) {
                object.previousDamage = damage;
                object.augment = true;
            }
            DoubleUnaryOperator function = incomingDamage -> {
                incomingDamage *= 25;
                if (object.augment) {
                    // This is the damage that needs to be archived for the "first" armor modifier
                    // function since the armor modifiers work based on the initial damage and not as
                    // a chain one after another.
                    damageToHandle = incomingDamage;
                }
                double functionDamage = damageToHandle;
                object.previousDamage = functionDamage;
                object.ratio = prop.ratio;
                object.ratio += prop.ratio;
                return -((functionDamage * prop.ratio) / 25);
            };
            ratio += prop.ratio;
            // TODO: direct cause creation: bad bad bad
            DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), ((org.spongepowered.api.item.inventory.ItemStack) inventory[prop.slot]).createSnapshot(), // We need this property to refer to the slot.
            prop, // We need this object later on.
            object)).type(DamageModifierTypes.ARMOR).build();
            modifiers.add(new DamageFunction(modifier, function));
            first = false;
        }
        if (!modifiers.isEmpty()) {
            return Optional.of(modifiers);
        }
    }
    return Optional.empty();
}
Also used : EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Item(net.minecraft.item.Item) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) NBTTagList(net.minecraft.nbt.NBTTagList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) EntityDamageSourceIndirect(net.minecraft.util.EntityDamageSourceIndirect) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ItemArmor(net.minecraft.item.ItemArmor) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) NbtDataUtil(org.spongepowered.common.data.util.NbtDataUtil) Location(org.spongepowered.api.world.Location) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) Predicate(java.util.function.Predicate) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) EntityUtil(org.spongepowered.common.entity.EntityUtil) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(org.spongepowered.api.world.World) Optional(java.util.Optional) MobEffects(net.minecraft.init.MobEffects) EventContext(org.spongepowered.api.event.cause.EventContext) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) Iterables(com.google.common.collect.Iterables) Enchantment(net.minecraft.enchantment.Enchantment) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) EntityEquipmentSlot(net.minecraft.inventory.EntityEquipmentSlot) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) Multimap(com.google.common.collect.Multimap) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Chunk(net.minecraft.world.chunk.Chunk) Entity(net.minecraft.entity.Entity) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) BlockPos(net.minecraft.util.math.BlockPos) DamageSource(net.minecraft.util.DamageSource) IBlockState(net.minecraft.block.state.IBlockState) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EntityLivingBase(net.minecraft.entity.EntityLivingBase) MathHelper(net.minecraft.util.math.MathHelper) EntityDamageSource(net.minecraft.util.EntityDamageSource) EnumCreatureAttribute(net.minecraft.entity.EnumCreatureAttribute) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) ItemArmor(net.minecraft.item.ItemArmor) ArrayList(java.util.ArrayList) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Item(net.minecraft.item.Item) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 8 with EquipmentType

use of org.spongepowered.api.item.inventory.equipment.EquipmentType in project SpongeCommon by SpongePowered.

the class LivingEntityMixin_Inventory method impl$getSpongeSlot.

@SuppressWarnings("ConstantConditions")
protected Slot impl$getSpongeSlot(final EquipmentSlot equipmentSlot) {
    final EquipmentType equipmentType = (EquipmentType) (Object) equipmentSlot;
    if (this instanceof InventoryBridge) {
        final InventoryAdapter adapter = ((InventoryBridge) this).bridge$getAdapter();
        final Lens lens = adapter.inventoryAdapter$getRootLens();
        if (lens instanceof EquipmentInventoryLens) {
            final SlotLens slotLens = ((EquipmentInventoryLens) lens).getSlotLens(equipmentType);
            return slotLens.getAdapter(adapter.inventoryAdapter$getFabric(), (Inventory) adapter);
        }
        throw new IllegalStateException("Expected EquipmentInventoryLens for " + this.getClass().getName() + " Inventory but found: " + lens.getClass().getName());
    }
    throw new IllegalStateException("Living Entity has no InventoryAdapter: " + this.getClass().getName());
}
Also used : InventoryAdapter(org.spongepowered.common.inventory.adapter.InventoryAdapter) InventoryBridge(org.spongepowered.common.bridge.world.inventory.InventoryBridge) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) Lens(org.spongepowered.common.inventory.lens.Lens) EquipmentInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.EquipmentInventoryLens) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) EquipmentInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.EquipmentInventoryLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens)

Example 9 with EquipmentType

use of org.spongepowered.api.item.inventory.equipment.EquipmentType in project modules-extra by CubeEngine.

the class PlayerCommands method hat.

@Command(desc = "Gives a player a hat")
public void hat(CommandSource context, @Optional String item, @Named({ "player", "p" }) Player player, @Flag boolean quiet) {
    ItemStack head;
    if (player != null) {
        if (!context.hasPermission(module.perms().COMMAND_HAT_OTHER.getId())) {
            i18n.send(context, NEGATIVE, "You can't set the hat of an other player.");
            return;
        }
    } else if (context instanceof Player) {
        player = (Player) context;
    } else {
        i18n.send(context, NEGATIVE, "You have to specify a player!");
        return;
    }
    if (item != null) {
        if (!context.hasPermission(module.perms().COMMAND_HAT_ITEM.getId())) {
            i18n.send(context, NEGATIVE, "You can only use your item in hand!");
            return;
        }
        head = materialMatcher.itemStack(item);
        if (head == null) {
            i18n.send(context, NEGATIVE, "Item not found!");
            return;
        }
    } else if (context instanceof Player) {
        head = ((Player) context).getItemInHand(MAIN_HAND).orElse(null);
    } else {
        i18n.send(context, NEGATIVE, "Trying to be Notch? No hat for you!");
        i18n.send(context, NEUTRAL, "Please specify an item!");
        return;
    }
    if (head == null) {
        i18n.send(context, NEGATIVE, "You do not have any item in your hand!");
        return;
    }
    EquipmentType type = head.getType().getDefaultProperty(EquipmentProperty.class).map(EquipmentProperty::getValue).orElse(null);
    if (type == null || type != EquipmentTypes.HEADWEAR) {
        if (!context.hasPermission(module.perms().COMMAND_HAT_MORE_ARMOR.getId())) {
            i18n.send(context, NEGATIVE, "You are not allowed to use other armor as headpiece");
            return;
        }
    }
    head.setQuantity(1);
    if (item == null && context instanceof Player) {
        ItemStack clone = head.copy();
        clone.setQuantity(head.getQuantity() - 1);
        ((Player) context).setItemInHand(MAIN_HAND, clone);
    }
    if (player.getHelmet().isPresent()) {
        player.getInventory().offer(player.getHelmet().get());
    }
    player.setHelmet(head);
    if (!(quiet && context.hasPermission(module.perms().COMMAND_HAT_QUIET.getId())) && player.hasPermission(module.perms().COMMAND_HAT_NOTIFY.getId())) {
        i18n.send(player, POSITIVE, "Your hat was changed");
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ItemStack(org.spongepowered.api.item.inventory.ItemStack) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Command(org.cubeengine.butler.parametric.Command)

Example 10 with EquipmentType

use of org.spongepowered.api.item.inventory.equipment.EquipmentType 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)

Aggregations

EquipmentType (org.spongepowered.api.item.inventory.equipment.EquipmentType)10 Optional (java.util.Optional)4 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)4 SlotLens (org.spongepowered.common.inventory.lens.slots.SlotLens)4 Map (java.util.Map)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Lists (com.google.common.collect.Lists)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 DoubleUnaryOperator (java.util.function.DoubleUnaryOperator)2 Predicate (java.util.function.Predicate)2 IBlockState (net.minecraft.block.state.IBlockState)2 Entity (net.minecraft.entity.Entity)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 ItemArmor (net.minecraft.item.ItemArmor)2 DamageSource (net.minecraft.util.DamageSource)2 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)2