2021-03-01 11:56:30 +01:00
|
|
|
package com.minelittlepony.unicopia.item;
|
|
|
|
|
|
|
|
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-05 14:18:32 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.Spell;
|
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;
|
2021-11-08 13:25:59 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
|
2021-12-24 17:35:12 +01:00
|
|
|
import com.minelittlepony.unicopia.client.FlowingText;
|
2021-12-23 17:52:40 +01:00
|
|
|
import com.minelittlepony.unicopia.entity.player.PlayerCharmTracker;
|
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
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.ItemGroup;
|
|
|
|
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;
|
2021-03-01 11:56:30 +01:00
|
|
|
import net.minecraft.util.Identifier;
|
2021-03-02 18:38:54 +01:00
|
|
|
import net.minecraft.util.TypedActionResult;
|
2021-03-01 11:56:30 +01:00
|
|
|
import net.minecraft.util.collection.DefaultedList;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
public class GemstoneItem extends Item {
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
TypedActionResult<CustomisedSpellType<?>> spell = consumeSpell(stack, user, ((Predicate<CustomisedSpellType<?>>)charms.getEquippedSpell(hand)::equals).negate());
|
2022-09-11 15:42:57 +02:00
|
|
|
|
|
|
|
CustomisedSpellType<?> existing = charms.getEquippedSpell(hand);
|
|
|
|
|
|
|
|
if (existing != null) {
|
|
|
|
stack = existing.traits().applyTo(enchant(stack, existing.type()));
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
charms.equipSpell(hand, SpellType.EMPTY_KEY.withTraits());
|
2021-12-23 17:52:40 +01:00
|
|
|
}
|
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
|
|
|
|
|
|
|
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());
|
2021-03-02 19:12:57 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-03-01 11:56:30 +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()) {
|
2021-12-23 17:52:40 +01:00
|
|
|
items.add(enchant(getDefaultStack(), type, i));
|
2021-03-02 14:40:37 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-01 14:09:38 +01:00
|
|
|
public boolean hasGlint(ItemStack stack) {
|
|
|
|
return super.hasGlint(stack) || (Unicopia.SIDE.getPlayerSpecies().canCast() && isEnchanted(stack));
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Text getName(ItemStack stack) {
|
|
|
|
if (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
|
|
|
}
|
|
|
|
|
2022-06-25 00:19:55 +02:00
|
|
|
return Text.translatable(getTranslationKey(stack) + ".enchanted", getSpellKey(stack).getName());
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
return super.getName();
|
|
|
|
}
|
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
public static TypedActionResult<CustomisedSpellType<?>> consumeSpell(ItemStack stack, PlayerEntity player, @Nullable Predicate<CustomisedSpellType<?>> filter) {
|
2021-03-02 18:38:54 +01:00
|
|
|
|
|
|
|
if (!isEnchanted(stack)) {
|
|
|
|
return TypedActionResult.pass(null);
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:10:52 +01:00
|
|
|
SpellType<Spell> key = getSpellKey(stack);
|
2021-03-01 14:09:38 +01:00
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
if (key.isEmpty()) {
|
2021-03-02 18:38:54 +01:00
|
|
|
return TypedActionResult.fail(null);
|
2021-03-01 14:09:38 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
CustomisedSpellType<?> result = key.withTraits(SpellTraits.of(stack));
|
|
|
|
|
|
|
|
if (filter != null && !filter.test(result)) {
|
2021-03-02 18:38:54 +01:00
|
|
|
return TypedActionResult.fail(null);
|
2021-03-01 14:09:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!player.world.isClient) {
|
2021-03-02 18:38:54 +01:00
|
|
|
player.swingHand(player.getStackInHand(Hand.OFF_HAND) == stack ? Hand.OFF_HAND : Hand.MAIN_HAND);
|
|
|
|
|
2021-03-01 14:09:38 +01:00
|
|
|
if (stack.getCount() == 1) {
|
2021-12-23 17:52:40 +01:00
|
|
|
unenchant(stack);
|
2021-03-01 14:09:38 +01:00
|
|
|
} else {
|
2021-12-23 17:52:40 +01:00
|
|
|
player.giveItemStack(unenchant(stack.split(1)));
|
2021-03-01 14:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
return TypedActionResult.consume(result);
|
2021-03-01 14:09:38 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 11:56:30 +01:00
|
|
|
public static boolean isEnchanted(ItemStack stack) {
|
2021-12-22 10:15:09 +01:00
|
|
|
return !stack.isEmpty() && stack.hasNbt() && stack.getNbt().contains("spell");
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
public static ItemStack enchant(ItemStack stack, SpellType<?> type) {
|
|
|
|
return enchant(stack, type, type.getAffinity());
|
2021-03-02 14:40:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
public static ItemStack enchant(ItemStack stack, SpellType<?> type, Affinity affinity) {
|
2021-12-29 16:50:19 +01:00
|
|
|
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);
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:52:40 +01:00
|
|
|
public static ItemStack unenchant(ItemStack stack) {
|
2021-12-22 10:15:09 +01:00
|
|
|
stack.removeSubNbt("spell");
|
2021-03-01 11:56:30 +01:00
|
|
|
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);
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
}
|