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

103 lines
4.3 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;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-05 23:32:31 +01:00
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.*;
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);
2021-12-05 23:32:31 +01:00
private static final Map<ArmorMaterial, Item[]> SETS = new HashMap<>();
2018-12-01 18:56:05 +01:00
private final String baseName;
2021-12-05 23:32:31 +01:00
public ItemArmor(String baseName, ArmorMaterial materialIn, EquipmentSlot equipmentSlotIn) {
super(materialIn, equipmentSlotIn, new Properties().tab(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
}
2021-12-05 23:32:31 +01:00
public static boolean isFullSetEquipped(LivingEntity entity, ArmorMaterial material) {
var set = SETS.computeIfAbsent(material, m -> ForgeRegistries.ITEMS.getValues().stream()
.filter(i -> i instanceof ItemArmor && ((ItemArmor) i).getMaterial() == material)
.sorted(Comparator.comparingInt(i -> ((ItemArmor) i).getSlot().ordinal()))
2020-05-13 15:52:46 +02:00
.toArray(Item[]::new));
2021-12-05 23:32:31 +01:00
for (var i = 0; i < 4; i++) {
var slot = EquipmentSlot.values()[i + 2];
var stack = entity.getItemBySlot(slot);
if (stack.getItem() != set[i] && (slot != EquipmentSlot.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
2021-12-04 15:40:09 +01:00
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
2020-02-07 15:22:30 +01:00
return Helper.makeRechargeProvider(stack, false);
}
@Mod.EventBusSubscriber
private static final class EventHandler {
@SubscribeEvent
public static void onAttack(LivingAttackEvent event) {
2021-12-05 23:32:31 +01:00
var entity = event.getEntityLiving();
2021-12-04 15:40:09 +01:00
if (!entity.level.isClientSide) {
2020-05-13 15:52:46 +02:00
if (!isFullSetEquipped(entity, ModArmorMaterial.INFUSED))
return;
2021-12-05 23:32:31 +01:00
var source = event.getSource().getEntity();
if (source instanceof LivingEntity)
2021-12-05 23:32:31 +01:00
((LivingEntity) source).addEffect(new MobEffectInstance(MobEffects.WITHER, 40));
}
}
2020-05-13 15:52:46 +02:00
@SubscribeEvent
public static void update(TickEvent.PlayerTickEvent event) {
2021-12-05 23:32:31 +01:00
var player = event.player;
var speed = player.getAttribute(Attributes.MOVEMENT_SPEED);
var key = NaturesAura.MOD_ID + ":sky_equipped";
var nbt = player.getPersistentData();
var equipped = isFullSetEquipped(player, ModArmorMaterial.SKY);
2020-05-13 15:52:46 +02:00
if (equipped && !nbt.getBoolean(key)) {
// we just equipped it
nbt.putBoolean(key, true);
2021-12-05 23:32:31 +01:00
player.maxUpStep = 1.1F;
2020-05-13 15:52:46 +02:00
if (!speed.hasModifier(SKY_MOVEMENT_MODIFIER))
2021-12-05 23:32:31 +01:00
speed.addPermanentModifier(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);
2021-12-05 23:32:31 +01:00
player.maxUpStep = 0.6F;
2020-05-13 15:52:46 +02:00
speed.removeModifier(SKY_MOVEMENT_MODIFIER);
}
}
}
2018-12-01 18:56:05 +01:00
}