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

98 lines
3.1 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.entity;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
2021-08-04 15:38:03 +02:00
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.item.enchantment.SimpleEnchantment;
import com.minelittlepony.unicopia.item.enchantment.UEnchantments;
import com.minelittlepony.unicopia.util.NbtSerialisable;
2021-08-04 15:38:03 +02:00
import com.minelittlepony.unicopia.util.Tickable;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
2021-08-04 15:38:03 +02:00
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.util.Identifier;
2022-12-18 22:07:24 +01:00
import net.minecraft.registry.Registries;
public class Enchantments implements NbtSerialisable, Tickable {
private final Living<?> entity;
private final Set<Enchantment> equippedEnchantments = new HashSet<>();
private final Map<Enchantment, SimpleEnchantment.Data> data = new HashMap<>();
Enchantments(Living<?> entity) {
this.entity = entity;
}
@SuppressWarnings("unchecked")
public <T extends SimpleEnchantment.Data> Optional<T> getOrEmpty(Enchantment enchantment) {
return Optional.ofNullable((T)data.get(enchantment));
}
@SuppressWarnings("unchecked")
public <T extends SimpleEnchantment.Data> T computeIfAbsent(Enchantment enchantment, Supplier<T> factory) {
return (T)data.computeIfAbsent(enchantment, e -> factory.get());
}
@Nullable
@SuppressWarnings("unchecked")
public <T extends SimpleEnchantment.Data> T remove(Enchantment enchantment) {
return (T)data.remove(enchantment);
}
@Override
public void tick() {
UEnchantments.REGISTRY.forEach(ench -> {
2022-12-19 20:45:02 +01:00
int level = EnchantmentHelper.getEquipmentLevel(ench, entity.asEntity());
boolean active = level > 0;
if (active != equippedEnchantments.contains(ench)) {
if (active) {
equippedEnchantments.add(ench);
ench.onEquipped(entity);
} else {
equippedEnchantments.remove(ench);
ench.onUnequipped(entity);
}
}
if (active) {
ench.onUserTick(entity, level);
}
});
}
@Override
2021-08-04 15:38:03 +02:00
public void toNBT(NbtCompound compound) {
NbtList list = new NbtList();
equippedEnchantments.forEach(enchant -> {
2022-12-18 22:07:24 +01:00
Identifier id = Registries.ENCHANTMENT.getId(enchant);
if (id != null) {
2021-08-04 15:38:03 +02:00
list.add(NbtString.of(id.toString()));
}
});
compound.put("enchants", list);
}
@Override
2021-08-04 15:38:03 +02:00
public void fromNBT(NbtCompound compound) {
equippedEnchantments.clear();
if (compound.contains("enchants")) {
compound.getList("enchants", 8).forEach(tag -> {
2022-12-18 22:07:24 +01:00
Registries.ENCHANTMENT.getOrEmpty(new Identifier(tag.asString())).ifPresent(equippedEnchantments::add);
});
}
}
}