Some minor cleanup

This commit is contained in:
Sollace 2021-12-29 18:11:32 +02:00
parent 18fffef06a
commit 7a05efa23a
12 changed files with 63 additions and 66 deletions

View file

@ -38,7 +38,7 @@ public class CompoundSpell extends AbstractDelegatingSpell {
public void toNBT(NbtCompound compound) {
super.toNBT(compound);
NbtList spells = new NbtList();
this.spells.stream().map(SpellType::toNBT).forEach(spells::add);
this.spells.stream().map(Spell::writeNbt).forEach(spells::add);
compound.put("spells", spells);
}
@ -48,7 +48,7 @@ public class CompoundSpell extends AbstractDelegatingSpell {
spells.clear();
if (compound.contains("spells", NbtElement.LIST_TYPE)) {
spells.addAll(compound.getList("spells", NbtElement.COMPOUND_TYPE).stream()
.map(el -> SpellType.fromNBT((NbtCompound)el))
.map(el -> Spell.readNbt((NbtCompound)el))
.filter(Objects::nonNull)
.toList());
}

View file

@ -102,7 +102,7 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
compound.putString("dimension", dimension.toString());
}
compound.put("castEntity", castEntity.toNBT());
compound.put("spell", SpellType.toNBT(spell));
compound.put("spell", Spell.writeNbt(spell));
}
@Override
@ -114,7 +114,7 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
if (compound.contains("castEntity")) {
castEntity.fromNBT(compound.getCompound("castEntity"));
}
spell = SpellType.fromNBT(compound.getCompound("spell"));
spell = Spell.readNbt(compound.getCompound("spell"));
}
@Override

View file

@ -4,6 +4,7 @@ import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.include.com.google.common.base.Objects;
import com.minelittlepony.unicopia.ability.magic.Affine;
@ -12,6 +13,8 @@ import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.nbt.NbtCompound;
/**
* Interface for a magic spells
*/
@ -104,4 +107,25 @@ public interface Spell extends NbtSerialisable, Affine {
default ThrowableSpell toThrowable() {
return SpellType.THROWN_SPELL.create(SpellTraits.EMPTY).setSpell(this);
}
@Nullable
static Spell readNbt(@Nullable NbtCompound compound) {
if (compound != null && compound.contains("effect_id")) {
Spell effect = SpellType.getKey(compound).create(SpellTraits.EMPTY);
if (effect != null) {
effect.fromNBT(compound);
}
return effect;
}
return null;
}
static NbtCompound writeNbt(Spell effect) {
NbtCompound compound = effect.toNBT();
effect.getType().toNbt(compound);
return compound;
}
}

View file

@ -74,13 +74,13 @@ public final class ThrowableSpell extends AbstractDelegatingSpell {
@Override
public void toNBT(NbtCompound compound) {
super.toNBT(compound);
compound.put("spell", SpellType.toNBT(spell));
compound.put("spell", Spell.writeNbt(spell));
}
@Override
public void fromNBT(NbtCompound compound) {
super.fromNBT(compound);
spell = SpellType.fromNBT(compound.getCompound("spell"));
spell = Spell.readNbt(compound.getCompound("spell"));
}
@Override

View file

@ -61,7 +61,7 @@ public class IngredientWithSpell implements Predicate<ItemStack> {
ingredient.stack = Optional.ofNullable(Ingredient.fromPacket(buf));
}
if (buf.readBoolean()) {
ingredient.spell = Optional.of(SpellType.getKey(buf.readIdentifier()));
ingredient.spell = SpellType.REGISTRY.getOrEmpty(buf.readIdentifier());
}
return ingredient;
@ -76,7 +76,7 @@ public class IngredientWithSpell implements Predicate<ItemStack> {
ingredient.stack = Optional.ofNullable(Ingredient.fromJson(JsonHelper.getObject(json, "item")));
}
if (json.has("spell")) {
ingredient.spell = Optional.ofNullable(Identifier.tryParse(JsonHelper.getString(json, "spell"))).map(SpellType::getKey);
ingredient.spell = SpellType.REGISTRY.getOrEmpty(Identifier.tryParse(JsonHelper.getString(json, "spell")));
}
} else {
ingredient.stack = Optional.ofNullable(Ingredient.fromJson(json));

View file

@ -44,7 +44,7 @@ public record CustomisedSpellType<T extends Spell> (
public NbtCompound toNBT() {
NbtCompound tag = new NbtCompound();
type.writeId(tag);
type.toNbt(tag);
tag.put("traits", traits.toNbt());
return tag;
}

View file

@ -3,7 +3,6 @@ package com.minelittlepony.unicopia.ability.magic.spell.effect;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.jetbrains.annotations.Nullable;
@ -36,15 +35,15 @@ public final class SpellType<T extends Spell> implements Affine, SpellPredicate<
public static final Identifier EMPTY_ID = new Identifier("unicopia", "none");
public static final SpellType<?> EMPTY_KEY = new SpellType<>(EMPTY_ID, Affinity.NEUTRAL, 0xFFFFFF, false, SpellTraits.EMPTY, (t, c) -> null);
private static final Registry<SpellType<?>> REGISTRY = Registries.createSimple(new Identifier("unicopia", "spells"));
public static final Registry<SpellType<?>> REGISTRY = Registries.createSimple(new Identifier("unicopia", "spells"));
private static final Map<Affinity, Set<SpellType<?>>> BY_AFFINITY = new EnumMap<>(Affinity.class);
public static final SpellType<CompoundSpell> COMPOUND_SPELL = register("compound", Affinity.NEUTRAL, 0, false, CompoundSpell::new);
public static final SpellType<PlaceableSpell> PLACED_SPELL = register("placed", Affinity.NEUTRAL, 0, false, PlaceableSpell::new);
public static final SpellType<ThrowableSpell> THROWN_SPELL = register("thrown", Affinity.NEUTRAL, 0, false, ThrowableSpell::new);
public static final SpellType<CompoundSpell> COMPOUND_SPELL = register("compound", Affinity.NEUTRAL, 0, false, SpellTraits.EMPTY, CompoundSpell::new);
public static final SpellType<PlaceableSpell> PLACED_SPELL = register("placed", Affinity.NEUTRAL, 0, false, SpellTraits.EMPTY, PlaceableSpell::new);
public static final SpellType<ThrowableSpell> THROWN_SPELL = register("thrown", Affinity.NEUTRAL, 0, false, SpellTraits.EMPTY, ThrowableSpell::new);
public static final SpellType<AbstractDisguiseSpell> CHANGELING_DISGUISE = register("disguise", Affinity.BAD, 0x19E48E, false, DispersableDisguiseSpell::new);
public static final SpellType<RainboomAbilitySpell> RAINBOOM = register("rainboom", Affinity.GOOD, 0xBDBDF9, false, RainboomAbilitySpell::new);
public static final SpellType<AbstractDisguiseSpell> CHANGELING_DISGUISE = register("disguise", Affinity.BAD, 0x19E48E, false, SpellTraits.EMPTY, DispersableDisguiseSpell::new);
public static final SpellType<RainboomAbilitySpell> RAINBOOM = register("rainboom", Affinity.GOOD, 0xBDBDF9, false, SpellTraits.EMPTY, RainboomAbilitySpell::new);
public static final SpellType<IceSpell> FROST = register("frost", Affinity.GOOD, 0xBDBDF9, true, IceSpell.DEFAULT_TRAITS, IceSpell::new);
public static final SpellType<ScorchSpell> SCORCH = register("scorch", Affinity.BAD, 0, true, ScorchSpell.DEFAULT_TRAITS, ScorchSpell::new);
@ -166,10 +165,23 @@ public final class SpellType<T extends Spell> implements Affine, SpellPredicate<
return spell != null && spell.getType() == this;
}
public void toNbt(NbtCompound tag) {
tag.putString("effect_id", getId().toString());
}
public boolean isEmpty() {
return this == EMPTY_KEY;
}
@Deprecated(forRemoval = true)
public static <T extends Spell> SpellType<T> register(String name, Affinity affinity, int color, boolean obtainable, Factory<T> factory) {
return register(name, affinity, color, obtainable, SpellTraits.EMPTY, factory);
}
public static <T extends Spell> SpellType<T> register(String name, Affinity affinity, int color, boolean obtainable, SpellTraits traits, Factory<T> factory) {
return register(new Identifier("unicopia", name), affinity, color, obtainable, traits, factory);
}
public static <T extends Spell> SpellType<T> register(Identifier id, Affinity affinity, int color, boolean obtainable, SpellTraits traits, Factory<T> factory) {
SpellType<T> type = new SpellType<>(id, affinity, color, obtainable, traits, factory);
byAffinity(affinity).add(type);
@ -177,14 +189,6 @@ public final class SpellType<T extends Spell> implements Affine, SpellPredicate<
return type;
}
public static <T extends Spell> SpellType<T> register(String name, Affinity affinity, int color, boolean obtainable, Factory<T> factory) {
return register(name, affinity, color, obtainable, SpellTraits.EMPTY, factory);
}
public static <T extends Spell> SpellType<T> register(String name, Affinity affinity, int color, boolean obtainable, SpellTraits traits, Factory<T> factory) {
return register(new Identifier("unicopia", name), affinity, color, obtainable, traits, factory);
}
@SuppressWarnings("unchecked")
public static <T extends Spell> SpellType<T> empty() {
return (SpellType<T>)EMPTY_KEY;
@ -196,42 +200,13 @@ public final class SpellType<T extends Spell> implements Affine, SpellPredicate<
@SuppressWarnings("unchecked")
public static <T extends Spell> SpellType<T> getKey(@Nullable Identifier id) {
return (SpellType<T>)(id == null || EMPTY_ID.equals(id) ? EMPTY_KEY : REGISTRY.getOrEmpty(id).orElse(EMPTY_KEY));
}
public static SpellType<?> random(Random random) {
return REGISTRY.getRandom(random);
return (SpellType<T>)REGISTRY.get(id);
}
public static Set<SpellType<?>> byAffinity(Affinity affinity) {
return BY_AFFINITY.computeIfAbsent(affinity, a -> new HashSet<>());
}
@Nullable
public static Spell fromNBT(@Nullable NbtCompound compound) {
if (compound != null && compound.contains("effect_id")) {
Spell effect = getKey(compound).create(SpellTraits.EMPTY);
if (effect != null) {
effect.fromNBT(compound);
}
return effect;
}
return null;
}
public static NbtCompound toNBT(Spell effect) {
NbtCompound compound = effect.toNBT();
effect.getType().writeId(compound);
return compound;
}
public void writeId(NbtCompound tag) {
tag.putString("effect_id", getId().toString());
}
public interface Factory<T extends Spell> {
T create(SpellType<T> type, SpellTraits traits);
}

View file

@ -4,7 +4,7 @@ import com.minelittlepony.unicopia.Affinity;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ability.magic.Affine;
import com.minelittlepony.unicopia.ability.magic.Levelled;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.entity.ai.BreakHeartGoal;
import com.minelittlepony.unicopia.entity.ai.DynamicTargetGoal;
import com.minelittlepony.unicopia.entity.ai.WantItTakeItGoal;
@ -97,7 +97,7 @@ public class Creature extends Living<LivingEntity> {
public void toNBT(NbtCompound compound) {
super.toNBT(compound);
getSpellSlot().get(true).ifPresent(effect -> {
compound.put("effect", SpellType.toNBT(effect));
compound.put("effect", Spell.writeNbt(effect));
});
physics.toNBT(compound);
}
@ -106,7 +106,7 @@ public class Creature extends Living<LivingEntity> {
public void fromNBT(NbtCompound compound) {
super.fromNBT(compound);
if (compound.contains("effect")) {
getSpellSlot().put(SpellType.fromNBT(compound.getCompound("effect")));
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
}
physics.fromNBT(compound);
}

View file

@ -36,7 +36,7 @@ public interface UTradeOffers {
TradeOfferHelper.registerWanderingTraderOffers(1, factories -> {
factories.add(buyTiered(UItems.GEMSTONE, 30, UItems.GOLDEN_FEATHER, 1, UItems.GOLDEN_WING, 1, 30, 2, 0.05F));
factories.add((e, rng) -> new TradeOffer(new ItemStack(UItems.GEMSTONE, 3), GemstoneItem.enchant(UItems.GEMSTONE.getDefaultStack(), SpellType.random(rng)), 20, 1, 0.05F));
factories.add((e, rng) -> new TradeOffer(new ItemStack(UItems.GEMSTONE, 3), GemstoneItem.enchant(UItems.GEMSTONE.getDefaultStack(), SpellType.REGISTRY.getRandom(rng)), 20, 1, 0.05F));
factories.add(buy(UItems.GEMSTONE, 20, UItems.HAY_FRIES, 5, 50, 3, 0.06F));
factories.add(buy(Items.WHEAT, 17, UItems.HAY_BURGER, 1, 10, 6, 0.08F));
factories.add(buy(ItemTags.SMALL_FLOWERS, 2, UItems.DAFFODIL_DAISY_SANDWICH, 1, 10, 6, 0.08F));

View file

@ -482,7 +482,7 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
compound.put("discoveries", discoveries.toNBT());
getSpellSlot().get(true).ifPresent(effect ->{
compound.put("effect", SpellType.toNBT(effect));
compound.put("effect", Spell.writeNbt(effect));
});
}
@ -500,7 +500,7 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
magicExhaustion = compound.getFloat("magicExhaustion");
if (compound.contains("effect")) {
getSpellSlot().put(SpellType.fromNBT(compound.getCompound("effect")));
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
}
}

View file

@ -7,7 +7,6 @@ import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import net.minecraft.nbt.NbtCompound;
@ -78,7 +77,7 @@ public class SpellNetworkedReference<T extends Spell> implements NetworkedRefere
}
if (mustReplace(comp)) {
updateReference((T)SpellType.fromNBT(comp));
updateReference((T)Spell.readNbt(comp));
return false;
}
@ -98,7 +97,7 @@ public class SpellNetworkedReference<T extends Spell> implements NetworkedRefere
@Override
public NbtCompound toNbt() {
dirty = false;
return getReference().map(SpellType::toNBT).orElseGet(NbtCompound::new);
return getReference().map(Spell::writeNbt).orElseGet(NbtCompound::new);
}
@Override

View file

@ -12,7 +12,6 @@ import com.minelittlepony.unicopia.ability.magic.SpellContainer;
import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.spell.Situation;
import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.entity.EntityPhysics;
import com.minelittlepony.unicopia.entity.EntityReference;
import com.minelittlepony.unicopia.entity.Physics;
@ -223,7 +222,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Li
physics.fromNBT(compound);
homingTarget.fromNBT(compound.getCompound("homingTarget"));
if (compound.contains("effect")) {
getSpellSlot().put(SpellType.fromNBT(compound.getCompound("effect")));
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
}
}
@ -233,7 +232,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Li
physics.toNBT(compound);
compound.put("homingTarget", homingTarget.toNBT());
getSpellSlot().get(true).ifPresent(effect -> {
compound.put("effect", SpellType.toNBT(effect));
compound.put("effect", Spell.writeNbt(effect));
});
}