mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-27 15:17:59 +01:00
Cleanup post porting
This commit is contained in:
parent
24f1f4bdcd
commit
c3c2ba461a
3 changed files with 38 additions and 36 deletions
|
@ -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<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();
|
||||
|
||||
public static final Codec<IngredientWithSpell> CODEC = Codec.of(new Encoder<IngredientWithSpell>() {
|
||||
@Override
|
||||
public <T> DataResult<T> encode(IngredientWithSpell input, DynamicOps<T> ops, T prefix) {
|
||||
|
@ -32,27 +34,35 @@ public class IngredientWithSpell implements Predicate<ItemStack> {
|
|||
@Override
|
||||
public <T> DataResult<Pair<IngredientWithSpell, T>> decode(DynamicOps<T> ops, T input) {
|
||||
// TODO: Doing codecs properly is an exercise left to the readers
|
||||
DataResult<Pair<Ingredient, T>> 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<DefaultedList<IngredientWithSpell>> LIST_CODEC = CODEC.listOf().xmap(list -> {
|
||||
return DefaultedList.<IngredientWithSpell>copyOf(EMPTY, list.toArray(IngredientWithSpell[]::new));
|
||||
}, a -> a);
|
||||
public static final Codec<DefaultedList<IngredientWithSpell>> LIST_CODEC = CODEC.listOf().xmap(
|
||||
list -> DefaultedList.<IngredientWithSpell>copyOf(EMPTY, list.toArray(IngredientWithSpell[]::new)),
|
||||
Function.identity()
|
||||
);
|
||||
|
||||
private Optional<Ingredient> stack = Optional.empty();
|
||||
private Optional<SpellType<?>> spell = Optional.empty();
|
||||
private final Optional<Ingredient> stack;
|
||||
private final Optional<SpellType<?>> spell;
|
||||
|
||||
@Nullable
|
||||
private ItemStack[] stacks;
|
||||
private final Supplier<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
|
||||
public boolean test(ItemStack t) {
|
||||
|
@ -62,14 +72,7 @@ public class IngredientWithSpell implements Predicate<ItemStack> {
|
|||
}
|
||||
|
||||
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<ItemStack> {
|
|||
}
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -340,6 +340,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Packet<ClientPlayPacketListener> createSpawnPacket() {
|
||||
return (Packet<ClientPlayPacketListener>)(Object)Channel.SERVER_SPAWN_PROJECTILE.toPacket(new MsgSpawnProjectile(this));
|
||||
|
|
Loading…
Reference in a new issue