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

117 lines
5.4 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;
2023-03-02 14:40:57 +01:00
import net.minecraft.world.entity.player.Player;
2021-12-05 23:32:31 +01:00
import net.minecraft.world.item.*;
2023-03-02 14:40:57 +01:00
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.ForgeMod;
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.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
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 AttributeModifier SKY_STEP_MODIFIER = new AttributeModifier(UUID.fromString("ac3ce414-7243-418c-97f8-ac84c2c102f6"), NaturesAura.MOD_ID + ":sky_step_modifier", 0.5F, AttributeModifier.Operation.ADDITION);
private static final Map<ArmorMaterial, Item[]> SETS = new ConcurrentHashMap<>();
2018-12-01 18:56:05 +01:00
private final String baseName;
2023-07-08 12:32:27 +02:00
public ItemArmor(String baseName, ArmorMaterial materialIn, ArmorItem.Type equipmentSlotIn) {
super(materialIn, equipmentSlotIn, new Properties());
2018-12-01 18:56:05 +01:00
this.baseName = baseName;
2022-06-27 15:24:04 +02:00
ModRegistry.ALL_ITEMS.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) {
2022-06-27 15:24:04 +02:00
var set = ItemArmor.SETS.computeIfAbsent(material, m -> ForgeRegistries.ITEMS.getValues().stream()
2021-12-05 23:32:31 +01:00
.filter(i -> i instanceof ItemArmor && ((ItemArmor) i).getMaterial() == material)
2023-07-08 12:32:27 +02:00
.sorted(Comparator.comparingInt(i -> ((ItemArmor) i).getEquipmentSlot().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) {
var entity = event.getEntity();
2023-07-08 12:32:27 +02:00
if (!entity.level().isClientSide) {
2023-03-02 14:40:57 +01:00
if (ItemArmor.isFullSetEquipped(entity, ModArmorMaterial.INFUSED)) {
var source = event.getSource().getEntity();
if (source instanceof LivingEntity)
((LivingEntity) source).addEffect(new MobEffectInstance(MobEffects.WITHER, 40));
} else if (ItemArmor.isFullSetEquipped(entity, ModArmorMaterial.DEPTH)) {
for (var other : entity.level().getEntitiesOfClass(LivingEntity.class, new AABB(entity.position(), entity.position()).inflate(2))) {
if (other != entity && (!(entity instanceof Player player) || !player.isAlliedTo(other)))
2023-03-02 14:40:57 +01:00
other.addEffect(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 60, 255));
}
}
}
}
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 step = player.getAttribute(ForgeMod.STEP_HEIGHT_ADDITION.get());
2021-12-05 23:32:31 +01:00
var key = NaturesAura.MOD_ID + ":sky_equipped";
var nbt = player.getPersistentData();
2022-06-27 15:24:04 +02:00
var equipped = ItemArmor.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);
if (!step.hasModifier(ItemArmor.SKY_STEP_MODIFIER))
step.addPermanentModifier(ItemArmor.SKY_STEP_MODIFIER);
2022-06-27 15:24:04 +02:00
if (!speed.hasModifier(ItemArmor.SKY_MOVEMENT_MODIFIER))
speed.addPermanentModifier(ItemArmor.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);
step.removeModifier(ItemArmor.SKY_STEP_MODIFIER);
2022-06-27 15:24:04 +02:00
speed.removeModifier(ItemArmor.SKY_MOVEMENT_MODIFIER);
2020-05-13 15:52:46 +02:00
}
}
}
2018-12-01 18:56:05 +01:00
}