diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java index d4b8dfe3..d9743a90 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java @@ -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()); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java index 39809964..eaab3e8a 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java @@ -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 diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java index ce527677..d82db6ce 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java @@ -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; + } } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java index 26600ee3..ef1b699b 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java @@ -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 diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/IngredientWithSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/IngredientWithSpell.java index d607e59a..b00f56ac 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/IngredientWithSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/IngredientWithSpell.java @@ -61,7 +61,7 @@ public class IngredientWithSpell implements Predicate { 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 { 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)); diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/CustomisedSpellType.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/CustomisedSpellType.java index f492c709..00a25c09 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/CustomisedSpellType.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/CustomisedSpellType.java @@ -44,7 +44,7 @@ public record CustomisedSpellType ( public NbtCompound toNBT() { NbtCompound tag = new NbtCompound(); - type.writeId(tag); + type.toNbt(tag); tag.put("traits", traits.toNbt()); return tag; } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java index ed11b3a9..d24c5769 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/SpellType.java @@ -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 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> REGISTRY = Registries.createSimple(new Identifier("unicopia", "spells")); + public static final Registry> REGISTRY = Registries.createSimple(new Identifier("unicopia", "spells")); private static final Map>> BY_AFFINITY = new EnumMap<>(Affinity.class); - public static final SpellType COMPOUND_SPELL = register("compound", Affinity.NEUTRAL, 0, false, CompoundSpell::new); - public static final SpellType PLACED_SPELL = register("placed", Affinity.NEUTRAL, 0, false, PlaceableSpell::new); - public static final SpellType THROWN_SPELL = register("thrown", Affinity.NEUTRAL, 0, false, ThrowableSpell::new); + public static final SpellType COMPOUND_SPELL = register("compound", Affinity.NEUTRAL, 0, false, SpellTraits.EMPTY, CompoundSpell::new); + public static final SpellType PLACED_SPELL = register("placed", Affinity.NEUTRAL, 0, false, SpellTraits.EMPTY, PlaceableSpell::new); + public static final SpellType THROWN_SPELL = register("thrown", Affinity.NEUTRAL, 0, false, SpellTraits.EMPTY, ThrowableSpell::new); - public static final SpellType CHANGELING_DISGUISE = register("disguise", Affinity.BAD, 0x19E48E, false, DispersableDisguiseSpell::new); - public static final SpellType RAINBOOM = register("rainboom", Affinity.GOOD, 0xBDBDF9, false, RainboomAbilitySpell::new); + public static final SpellType CHANGELING_DISGUISE = register("disguise", Affinity.BAD, 0x19E48E, false, SpellTraits.EMPTY, DispersableDisguiseSpell::new); + public static final SpellType RAINBOOM = register("rainboom", Affinity.GOOD, 0xBDBDF9, false, SpellTraits.EMPTY, RainboomAbilitySpell::new); public static final SpellType FROST = register("frost", Affinity.GOOD, 0xBDBDF9, true, IceSpell.DEFAULT_TRAITS, IceSpell::new); public static final SpellType SCORCH = register("scorch", Affinity.BAD, 0, true, ScorchSpell.DEFAULT_TRAITS, ScorchSpell::new); @@ -166,10 +165,23 @@ public final class SpellType 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 SpellType register(String name, Affinity affinity, int color, boolean obtainable, Factory factory) { + return register(name, affinity, color, obtainable, SpellTraits.EMPTY, factory); + } + + public static SpellType register(String name, Affinity affinity, int color, boolean obtainable, SpellTraits traits, Factory factory) { + return register(new Identifier("unicopia", name), affinity, color, obtainable, traits, factory); + } + public static SpellType register(Identifier id, Affinity affinity, int color, boolean obtainable, SpellTraits traits, Factory factory) { SpellType type = new SpellType<>(id, affinity, color, obtainable, traits, factory); byAffinity(affinity).add(type); @@ -177,14 +189,6 @@ public final class SpellType implements Affine, SpellPredicate< return type; } - public static SpellType register(String name, Affinity affinity, int color, boolean obtainable, Factory factory) { - return register(name, affinity, color, obtainable, SpellTraits.EMPTY, factory); - } - - public static SpellType register(String name, Affinity affinity, int color, boolean obtainable, SpellTraits traits, Factory factory) { - return register(new Identifier("unicopia", name), affinity, color, obtainable, traits, factory); - } - @SuppressWarnings("unchecked") public static SpellType empty() { return (SpellType)EMPTY_KEY; @@ -196,42 +200,13 @@ public final class SpellType implements Affine, SpellPredicate< @SuppressWarnings("unchecked") public static SpellType getKey(@Nullable Identifier id) { - return (SpellType)(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)REGISTRY.get(id); } public static Set> 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 create(SpellType type, SpellTraits traits); } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Creature.java b/src/main/java/com/minelittlepony/unicopia/entity/Creature.java index ec93db12..0567350f 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Creature.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Creature.java @@ -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 { 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 { 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); } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/UTradeOffers.java b/src/main/java/com/minelittlepony/unicopia/entity/UTradeOffers.java index 3e0d3428..5b7be521 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/UTradeOffers.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/UTradeOffers.java @@ -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)); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index 197d2e0e..8399c91c 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -482,7 +482,7 @@ public class Pony extends Living 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 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"))); } } diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/SpellNetworkedReference.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/SpellNetworkedReference.java index 605959fc..16f4a7ce 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/SpellNetworkedReference.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/SpellNetworkedReference.java @@ -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 implements NetworkedRefere } if (mustReplace(comp)) { - updateReference((T)SpellType.fromNBT(comp)); + updateReference((T)Spell.readNbt(comp)); return false; } @@ -98,7 +97,7 @@ public class SpellNetworkedReference 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 diff --git a/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java b/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java index 3ba442c0..bfc433bf 100644 --- a/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java @@ -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
  • { - compound.put("effect", SpellType.toNBT(effect)); + compound.put("effect", Spell.writeNbt(effect)); }); }