Search in sources :

Example 1 with CycleBlockStateRenderer

use of com.cleanroommc.multiblocked.client.renderer.impl.CycleBlockStateRenderer in project Multiblocked by CleanroomMC.

the class TemplateBuilderWidget method getComponentDefinition.

public static ComponentDefinition getComponentDefinition(ComponentDefinition definition, Set<BlockInfo> candidates) {
    if (candidates.size() == 1) {
        definition = new PartDefinition(new ResourceLocation(Multiblocked.MODID, "i_renderer"));
        definition.baseRenderer = new BlockStateRenderer(candidates.toArray(new BlockInfo[0])[0]);
    } else if (!candidates.isEmpty()) {
        definition = new PartDefinition(new ResourceLocation(Multiblocked.MODID, "i_renderer"));
        definition.baseRenderer = new CycleBlockStateRenderer(candidates.toArray(new BlockInfo[0]));
    }
    return definition;
}
Also used : CycleBlockStateRenderer(com.cleanroommc.multiblocked.client.renderer.impl.CycleBlockStateRenderer) BlockStateRenderer(com.cleanroommc.multiblocked.client.renderer.impl.BlockStateRenderer) CycleBlockStateRenderer(com.cleanroommc.multiblocked.client.renderer.impl.CycleBlockStateRenderer) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) ResourceLocation(net.minecraft.util.ResourceLocation) PartDefinition(com.cleanroommc.multiblocked.api.definition.PartDefinition)

Example 2 with CycleBlockStateRenderer

use of com.cleanroommc.multiblocked.client.renderer.impl.CycleBlockStateRenderer in project Multiblocked by CleanroomMC.

the class BlockPattern method autoBuild.

public void autoBuild(EntityPlayer player, MultiblockState worldState) {
    World world = player.world;
    int minZ = -centerOffset[4];
    worldState.clean();
    ControllerTileEntity controller = worldState.getController();
    BlockPos centerPos = controller.getPos();
    EnumFacing facing = controller.getFrontFacing();
    Map<SimplePredicate, Integer> cacheGlobal = worldState.globalCount;
    Map<BlockPos, Object> blocks = new HashMap<>();
    blocks.put(centerPos, controller);
    for (int c = 0, z = minZ++, r; c < this.fingerLength; c++) {
        for (r = 0; r < aisleRepetitions[c][0]; r++) {
            for (int b = 0, y = -centerOffset[1]; b < this.thumbLength; b++, y++) {
                for (int a = 0, x = -centerOffset[0]; a < this.palmLength; a++, x++) {
                    TraceabilityPredicate predicate = this.blockMatches[c][b][a];
                    BlockPos pos = setActualRelativeOffset(x, y, z, facing).add(centerPos.getX(), centerPos.getY(), centerPos.getZ());
                    worldState.update(pos, predicate);
                    if (!world.isAirBlock(pos)) {
                        blocks.put(pos, world.getBlockState(pos));
                        for (SimplePredicate limit : predicate.limited) {
                            limit.testLimited(worldState);
                        }
                    } else {
                        boolean find = false;
                        BlockInfo[] infos = new BlockInfo[0];
                        for (SimplePredicate limit : predicate.limited) {
                            if (limit.minCount > 0) {
                                if (!cacheGlobal.containsKey(limit)) {
                                    cacheGlobal.put(limit, 1);
                                } else if (cacheGlobal.get(limit) < limit.minCount && (limit.maxCount == -1 || cacheGlobal.get(limit) < limit.maxCount)) {
                                    cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
                                } else {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                            infos = limit.candidates == null ? null : limit.candidates.get();
                            find = true;
                            break;
                        }
                        if (!find) {
                            // no limited
                            for (SimplePredicate limit : predicate.limited) {
                                if (limit.maxCount != -1 && cacheGlobal.getOrDefault(limit, Integer.MAX_VALUE) == limit.maxCount)
                                    continue;
                                if (cacheGlobal.containsKey(limit)) {
                                    cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
                                } else {
                                    cacheGlobal.put(limit, 1);
                                }
                                infos = ArrayUtils.addAll(infos, limit.candidates == null ? null : limit.candidates.get());
                            }
                            for (SimplePredicate common : predicate.common) {
                                infos = ArrayUtils.addAll(infos, common.candidates == null ? null : common.candidates.get());
                            }
                        }
                        List<ItemStack> candidates = new ArrayList<>();
                        if (infos != null) {
                            for (BlockInfo info : infos) {
                                if (info.getBlockState().getBlock() != Blocks.AIR) {
                                    IBlockState blockState = info.getBlockState();
                                    if (blockState.getBlock() instanceof BlockComponent && ((BlockComponent) blockState.getBlock()).definition != null) {
                                        if (((BlockComponent) blockState.getBlock()).definition.baseRenderer instanceof CycleBlockStateRenderer) {
                                            CycleBlockStateRenderer renderer = (CycleBlockStateRenderer) ((BlockComponent) blockState.getBlock()).definition.baseRenderer;
                                            for (BlockInfo blockInfo : renderer.blockInfos) {
                                                candidates.add(blockInfo.getItemStackForm());
                                            }
                                        } else {
                                            candidates.add(info.getItemStackForm());
                                        }
                                    } else {
                                        candidates.add(info.getItemStackForm());
                                    }
                                }
                            }
                        }
                        // check inventory
                        ItemStack found = null;
                        if (!player.isCreative()) {
                            for (ItemStack itemStack : player.inventory.mainInventory) {
                                if (candidates.stream().anyMatch(candidate -> candidate.isItemEqual(itemStack)) && !itemStack.isEmpty() && itemStack.getItem() instanceof ItemBlock) {
                                    found = itemStack.copy();
                                    itemStack.setCount(itemStack.getCount() - 1);
                                    break;
                                }
                            }
                        } else {
                            for (ItemStack candidate : candidates) {
                                found = candidate.copy();
                                if (!found.isEmpty() && found.getItem() instanceof ItemBlock) {
                                    break;
                                }
                                found = null;
                            }
                        }
                        if (found == null)
                            continue;
                        ItemBlock itemBlock = (ItemBlock) found.getItem();
                        int meta = itemBlock.getMetadata(found.getMetadata());
                        float hitX = pos.getX() + 0.5f;
                        float hitY = pos.getY();
                        float hitZ = pos.getZ() + 0.5f;
                        IBlockState newState = itemBlock.getBlock().getStateForPlacement(world, pos, facing, hitX, hitY, hitZ, meta, player, EnumHand.MAIN_HAND);
                        itemBlock.placeBlockAt(found, player, world, pos, player.getHorizontalFacing(), hitX, hitY, hitZ, newState);
                        TileEntity tileEntity = world.getTileEntity(pos);
                        if (tileEntity instanceof ComponentTileEntity) {
                            blocks.put(pos, tileEntity);
                        }
                    }
                }
            }
            z++;
        }
    }
    // follow controller first
    EnumFacing[] facings = ArrayUtils.addAll(new EnumFacing[] { facing }, FACINGS);
    blocks.forEach((pos, block) -> {
        // adjust facing
        if (block instanceof ComponentTileEntity) {
            ComponentTileEntity<?> componentTileEntity = (ComponentTileEntity<?>) block;
            boolean find = false;
            for (EnumFacing enumFacing : facings) {
                if (componentTileEntity.isValidFrontFacing(enumFacing)) {
                    if (!blocks.containsKey(pos.offset(enumFacing))) {
                        componentTileEntity.setFrontFacing(enumFacing);
                        find = true;
                        break;
                    }
                }
            }
            if (!find) {
                for (EnumFacing enumFacing : FACINGS) {
                    if (world.isAirBlock(pos.offset(enumFacing)) && componentTileEntity.isValidFrontFacing(enumFacing)) {
                        componentTileEntity.setFrontFacing(enumFacing);
                        break;
                    }
                }
            }
        }
    });
}
Also used : HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) World(net.minecraft.world.World) ControllerTileEntity(com.cleanroommc.multiblocked.api.tile.ControllerTileEntity) PartTileEntity(com.cleanroommc.multiblocked.api.tile.part.PartTileEntity) ComponentTileEntity(com.cleanroommc.multiblocked.api.tile.ComponentTileEntity) ControllerTileEntity(com.cleanroommc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) BlockPos(net.minecraft.util.math.BlockPos) CycleBlockStateRenderer(com.cleanroommc.multiblocked.client.renderer.impl.CycleBlockStateRenderer) IBlockState(net.minecraft.block.state.IBlockState) ItemBlock(net.minecraft.item.ItemBlock) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) BlockComponent(com.cleanroommc.multiblocked.api.block.BlockComponent) ComponentTileEntity(com.cleanroommc.multiblocked.api.tile.ComponentTileEntity) ItemStack(net.minecraft.item.ItemStack)

Example 3 with CycleBlockStateRenderer

use of com.cleanroommc.multiblocked.client.renderer.impl.CycleBlockStateRenderer in project Multiblocked by CleanroomMC.

the class SlotWidget method getRealStack.

public ItemStack getRealStack(ItemStack itemStack) {
    if (itemStack.getItem() instanceof ItemBlock) {
        Block block = ((ItemBlock) itemStack.getItem()).getBlock();
        if (block instanceof BlockComponent) {
            if (((BlockComponent) block).definition.baseRenderer instanceof CycleBlockStateRenderer) {
                CycleBlockStateRenderer renderer = ((CycleBlockStateRenderer) ((BlockComponent) block).definition.baseRenderer);
                itemStack = renderer.getBlockInfo().getItemStackForm();
            }
        }
    }
    return itemStack;
}
Also used : BlockComponent(com.cleanroommc.multiblocked.api.block.BlockComponent) CycleBlockStateRenderer(com.cleanroommc.multiblocked.client.renderer.impl.CycleBlockStateRenderer) Block(net.minecraft.block.Block) ItemBlock(net.minecraft.item.ItemBlock) ItemBlock(net.minecraft.item.ItemBlock)

Aggregations

CycleBlockStateRenderer (com.cleanroommc.multiblocked.client.renderer.impl.CycleBlockStateRenderer)3 BlockComponent (com.cleanroommc.multiblocked.api.block.BlockComponent)2 BlockInfo (com.cleanroommc.multiblocked.api.pattern.util.BlockInfo)2 ItemBlock (net.minecraft.item.ItemBlock)2 PartDefinition (com.cleanroommc.multiblocked.api.definition.PartDefinition)1 SimplePredicate (com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate)1 ComponentTileEntity (com.cleanroommc.multiblocked.api.tile.ComponentTileEntity)1 ControllerTileEntity (com.cleanroommc.multiblocked.api.tile.ControllerTileEntity)1 PartTileEntity (com.cleanroommc.multiblocked.api.tile.part.PartTileEntity)1 BlockStateRenderer (com.cleanroommc.multiblocked.client.renderer.impl.BlockStateRenderer)1 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Block (net.minecraft.block.Block)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemStack (net.minecraft.item.ItemStack)1 TileEntity (net.minecraft.tileentity.TileEntity)1 EnumFacing (net.minecraft.util.EnumFacing)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 BlockPos (net.minecraft.util.math.BlockPos)1