Unicopia/src/main/java/com/minelittlepony/unicopia/entity/ItemTracker.java

120 lines
4 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;
2022-12-19 16:10:09 +01:00
import com.minelittlepony.unicopia.entity.player.Pony;
2022-12-10 01:22:24 +01:00
import com.minelittlepony.unicopia.util.Copyable;
import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
2022-12-18 22:07:24 +01:00
import net.minecraft.registry.Registries;
2022-12-10 01:22:24 +01:00
public class ItemTracker implements NbtSerialisable, Copyable<ItemTracker> {
public static final long TICKS = 1;
public static final long SECONDS = 20 * TICKS;
public static final long HOURS = 1000 * TICKS;
public static final long DAYS = 24 * HOURS;
public static String formatTicks(long ticks) {
2022-12-15 10:15:11 +01:00
long days = ticks / (SECONDS * 60 * 24);
ticks %= (SECONDS * 60 * 60 * 24);
long hours = ticks / (SECONDS * 60 * 60);
ticks %= (SECONDS * 60 * 60);
long minutes = ticks / (SECONDS * 60);
ticks %= (SECONDS * 60);
long seconds = ticks / SECONDS;
2022-12-15 10:15:11 +01:00
return String.format("%dd, %dh %dm %ds", days, hours, minutes, seconds);
}
private final Map<Trackable, Long> items = new HashMap<>();
public static Predicate<LivingEntity> wearing(Trackable charm, Predicate<Long> range) {
return e -> Living.getOrEmpty(e)
.map(Living::getArmour)
.map(a -> a.getTicks(charm))
.filter(range)
.isPresent();
}
public static Predicate<Long> between(long minTime, long maxTime) {
return before(maxTime).and(after(minTime));
}
public static Predicate<Long> before(long maxTime) {
return ticks -> ticks <= maxTime;
}
public static Predicate<Long> after(long maxTime) {
return ticks -> ticks <= maxTime;
}
public void update(Living<?> living, Stream<ItemStack> stacks) {
final Set<Trackable> found = new HashSet<>();
final Set<ItemStack> foundStacks = new HashSet<>();
stacks.forEach(stack -> {
if (stack.getItem() instanceof Trackable trackable) {
items.compute(trackable, (item, prev) -> prev == null ? 1 : prev + 1);
found.add(trackable);
foundStacks.add(stack);
}
});
items.entrySet().removeIf(e -> {
if (!found.contains(e.getKey())) {
e.getKey().onUnequipped(living, e.getValue());
return true;
}
return false;
});
2022-12-19 16:10:09 +01:00
if (!(living instanceof Pony)) {
foundStacks.forEach(stack -> {
if (getTicks((Trackable)stack.getItem()) == 1) {
2022-12-19 16:10:09 +01:00
stack.inventoryTick(living.getReferenceWorld(), living.asEntity(), 0, false);
}
});
}
}
public long getTicks(Trackable charm) {
return items.getOrDefault(charm.asItem(), 0L);
}
public boolean contains(Trackable charm) {
return getTicks(charm) > 0;
}
@Override
public void toNBT(NbtCompound compound) {
items.forEach((charm, count) -> {
2022-12-18 22:07:24 +01:00
compound.putLong(Registries.ITEM.getId(charm.asItem()).toString(), count);
});
}
@Override
public void fromNBT(NbtCompound compound) {
items.clear();
compound.getKeys().stream().map(Identifier::tryParse)
.filter(Objects::nonNull)
2022-12-18 22:07:24 +01:00
.map(id -> Map.entry(Registries.ITEM.get(id), compound.getLong(id.toString())))
.filter(i -> i.getKey() instanceof Trackable && i.getValue() > 0)
.forEach(item -> items.put((Trackable)item.getKey(), item.getValue()));
}
@Override
public void copyFrom(ItemTracker other) {
items.clear();
items.putAll(other.items);
}
public interface Trackable extends ItemConvertible {
void onUnequipped(Living<?> living, long timeWorn);
void onEquipped(Living<?> living);
}
}