Skip to content

Commit

Permalink
Reimplemented the alicorn amulet
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Aug 15, 2021
1 parent 21812b6 commit a1c3fa9
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.minelittlepony.unicopia.entity.player;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

import com.minelittlepony.unicopia.ability.magic.Affine;
import com.minelittlepony.unicopia.util.NbtSerialisable;
import com.minelittlepony.unicopia.util.Tickable;

import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class PlayerCharmTracker implements Tickable, NbtSerialisable {

private final Pony pony;

private final ItemTracker armour = new ItemTracker();

PlayerCharmTracker(Pony pony) {
this.pony = pony;
}

@Override
public void tick() {
armour.update(pony.getMaster().getInventory().armor.stream());
}

public ItemTracker getArmour() {
return armour;
}

@Override
public void toNBT(NbtCompound compound) {
compound.put("armour", armour.toNBT());
}

@Override
public void fromNBT(NbtCompound compound) {
armour.fromNBT(compound.getCompound("armour"));
}

public class ItemTracker implements NbtSerialisable {
private final Map<Charm, Integer> items = new HashMap<>();

public void update(Stream<ItemStack> stacks) {
Set<Charm> found = new HashSet<>();
stacks.forEach(stack -> {
if (stack.getItem() instanceof Charm) {
items.compute((Charm)stack.getItem(), (item, prev) -> prev == null ? 1 : prev + 1);
found.add((Charm)stack.getItem());
}
});
items.entrySet().removeIf(e -> {
if (!found.contains(e.getKey())) {
e.getKey().onRemoved(pony, e.getValue());
return true;
}
return false;
});
}

public int getTicks(Charm charm) {
return items.getOrDefault(charm.asItem(), 0);
}

public boolean contains(Charm charm) {
return getTicks(charm) > 0;
}

@Override
public void toNBT(NbtCompound compound) {
items.forEach((charm, count) -> {
compound.putInt(Registry.ITEM.getId(charm.asItem()).toString(), count);
});
}

@Override
public void fromNBT(NbtCompound compound) {
items.clear();
compound.getKeys().stream().map(Identifier::tryParse)
.filter(Objects::nonNull)
.map(id -> Map.entry(Registry.ITEM.get(id), compound.getInt(id.toString())))
.filter(i -> i.getKey() instanceof Charm && i.getValue() > 0)
.forEach(item -> items.put((Charm)item.getKey(), item.getValue()));
}
}

public interface Charm extends Affine, ItemConvertible {
void onRemoved(Pony pony, int timeWorn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.minelittlepony.unicopia.UTags;
import com.minelittlepony.unicopia.WorldTribeManager;
import com.minelittlepony.unicopia.ability.AbilityDispatcher;
import com.minelittlepony.unicopia.ability.magic.Affine;
import com.minelittlepony.unicopia.ability.magic.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.SpellType;
import com.minelittlepony.unicopia.advancement.UCriteria;
Expand All @@ -22,6 +23,7 @@
import com.minelittlepony.unicopia.entity.Living;
import com.minelittlepony.unicopia.entity.Trap;
import com.minelittlepony.unicopia.entity.effect.SunBlindnessStatusEffect;
import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.item.toxin.FoodType;
import com.minelittlepony.unicopia.item.toxin.Toxicity;
import com.minelittlepony.unicopia.item.toxin.Toxin;
Expand Down Expand Up @@ -71,6 +73,7 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab

private final AbilityDispatcher powers = new AbilityDispatcher(this);
private final PlayerPhysics gravity = new PlayerPhysics(this);
private final PlayerCharmTracker charms = new PlayerCharmTracker(this);
private final PlayerAttributes attributes = new PlayerAttributes(this);
private final PlayerCamera camera = new PlayerCamera(this);
private final ManaContainer mana;
Expand Down Expand Up @@ -100,7 +103,7 @@ public Pony(PlayerEntity player) {
super(player, EFFECT);
this.mana = new ManaContainer(this);
this.levels = new PlayerLevelStore(this);
this.tickers = Lists.newArrayList(gravity, mana, attributes);
this.tickers = Lists.newArrayList(gravity, mana, attributes, charms);

player.getDataTracker().startTracking(RACE, Race.HUMAN.ordinal());
}
Expand Down Expand Up @@ -135,6 +138,10 @@ public MagicReserves getMagicalReserves() {
return mana;
}

public PlayerCharmTracker getCharms() {
return charms;
}

@Override
public LevelStore getLevel() {
return levels;
Expand Down Expand Up @@ -395,12 +402,22 @@ protected void giveBackItem(ItemStack stack) {
}

public Optional<Text> trySleep(BlockPos pos) {

if (UItems.ALICORN_AMULET.isApplicable(entity)) {
return Optional.of(new TranslatableText("block.unicopia.bed.not_tired"));
}

return findAllSpellsInRange(10)
.filter(p -> p instanceof Pony && ((Pony)p).isEnemy(this))
.findFirst()
.map(p -> new TranslatableText("block.unicopia.bed.not_safe"));
}

@Override
public boolean isEnemy(Affine other) {
return getCharms().getArmour().contains(UItems.ALICORN_AMULET) || super.isEnemy(other);
}

public void onEat(ItemStack stack) {
if (getSpecies() == Race.CHANGELING) {
Toxin.POISON.afflict(getMaster(), FoodType.RAW_MEAT, Toxicity.SAFE, stack);
Expand All @@ -416,6 +433,7 @@ public void toNBT(NbtCompound compound) {

compound.put("powers", powers.toNBT());
compound.put("gravity", gravity.toNBT());
compound.put("charms", charms.toNBT());

getSpellSlot().get(true).ifPresent(effect ->{
compound.put("effect", SpellType.toNBT(effect));
Expand All @@ -430,6 +448,7 @@ public void fromNBT(NbtCompound compound) {

powers.fromNBT(compound.getCompound("powers"));
gravity.fromNBT(compound.getCompound("gravity"));
charms.fromNBT(compound.getCompound("charms"));

magicExhaustion = compound.getFloat("magicExhaustion");

Expand Down
Loading

0 comments on commit a1c3fa9

Please sign in to comment.