-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
426 additions
and
9 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/main/java/com/minelittlepony/unicopia/entity/player/PlayerCharmTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.