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;
|
2020-10-17 23:13:20 +02:00
|
|
|
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;
|
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)
|
|
|
|
.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-01-24 20:49:09 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-17 23:13:20 +02:00
|
|
|
@Mod.EventBusSubscriber
|
|
|
|
private static final class EventHandler {
|
2020-01-24 20:49:09 +01:00
|
|
|
|
|
|
|
@SubscribeEvent
|
2020-10-17 23:13:20 +02:00
|
|
|
public static void onAttack(LivingAttackEvent event) {
|
2022-08-01 16:14:37 +02:00
|
|
|
var entity = event.getEntity();
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!entity.level.isClientSide) {
|
2022-06-27 15:24:04 +02:00
|
|
|
if (!ItemArmor.isFullSetEquipped(entity, ModArmorMaterial.INFUSED))
|
2020-01-24 20:49:09 +01:00
|
|
|
return;
|
2021-12-05 23:32:31 +01:00
|
|
|
var source = event.getSource().getEntity();
|
2020-01-24 20:49:09 +01:00
|
|
|
if (source instanceof LivingEntity)
|
2021-12-05 23:32:31 +01:00
|
|
|
((LivingEntity) source).addEffect(new MobEffectInstance(MobEffects.WITHER, 40));
|
2020-01-24 20:49:09 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-13 15:52:46 +02:00
|
|
|
|
|
|
|
@SubscribeEvent
|
2020-10-17 23:13:20 +02:00
|
|
|
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();
|
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);
|
2023-02-15 23:45:50 +01:00
|
|
|
// TODO use new forge attribute for step height
|
2021-12-05 23:32:31 +01:00
|
|
|
player.maxUpStep = 1.1F;
|
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);
|
2021-12-05 23:32:31 +01:00
|
|
|
player.maxUpStep = 0.6F;
|
2022-06-27 15:24:04 +02:00
|
|
|
speed.removeModifier(ItemArmor.SKY_MOVEMENT_MODIFIER);
|
2020-05-13 15:52:46 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 20:49:09 +01:00
|
|
|
}
|
2018-12-01 18:56:05 +01:00
|
|
|
}
|