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

160 lines
5.6 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.item;
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.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
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;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
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.Identifier;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;
public class GemstoneItem extends Item {
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();
TypedActionResult<CustomisedSpellType<?>> spell = consumeSpell(stack, user, ((Predicate<CustomisedSpellType<?>>)charms.getEquippedSpell(hand)::equals).negate());
if (spell.getResult().isAccepted()) {
charms.equipSpell(hand, spell.getValue());
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 (isEnchanted(stack)) {
SpellType<?> key = getSpellKey(stack);
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
public void appendStacks(ItemGroup tab, DefaultedList<ItemStack> items) {
super.appendStacks(tab, items);
if (isIn(tab)) {
2021-03-02 14:40:37 +01:00
for (Affinity i : Affinity.VALUES) {
SpellType.byAffinity(i).forEach(type -> {
if (type.isObtainable()) {
items.add(enchant(getDefaultStack(), type, i));
2021-03-02 14:40:37 +01:00
}
});
}
}
}
@Override
public boolean hasGlint(ItemStack stack) {
return super.hasGlint(stack) || (Unicopia.SIDE.getPlayerSpecies().canCast() && isEnchanted(stack));
}
@Override
public Text getName(ItemStack stack) {
if (isEnchanted(stack)) {
if (!Unicopia.SIDE.getPlayerSpecies().canCast()) {
2022-06-25 00:19:55 +02:00
return Text.translatable(getTranslationKey(stack) + ".obfuscated");
}
2022-06-25 00:19:55 +02:00
return Text.translatable(getTranslationKey(stack) + ".enchanted", getSpellKey(stack).getName());
}
return super.getName();
}
public static TypedActionResult<CustomisedSpellType<?>> consumeSpell(ItemStack stack, PlayerEntity player, @Nullable Predicate<CustomisedSpellType<?>> filter) {
if (!isEnchanted(stack)) {
return TypedActionResult.pass(null);
}
2021-03-02 21:10:52 +01:00
SpellType<Spell> key = getSpellKey(stack);
if (key.isEmpty()) {
return TypedActionResult.fail(null);
}
CustomisedSpellType<?> result = key.withTraits(SpellTraits.of(stack));
if (filter != null && !filter.test(result)) {
return TypedActionResult.fail(null);
}
if (!player.world.isClient) {
player.swingHand(player.getStackInHand(Hand.OFF_HAND) == stack ? Hand.OFF_HAND : Hand.MAIN_HAND);
if (stack.getCount() == 1) {
unenchant(stack);
} else {
player.giveItemStack(unenchant(stack.split(1)));
}
}
return TypedActionResult.consume(result);
}
public static boolean isEnchanted(ItemStack stack) {
2021-12-22 10:15:09 +01:00
return !stack.isEmpty() && stack.hasNbt() && stack.getNbt().contains("spell");
}
public static ItemStack enchant(ItemStack stack, SpellType<?> type) {
return enchant(stack, type, type.getAffinity());
2021-03-02 14:40:37 +01:00
}
public static ItemStack enchant(ItemStack stack, SpellType<?> type, Affinity affinity) {
if (type.isEmpty()) {
return unenchant(stack);
}
2021-12-22 10:15:09 +01:00
stack.getOrCreateNbt().putString("spell", type.getId().toString());
2021-11-21 17:50:02 +01:00
return type.getTraits().applyTo(stack);
}
public static ItemStack unenchant(ItemStack stack) {
2021-12-22 10:15:09 +01:00
stack.removeSubNbt("spell");
return stack;
}
public static <T extends Spell> SpellType<T> getSpellKey(ItemStack stack) {
2021-12-22 10:15:09 +01:00
return SpellType.getKey(isEnchanted(stack) ? new Identifier(stack.getNbt().getString("spell")) : SpellType.EMPTY_ID);
}
}