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 6e6c1afa..0c5e1b43 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 @@ -2,10 +2,11 @@ package com.minelittlepony.unicopia.ability.magic.spell.crafting; import java.util.Arrays; import java.util.Optional; +import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.Supplier; -import org.jetbrains.annotations.Nullable; - +import com.google.common.base.Suppliers; import com.google.gson.JsonParseException; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.item.EnchantableItem; @@ -21,8 +22,9 @@ import net.minecraft.recipe.Ingredient; import net.minecraft.util.collection.DefaultedList; public class IngredientWithSpell implements Predicate { - private static final IngredientWithSpell EMPTY = new IngredientWithSpell(); + private static final IngredientWithSpell EMPTY = new IngredientWithSpell(Optional.empty(), Optional.empty()); private static final Predicate INGREDIENT_IS_PRESENT = ((Predicate)(Ingredient::isEmpty)).negate(); + public static final Codec CODEC = Codec.of(new Encoder() { @Override public DataResult encode(IngredientWithSpell input, DynamicOps ops, T prefix) { @@ -32,27 +34,35 @@ public class IngredientWithSpell implements Predicate { @Override public DataResult> decode(DynamicOps ops, T input) { // TODO: Doing codecs properly is an exercise left to the readers - DataResult> stack = Ingredient.ALLOW_EMPTY_CODEC.decode(ops, input); - IngredientWithSpell ingredient = new IngredientWithSpell(); - ingredient.stack = stack.result().map(Pair::getFirst); - ingredient.spell = Optional.ofNullable(((com.mojang.serialization.MapLike)input).get("Spell")).flatMap(unserializedSpell -> { - return SpellType.REGISTRY.getCodec().parse((DynamicOps)ops, unserializedSpell).result(); - }); - - return DataResult.success(new Pair<>(ingredient, null)); + return DataResult.success(new Pair<>( + new IngredientWithSpell( + Ingredient.ALLOW_EMPTY_CODEC.decode(ops, input).map(Pair::getFirst).result(), + ops.getMap(input).flatMap(maplike -> SpellType.REGISTRY.getCodec().parse(ops, maplike.get("spell"))).result() + ), input) + ); } }); - public static final Codec> LIST_CODEC = CODEC.listOf().xmap(list -> { - return DefaultedList.copyOf(EMPTY, list.toArray(IngredientWithSpell[]::new)); - }, a -> a); + public static final Codec> LIST_CODEC = CODEC.listOf().xmap( + list -> DefaultedList.copyOf(EMPTY, list.toArray(IngredientWithSpell[]::new)), + Function.identity() + ); - private Optional stack = Optional.empty(); - private Optional> spell = Optional.empty(); + private final Optional stack; + private final Optional> spell; - @Nullable - private ItemStack[] stacks; + private final Supplier stacks; - private IngredientWithSpell() {} + private IngredientWithSpell(Optional stack, Optional> spell) { + this.stack = stack; + this.spell = spell; + stacks = Suppliers.memoize(() -> { + return stack.stream() + .map(Ingredient::getMatchingStacks) + .flatMap(Arrays::stream) + .map(s -> spell.map(p -> EnchantableItem.enchant(s, p)).orElse(s)) + .toArray(ItemStack[]::new); + }); + } @Override public boolean test(ItemStack t) { @@ -62,14 +72,7 @@ public class IngredientWithSpell implements Predicate { } public ItemStack[] getMatchingStacks() { - if (stacks == null) { - stacks = stack.stream() - .map(Ingredient::getMatchingStacks) - .flatMap(Arrays::stream) - .map(stack -> spell.map(spell -> EnchantableItem.enchant(stack, spell)).orElse(stack)) - .toArray(ItemStack[]::new); - } - return stacks; + return stacks.get(); } public boolean isEmpty() { @@ -82,9 +85,9 @@ public class IngredientWithSpell implements Predicate { } public static IngredientWithSpell fromPacket(PacketByteBuf buf) { - IngredientWithSpell ingredient = new IngredientWithSpell(); - ingredient.stack = buf.readOptional(Ingredient::fromPacket); - ingredient.spell = buf.readOptional(PacketByteBuf::readIdentifier).flatMap(SpellType.REGISTRY::getOrEmpty); - return ingredient; + return new IngredientWithSpell( + buf.readOptional(Ingredient::fromPacket), + buf.readOptional(PacketByteBuf::readIdentifier).flatMap(SpellType.REGISTRY::getOrEmpty) + ); } } diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/PolearmRenderer.java b/src/main/java/com/minelittlepony/unicopia/client/render/PolearmRenderer.java index 6d94f06b..bb78a379 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/render/PolearmRenderer.java +++ b/src/main/java/com/minelittlepony/unicopia/client/render/PolearmRenderer.java @@ -1,6 +1,6 @@ package com.minelittlepony.unicopia.client.render; -import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry; +import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin; import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry; import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry.DynamicItemRenderer; import net.minecraft.client.MinecraftClient; @@ -13,8 +13,6 @@ import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.json.ModelTransformationMode; import net.minecraft.client.util.ModelIdentifier; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.client.world.ClientWorld; -import net.minecraft.entity.LivingEntity; import net.minecraft.item.*; import net.minecraft.util.Identifier; import net.minecraft.util.math.RotationAxis; @@ -29,10 +27,10 @@ public class PolearmRenderer implements DynamicItemRenderer { public static void register(Item item) { BuiltinItemRendererRegistry.INSTANCE.register(item, INSTANCE); - ModelPredicateProviderRegistry.register(item, THROWING, (ItemStack stack, ClientWorld world, LivingEntity entity, int seed) -> { + ModelPredicateProviderRegistry.register(item, THROWING, (stack, world, entity, seed) -> { return entity != null && entity.isUsingItem() && entity.getActiveItem() == stack ? 1 : 0; }); - ModelLoadingRegistry.INSTANCE.registerModelProvider((renderer, out) -> out.accept(getModelId(item))); + ModelLoadingPlugin.register(context -> context.addModels(getModelId(item))); } static ModelIdentifier getModelId(ItemConvertible item) { diff --git a/src/main/java/com/minelittlepony/unicopia/item/URecipes.java b/src/main/java/com/minelittlepony/unicopia/item/URecipes.java index 9bafb923..08a3e7d7 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/URecipes.java +++ b/src/main/java/com/minelittlepony/unicopia/item/URecipes.java @@ -1,8 +1,5 @@ package com.minelittlepony.unicopia.item; -import java.util.List; - -import com.google.gson.JsonArray; import com.minelittlepony.unicopia.ability.magic.spell.crafting.*; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; @@ -15,7 +12,6 @@ import net.minecraft.recipe.Ingredient; import net.minecraft.recipe.RecipeSerializer; import net.minecraft.recipe.RecipeType; import net.minecraft.recipe.ShapedRecipe; -import net.minecraft.recipe.ShapelessRecipe; import net.minecraft.recipe.SpecialRecipeSerializer; import net.minecraft.util.Identifier; import net.minecraft.util.collection.DefaultedList; diff --git a/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java b/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java index bdef3201..dacbab72 100644 --- a/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/projectile/MagicProjectileEntity.java @@ -340,6 +340,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster createSpawnPacket() { return (Packet)(Object)Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this));