2021-02-16 12:39:39 +01:00
|
|
|
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;
|
2021-02-16 12:39:39 +01:00
|
|
|
|
|
|
|
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;
|
2021-02-16 12:39:39 +01:00
|
|
|
|
|
|
|
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;
|
2021-02-16 12:39:39 +01:00
|
|
|
import net.minecraft.util.Identifier;
|
2022-12-18 22:07:24 +01:00
|
|
|
import net.minecraft.registry.Registries;
|
2021-02-16 12:39:39 +01:00
|
|
|
|
|
|
|
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());
|
2021-02-16 12:39:39 +01:00
|
|
|
|
|
|
|
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();
|
2021-02-16 12:39:39 +01:00
|
|
|
equippedEnchantments.forEach(enchant -> {
|
2022-12-18 22:07:24 +01:00
|
|
|
Identifier id = Registries.ENCHANTMENT.getId(enchant);
|
2021-02-16 12:39:39 +01:00
|
|
|
if (id != null) {
|
2021-08-04 15:38:03 +02:00
|
|
|
list.add(NbtString.of(id.toString()));
|
2021-02-16 12:39:39 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
compound.put("enchants", list);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
public void fromNBT(NbtCompound compound) {
|
2021-02-16 12:39:39 +01:00
|
|
|
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);
|
2021-02-16 12:39:39 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|