2021-02-27 11:24:09 +01:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
2021-08-13 15:57:21 +02:00
|
|
|
import java.util.UUID;
|
2021-02-27 11:24:09 +01:00
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-02-27 11:24:09 +01:00
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableMultimap;
|
|
|
|
import com.google.common.collect.Multimap;
|
2022-09-20 23:50:15 +02:00
|
|
|
import com.minelittlepony.unicopia.trinkets.TrinketsDelegate;
|
2021-02-27 11:24:09 +01:00
|
|
|
|
|
|
|
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
|
|
|
|
import net.minecraft.client.MinecraftClient;
|
|
|
|
import net.minecraft.client.item.TooltipContext;
|
|
|
|
import net.minecraft.entity.EquipmentSlot;
|
|
|
|
import net.minecraft.entity.LivingEntity;
|
|
|
|
import net.minecraft.entity.attribute.EntityAttribute;
|
|
|
|
import net.minecraft.entity.attribute.EntityAttributeModifier;
|
2022-01-02 22:54:08 +01:00
|
|
|
import net.minecraft.item.ArmorMaterials;
|
2021-02-27 11:24:09 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2022-01-02 22:54:08 +01:00
|
|
|
import net.minecraft.sound.SoundEvent;
|
2021-02-27 11:24:09 +01:00
|
|
|
import net.minecraft.text.MutableText;
|
|
|
|
import net.minecraft.text.StringVisitable;
|
|
|
|
import net.minecraft.text.Style;
|
|
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.minecraft.util.Formatting;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2021-02-27 17:42:29 +01:00
|
|
|
public class AmuletItem extends WearableItem {
|
2021-02-27 11:24:09 +01:00
|
|
|
|
|
|
|
private final int maxEnergy;
|
|
|
|
|
|
|
|
private final ImmutableMultimap<EntityAttribute, EntityAttributeModifier> modifiers;
|
|
|
|
|
2021-03-03 19:32:51 +01:00
|
|
|
public AmuletItem(FabricItemSettings settings, int maxEnergy) {
|
2021-08-13 15:57:21 +02:00
|
|
|
this(settings, maxEnergy, ImmutableMultimap.of());
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|
|
|
|
|
2021-08-13 15:57:21 +02:00
|
|
|
public AmuletItem(FabricItemSettings settings, int maxEnergy, ImmutableMultimap<EntityAttribute, EntityAttributeModifier> modifiers) {
|
2021-02-27 17:42:29 +01:00
|
|
|
super(settings);
|
2021-02-27 11:24:09 +01:00
|
|
|
this.maxEnergy = maxEnergy;
|
2021-08-13 15:57:21 +02:00
|
|
|
this.modifiers = modifiers;
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> list, TooltipContext tooltipContext) {
|
|
|
|
|
|
|
|
for (StringVisitable line : MinecraftClient.getInstance().textRenderer.getTextHandler().wrapLines(
|
2022-06-25 00:19:55 +02:00
|
|
|
Text.translatable(getTranslationKey(stack) + ".lore"), 150, Style.EMPTY)) {
|
|
|
|
MutableText compiled = Text.literal("").formatted(Formatting.ITALIC, Formatting.GRAY);
|
2021-02-27 11:24:09 +01:00
|
|
|
line.visit(s -> {
|
|
|
|
compiled.append(s);
|
|
|
|
return Optional.empty();
|
|
|
|
});
|
|
|
|
list.add(compiled);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isChargable()) {
|
2022-06-25 00:19:55 +02:00
|
|
|
list.add(Text.translatable("item.unicopia.amulet.energy", (int)Math.floor(getEnergy(stack)), maxEnergy));
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 22:54:08 +01:00
|
|
|
@Override
|
|
|
|
public SoundEvent getEquipSound() {
|
|
|
|
return ArmorMaterials.IRON.getEquipSound();
|
|
|
|
}
|
|
|
|
|
2021-02-27 17:42:29 +01:00
|
|
|
@Override
|
|
|
|
public EquipmentSlot getPreferredSlot(ItemStack stack) {
|
|
|
|
return EquipmentSlot.CHEST;
|
|
|
|
}
|
|
|
|
|
2021-02-27 11:24:09 +01:00
|
|
|
@Override
|
|
|
|
public boolean hasGlint(ItemStack stack) {
|
|
|
|
return !isChargable() || stack.hasEnchantments() || getEnergy(stack) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(EquipmentSlot slot) {
|
2021-02-27 16:47:54 +01:00
|
|
|
return slot == EquipmentSlot.CHEST ? modifiers : ImmutableMultimap.of();
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isApplicable(ItemStack stack) {
|
2021-08-13 15:57:21 +02:00
|
|
|
return stack.getItem() == this && (!isChargable() || getEnergy(stack) > 0);
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isApplicable(LivingEntity entity) {
|
2022-09-20 23:50:15 +02:00
|
|
|
return isApplicable(getForEntity(entity));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack getForEntity(LivingEntity entity) {
|
|
|
|
return TrinketsDelegate.getInstance().getEquipped(entity, TrinketsDelegate.NECKLACE)
|
|
|
|
.filter(stack -> stack.getItem() instanceof AmuletItem)
|
|
|
|
.findFirst()
|
|
|
|
.orElse(ItemStack.EMPTY);
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isChargable() {
|
|
|
|
return maxEnergy > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canCharge(ItemStack stack) {
|
|
|
|
return isChargable() && getEnergy(stack) < maxEnergy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getChargeRemainder(ItemStack stack) {
|
|
|
|
return Math.max(0, maxEnergy - getEnergy(stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void consumeEnergy(ItemStack stack, float amount) {
|
|
|
|
setEnergy(stack, getEnergy(stack) - amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static float getEnergy(ItemStack stack) {
|
2021-12-22 10:15:09 +01:00
|
|
|
return stack.hasNbt() && stack.getNbt().contains("energy") ? stack.getNbt().getFloat("energy") : 0;
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void setEnergy(ItemStack stack, float energy) {
|
|
|
|
if (energy <= 0) {
|
2021-12-22 10:15:09 +01:00
|
|
|
stack.removeSubNbt("energy");
|
2021-02-27 11:24:09 +01:00
|
|
|
} else {
|
2021-12-22 10:15:09 +01:00
|
|
|
stack.getOrCreateNbt().putFloat("energy", energy);
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-13 15:57:21 +02:00
|
|
|
|
|
|
|
public static class ModifiersBuilder {
|
|
|
|
private static final UUID SLOT_UUID = UUID.fromString("9F3D476D-C118-4544-8365-64846904B48E");
|
|
|
|
|
|
|
|
private final ImmutableMultimap.Builder<EntityAttribute, EntityAttributeModifier> modifiers = new ImmutableMultimap.Builder<>();
|
|
|
|
|
|
|
|
public ModifiersBuilder add(EntityAttribute attribute, double amount) {
|
|
|
|
modifiers.put(attribute, new EntityAttributeModifier(SLOT_UUID, "Armor modifier", amount, EntityAttributeModifier.Operation.ADDITION));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImmutableMultimap<EntityAttribute, EntityAttributeModifier> build() {
|
|
|
|
return modifiers.build();
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 11:24:09 +01:00
|
|
|
}
|