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);
}
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();
}
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());
}
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");
}
}
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));
}
};
}
Aggregations