Cleanup post porting

This commit is contained in:
Sollace 2023-09-29 20:54:33 +01:00
parent 0c236e90eb
commit 7b4e9174f9
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
4 changed files with 38 additions and 40 deletions

View file

@ -2,10 +2,11 @@ package com.minelittlepony.unicopia.ability.magic.spell.crafting;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate; 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.google.gson.JsonParseException;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.item.EnchantableItem; import com.minelittlepony.unicopia.item.EnchantableItem;
@ -21,8 +22,9 @@ import net.minecraft.recipe.Ingredient;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;
public class IngredientWithSpell implements Predicate<ItemStack> { public class IngredientWithSpell implements Predicate<ItemStack> {
private static final IngredientWithSpell EMPTY = new IngredientWithSpell(); private static final IngredientWithSpell EMPTY = new IngredientWithSpell(Optional.empty(), Optional.empty());
private static final Predicate<Ingredient> INGREDIENT_IS_PRESENT = ((Predicate<Ingredient>)(Ingredient::isEmpty)).negate(); private static final Predicate<Ingredient> INGREDIENT_IS_PRESENT = ((Predicate<Ingredient>)(Ingredient::isEmpty)).negate();
public static final Codec<IngredientWithSpell> CODEC = Codec.of(new Encoder<IngredientWithSpell>() { public static final Codec<IngredientWithSpell> CODEC = Codec.of(new Encoder<IngredientWithSpell>() {
@Override @Override
public <T> DataResult<T> encode(IngredientWithSpell input, DynamicOps<T> ops, T prefix) { public <T> DataResult<T> encode(IngredientWithSpell input, DynamicOps<T> ops, T prefix) {
@ -32,27 +34,35 @@ public class IngredientWithSpell implements Predicate<ItemStack> {
@Override @Override
public <T> DataResult<Pair<IngredientWithSpell, T>> decode(DynamicOps<T> ops, T input) { public <T> DataResult<Pair<IngredientWithSpell, T>> decode(DynamicOps<T> ops, T input) {
// TODO: Doing codecs properly is an exercise left to the readers // TODO: Doing codecs properly is an exercise left to the readers
DataResult<Pair<Ingredient, T>> stack = Ingredient.ALLOW_EMPTY_CODEC.decode(ops, input); return DataResult.success(new Pair<>(
IngredientWithSpell ingredient = new IngredientWithSpell(); new IngredientWithSpell(
ingredient.stack = stack.result().map(Pair::getFirst); Ingredient.ALLOW_EMPTY_CODEC.decode(ops, input).map(Pair::getFirst).result(),
ingredient.spell = Optional.ofNullable(((com.mojang.serialization.MapLike)input).get("Spell")).flatMap(unserializedSpell -> { ops.getMap(input).flatMap(maplike -> SpellType.REGISTRY.getCodec().parse(ops, maplike.get("spell"))).result()
return SpellType.REGISTRY.getCodec().parse((DynamicOps)ops, unserializedSpell).result(); ), input)
}); );
return DataResult.success(new Pair<>(ingredient, null));
} }
}); });
public static final Codec<DefaultedList<IngredientWithSpell>> LIST_CODEC = CODEC.listOf().xmap(list -> { public static final Codec<DefaultedList<IngredientWithSpell>> LIST_CODEC = CODEC.listOf().xmap(
return DefaultedList.<IngredientWithSpell>copyOf(EMPTY, list.toArray(IngredientWithSpell[]::new)); list -> DefaultedList.<IngredientWithSpell>copyOf(EMPTY, list.toArray(IngredientWithSpell[]::new)),
}, a -> a); Function.identity()
);
private Optional<Ingredient> stack = Optional.empty(); private final Optional<Ingredient> stack;
private Optional<SpellType<?>> spell = Optional.empty(); private final Optional<SpellType<?>> spell;
@Nullable private final Supplier<ItemStack[]> stacks;
private ItemStack[] stacks;
private IngredientWithSpell() {} private IngredientWithSpell(Optional<Ingredient> stack, Optional<SpellType<?>> 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 @Override
public boolean test(ItemStack t) { public boolean test(ItemStack t) {
@ -62,14 +72,7 @@ public class IngredientWithSpell implements Predicate<ItemStack> {
} }
public ItemStack[] getMatchingStacks() { public ItemStack[] getMatchingStacks() {
if (stacks == null) { return stacks.get();
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;
} }
public boolean isEmpty() { public boolean isEmpty() {
@ -82,9 +85,9 @@ public class IngredientWithSpell implements Predicate<ItemStack> {
} }
public static IngredientWithSpell fromPacket(PacketByteBuf buf) { public static IngredientWithSpell fromPacket(PacketByteBuf buf) {
IngredientWithSpell ingredient = new IngredientWithSpell(); return new IngredientWithSpell(
ingredient.stack = buf.readOptional(Ingredient::fromPacket); buf.readOptional(Ingredient::fromPacket),
ingredient.spell = buf.readOptional(PacketByteBuf::readIdentifier).flatMap(SpellType.REGISTRY::getOrEmpty); buf.readOptional(PacketByteBuf::readIdentifier).flatMap(SpellType.REGISTRY::getOrEmpty)
return ingredient; );
} }
} }

View file

@ -1,6 +1,6 @@
package com.minelittlepony.unicopia.client.render; 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;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry.DynamicItemRenderer; import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry.DynamicItemRenderer;
import net.minecraft.client.MinecraftClient; 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.render.model.json.ModelTransformationMode;
import net.minecraft.client.util.ModelIdentifier; import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.client.util.math.MatrixStack; 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.item.*;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.RotationAxis; import net.minecraft.util.math.RotationAxis;
@ -29,10 +27,10 @@ public class PolearmRenderer implements DynamicItemRenderer {
public static void register(Item item) { public static void register(Item item) {
BuiltinItemRendererRegistry.INSTANCE.register(item, INSTANCE); 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; 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) { static ModelIdentifier getModelId(ItemConvertible item) {

View file

@ -1,8 +1,5 @@
package com.minelittlepony.unicopia.item; package com.minelittlepony.unicopia.item;
import java.util.List;
import com.google.gson.JsonArray;
import com.minelittlepony.unicopia.ability.magic.spell.crafting.*; import com.minelittlepony.unicopia.ability.magic.spell.crafting.*;
import com.mojang.serialization.Codec; import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult; import com.mojang.serialization.DataResult;
@ -15,7 +12,6 @@ import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.RecipeSerializer; import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType; import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.ShapedRecipe; import net.minecraft.recipe.ShapedRecipe;
import net.minecraft.recipe.ShapelessRecipe;
import net.minecraft.recipe.SpecialRecipeSerializer; import net.minecraft.recipe.SpecialRecipeSerializer;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;

View file

@ -340,6 +340,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
} }
} }
@SuppressWarnings("unchecked")
@Override @Override
public Packet<ClientPlayPacketListener> createSpawnPacket() { public Packet<ClientPlayPacketListener> createSpawnPacket() {
return (Packet<ClientPlayPacketListener>)(Object)Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this)); return (Packet<ClientPlayPacketListener>)(Object)Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this));