|
| 1 | +package com.cleanroommc.modularui.factory.inventory; |
| 2 | + |
| 3 | +import com.cleanroommc.modularui.network.NetworkUtils; |
| 4 | + |
| 5 | +import com.cleanroommc.modularui.utils.Platform; |
| 6 | + |
| 7 | +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; |
| 8 | + |
| 9 | +import net.minecraft.entity.player.EntityPlayer; |
| 10 | +import net.minecraft.item.ItemStack; |
| 11 | +import net.minecraft.network.PacketBuffer; |
| 12 | +import net.minecraftforge.items.ItemHandlerHelper; |
| 13 | + |
| 14 | +import org.apache.commons.lang3.tuple.Pair; |
| 15 | + |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * A way of finding and setting an item in an inventory, that is owned by a player. |
| 22 | + * This includes the normal player inventory with all its slots including main hand, off-hand and armor slots. |
| 23 | + * It can also be used for bauble inventory if baubles is loaded. |
| 24 | + * An inventory type has an id assigned used for syncing. It is crucial, types are created in the same order on client and server. |
| 25 | + * Currently, the amount of types is limited to 16, but I don't think we will ever fill that. |
| 26 | + * @see InventoryTypes InventoryTypes for default implementations |
| 27 | + */ |
| 28 | +public abstract class InventoryType { |
| 29 | + |
| 30 | + private static final Map<String, InventoryType> inventoryTypes = new Object2ObjectOpenHashMap<>(); |
| 31 | + |
| 32 | + private final String id; |
| 33 | + |
| 34 | + public InventoryType(String id) { |
| 35 | + this.id = id; |
| 36 | + inventoryTypes.put(id, this); |
| 37 | + } |
| 38 | + |
| 39 | + public String getId() { |
| 40 | + return id; |
| 41 | + } |
| 42 | + |
| 43 | + public abstract ItemStack getStackInSlot(EntityPlayer player, int index); |
| 44 | + |
| 45 | + public abstract void setStackInSlot(EntityPlayer player, int index, ItemStack stack); |
| 46 | + |
| 47 | + public abstract int getSlotCount(EntityPlayer player); |
| 48 | + |
| 49 | + public int findFirstStackable(EntityPlayer player, ItemStack stack) { |
| 50 | + for (int i = 0, n = getSlotCount(player); i < n; ++i) { |
| 51 | + ItemStack stackInSlot = getStackInSlot(player, i); |
| 52 | + if (Platform.isStackEmpty(stackInSlot)) { |
| 53 | + if (Platform.isStackEmpty(stack)) { |
| 54 | + return i; |
| 55 | + } |
| 56 | + continue; |
| 57 | + } |
| 58 | + if (ItemHandlerHelper.canItemStacksStack(stackInSlot, stack)) { |
| 59 | + return i; |
| 60 | + } |
| 61 | + } |
| 62 | + return -1; |
| 63 | + } |
| 64 | + |
| 65 | + public boolean visitAllStackable(EntityPlayer player, ItemStack stack, InventoryVisitor visitor) { |
| 66 | + for (int i = 0, n = getSlotCount(player); i < n; ++i) { |
| 67 | + ItemStack stackInSlot = getStackInSlot(player, i); |
| 68 | + if (Platform.isStackEmpty(stackInSlot)) { |
| 69 | + if (Platform.isStackEmpty(stack)) { |
| 70 | + if (visitor.visit(this, i, stackInSlot)) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + continue; |
| 75 | + } |
| 76 | + if (ItemHandlerHelper.canItemStacksStack(stackInSlot, stack)) { |
| 77 | + if (visitor.visit(this, i, stackInSlot)) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + public boolean visitAll(EntityPlayer player, InventoryVisitor visitor) { |
| 86 | + for (int i = 0, n = getSlotCount(player); i < n; ++i) { |
| 87 | + ItemStack stackInSlot = getStackInSlot(player, i); |
| 88 | + if (visitor.visit(this, i, stackInSlot)) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + } |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + public void write(PacketBuffer buf) { |
| 96 | + NetworkUtils.writeStringSafe(buf, id); |
| 97 | + } |
| 98 | + |
| 99 | + public static InventoryType read(PacketBuffer buf) { |
| 100 | + return getFromId(NetworkUtils.readStringSafe(buf)); |
| 101 | + } |
| 102 | + |
| 103 | + public static InventoryType getFromId(String id) { |
| 104 | + return inventoryTypes.get(id); |
| 105 | + } |
| 106 | + |
| 107 | + public static Collection<InventoryType> getAll() { |
| 108 | + return Collections.unmodifiableCollection(inventoryTypes.values()); |
| 109 | + } |
| 110 | + |
| 111 | + public static Pair<InventoryType, Integer> findFirstStackableInAll(EntityPlayer player, ItemStack stack) { |
| 112 | + for (InventoryType type : getAll()) { |
| 113 | + int i = type.findFirstStackable(player, stack); |
| 114 | + if (i >= 0) return Pair.of(type, i); |
| 115 | + } |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + public static void visitAllStackableInAll(EntityPlayer player, ItemStack stack, InventoryVisitor visitor) { |
| 120 | + for (InventoryType type : getAll()) { |
| 121 | + if (type.visitAllStackable(player, stack, visitor)) { |
| 122 | + return; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static void visitAllInAll(EntityPlayer player, InventoryVisitor visitor) { |
| 128 | + for (InventoryType type : getAll()) { |
| 129 | + if (type.visitAll(player, visitor)) { |
| 130 | + return; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments