Search in sources :

Example 1 with BlockInfo

use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.

the class BlockPattern method getPreview.

public BlockInfo[][][] getPreview(int[] repetition) {
    Map<SimplePredicate, Integer> cacheGlobal = new HashMap<>();
    Map<BlockPos, BlockInfo> blocks = new HashMap<>();
    int minX = Integer.MAX_VALUE;
    int minY = Integer.MAX_VALUE;
    int minZ = Integer.MAX_VALUE;
    int maxX = Integer.MIN_VALUE;
    int maxY = Integer.MIN_VALUE;
    int maxZ = Integer.MIN_VALUE;
    for (int l = 0, x = 0; l < this.fingerLength; l++) {
        for (int r = 0; r < repetition[l]; r++) {
            // Checking single slice
            for (int y = 0; y < this.thumbLength; y++) {
                for (int z = 0; z < this.palmLength; z++) {
                    TraceabilityPredicate predicate = this.blockMatches[l][y][z];
                    boolean find = false;
                    BlockInfo[] infos = null;
                    // check global and previewCount
                    for (SimplePredicate limit : predicate.limited) {
                        if (limit.minCount == -1 && limit.previewCount == -1)
                            continue;
                        if (cacheGlobal.getOrDefault(limit, 0) < limit.previewCount) {
                            if (!cacheGlobal.containsKey(limit)) {
                                cacheGlobal.put(limit, 1);
                            } else if (cacheGlobal.get(limit) < limit.previewCount) {
                                cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
                            } else {
                                continue;
                            }
                        } else if (limit.minCount > 0) {
                            if (!cacheGlobal.containsKey(limit)) {
                                cacheGlobal.put(limit, 1);
                            } else if (cacheGlobal.get(limit) < limit.minCount) {
                                cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
                            } else {
                                continue;
                            }
                        } else {
                            continue;
                        }
                        infos = limit.candidates == null ? null : limit.candidates.get();
                        find = true;
                        break;
                    }
                    if (!find) {
                        // check common with previewCount
                        for (SimplePredicate common : predicate.common) {
                            if (common.previewCount > 0) {
                                if (!cacheGlobal.containsKey(common)) {
                                    cacheGlobal.put(common, 1);
                                } else if (cacheGlobal.get(common) < common.previewCount) {
                                    cacheGlobal.put(common, cacheGlobal.get(common) + 1);
                                } else {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                            infos = common.candidates == null ? null : common.candidates.get();
                            find = true;
                            break;
                        }
                    }
                    if (!find) {
                        // check without previewCount
                        for (SimplePredicate common : predicate.common) {
                            if (common.previewCount == -1) {
                                infos = common.candidates == null ? null : common.candidates.get();
                                find = true;
                                break;
                            }
                        }
                    }
                    if (!find) {
                        // check max
                        for (SimplePredicate limit : predicate.limited) {
                            if (limit.previewCount != -1) {
                                continue;
                            } else if (limit.maxCount != -1) {
                                if (cacheGlobal.getOrDefault(limit, 0) < limit.maxCount) {
                                    if (!cacheGlobal.containsKey(limit)) {
                                        cacheGlobal.put(limit, 1);
                                    } else {
                                        cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
                                    }
                                } else {
                                    continue;
                                }
                            }
                            infos = limit.candidates == null ? null : limit.candidates.get();
                            break;
                        }
                    }
                    BlockInfo info = infos == null || infos.length == 0 ? BlockInfo.EMPTY : infos[0];
                    BlockPos pos = setActualRelativeOffset(z, y, x, Direction.NORTH);
                    blocks.put(pos, info);
                    minX = Math.min(pos.getX(), minX);
                    minY = Math.min(pos.getY(), minY);
                    minZ = Math.min(pos.getZ(), minZ);
                    maxX = Math.max(pos.getX(), maxX);
                    maxY = Math.max(pos.getY(), maxY);
                    maxZ = Math.max(pos.getZ(), maxZ);
                }
            }
            x++;
        }
    }
    BlockInfo[][][] result = (BlockInfo[][][]) Array.newInstance(BlockInfo.class, maxX - minX + 1, maxY - minY + 1, maxZ - minZ + 1);
    int finalMinX = minX;
    int finalMinY = minY;
    int finalMinZ = minZ;
    blocks.forEach((pos, info) -> {
        TileEntity te = blocks.get(pos).getTileEntity();
        resetFacing(pos, info.getBlockState(), null, (p, f) -> {
            BlockInfo blockInfo = blocks.get(p.relative(f));
            if (blockInfo == null || blockInfo.getBlockState().getBlock() == Blocks.AIR) {
                if (te instanceof ComponentTileEntity) {
                    return ((ComponentTileEntity<?>) te).isValidFrontFacing(f);
                }
                return true;
            }
            return false;
        }, info::setBlockState);
        result[pos.getX() - finalMinX][pos.getY() - finalMinY][pos.getZ() - finalMinZ] = info;
    });
    return result;
}
Also used : HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) SimplePredicate(com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate) ComponentTileEntity(com.lowdragmc.multiblocked.api.tile.ComponentTileEntity) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) ComponentTileEntity(com.lowdragmc.multiblocked.api.tile.ComponentTileEntity) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with BlockInfo

use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.

the class PredicateAnyCapability method buildPredicate.

@Override
public SimplePredicate buildPredicate() {
    MultiblockCapability<?> capability = MbdCapabilities.get(this.capability);
    if (capability == null) {
        predicate = state -> false;
        candidates = () -> new BlockInfo[] { new BlockInfo(Blocks.BARRIER) };
        return this;
    }
    predicate = state -> state.getBlockState().getBlock() == capability.getAnyBlock() || checkCapability(io, capability, state);
    candidates = () -> new BlockInfo[] { BlockInfo.fromBlockState(capability.getAnyBlock().defaultBlockState()) };
    toolTips = new ArrayList<>();
    toolTips.add(String.format("Any Capability: %s IO: %s", capability.name, io == null ? "NULL" : io.name()));
    return this;
}
Also used : BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo)

Example 3 with BlockInfo

use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.

the class FEMultiblockCapability method getCandidates.

@Override
public BlockInfo[] getCandidates() {
    List<BlockInfo> list = new ArrayList<>();
    TrackedDummyWorld dummyWorld = new TrackedDummyWorld();
    for (Block block : ForgeRegistries.BLOCKS.getValues()) {
        if (block.getRegistryName() != null) {
            String path = block.getRegistryName().getPath();
            if (path.contains("energy") || path.contains("rf")) {
                try {
                    if (block.hasTileEntity(block.defaultBlockState())) {
                        TileEntity tileEntity = block.createTileEntity(block.defaultBlockState(), dummyWorld);
                        if (tileEntity != null && isBlockHasCapability(IO.BOTH, tileEntity)) {
                            list.add(new BlockInfo(block.defaultBlockState(), tileEntity));
                        }
                    }
                } catch (Throwable ignored) {
                }
            }
        }
    }
    list.add(BlockInfo.fromBlock(MbdComponents.COMPONENT_BLOCKS_REGISTRY.get(new ResourceLocation(Multiblocked.MODID, "energy_input"))));
    list.add(BlockInfo.fromBlock(MbdComponents.COMPONENT_BLOCKS_REGISTRY.get(new ResourceLocation(Multiblocked.MODID, "energy_output"))));
    return list.toArray(new BlockInfo[0]);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) TrackedDummyWorld(com.lowdragmc.lowdraglib.utils.TrackedDummyWorld) ResourceLocation(net.minecraft.util.ResourceLocation) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block)

Example 4 with BlockInfo

use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.

the class FluidMultiblockCapability method getCandidates.

@Override
public BlockInfo[] getCandidates() {
    List<BlockInfo> list = new ArrayList<>();
    TrackedDummyWorld dummyWorld = new TrackedDummyWorld();
    for (Block block : ForgeRegistries.BLOCKS.getValues()) {
        if (block.getRegistryName() != null) {
            String path = block.getRegistryName().getPath();
            if (path.contains("tank") || path.contains("fluid") || path.contains("liquid")) {
                try {
                    if (block.hasTileEntity(block.defaultBlockState())) {
                        TileEntity tileEntity = block.createTileEntity(block.defaultBlockState(), dummyWorld);
                        if (tileEntity != null && isBlockHasCapability(IO.BOTH, tileEntity)) {
                            list.add(new BlockInfo(block.defaultBlockState(), tileEntity));
                        }
                    }
                } catch (Throwable ignored) {
                }
            }
        }
    }
    list.add(BlockInfo.fromBlock(MbdComponents.COMPONENT_BLOCKS_REGISTRY.get(new ResourceLocation(Multiblocked.MODID, "fluid_input"))));
    list.add(BlockInfo.fromBlock(MbdComponents.COMPONENT_BLOCKS_REGISTRY.get(new ResourceLocation(Multiblocked.MODID, "fluid_output"))));
    return list.toArray(new BlockInfo[0]);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) TrackedDummyWorld(com.lowdragmc.lowdraglib.utils.TrackedDummyWorld) ResourceLocation(net.minecraft.util.ResourceLocation) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block)

Example 5 with BlockInfo

use of com.lowdragmc.lowdraglib.utils.BlockInfo in project Multiblocked by Low-Drag-MC.

the class MultiblockPreviewRenderer method renderControllerInList.

public static void renderControllerInList(ControllerTileEntity controllerBase, MultiblockShapeInfo shapeInfo, int layer) {
    Direction frontFacing;
    previewFacing = Direction.NORTH;
    controllerPos = BlockPos.ZERO;
    mte = null;
    BlockInfo[][][] blocks = shapeInfo.getBlocks();
    blockMap = new HashMap<>();
    int maxY = 0;
    for (int x = 0; x < blocks.length; x++) {
        BlockInfo[][] aisle = blocks[x];
        maxY = Math.max(maxY, aisle.length);
        for (int y = 0; y < aisle.length; y++) {
            BlockInfo[] column = aisle[y];
            for (int z = 0; z < column.length; z++) {
                blockMap.put(new BlockPos(x, y, z), column[z]);
                ControllerTileEntity metaTE = column[z].getTileEntity() instanceof ControllerTileEntity ? (ControllerTileEntity) column[z].getTileEntity() : null;
                if (metaTE != null) {
                    if (metaTE.getDefinition().location.equals(controllerBase.getDefinition().location)) {
                        controllerPos = new BlockPos(x, y, z);
                        mte = metaTE;
                    }
                }
            }
        }
    }
    world = new TrackedDummyWorld();
    world.addBlocks(blockMap);
    int finalMaxY = layer % (maxY + 1);
    world.setRenderFilter(pos -> pos.getY() + 1 == finalMaxY || finalMaxY == 0);
    facing = controllerBase.getFrontFacing();
    spin = Direction.NORTH;
    frontFacing = facing.getStepY() == 0 ? facing : facing.getStepY() < 0 ? spin : spin.getOpposite();
    rotatePreviewBy = Rotation.values()[(4 + frontFacing.get2DDataValue() - previewFacing.get2DDataValue()) % 4];
    if (mte != null) {
        mbpPos = controllerBase.getBlockPos();
    }
}
Also used : BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) TrackedDummyWorld(com.lowdragmc.lowdraglib.utils.TrackedDummyWorld) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)

Aggregations

BlockInfo (com.lowdragmc.lowdraglib.utils.BlockInfo)10 TileEntity (net.minecraft.tileentity.TileEntity)6 BlockPos (net.minecraft.util.math.BlockPos)5 TrackedDummyWorld (com.lowdragmc.lowdraglib.utils.TrackedDummyWorld)4 ControllerTileEntity (com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)4 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 BlockState (net.minecraft.block.BlockState)3 ItemStackKey (com.lowdragmc.lowdraglib.utils.ItemStackKey)2 SimplePredicate (com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate)2 ComponentTileEntity (com.lowdragmc.multiblocked.api.tile.ComponentTileEntity)2 CycleBlockStateRenderer (com.lowdragmc.multiblocked.client.renderer.impl.CycleBlockStateRenderer)2 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)2 Map (java.util.Map)2 Block (net.minecraft.block.Block)2 ItemStack (net.minecraft.item.ItemStack)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 World (net.minecraft.world.World)2 BlockComponent (com.lowdragmc.multiblocked.api.block.BlockComponent)1 IO (com.lowdragmc.multiblocked.api.capability.IO)1