| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.resources.ResourceKey;
|
| 8 | import net.minecraft.world.item.ItemStack;
|
| 9 | import net.minecraft.world.item.equipment.Equippable;
|
| 10 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 11 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public interface EquipmentUser {
|
| 15 | void setItemSlot(final EquipmentSlot slot, final ItemStack stack);
|
| 16 |
|
| 17 | ItemStack getItemBySlot(final EquipmentSlot slot);
|
| 18 |
|
| 19 | void setDropChance(final EquipmentSlot slot, final float dropChance);
|
| 20 |
|
| 21 | default void equip(final EquipmentTable equipment, final LootParams lootParams) {
|
| 22 | this.equip(equipment.lootTable(), lootParams, equipment.slotDropChances());
|
| 23 | }
|
| 24 |
|
| 25 | default void equip(final ResourceKey<LootTable> lootTable, final LootParams lootParams, final Map<EquipmentSlot, Float> dropChances) {
|
| 26 | this.equip(lootTable, lootParams, 0L, dropChances);
|
| 27 | }
|
| 28 |
|
| 29 | default void equip(
|
| 30 | final ResourceKey<LootTable> lootTable, final LootParams lootParams, final long optionalLootTableSeed, final Map<EquipmentSlot, Float> dropChances
|
| 31 | ) {
|
| 32 | LootTable table = lootParams.getLevel().getServer().reloadableRegistries().getLootTable(lootTable);
|
| 33 | if (table != LootTable.EMPTY) {
|
| 34 | List<ItemStack> possibleEquipment = table.getRandomItems(lootParams, optionalLootTableSeed);
|
| 35 | List<EquipmentSlot> insertedIntoSlots = new ArrayList<>();
|
| 36 |
|
| 37 | for (ItemStack toEquip : possibleEquipment) {
|
| 38 | EquipmentSlot slot = this.resolveSlot(toEquip, insertedIntoSlots);
|
| 39 | if (slot != null) {
|
| 40 | ItemStack equipped = slot.limit(toEquip);
|
| 41 | this.setItemSlot(slot, equipped);
|
| 42 | Float dropChance = dropChances.get(slot);
|
| 43 | if (dropChance != null) {
|
| 44 | this.setDropChance(slot, dropChance);
|
| 45 | }
|
| 46 |
|
| 47 | insertedIntoSlots.add(slot);
|
| 48 | }
|
| 49 | }
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | default @Nullable EquipmentSlot resolveSlot(final ItemStack toEquip, final List<EquipmentSlot> alreadyInsertedIntoSlots) {
|
| 54 | if (toEquip.isEmpty()) {
|
| 55 | return null;
|
| 56 | }
|
| 57 |
|
| 58 | Equippable equippable = toEquip.get(DataComponents.EQUIPPABLE);
|
| 59 | if (equippable != null) {
|
| 60 | EquipmentSlot slot = equippable.slot();
|
| 61 | if (!alreadyInsertedIntoSlots.contains(slot)) {
|
| 62 | return slot;
|
| 63 | }
|
| 64 | } else if (!alreadyInsertedIntoSlots.contains(EquipmentSlot.MAINHAND)) {
|
| 65 | return EquipmentSlot.MAINHAND;
|
| 66 | }
|
| 67 |
|
| 68 | return null;
|
| 69 | }
|
| 70 | }
|
| 71 |
|