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.SharedMonsterAttributes;
|
|
|
|
import net.minecraft.entity.ai.attributes.AttributeModifier;
|
|
|
|
import net.minecraft.entity.ai.attributes.IAttributeInstance;
|
|
|
|
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.MinecraftForge;
|
|
|
|
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-05-13 15:52:46 +02:00
|
|
|
import net.minecraftforge.registries.ForgeRegistries;
|
2018-12-01 18:56:05 +01:00
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
2020-05-13 15:52:46 +02:00
|
|
|
import java.util.*;
|
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-24 20:49:09 +01:00
|
|
|
MinecraftForge.EVENT_BUS.register(new EventHandler());
|
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-01-24 20:49:09 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:49:09 +01:00
|
|
|
private static class EventHandler {
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public 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))
|
2020-01-24 20:49:09 +01:00
|
|
|
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 void update(TickEvent.PlayerTickEvent event) {
|
|
|
|
PlayerEntity player = event.player;
|
|
|
|
IAttributeInstance speed = player.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED);
|
|
|
|
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))
|
|
|
|
speed.applyModifier(SKY_MOVEMENT_MODIFIER);
|
|
|
|
} else if (!equipped && nbt.getBoolean(key)) {
|
|
|
|
// we just unequipped it
|
|
|
|
nbt.putBoolean(key, false);
|
|
|
|
player.stepHeight = 0.6F;
|
|
|
|
speed.removeModifier(SKY_MOVEMENT_MODIFIER);
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 20:49:09 +01:00
|
|
|
}
|
2018-12-01 18:56:05 +01:00
|
|
|
}
|