Casting a spell from a gem will now consume the spell but leave the gem

This commit is contained in:
Sollace 2021-03-01 15:09:38 +02:00
parent b9fa4b072d
commit 4843a5c5f8
6 changed files with 56 additions and 12 deletions

View file

@ -0,0 +1,5 @@
package com.minelittlepony.unicopia;
public interface SidedAccess {
Race getObservingPlayerRace();
}

View file

@ -5,11 +5,14 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.resource.ResourceType; import net.minecraft.resource.ResourceType;
import java.util.Optional;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import com.minelittlepony.unicopia.command.Commands; import com.minelittlepony.unicopia.command.Commands;
import com.minelittlepony.unicopia.entity.effect.UPotions; 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.UItems;
import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.item.enchantment.UEnchantments;
import com.minelittlepony.unicopia.network.Channel; import com.minelittlepony.unicopia.network.Channel;
@ -18,6 +21,8 @@ public class Unicopia implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static SidedAccess SIDE = Optional::empty;
private static Config CONFIG; private static Config CONFIG;
public static Config getConfig() { public static Config getConfig() {
@ -47,4 +52,12 @@ public class Unicopia implements ModInitializer {
UItems.bootstrap(); UItems.bootstrap();
UPotions.bootstrap(); UPotions.bootstrap();
} }
public interface SidedAccess {
Optional<Pony> getPony();
default Race getPlayerSpecies() {
return getPony().map(Pony::getSpecies).orElse(Race.HUMAN);
}
}
} }

View file

@ -1,6 +1,5 @@
package com.minelittlepony.unicopia.ability; package com.minelittlepony.unicopia.ability;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Random; import java.util.Random;
@ -9,6 +8,7 @@ import javax.annotation.Nullable;
import com.google.common.collect.Streams; import com.google.common.collect.Streams;
import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ability.data.Hit; 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;
import com.minelittlepony.unicopia.ability.magic.spell.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
@ -48,7 +48,6 @@ public class UnicornCastingAbility implements Ability<Hit> {
@Override @Override
@Nullable @Nullable
public Hit tryActivate(Pony player) { public Hit tryActivate(Pony player) {
System.out.println(getCostEstimate(player) + " " + player.getMagicalReserves().getMana().get());
return getCostEstimate(player) <= player.getMagicalReserves().getMana().get() ? Hit.INSTANCE : null; return getCostEstimate(player) <= player.getMagicalReserves().getMana().get() ? Hit.INSTANCE : null;
} }
@ -106,10 +105,7 @@ public class UnicornCastingAbility implements Ability<Hit> {
private Optional<Spell> getNewSpell(Pony player) { private Optional<Spell> getNewSpell(Pony player) {
final SpellType<?> current = player.hasSpell() ? player.getSpell(true).getType() : null; final SpellType<?> current = player.hasSpell() ? player.getSpell(true).getType() : null;
return Streams.stream(player.getMaster().getItemsHand()) return Streams.stream(player.getMaster().getItemsHand())
.map(GemstoneItem::getSpellKey) .flatMap(stack -> GemstoneItem.consumeSpell(stack, player.getMaster(), current, i -> i instanceof Attached))
.filter(i -> !Objects.equals(i, current))
.map(SpellType::create)
.filter(Objects::nonNull)
.findFirst(); .findFirst();
} }

View file

@ -66,9 +66,7 @@ public class UnicornProjectileAbility implements Ability<Hit> {
private Optional<Thrown> getThrown(Pony player) { private Optional<Thrown> getThrown(Pony player) {
return Streams.stream(player.getMaster().getItemsHand()) return Streams.stream(player.getMaster().getItemsHand())
.map(GemstoneItem::getSpellKey) .flatMap(stack -> GemstoneItem.consumeSpell(stack, player.getMaster(), null, i -> i instanceof Thrown))
.map(SpellType::create)
.filter(i -> i != null && i instanceof Thrown)
.map(Thrown.class::cast) .map(Thrown.class::cast)
.findFirst(); .findFirst();
} }

View file

@ -58,6 +58,8 @@ public class UnicopiaClient implements ClientModInitializer {
ClientTickEvents.END_CLIENT_TICK.register(this::onTick); ClientTickEvents.END_CLIENT_TICK.register(this::onTick);
ScreenInitCallback.EVENT.register(this::onScreenInit); ScreenInitCallback.EVENT.register(this::onScreenInit);
ItemTooltipCallback.EVENT.register(new ModifierTooltipRenderer()); ItemTooltipCallback.EVENT.register(new ModifierTooltipRenderer());
Unicopia.SIDE = () -> Optional.ofNullable(MinecraftClient.getInstance().player).map(Pony::of);
} }
private void onTick(MinecraftClient client) { private void onTick(MinecraftClient client) {

View file

@ -1,13 +1,19 @@
package com.minelittlepony.unicopia.item; package com.minelittlepony.unicopia.item;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.minelittlepony.unicopia.Affinity; import com.minelittlepony.unicopia.Affinity;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.ability.magic.Spell; import com.minelittlepony.unicopia.ability.magic.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
import net.minecraft.client.item.TooltipContext; import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -40,8 +46,8 @@ public class GemstoneItem extends Item {
} }
@Override @Override
public String getTranslationKey(ItemStack stack) { public boolean hasGlint(ItemStack stack) {
return getTranslationKey(); return super.hasGlint(stack) || (Unicopia.SIDE.getPlayerSpecies().canCast() && isEnchanted(stack));
} }
@Override @Override
@ -52,6 +58,30 @@ public class GemstoneItem extends Item {
return super.getName(); return super.getName();
} }
public static Stream<Spell> consumeSpell(ItemStack stack, PlayerEntity player, @Nullable SpellType<?> exclude, Predicate<Spell> test) {
SpellType<Spell> 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) { public static boolean isEnchanted(ItemStack stack) {
return !stack.isEmpty() && stack.hasTag() && stack.getTag().contains("spell"); return !stack.isEmpty() && stack.hasTag() && stack.getTag().contains("spell");
} }
@ -61,7 +91,7 @@ public class GemstoneItem extends Item {
return stack; return stack;
} }
public ItemStack unenchanted(ItemStack stack) { public static ItemStack unenchanted(ItemStack stack) {
if (isEnchanted(stack)) { if (isEnchanted(stack)) {
stack.getTag().remove("spell"); stack.getTag().remove("spell");