Unicopia/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java

125 lines
4.4 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.item;
2022-12-19 00:12:49 +01:00
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
2021-08-04 15:38:03 +02:00
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.Affinity;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
2021-12-24 17:35:12 +01:00
import com.minelittlepony.unicopia.client.FlowingText;
import com.minelittlepony.unicopia.entity.player.PlayerCharmTracker;
import com.minelittlepony.unicopia.entity.player.Pony;
2022-12-19 00:12:49 +01:00
import com.minelittlepony.unicopia.item.group.MultiItem;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
public class GemstoneItem extends Item implements MultiItem, EnchantableItem {
public GemstoneItem(Settings settings) {
super(settings);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
TypedActionResult<ItemStack> result = super.use(world, user, hand);
if (!result.getResult().isAccepted()) {
ItemStack stack = user.getStackInHand(hand);
PlayerCharmTracker charms = Pony.of(user).getCharms();
if (!Pony.of(user).getObservedSpecies().canCast()) {
return result;
}
hand = user.isSneaking() ? Hand.OFF_HAND : Hand.MAIN_HAND;
TypedActionResult<CustomisedSpellType<?>> spell = EnchantableItem.consumeSpell(stack, user, ((Predicate<CustomisedSpellType<?>>)charms.getEquippedSpell(hand)::equals).negate(), true);
CustomisedSpellType<?> existing = charms.getEquippedSpell(hand);
if (!existing.isEmpty()) {
if (stack.getCount() == 1) {
stack = existing.traits().applyTo(EnchantableItem.enchant(stack, existing.type()));
} else {
user.giveItemStack(existing.traits().applyTo(EnchantableItem.enchant(stack.split(1), existing.type())));
}
}
if (spell.getResult().isAccepted()) {
charms.equipSpell(hand, spell.getValue());
} else {
if (existing.isEmpty()) {
return result;
}
charms.equipSpell(hand, SpellType.EMPTY_KEY.withTraits());
}
user.getItemCooldownManager().set(this, 20);
return TypedActionResult.success(stack, true);
}
return result;
}
@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> lines, TooltipContext tooltipContext) {
2021-11-21 17:50:02 +01:00
super.appendTooltip(stack, world, lines, tooltipContext);
2021-03-02 14:40:37 +01:00
if (EnchantableItem.isEnchanted(stack)) {
SpellType<?> key = EnchantableItem.getSpellKey(stack);
2021-03-02 14:40:37 +01:00
2022-06-25 00:19:55 +02:00
MutableText line = Text.translatable(key.getTranslationKey() + ".lore").formatted(key.getAffinity().getColor());
if (!Unicopia.SIDE.getPlayerSpecies().canCast()) {
line = line.formatted(Formatting.OBFUSCATED);
}
2021-12-24 17:35:12 +01:00
lines.addAll(FlowingText.wrap(line, 180).toList());
2021-03-02 14:40:37 +01:00
}
}
@Override
2022-12-19 00:12:49 +01:00
public List<ItemStack> getDefaultStacks() {
return Arrays.stream(Affinity.VALUES)
.flatMap(i -> SpellType.byAffinity(i).stream()
.filter(type -> type.isObtainable())
.map(type -> EnchantableItem.enchant(getDefaultStack(), type, i))
2022-12-19 00:12:49 +01:00
)
.toList();
}
@Override
public boolean hasGlint(ItemStack stack) {
return super.hasGlint(stack) || (Unicopia.SIDE.getPlayerSpecies().canCast() && EnchantableItem.isEnchanted(stack));
}
@Override
public Text getName(ItemStack stack) {
if (EnchantableItem.isEnchanted(stack)) {
if (!Unicopia.SIDE.getPlayerSpecies().canCast()) {
2022-06-25 00:19:55 +02:00
return Text.translatable(getTranslationKey(stack) + ".obfuscated");
}
return Text.translatable(getTranslationKey(stack) + ".enchanted", EnchantableItem.getSpellKey(stack).getName());
}
return super.getName();
}
}