All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.magistuarmory.util.MobEquipment Maven / Gradle / Ivy

package com.magistuarmory.util;

import com.magistuarmory.EpicKnights;
import com.magistuarmory.config.MobEquipmentConfig;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ShieldItem;
import net.minecraft.world.level.Level;

import java.util.*;

public class MobEquipment
{
    public static final MobEquipmentConfig MOBS_EQUIPMENT_CONFIG = EpicKnights.CONFIG.mobEquipments;
    static Map, ResourceKey>, List> EQUIPMENTS = new HashMap<>();

    public List> entities = new ArrayList<>();

    public List> dimensions;

    public List helmets = new ArrayList<>();
    public List chestplates = new ArrayList<>();
    public List leggings = new ArrayList<>();
    public List boots = new ArrayList<>();

    public List weapons = new ArrayList<>();
    public List shields = new ArrayList<>();

    public double chance;

    @SuppressWarnings("unchecked")
    MobEquipment(MinecraftServer server, String[] ids)
    {
        List> dimensions = new ArrayList<>();
        this.chance = EpicKnights.GENERAL_CONFIG.equipChance;

        for (String id : ids)
        {
            ResourceLocation resloc = new ResourceLocation(id);

            Optional> entityoptional = Registry.f_122826_.m_6612_(resloc);
            if (entityoptional.isPresent())
            {
                try
                {
                    this.entities.add((EntityType) entityoptional.get());
                    continue;
                }
                catch (ClassCastException e)
                {
                    System.out.println("[Epic-Knights Mob Equipment] Non-living entity type \"" + id + "\" is not allowed");
                }
            }

            Optional itemoptional = Registry.f_122827_.m_6612_(resloc);
            if (itemoptional.isPresent())
            {
                if (itemoptional.get() instanceof ArmorItem armor)
                {
                    switch (armor.m_40402_())
                    {
                        case HEAD -> this.helmets.add(armor);
                        case CHEST -> this.chestplates.add(armor);
                        case LEGS -> this.leggings.add(armor);
                        case FEET -> this.boots.add(armor);
                    }
                    continue;
                }
                if (itemoptional.get() instanceof ShieldItem shield)
                {
                    this.shields.add(shield);
                    continue;
                }
                this.weapons.add(itemoptional.get());
                continue;
            }

            ResourceKey resourcekey = ResourceKey.m_135785_(Registry.f_122819_, resloc);
            ServerLevel serverlevel = server.m_129880_(resourcekey);
            if (serverlevel != null)
            {
                dimensions.add(serverlevel.m_46472_());
                continue;
            }

            if (id.matches("[-+]?[0-9]*\\.?[0-9]+"))
            {
                this.chance = Double.parseDouble(id);
                continue;
            }
        }

        if (dimensions.size() == 0)
            server.m_129785_().forEach(serverlevel -> dimensions.add(serverlevel.m_46472_()));
        this.dimensions = dimensions;
    }

    public void equip(LivingEntity entity, RandomSource rand)
    {
        MobEquipmentHelper.setRandomItemSlot(entity, EquipmentSlot.HEAD, this.helmets, this.chance, rand);
        MobEquipmentHelper.setRandomItemSlot(entity, EquipmentSlot.CHEST, this.chestplates, this.chance, rand);
        MobEquipmentHelper.setRandomItemSlot(entity, EquipmentSlot.LEGS, this.leggings, this.chance, rand);
        MobEquipmentHelper.setRandomItemSlot(entity, EquipmentSlot.FEET, this.boots, this.chance, rand);
        MobEquipmentHelper.setRandomItemSlot(entity, EquipmentSlot.MAINHAND, this.weapons, this.chance, rand);
        MobEquipmentHelper.setRandomItemSlot(entity, EquipmentSlot.OFFHAND, this.shields, 0.5f * chance, rand);
    }

    public static void setup(MinecraftServer server)
    {
        for (String ids : MOBS_EQUIPMENT_CONFIG.equipments)
        {
            MobEquipment equipment = new MobEquipment(server, ids.split(" "));
            equipment.entities.forEach(type -> equipment.dimensions.forEach(dimension -> {
                DualKey, ResourceKey> key = new DualKey<>(type, dimension);
                EQUIPMENTS.putIfAbsent(key, new ArrayList<>());
                EQUIPMENTS.get(key).add(equipment);
            }));
        }
    }

    public static List get(LivingEntity entity)
    {
        return EQUIPMENTS.getOrDefault(new DualKey<>(entity.m_6095_(), entity.f_19853_.m_46472_()), new ArrayList<>());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy