From 4843a5c5f880fd4be3c3f95c79eaa7d766cf9cb8 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 1 Mar 2021 15:09:38 +0200 Subject: [PATCH] Casting a spell from a gem will now consume the spell but leave the gem --- .../minelittlepony/unicopia/SidedAccess.java | 5 +++ .../com/minelittlepony/unicopia/Unicopia.java | 13 +++++++ .../ability/UnicornCastingAbility.java | 8 ++--- .../ability/UnicornProjectileAbility.java | 4 +-- .../unicopia/client/UnicopiaClient.java | 2 ++ .../unicopia/item/GemstoneItem.java | 36 +++++++++++++++++-- 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/SidedAccess.java diff --git a/src/main/java/com/minelittlepony/unicopia/SidedAccess.java b/src/main/java/com/minelittlepony/unicopia/SidedAccess.java new file mode 100644 index 00000000..ae28c55e --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/SidedAccess.java @@ -0,0 +1,5 @@ +package com.minelittlepony.unicopia; + +public interface SidedAccess { + Race getObservingPlayerRace(); +} diff --git a/src/main/java/com/minelittlepony/unicopia/Unicopia.java b/src/main/java/com/minelittlepony/unicopia/Unicopia.java index 35327e64..cb18b016 100644 --- a/src/main/java/com/minelittlepony/unicopia/Unicopia.java +++ b/src/main/java/com/minelittlepony/unicopia/Unicopia.java @@ -5,11 +5,14 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.minecraft.resource.ResourceType; +import java.util.Optional; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.minelittlepony.unicopia.command.Commands; import com.minelittlepony.unicopia.entity.effect.UPotions; +import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.item.UItems; import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.network.Channel; @@ -18,6 +21,8 @@ public class Unicopia implements ModInitializer { public static final Logger LOGGER = LogManager.getLogger(); + public static SidedAccess SIDE = Optional::empty; + private static Config CONFIG; public static Config getConfig() { @@ -47,4 +52,12 @@ public class Unicopia implements ModInitializer { UItems.bootstrap(); UPotions.bootstrap(); } + + public interface SidedAccess { + Optional getPony(); + + default Race getPlayerSpecies() { + return getPony().map(Pony::getSpecies).orElse(Race.HUMAN); + } + } } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/UnicornCastingAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/UnicornCastingAbility.java index e7ffde31..5d38edd3 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/UnicornCastingAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/UnicornCastingAbility.java @@ -1,6 +1,5 @@ package com.minelittlepony.unicopia.ability; -import java.util.Objects; import java.util.Optional; import java.util.Random; @@ -9,6 +8,7 @@ import javax.annotation.Nullable; import com.google.common.collect.Streams; import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.ability.data.Hit; +import com.minelittlepony.unicopia.ability.magic.Attached; import com.minelittlepony.unicopia.ability.magic.Spell; import com.minelittlepony.unicopia.ability.magic.spell.SpellType; import com.minelittlepony.unicopia.entity.player.Pony; @@ -48,7 +48,6 @@ public class UnicornCastingAbility implements Ability { @Override @Nullable public Hit tryActivate(Pony player) { - System.out.println(getCostEstimate(player) + " " + player.getMagicalReserves().getMana().get()); return getCostEstimate(player) <= player.getMagicalReserves().getMana().get() ? Hit.INSTANCE : null; } @@ -106,10 +105,7 @@ public class UnicornCastingAbility implements Ability { private Optional getNewSpell(Pony player) { final SpellType current = player.hasSpell() ? player.getSpell(true).getType() : null; return Streams.stream(player.getMaster().getItemsHand()) - .map(GemstoneItem::getSpellKey) - .filter(i -> !Objects.equals(i, current)) - .map(SpellType::create) - .filter(Objects::nonNull) + .flatMap(stack -> GemstoneItem.consumeSpell(stack, player.getMaster(), current, i -> i instanceof Attached)) .findFirst(); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/UnicornProjectileAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/UnicornProjectileAbility.java index 97b0d2c9..374500c3 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/UnicornProjectileAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/UnicornProjectileAbility.java @@ -66,9 +66,7 @@ public class UnicornProjectileAbility implements Ability { private Optional getThrown(Pony player) { return Streams.stream(player.getMaster().getItemsHand()) - .map(GemstoneItem::getSpellKey) - .map(SpellType::create) - .filter(i -> i != null && i instanceof Thrown) + .flatMap(stack -> GemstoneItem.consumeSpell(stack, player.getMaster(), null, i -> i instanceof Thrown)) .map(Thrown.class::cast) .findFirst(); } diff --git a/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java b/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java index 5e78dcab..0e139746 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java +++ b/src/main/java/com/minelittlepony/unicopia/client/UnicopiaClient.java @@ -58,6 +58,8 @@ public class UnicopiaClient implements ClientModInitializer { ClientTickEvents.END_CLIENT_TICK.register(this::onTick); ScreenInitCallback.EVENT.register(this::onScreenInit); ItemTooltipCallback.EVENT.register(new ModifierTooltipRenderer()); + + Unicopia.SIDE = () -> Optional.ofNullable(MinecraftClient.getInstance().player).map(Pony::of); } private void onTick(MinecraftClient client) { diff --git a/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java b/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java index 0ee7c1b2..83ad279b 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java @@ -1,13 +1,19 @@ package com.minelittlepony.unicopia.item; import java.util.List; +import java.util.Objects; +import java.util.function.Predicate; +import java.util.stream.Stream; + import javax.annotation.Nullable; import com.minelittlepony.unicopia.Affinity; +import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.ability.magic.Spell; import com.minelittlepony.unicopia.ability.magic.spell.SpellType; 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; @@ -40,8 +46,8 @@ public class GemstoneItem extends Item { } @Override - public String getTranslationKey(ItemStack stack) { - return getTranslationKey(); + public boolean hasGlint(ItemStack stack) { + return super.hasGlint(stack) || (Unicopia.SIDE.getPlayerSpecies().canCast() && isEnchanted(stack)); } @Override @@ -52,6 +58,30 @@ public class GemstoneItem extends Item { return super.getName(); } + public static Stream consumeSpell(ItemStack stack, PlayerEntity player, @Nullable SpellType exclude, Predicate test) { + SpellType key = GemstoneItem.getSpellKey(stack); + + if (key == null || Objects.equals(key, exclude)) { + return Stream.empty(); + } + + Spell spell = key.create(); + + if (spell == null || !test.test(spell)) { + return Stream.empty(); + } + + if (!player.world.isClient) { + if (stack.getCount() == 1) { + GemstoneItem.unenchanted(stack); + } else { + player.giveItemStack(GemstoneItem.unenchanted(stack.split(1))); + } + } + + return Stream.of(spell); + } + public static boolean isEnchanted(ItemStack stack) { return !stack.isEmpty() && stack.hasTag() && stack.getTag().contains("spell"); } @@ -61,7 +91,7 @@ public class GemstoneItem extends Item { return stack; } - public ItemStack unenchanted(ItemStack stack) { + public static ItemStack unenchanted(ItemStack stack) { if (isEnchanted(stack)) { stack.getTag().remove("spell");