NaturesAura/src/main/java/de/ellpeck/naturesaura/items/tools/ItemArmor.java

106 lines
4.5 KiB
Java
Raw Normal View History

2018-12-01 18:56:05 +01:00
package de.ellpeck.naturesaura.items.tools;
import de.ellpeck.naturesaura.Helper;
2019-11-04 19:08:49 +01:00
import de.ellpeck.naturesaura.NaturesAura;
2018-12-01 18:56:05 +01:00
import de.ellpeck.naturesaura.reg.IModItem;
2020-05-13 15:52:46 +02:00
import de.ellpeck.naturesaura.reg.ModArmorMaterial;
2020-01-22 01:32:26 +01:00
import de.ellpeck.naturesaura.reg.ModRegistry;
2018-12-01 18:56:05 +01:00
import net.minecraft.entity.Entity;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.LivingEntity;
2020-05-13 15:52:46 +02:00
import net.minecraft.entity.ai.attributes.AttributeModifier;
2020-09-22 03:17:02 +02:00
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.ai.attributes.ModifiableAttributeInstance;
2020-05-13 15:52:46 +02:00
import net.minecraft.entity.player.PlayerEntity;
2019-10-20 22:30:49 +02:00
import net.minecraft.inventory.EquipmentSlotType;
2020-05-13 15:52:46 +02:00
import net.minecraft.item.*;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.potion.EffectInstance;
2019-11-04 19:08:49 +01:00
import net.minecraft.potion.Effects;
2018-12-01 18:56:05 +01:00
import net.minecraftforge.common.capabilities.ICapabilityProvider;
2020-05-13 15:52:46 +02:00
import net.minecraftforge.event.TickEvent;
2018-12-01 18:56:05 +01:00
import net.minecraftforge.event.entity.living.LivingAttackEvent;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
2020-05-13 15:52:46 +02:00
import net.minecraftforge.registries.ForgeRegistries;
2018-12-01 18:56:05 +01:00
import javax.annotation.Nullable;
2021-01-14 23:15:02 +01:00
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
2018-12-01 18:56:05 +01:00
2020-01-29 00:40:28 +01:00
public class ItemArmor extends ArmorItem implements IModItem {
2018-12-01 18:56:05 +01:00
2020-05-13 15:52:46 +02:00
private static final AttributeModifier SKY_MOVEMENT_MODIFIER = new AttributeModifier(UUID.fromString("c1f96acc-e117-4dc1-a351-e196a4de6071"), NaturesAura.MOD_ID + ":sky_movement_speed", 0.15F, AttributeModifier.Operation.MULTIPLY_TOTAL);
private static final Map<IArmorMaterial, Item[]> SETS = new HashMap<>();
2018-12-01 18:56:05 +01:00
private final String baseName;
2020-01-26 01:41:49 +01:00
public ItemArmor(String baseName, IArmorMaterial materialIn, EquipmentSlotType equipmentSlotIn) {
2019-11-04 19:08:49 +01:00
super(materialIn, equipmentSlotIn, new Properties().group(NaturesAura.CREATIVE_TAB));
2018-12-01 18:56:05 +01:00
this.baseName = baseName;
2020-01-22 01:32:26 +01:00
ModRegistry.add(this);
2018-12-01 18:56:05 +01:00
}
2020-05-13 15:52:46 +02:00
public static boolean isFullSetEquipped(LivingEntity entity, IArmorMaterial material) {
Item[] set = SETS.computeIfAbsent(material, m -> ForgeRegistries.ITEMS.getValues().stream()
.filter(i -> i instanceof ItemArmor && ((ItemArmor) i).getArmorMaterial() == material)
.sorted(Comparator.comparingInt(i -> ((ItemArmor) i).getEquipmentSlot().ordinal()))
.toArray(Item[]::new));
2018-12-01 18:56:05 +01:00
for (int i = 0; i < 4; i++) {
2019-10-20 22:30:49 +02:00
EquipmentSlotType slot = EquipmentSlotType.values()[i + 2];
2018-12-01 18:56:05 +01:00
ItemStack stack = entity.getItemStackFromSlot(slot);
2020-05-13 17:57:31 +02:00
if (stack.getItem() != set[i] && (slot != EquipmentSlotType.CHEST || stack.getItem() != Items.ELYTRA))
2018-12-01 18:56:05 +01:00
return false;
}
return true;
}
2020-02-07 15:22:30 +01:00
@Override
public String getBaseName() {
return this.baseName;
}
@Nullable
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
return Helper.makeRechargeProvider(stack, false);
}
@Mod.EventBusSubscriber
private static final class EventHandler {
@SubscribeEvent
public static void onAttack(LivingAttackEvent event) {
LivingEntity entity = event.getEntityLiving();
if (!entity.world.isRemote) {
2020-05-13 15:52:46 +02:00
if (!isFullSetEquipped(entity, ModArmorMaterial.INFUSED))
return;
Entity source = event.getSource().getTrueSource();
if (source instanceof LivingEntity)
((LivingEntity) source).addPotionEffect(new EffectInstance(Effects.WITHER, 40));
}
}
2020-05-13 15:52:46 +02:00
@SubscribeEvent
public static void update(TickEvent.PlayerTickEvent event) {
2020-05-13 15:52:46 +02:00
PlayerEntity player = event.player;
2020-09-22 03:17:02 +02:00
ModifiableAttributeInstance speed = player.getAttribute(Attributes.MOVEMENT_SPEED);
2020-05-13 15:52:46 +02:00
String key = NaturesAura.MOD_ID + ":sky_equipped";
CompoundNBT nbt = player.getPersistentData();
boolean equipped = isFullSetEquipped(player, ModArmorMaterial.SKY);
if (equipped && !nbt.getBoolean(key)) {
// we just equipped it
nbt.putBoolean(key, true);
player.stepHeight = 1.1F;
if (!speed.hasModifier(SKY_MOVEMENT_MODIFIER))
2020-09-22 03:17:02 +02:00
speed.applyNonPersistentModifier(SKY_MOVEMENT_MODIFIER);
2020-05-13 15:52:46 +02:00
} else if (!equipped && nbt.getBoolean(key)) {
// we just unequipped it
nbt.putBoolean(key, false);
player.stepHeight = 0.6F;
speed.removeModifier(SKY_MOVEMENT_MODIFIER);
}
}
}
2018-12-01 18:56:05 +01:00
}