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.Objects;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
2021-03-01 11:56:30 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.Affinity;
|
2021-03-01 14:09:38 +01:00
|
|
|
import com.minelittlepony.unicopia.Unicopia;
|
2021-03-01 11:56:30 +01:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.Spell;
|
|
|
|
import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import net.minecraft.text.TranslatableText;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-02 19:12:57 +01:00
|
|
|
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> lines, TooltipContext tooltipContext) {
|
2021-03-02 14:40:37 +01:00
|
|
|
|
|
|
|
if (isEnchanted(stack)) {
|
|
|
|
SpellType<?> key = getSpellKey(stack);
|
|
|
|
Affinity affinity = getAffinity(stack);
|
|
|
|
|
2021-03-02 19:12:57 +01:00
|
|
|
MutableText line = new TranslatableText(key.getTranslationKey(affinity) + ".lore").formatted(affinity.getColor());
|
|
|
|
|
|
|
|
if (!Unicopia.SIDE.getPlayerSpecies().canCast()) {
|
|
|
|
line = line.formatted(Formatting.OBFUSCATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
lines.add(line);
|
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()) {
|
|
|
|
items.add(enchanted(getDefaultStack(), type, i));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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()) {
|
|
|
|
return new TranslatableText(getTranslationKey(stack) + ".obfuscated");
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:40:37 +01:00
|
|
|
return new TranslatableText(getTranslationKey(stack) + ".enchanted", getSpellKey(stack).getName(getAffinity(stack)));
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|
|
|
|
return super.getName();
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:38:54 +01:00
|
|
|
public static TypedActionResult<Spell> consumeSpell(ItemStack stack, PlayerEntity player, @Nullable SpellType<?> exclude, Predicate<Spell> test) {
|
|
|
|
|
|
|
|
if (!isEnchanted(stack)) {
|
|
|
|
return TypedActionResult.pass(null);
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:09:38 +01:00
|
|
|
SpellType<Spell> key = GemstoneItem.getSpellKey(stack);
|
|
|
|
|
2021-03-02 14:40:37 +01:00
|
|
|
if (Objects.equals(key, exclude)) {
|
2021-03-02 18:38:54 +01:00
|
|
|
return TypedActionResult.fail(null);
|
2021-03-01 14:09:38 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 14:40:37 +01:00
|
|
|
Spell spell = key.create(getAffinity(stack));
|
2021-03-01 14:09:38 +01:00
|
|
|
|
|
|
|
if (spell == null || !test.test(spell)) {
|
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) {
|
|
|
|
GemstoneItem.unenchanted(stack);
|
|
|
|
} else {
|
|
|
|
player.giveItemStack(GemstoneItem.unenchanted(stack.split(1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:38:54 +01:00
|
|
|
return TypedActionResult.consume(spell);
|
2021-03-01 14:09:38 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 11:56:30 +01:00
|
|
|
public static boolean isEnchanted(ItemStack stack) {
|
|
|
|
return !stack.isEmpty() && stack.hasTag() && stack.getTag().contains("spell");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack enchanted(ItemStack stack, SpellType<?> type) {
|
2021-03-02 14:40:37 +01:00
|
|
|
return enchanted(stack, type, type.getAffinity());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack enchanted(ItemStack stack, SpellType<?> type, Affinity affinity) {
|
2021-03-01 11:56:30 +01:00
|
|
|
stack.getOrCreateTag().putString("spell", type.getId().toString());
|
2021-03-02 14:40:37 +01:00
|
|
|
stack.getOrCreateTag().putInt("affinity", affinity.ordinal());
|
2021-03-01 11:56:30 +01:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2021-03-01 14:09:38 +01:00
|
|
|
public static ItemStack unenchanted(ItemStack stack) {
|
2021-03-02 14:40:37 +01:00
|
|
|
stack.removeSubTag("spell");
|
|
|
|
stack.removeSubTag("affinity");
|
2021-03-01 11:56:30 +01:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends Spell> SpellType<T> getSpellKey(ItemStack stack) {
|
|
|
|
return SpellType.getKey(isEnchanted(stack) ? new Identifier(stack.getTag().getString("spell")) : SpellType.EMPTY_ID);
|
|
|
|
}
|
2021-03-02 14:40:37 +01:00
|
|
|
|
|
|
|
public static Affinity getAffinity(ItemStack stack) {
|
|
|
|
Affinity fallback = getSpellKey(stack).getAffinity();
|
|
|
|
|
|
|
|
if (stack.hasTag() && stack.getTag().contains("affinity")) {
|
|
|
|
return Affinity.of(stack.getTag().getInt("affinity"), fallback);
|
|
|
|
}
|
|
|
|
return fallback;
|
|
|
|
}
|
2021-03-01 11:56:30 +01:00
|
|
|
}
|