Search in sources :

Example 16 with TileEntityRack

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));
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) HighlightManager(com.minecolonies.coremod.event.HighlightManager) BlockPos(net.minecraft.util.math.BlockPos) ItemStorage(com.minecolonies.api.crafting.ItemStorage)

Example 17 with TileEntityRack

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();
}
Also used : TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) Window(com.ldtteam.blockout.views.Window) java.util(java.util) MESSAGE_LOCATING_ITEMS(com.minecolonies.api.util.constant.TranslationConstants.MESSAGE_LOCATING_ITEMS) 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) ItemStack(net.minecraft.item.ItemStack) Minecraft(net.minecraft.client.Minecraft) Constants(com.minecolonies.api.util.constant.Constants) MessageUtils(com.minecolonies.api.util.MessageUtils) 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 18 with TileEntityRack

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();
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) BlockPos(net.minecraft.util.math.BlockPos) IWorkOrderView(com.minecolonies.api.colony.workorders.IWorkOrderView) IColonyView(com.minecolonies.api.colony.IColonyView) BuildingResourcesModuleView(com.minecolonies.coremod.colony.buildings.moduleviews.BuildingResourcesModuleView) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) BuildingResourcesModuleView(com.minecolonies.coremod.colony.buildings.moduleviews.BuildingResourcesModuleView)

Example 19 with TileEntityRack

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());
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityRack(com.minecolonies.api.tileentities.TileEntityRack) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) AbstractTileEntityColonyBuilding(com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding) TileEntityColonyBuilding(com.minecolonies.api.tileentities.TileEntityColonyBuilding) BlockMinecoloniesRack(com.minecolonies.coremod.blocks.BlockMinecoloniesRack) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut)

Example 20 with TileEntityRack

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;
}
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) ItemStorage(com.minecolonies.api.crafting.ItemStorage)

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