2021-03-01 11:56:30 +01:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
2022-12-19 00:12:49 +01:00
|
|
|
import java.util.Arrays;
|
2021-03-01 11:56:30 +01:00
|
|
|
import java.util.List;
|
2021-03-01 14:09:38 +01:00
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-03-01 11:56:30 +01:00
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.Affinity;
|
2021-03-01 14:09:38 +01:00
|
|
|
import com.minelittlepony.unicopia.Unicopia;
|
2021-11-08 13:25:59 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType;
|
2021-11-05 14:18:32 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
|
2024-02-12 20:36:43 +01:00
|
|
|
import com.minelittlepony.unicopia.client.TextHelper;
|
2021-12-23 17:52:40 +01:00
|
|
|
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;
|
2021-03-01 11:56:30 +01:00
|
|
|
|
|
|
|
import net.minecraft.client.item.TooltipContext;
|
2021-03-01 14:09:38 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2021-03-01 11:56:30 +01:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2021-03-02 19:12:57 +01:00
|
|
|
import net.minecraft.text.MutableText;
|
2021-03-01 11:56:30 +01:00
|
|
|
import net.minecraft.text.Text;
|
2021-03-02 19:12:57 +01:00
|
|
|
import net.minecraft.util.Formatting;
|
2021-03-02 18:38:54 +01:00
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.TypedActionResult;
|
2021-03-01 11:56:30 +01:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2023-02-28 17:44:14 +01:00
|
|
|
public class GemstoneItem extends Item implements MultiItem, EnchantableItem {
|
2021-03-01 11:56:30 +01:00
|
|
|
|
|
|
|
public GemstoneItem(Settings settings) {
|
|
|
|
super(settings);
|
|
|
|
}
|
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
@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();
|
|
|
|
|
2023-09-02 19:16:36 +02:00
|
|
|
if (!Pony.of(user).getCompositeRace().canCast()) {
|
2023-04-30 13:59:05 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-29 15:13:52 +02:00
|
|
|
hand = user.isSneaking() ? Hand.OFF_HAND : Hand.MAIN_HAND;
|
|
|
|
|
|
|
|
TypedActionResult<CustomisedSpellType<?>> spell = EnchantableItem.consumeSpell(stack, user, ((Predicate<CustomisedSpellType<?>>)charms.getEquippedSpell(hand)::equals).negate(), true);
|
2022-09-11 15:42:57 +02:00
|
|
|
|
|
|
|
CustomisedSpellType<?> existing = charms.getEquippedSpell(hand);
|
|
|
|
|
2022-09-12 19:53:02 +02:00
|
|
|
if (!existing.isEmpty()) {
|
|
|
|
|
|
|
|
if (stack.getCount() == 1) {
|
2023-02-28 17:44:14 +01:00
|
|
|
stack = existing.traits().applyTo(EnchantableItem.enchant(stack, existing.type()));
|
2022-09-12 19:53:02 +02:00
|
|
|
} else {
|
2023-02-28 17:44:14 +01:00
|
|
|
user.giveItemStack(existing.traits().applyTo(EnchantableItem.enchant(stack.split(1), existing.type())));
|
2022-09-12 19:53:02 +02:00
|
|
|
}
|
2022-09-11 15:42:57 +02:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
if (spell.getResult().isAccepted()) {
|
|
|
|
charms.equipSpell(hand, spell.getValue());
|
2022-09-11 15:42:57 +02:00
|
|
|
} else {
|
2022-09-12 19:53:02 +02:00
|
|
|
|
|
|
|
if (existing.isEmpty()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-11 15:42:57 +02:00
|
|
|
charms.equipSpell(hand, SpellType.EMPTY_KEY.withTraits());
|
2021-12-23 17:52:40 +01:00
|
|
|
}
|
2023-05-29 15:13:52 +02:00
|
|
|
|
|
|
|
user.getItemCooldownManager().set(this, 20);
|
2022-09-11 15:42:57 +02:00
|
|
|
return TypedActionResult.success(stack, true);
|
2021-12-23 17:52:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-01 11:56:30 +01:00
|
|
|
@Override
|
2021-03-02 19:12:57 +01:00
|
|
|
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
|
|
|
|
2023-02-28 17:44:14 +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());
|
2021-03-02 19:12:57 +01:00
|
|
|
|
|
|
|
if (!Unicopia.SIDE.getPlayerSpecies().canCast()) {
|
|
|
|
line = line.formatted(Formatting.OBFUSCATED);
|
|
|
|
}
|
|
|
|
|
2024-02-12 20:36:43 +01:00
|
|
|
lines.addAll(TextHelper.wrap(line, 180).toList());
|
2021-03-02 14:40:37 +01:00
|
|
|
}
|
2021-03-01 11:56:30 +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())
|
2023-02-28 17:44:14 +01:00
|
|
|
.map(type -> EnchantableItem.enchant(getDefaultStack(), type, i))
|
2022-12-19 00:12:49 +01:00
|
|
|
)
|
|
|
|
.toList();
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-01 14:09:38 +01:00
|
|
|
public boolean hasGlint(ItemStack stack) {
|
2023-02-28 17:44:14 +01:00
|
|
|
return super.hasGlint(stack) || (Unicopia.SIDE.getPlayerSpecies().canCast() && EnchantableItem.isEnchanted(stack));
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Text getName(ItemStack stack) {
|
2023-02-28 17:44:14 +01:00
|
|
|
if (EnchantableItem.isEnchanted(stack)) {
|
2021-03-02 19:12:57 +01:00
|
|
|
if (!Unicopia.SIDE.getPlayerSpecies().canCast()) {
|
2022-06-25 00:19:55 +02:00
|
|
|
return Text.translatable(getTranslationKey(stack) + ".obfuscated");
|
2021-03-02 19:12:57 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 17:44:14 +01:00
|
|
|
return Text.translatable(getTranslationKey(stack) + ".enchanted", EnchantableItem.getSpellKey(stack).getName());
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
return super.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|