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;
}
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();
}
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;
}
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;
}
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;
}
Aggregations