Search in sources :

Example 6 with ControllerTileEntity

use of com.lowdragmc.multiblocked.api.tile.ControllerTileEntity 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)

Example 7 with ControllerTileEntity

use of com.lowdragmc.multiblocked.api.tile.ControllerTileEntity in project Multiblocked by Low-Drag-MC.

the class PartTileEntity method addedToController.

public void addedToController(@Nonnull IControllerComponent controller) {
    if (controllerPos.add(controller.self().getBlockPos())) {
        writeCustomData(-1, this::writeControllersToBuffer);
        if (Multiblocked.isKubeJSLoaded() && controller instanceof ControllerTileEntity) {
            new PartAddedEvent((ControllerTileEntity) controller).post(ScriptType.SERVER, PartAddedEvent.ID, getSubID());
        }
        setStatus("idle");
    }
}
Also used : PartAddedEvent(com.lowdragmc.multiblocked.api.kubejs.events.PartAddedEvent) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)

Example 8 with ControllerTileEntity

use of com.lowdragmc.multiblocked.api.tile.ControllerTileEntity in project Multiblocked by Low-Drag-MC.

the class TemplateBuilderWidget method onBuildTemplate.

protected void onBuildTemplate(ClickData clickData) {
    if (isRemote() && ItemBlueprint.isItemBlueprint(selected)) {
        JsonBlockPattern pattern = null;
        if (ItemBlueprint.isRaw(selected)) {
            BlockPos[] poses = ItemBlueprint.getPos(selected);
            World world = table.getLevel();
            if (poses != null && world.hasChunksAt(poses[0], poses[1])) {
                ControllerTileEntity controller = null;
                for (int x = poses[0].getX(); x <= poses[1].getX(); x++) {
                    for (int y = poses[0].getY(); y <= poses[1].getY(); y++) {
                        for (int z = poses[0].getZ(); z <= poses[1].getZ(); z++) {
                            TileEntity te = world.getBlockEntity(new BlockPos(x, y, z));
                            if (te instanceof ControllerTileEntity) {
                                controller = (ControllerTileEntity) te;
                            }
                        }
                    }
                }
                if (controller != null) {
                    pattern = new JsonBlockPattern(table.getLevel(), controller.getLocation(), controller.getBlockPos(), controller.getFrontFacing(), poses[0].getX(), poses[0].getY(), poses[0].getZ(), poses[1].getX(), poses[1].getY(), poses[1].getZ());
                } else {
                // TODO tips dialog
                }
            } else {
            // TODO tips dialog
            }
        } else if (selected.getTagElement("pattern") != null) {
            String json = selected.getTagElement("pattern").getString("json");
            pattern = Multiblocked.GSON.fromJson(json, JsonBlockPattern.class);
        }
        if (pattern != null) {
            new JsonBlockPatternWidget(this, pattern, patternResult -> {
                if (patternResult != null) {
                    if (ItemBlueprint.setPattern(selected) && patternResult.predicates.get("controller") instanceof PredicateComponent) {
                        patternResult.cleanUp();
                        String json = patternResult.toJson();
                        String controller = ((PredicateComponent) patternResult.predicates.get("controller")).location.toString();
                        selected.getOrCreateTagElement("pattern").putString("json", json);
                        selected.getOrCreateTagElement("pattern").putString("controller", controller);
                        writeClientAction(-1, buffer -> {
                            buffer.writeVarInt(selectedSlot);
                            buffer.writeUtf(json);
                            buffer.writeUtf(controller);
                        });
                    }
                }
            });
        }
    }
}
Also used : BlueprintTableTileEntity(com.lowdragmc.multiblocked.api.tile.BlueprintTableTileEntity) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) DummyComponentTileEntity(com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) JsonBlockPatternWidget(com.lowdragmc.multiblocked.api.gui.dialogs.JsonBlockPatternWidget) BlockPos(net.minecraft.util.math.BlockPos) PredicateComponent(com.lowdragmc.multiblocked.api.pattern.predicates.PredicateComponent) TrackedDummyWorld(com.lowdragmc.lowdraglib.utils.TrackedDummyWorld) World(net.minecraft.world.World) ItemBlueprint(com.lowdragmc.multiblocked.api.item.ItemBlueprint) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)

Example 9 with ControllerTileEntity

use of com.lowdragmc.multiblocked.api.tile.ControllerTileEntity in project Multiblocked by Low-Drag-MC.

the class PatternWidget method initializePattern.

private MBPattern initializePattern(MultiblockShapeInfo shapeInfo, HashSet<ItemStackKey> blockDrops) {
    Map<BlockPos, BlockInfo> blockMap = new HashMap<>();
    ControllerTileEntity controllerBase = null;
    BlockPos multiPos = locateNextRegion(500);
    BlockInfo[][][] blocks = shapeInfo.getBlocks();
    for (int x = 0; x < blocks.length; x++) {
        BlockInfo[][] aisle = blocks[x];
        for (int y = 0; y < aisle.length; y++) {
            BlockInfo[] column = aisle[y];
            for (int z = 0; z < column.length; z++) {
                TileEntity tileEntity = column[z].getTileEntity();
                BlockState blockState = column[z].getBlockState();
                if (tileEntity == null && blockState.getBlock().hasTileEntity(blockState)) {
                    tileEntity = blockState.getBlock().createTileEntity(blockState, world);
                }
                if (tileEntity instanceof ControllerTileEntity) {
                    controllerBase = (ControllerTileEntity) tileEntity;
                }
                blockMap.put(multiPos.offset(x, y, z), new BlockInfo(blockState, tileEntity));
            }
        }
    }
    world.addBlocks(blockMap);
    Map<ItemStackKey, PartInfo> parts = gatherBlockDrops(blockMap);
    blockDrops.addAll(parts.keySet());
    Map<BlockPos, TraceabilityPredicate> predicateMap = new HashMap<>();
    if (controllerBase != null) {
        loadControllerFormed(predicateMap.keySet(), controllerBase);
        predicateMap = controllerBase.state.getMatchContext().get("predicates");
    }
    return controllerBase == null ? null : new MBPattern(blockMap, parts.values().stream().sorted((one, two) -> {
        if (one.isController)
            return -1;
        if (two.isController)
            return +1;
        if (one.isTile && !two.isTile)
            return -1;
        if (two.isTile && !one.isTile)
            return +1;
        if (one.blockId != two.blockId)
            return two.blockId - one.blockId;
        return two.amount - one.amount;
    }).map(PartInfo::getItemStack).toArray(ItemStack[]::new), predicateMap, controllerBase);
}
Also used : HashMap(java.util.HashMap) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) ItemStackKey(com.lowdragmc.lowdraglib.utils.ItemStackKey) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockState(net.minecraft.block.BlockState) BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) BlockPos(net.minecraft.util.math.BlockPos) TraceabilityPredicate(com.lowdragmc.multiblocked.api.pattern.TraceabilityPredicate)

Example 10 with ControllerTileEntity

use of com.lowdragmc.multiblocked.api.tile.ControllerTileEntity in project Multiblocked by Low-Drag-MC.

the class BlockPattern method autoBuild.

public void autoBuild(PlayerEntity player, MultiblockState worldState) {
    World world = player.level;
    int minZ = -centerOffset[4];
    worldState.clean();
    ControllerTileEntity controller = worldState.getController();
    BlockPos centerPos = controller.getBlockPos();
    Direction 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).offset(centerPos.getX(), centerPos.getY(), centerPos.getZ());
                    worldState.update(pos, predicate);
                    if (!world.isEmptyBlock(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) {
                                    BlockState 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.items) {
                                if (candidates.stream().anyMatch(candidate -> candidate.equals(itemStack, false)) && !itemStack.isEmpty() && itemStack.getItem() instanceof BlockItem) {
                                    found = itemStack.copy();
                                    itemStack.setCount(itemStack.getCount() - 1);
                                    break;
                                }
                            }
                        } else {
                            for (ItemStack candidate : candidates) {
                                found = candidate.copy();
                                if (!found.isEmpty() && found.getItem() instanceof BlockItem) {
                                    break;
                                }
                                found = null;
                            }
                        }
                        if (found == null)
                            continue;
                        BlockItem itemBlock = (BlockItem) found.getItem();
                        BlockItemUseContext context = new BlockItemUseContext(world, player, Hand.MAIN_HAND, found, BlockRayTraceResult.miss(player.getEyePosition(0), Direction.UP, pos));
                        itemBlock.place(context);
                        TileEntity tileEntity = world.getBlockEntity(pos);
                        if (tileEntity instanceof ComponentTileEntity) {
                            blocks.put(pos, tileEntity);
                        } else {
                            blocks.put(pos, world.getBlockState(pos));
                        }
                    }
                }
            }
            z++;
        }
    }
    Direction frontFacing = controller.getFrontFacing();
    blocks.forEach((pos, block) -> {
        // adjust facing
        if (block instanceof BlockState) {
            resetFacing(pos, (BlockState) block, frontFacing, (p, f) -> {
                Object object = blocks.get(p.relative(f));
                return object == null || (object instanceof BlockState && ((BlockState) object).getBlock() == Blocks.AIR);
            }, state -> world.setBlock(pos, state, 3));
        } else if (block instanceof ComponentTileEntity) {
            resetFacing(pos, ((ComponentTileEntity<?>) block).getBlockState(), frontFacing, (p, f) -> {
                Object object = blocks.get(p.relative(f));
                if (object == null || (object instanceof BlockState && ((BlockState) object).getBlock() == Blocks.AIR)) {
                    return ((ComponentTileEntity<?>) block).isValidFrontFacing(f);
                }
                return false;
            }, state -> world.setBlock(pos, state, 3));
        }
    });
}
Also used : PatternError(com.lowdragmc.multiblocked.api.pattern.error.PatternError) ComponentTileEntity(com.lowdragmc.multiblocked.api.tile.ComponentTileEntity) Array(java.lang.reflect.Array) BlockComponent(com.lowdragmc.multiblocked.api.block.BlockComponent) BiFunction(java.util.function.BiFunction) ArrayUtils(org.apache.commons.lang3.ArrayUtils) HashMap(java.util.HashMap) SinglePredicateError(com.lowdragmc.multiblocked.api.pattern.error.SinglePredicateError) Direction(net.minecraft.util.Direction) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) Supplier(java.util.function.Supplier) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) ArrayList(java.util.ArrayList) RelativeDirection(com.lowdragmc.multiblocked.api.pattern.util.RelativeDirection) HashSet(java.util.HashSet) ItemStack(net.minecraft.item.ItemStack) BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) IO(com.lowdragmc.multiblocked.api.capability.IO) Hand(net.minecraft.util.Hand) BlockState(net.minecraft.block.BlockState) PatternStringError(com.lowdragmc.multiblocked.api.pattern.error.PatternStringError) PatternMatchContext(com.lowdragmc.multiblocked.api.pattern.util.PatternMatchContext) PlayerEntity(net.minecraft.entity.player.PlayerEntity) CycleBlockStateRenderer(com.lowdragmc.multiblocked.client.renderer.impl.CycleBlockStateRenderer) EnumMap(java.util.EnumMap) World(net.minecraft.world.World) IPartComponent(com.lowdragmc.multiblocked.api.tile.part.IPartComponent) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) BlockItemUseContext(net.minecraft.item.BlockItemUseContext) Blocks(net.minecraft.block.Blocks) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) Consumer(java.util.function.Consumer) List(java.util.List) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) BlockItem(net.minecraft.item.BlockItem) SimplePredicate(com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate) TileEntity(net.minecraft.tileentity.TileEntity) DirectionProperty(net.minecraft.state.DirectionProperty) BlockStateProperties(net.minecraft.state.properties.BlockStateProperties) HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) ArrayList(java.util.ArrayList) World(net.minecraft.world.World) Direction(net.minecraft.util.Direction) RelativeDirection(com.lowdragmc.multiblocked.api.pattern.util.RelativeDirection) BlockItem(net.minecraft.item.BlockItem) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) BlockItemUseContext(net.minecraft.item.BlockItemUseContext) 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) BlockPos(net.minecraft.util.math.BlockPos) CycleBlockStateRenderer(com.lowdragmc.multiblocked.client.renderer.impl.CycleBlockStateRenderer) SimplePredicate(com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate) BlockComponent(com.lowdragmc.multiblocked.api.block.BlockComponent) BlockState(net.minecraft.block.BlockState) ComponentTileEntity(com.lowdragmc.multiblocked.api.tile.ComponentTileEntity) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ControllerTileEntity (com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)10 BlockPos (net.minecraft.util.math.BlockPos)5 TileEntity (net.minecraft.tileentity.TileEntity)4 BlockInfo (com.lowdragmc.lowdraglib.utils.BlockInfo)3 TrackedDummyWorld (com.lowdragmc.lowdraglib.utils.TrackedDummyWorld)2 MultiblockCapability (com.lowdragmc.multiblocked.api.capability.MultiblockCapability)2 JsonBlockPattern (com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern)2 PatternStringError (com.lowdragmc.multiblocked.api.pattern.error.PatternStringError)2 RelativeDirection (com.lowdragmc.multiblocked.api.pattern.util.RelativeDirection)2 HashMap (java.util.HashMap)2 BlockState (net.minecraft.block.BlockState)2 Direction (net.minecraft.util.Direction)2 World (net.minecraft.world.World)2 ItemStackKey (com.lowdragmc.lowdraglib.utils.ItemStackKey)1 BlockComponent (com.lowdragmc.multiblocked.api.block.BlockComponent)1 IO (com.lowdragmc.multiblocked.api.capability.IO)1 JsonBlockPatternWidget (com.lowdragmc.multiblocked.api.gui.dialogs.JsonBlockPatternWidget)1 ItemBlueprint (com.lowdragmc.multiblocked.api.item.ItemBlueprint)1 PartAddedEvent (com.lowdragmc.multiblocked.api.kubejs.events.PartAddedEvent)1 BlockPattern (com.lowdragmc.multiblocked.api.pattern.BlockPattern)1