Search in sources :

Example 6 with TileEntityRack

use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by ldtteam.

the class InventoryUtils method countEmptySlotsInBuilding.

/**
 * Calculate the number of empty slots in a given building.
 * @param ownBuilding the building to check.
 * @return the number of empty slots.
 */
public static int countEmptySlotsInBuilding(final IBuilding ownBuilding) {
    int totalCount = 0;
    final World world = ownBuilding.getColony().getWorld();
    for (final BlockPos pos : ownBuilding.getContainers()) {
        if (WorldUtil.isBlockLoaded(world, pos)) {
            final TileEntity entity = world.getBlockEntity(pos);
            if (entity instanceof TileEntityRack) {
                totalCount += ((TileEntityRack) entity).getFreeSlots();
            }
        }
    }
    return totalCount;
}
Also used : ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Example 7 with TileEntityRack

use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by ldtteam.

the class WindowHutAllInventory method updateResources.

/**
 * Update the item list.
 */
private void updateResources() {
    final Set<BlockPos> containerList = new HashSet<>(building.getContainerList());
    final Map<ItemStorage, Integer> storedItems = new HashMap<>();
    final World world = building.getColony().getWorld();
    containerList.add(building.getPosition());
    for (final BlockPos blockPos : containerList) {
        final TileEntity rack = world.getBlockEntity(blockPos);
        if (rack instanceof TileEntityRack) {
            final Map<ItemStorage, Integer> rackStorage = ((TileEntityRack) rack).getAllContent();
            for (final Map.Entry<ItemStorage, Integer> entry : rackStorage.entrySet()) {
                if (storedItems.containsKey(entry.getKey())) {
                    storedItems.put(entry.getKey(), storedItems.get(entry.getKey()) + entry.getValue());
                } else {
                    storedItems.put(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    final List<ItemStorage> filterItems = new ArrayList<>();
    storedItems.forEach((storage, amount) -> {
        storage.setAmount(amount);
        filterItems.add(storage);
    });
    final Predicate<ItemStorage> filterPredicate = stack -> filter.isEmpty() || stack.getItemStack().getDescriptionId().toLowerCase(Locale.US).contains(filter.toLowerCase(Locale.US)) || stack.getItemStack().getHoverName().getString().toLowerCase(Locale.US).contains(filter.toLowerCase(Locale.US));
    allItems.clear();
    if (filter.isEmpty()) {
        allItems.addAll(filterItems);
    } else {
        allItems.addAll(filterItems.stream().filter(filterPredicate).collect(Collectors.toList()));
    }
    allItems.sort(Comparator.comparingInt(s1 -> StringUtils.getLevenshteinDistance(s1.getItemStack().getHoverName().getString(), filter)));
    final Comparator<ItemStorage> compareByName = Comparator.comparing((ItemStorage o) -> o.getItemStack().getHoverName().getString());
    final Comparator<ItemStorage> compareByCount = Comparator.comparingInt(ItemStorage::getAmount);
    switch(sortDescriptor) {
        case NO_SORT:
            break;
        case ASC_SORT:
            allItems.sort(compareByName);
            break;
        case DESC_SORT:
            allItems.sort(compareByName.reversed());
            break;
        case COUNT_ASC_SORT:
            allItems.sort(compareByCount);
            break;
        case COUNT_DESC_SORT:
            allItems.sort(compareByCount.reversed());
            break;
        default:
            break;
    }
    updateResourceList();
}
Also used : TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) Window(com.ldtteam.blockout.views.Window) java.util(java.util) com.ldtteam.blockout.controls(com.ldtteam.blockout.controls) Item(net.minecraft.item.Item) ScrollingList(com.ldtteam.blockout.views.ScrollingList) Utils(com.minecolonies.api.util.Utils) StringUtils(org.apache.commons.lang3.StringUtils) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) Minecraft(net.minecraft.client.Minecraft) Constants(com.minecolonies.api.util.constant.Constants) World(net.minecraft.world.World) Predicate(java.util.function.Predicate) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) AbstractBuilding(com.minecolonies.coremod.colony.buildings.AbstractBuilding) BlockPos(net.minecraft.util.math.BlockPos) Pane(com.ldtteam.blockout.Pane) Collectors(java.util.stream.Collectors) HighlightManager(com.minecolonies.coremod.event.HighlightManager) Screen(net.minecraft.client.gui.screen.Screen) WindowConstants(com.minecolonies.api.util.constant.WindowConstants) TileEntity(net.minecraft.tileentity.TileEntity) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) World(net.minecraft.world.World) ItemStorage(com.minecolonies.api.crafting.ItemStorage) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) BlockPos(net.minecraft.util.math.BlockPos)

Example 8 with TileEntityRack

use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by Minecolonies.

the class TileEntityWareHouse method searchMostEmptyRack.

/**
 * Search for the chest with the least items in it.
 *
 * @return the tileEntity of this chest.
 */
@Nullable
private TileEntity searchMostEmptyRack() {
    int freeSlots = 0;
    TileEntity emptiestChest = null;
    for (@NotNull final BlockPos pos : getBuilding().getContainers()) {
        final TileEntity entity = getLevel().getBlockEntity(pos);
        if (entity instanceof TileEntityRack) {
            if (((AbstractTileEntityRack) entity).isEmpty()) {
                return entity;
            }
            final int tempFreeSlots = ((AbstractTileEntityRack) entity).getFreeSlots();
            if (tempFreeSlots > freeSlots) {
                freeSlots = tempFreeSlots;
                emptiestChest = entity;
            }
        }
    }
    return emptiestChest;
}
Also used : ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) AbstractTileEntityRack(com.minecolonies.api.tileentities.AbstractTileEntityRack) AbstractTileEntityRack(com.minecolonies.api.tileentities.AbstractTileEntityRack) BlockPos(net.minecraft.util.math.BlockPos) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with TileEntityRack

use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by Minecolonies.

the class InventoryUtils method getCountFromBuilding.

/**
 * Count the number of items a building has.
 *
 * @param provider  building to check in.
 * @param predicate the predicate to match.
 * @return Amount of occurrences of stacks that match the given stack.
 */
public static int getCountFromBuilding(@NotNull final IBuilding provider, @NotNull final Predicate<ItemStack> predicate) {
    int totalCount = 0;
    final World world = provider.getColony().getWorld();
    for (final BlockPos pos : provider.getContainers()) {
        if (WorldUtil.isBlockLoaded(world, pos)) {
            final TileEntity entity = world.getBlockEntity(pos);
            if (entity instanceof TileEntityRack) {
                totalCount += ((TileEntityRack) entity).getItemCount(predicate);
            }
        }
    }
    return totalCount;
}
Also used : ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Example 10 with TileEntityRack

use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by Minecolonies.

the class InventoryUtils method getCountFromBuilding.

/**
 * Count the number of items a building has.
 *
 * @param provider building to check in.
 * @param stack    the stack to check.
 * @return Amount of occurrences of stacks that match the given stack.
 */
public static int getCountFromBuilding(@NotNull final IBuilding provider, @NotNull final ItemStorage stack) {
    int totalCount = 0;
    final World world = provider.getColony().getWorld();
    for (final BlockPos pos : provider.getContainers()) {
        if (WorldUtil.isBlockLoaded(world, pos)) {
            final TileEntity entity = world.getBlockEntity(pos);
            if (entity instanceof TileEntityRack) {
                totalCount += ((TileEntityRack) entity).getCount(stack);
            } else if (entity instanceof ChestTileEntity) {
                totalCount += getItemCountInProvider(entity, itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack.getItemStack()));
            }
        }
    }
    return totalCount;
}
Also used : ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) BlockPos(net.minecraft.util.math.BlockPos) ChestTileEntity(net.minecraft.tileentity.ChestTileEntity) World(net.minecraft.world.World)

Aggregations

TileEntityRack (com.minecolonies.api.tileentities.TileEntityRack)29 TileEntity (net.minecraft.tileentity.TileEntity)29 BlockPos (net.minecraft.util.math.BlockPos)19 World (net.minecraft.world.World)14 AbstractTileEntityRack (com.minecolonies.api.tileentities.AbstractTileEntityRack)12 ChestTileEntity (net.minecraft.tileentity.ChestTileEntity)12 NotNull (org.jetbrains.annotations.NotNull)6 ItemStorage (com.minecolonies.api.crafting.ItemStorage)5 HighlightManager (com.minecolonies.coremod.event.HighlightManager)4 IBuildingView (com.minecolonies.api.colony.buildings.views.IBuildingView)3 Pane (com.ldtteam.blockout.Pane)2 com.ldtteam.blockout.controls (com.ldtteam.blockout.controls)2 ScrollingList (com.ldtteam.blockout.views.ScrollingList)2 Window (com.ldtteam.blockout.views.Window)2 AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)2 AbstractBlockMinecoloniesRack (com.minecolonies.api.blocks.AbstractBlockMinecoloniesRack)2 IColony (com.minecolonies.api.colony.IColony)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 AbstractTileEntityColonyBuilding (com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding)2 TileEntityColonyBuilding (com.minecolonies.api.tileentities.TileEntityColonyBuilding)2