use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by Minecolonies.
the class WindowHutAllInventory method locate.
private void locate(final Button button) {
final int row = stackList.getListElementIndexByPane(button);
final ItemStorage storage = allItems.get(row);
final Set<BlockPos> containerList = new HashSet<>(building.getContainerList());
containerList.add(building.getID());
HighlightManager.clearCategory("inventoryHighlight");
MessageUtils.format(MESSAGE_LOCATING_ITEMS).sendTo(Minecraft.getInstance().player);
close();
for (BlockPos blockPos : containerList) {
final TileEntity rack = Minecraft.getInstance().level.getBlockEntity(blockPos);
if (rack instanceof TileEntityRack) {
int count = ((TileEntityRack) rack).getCount(storage.getItemStack(), storage.ignoreDamageValue(), false);
if (count > 0) {
// Varies the color between yellow(low count) to green(64+)
final int color = 0x00FF00 + 0xFF0000 * Math.max(0, 1 - count / 64);
HighlightManager.addRenderBox("inventoryHighlight", new HighlightManager.TimedBoxRenderData().setPos(blockPos).setRemovalTimePoint(Minecraft.getInstance().level.getGameTime() + 60 * 20).addText("" + count).setColor(color));
}
}
}
}
use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by Minecolonies.
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 WindowResourceList method pullResourcesFromWarehouse.
/**
* Checks the resources in the warehouse to check for any resources required by the builder,
* only does anything when the warehouse position is provided.
*/
private void pullResourcesFromWarehouse() {
String currentWorkOrderHash = createWorkOrderHash();
if (!currentWorkOrderHash.equals(workOrderHash)) {
workOrderHash = currentWorkOrderHash;
warehouseSnapshot = new HashMap<>();
}
if (warehousePos != null) {
warehouseSnapshot = new HashMap<>();
final IBuildingView newView = builder.getColony().getBuilding(builder.getID());
if (newView instanceof BuildingBuilder.View) {
final BuildingResourcesModuleView moduleView = newView.getModuleView(BuildingResourcesModuleView.class);
List<BlockPos> containers = builder.getColony().getBuilding(warehousePos).getContainerList();
for (BlockPos container : containers) {
final TileEntity rack = Minecraft.getInstance().level.getBlockEntity(container);
if (rack instanceof TileEntityRack) {
((TileEntityRack) rack).getAllContent().forEach((item, amount) -> {
final int hashCode = item.getItemStack().hasTag() ? item.getItemStack().getTag().hashCode() : 0;
final String key = item.getItemStack().getDescriptionId() + "-" + hashCode;
if (!moduleView.getResources().containsKey(key)) {
return;
}
int oldAmount = warehouseSnapshot.getOrDefault(key, 0);
warehouseSnapshot.put(key, oldAmount + amount);
});
}
}
}
}
saveWarehouseSnapshotData();
}
use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by Minecolonies.
the class AbstractBuildingContainer method registerBlockPosition.
@Override
@SuppressWarnings("squid:S1172")
public void registerBlockPosition(@NotNull final Block block, @NotNull final BlockPos pos, @NotNull final World world) {
if (block instanceof AbstractBlockHut) {
final TileEntity entity = world.getBlockEntity(pos);
if (entity instanceof TileEntityColonyBuilding) {
((TileEntityColonyBuilding) entity).setStyle(this.getStyle());
((TileEntityColonyBuilding) entity).setMirror(isMirrored());
final IBuilding building = colony.getBuildingManager().getBuilding(pos);
if (building != null) {
building.setStyle(this.getStyle());
building.setParent(getID());
addChild(pos);
}
}
} else if (block instanceof BlockMinecoloniesRack) {
addContainerPosition(pos);
final TileEntity entity = world.getBlockEntity(pos);
if (entity instanceof TileEntityRack) {
((TileEntityRack) entity).setBuildingPos(this.getID());
}
}
}
use of com.minecolonies.api.tileentities.TileEntityRack in project minecolonies by Minecolonies.
the class InventoryUtils method getCountFromBuildingWithLimit.
/**
* Count the number of items a building has.
* Only count up to "limit" of a particular item.
*
* @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 getCountFromBuildingWithLimit(@NotNull final IBuilding provider, @NotNull final Predicate<ItemStack> predicate, final Function<ItemStack, Integer> limit) {
final World world = provider.getColony().getWorld();
final Map<ItemStorage, Integer> allMatching = new HashMap<>();
for (final BlockPos pos : provider.getContainers()) {
if (WorldUtil.isBlockLoaded(world, pos)) {
final TileEntity entity = world.getBlockEntity(pos);
if (entity instanceof TileEntityRack) {
for (final Map.Entry<ItemStorage, Integer> entry : ((TileEntityRack) entity).getAllContent().entrySet()) {
if (predicate.test(entry.getKey().getItemStack())) {
allMatching.put(entry.getKey(), allMatching.getOrDefault(entry.getKey(), 0) + entry.getValue());
}
}
}
}
}
int totalCount = 0;
for (final Map.Entry<ItemStorage, Integer> entry : allMatching.entrySet()) {
totalCount += Math.min(limit.apply(entry.getKey().getItemStack()), entry.getValue());
}
return totalCount;
}
Aggregations