MineLittlePony/src/main/java/com/minelittlepony/api/model/Models.java

58 lines
1.9 KiB
Java
Raw Normal View History

2023-09-26 01:45:28 +01:00
package com.minelittlepony.api.model;
2015-08-01 18:36:33 -04:00
2019-05-27 17:59:15 +02:00
import net.minecraft.entity.LivingEntity;
2023-02-15 19:09:59 +00:00
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ItemStack;
2019-05-27 17:59:15 +02:00
import org.jetbrains.annotations.Nullable;
import com.minelittlepony.api.pony.PonyData;
2023-09-26 01:45:28 +01:00
import com.minelittlepony.client.model.PlayerModelKey;
import com.minelittlepony.client.model.armour.*;
import com.minelittlepony.mson.api.ModelKey;
import com.minelittlepony.mson.api.MsonModel;
2015-11-17 00:09:04 -05:00
2023-02-15 19:09:59 +00:00
import java.util.*;
import java.util.function.Consumer;
/**
* Container class for the various models and their associated piece of armour.
*/
public class Models<T extends LivingEntity, M extends PonyModel<?>> {
2023-02-15 19:09:59 +00:00
@Nullable
private final MsonModel.Factory<PonyArmourModel<T>> armorFactory;
private final Map<ModelKey<PonyArmourModel<?>>, PonyArmourModel<T>> armor = new HashMap<>();
private final M body;
public Models(PlayerModelKey<T, ? super M> playerModelKey, boolean slimArms, @Nullable Consumer<M> initializer) {
2023-02-15 19:09:59 +00:00
this.armorFactory = playerModelKey.armorFactory();
this.body = playerModelKey.getKey(slimArms).createModel();
if (initializer != null) {
initializer.accept(this.body);
}
}
public Models(ModelKey<M> key) {
2023-02-15 19:09:59 +00:00
this.armorFactory = null;
this.body = key.createModel();
}
public M body() {
return body;
}
2023-03-21 21:38:32 +00:00
public Optional<PonyArmourModel<T>> getArmourModel(ItemStack stack, ArmourLayer layer, ArmourVariant variant) {
return ArmorModelRegistry.getModelKey(stack.getItem(), layer).or(() -> variant.getDefaultModel(layer).filter(l -> stack.getItem() instanceof ArmorItem))
2023-02-15 19:09:59 +00:00
.map(key -> armor.computeIfAbsent(key, k -> {
return armorFactory == null ? k.createModel() : k.createModel(armorFactory);
}));
2015-08-01 18:36:33 -04:00
}
public Models<T, M> applyMetadata(PonyData meta) {
body.setMetadata(meta);
2023-02-15 19:09:59 +00:00
armor.values().forEach(a -> a.setMetadata(meta));
2019-11-26 23:55:39 +02:00
return this;
2015-08-01 18:36:33 -04:00
}
}