Unicopia/src/main/java/com/minelittlepony/unicopia/trinkets/TrinketsDelegate.java

90 lines
2.8 KiB
Java
Raw Normal View History

2022-09-20 23:50:15 +02:00
package com.minelittlepony.unicopia.trinkets;
import java.util.*;
import java.util.stream.Collectors;
2022-09-20 23:50:15 +02:00
import java.util.stream.Stream;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
2022-09-20 23:50:15 +02:00
import net.minecraft.util.Identifier;
public interface TrinketsDelegate {
Identifier MAINHAND = new Identifier("hand:glove");
Identifier OFFHAND = new Identifier("offhand:glove");
Identifier NECKLACE = new Identifier("chest:necklace");
Identifier FACE = new Identifier("head:face");
2022-09-20 23:50:15 +02:00
Set<Identifier> ALL = new TreeSet<>(List.of(MAINHAND, OFFHAND, NECKLACE, FACE));
2022-09-20 23:50:15 +02:00
TrinketsDelegate EMPTY = new TrinketsDelegate() {};
static TrinketsDelegate getInstance() {
if (!hasTrinkets()) {
return EMPTY;
}
return TrinketsDelegateImpl.INSTANCE;
}
static boolean hasTrinkets() {
return FabricLoader.getInstance().isModLoaded("trinkets");
}
default boolean equipStack(LivingEntity entity, Identifier slot, ItemStack stack) {
EquipmentSlot eq = MobEntity.getPreferredEquipmentSlot(stack);
if (!entity.getEquippedStack(eq).isEmpty()) {
return false;
}
entity.equipStack(eq, stack.split(1));
2022-09-21 18:02:45 +02:00
if (entity instanceof MobEntity mob) {
mob.setEquipmentDropChance(eq, 2.0f);
mob.setPersistent();
}
2022-09-20 23:50:15 +02:00
return true;
}
default void setEquippedStack(LivingEntity entity, Identifier slot, ItemStack stack) {
EquipmentSlot eq = slot == FACE ? EquipmentSlot.HEAD
: slot == NECKLACE ? EquipmentSlot.CHEST
: slot == MAINHAND ? EquipmentSlot.CHEST
: slot == OFFHAND ? EquipmentSlot.OFFHAND
: null;
if (eq != null) {
entity.equipStack(eq, stack);
}
}
2022-09-20 23:50:15 +02:00
default Set<Identifier> getAvailableTrinketSlots(LivingEntity entity, Set<Identifier> probedSlots) {
return probedSlots.stream().filter(slot -> getEquipped(entity, slot).count() == 0).collect(Collectors.toSet());
2022-09-20 23:50:15 +02:00
}
default Stream<ItemStack> getEquipped(LivingEntity entity, Identifier slot) {
if (slot == FACE) {
return Stream.of(entity.getEquippedStack(EquipmentSlot.HEAD));
}
2022-09-20 23:50:15 +02:00
if (slot == NECKLACE || slot == MAINHAND) {
return Stream.of(entity.getEquippedStack(EquipmentSlot.CHEST));
}
if (slot == OFFHAND) {
return Stream.of(entity.getOffHandStack());
}
return Stream.empty();
}
default void registerTrinket(Item item) {
}
default boolean isTrinketSlot(Slot slot) {
return false;
}
2022-09-20 23:50:15 +02:00
}