Search in sources :

Example 1 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class IOPageWidget method setupSceneWidget.

@OnlyIn(Dist.CLIENT)
private void setupSceneWidget(SceneWidget sceneWidget) {
    sceneWidget.getRenderer().setAfterWorldRender(renderer -> {
        sceneWidget.renderBlockOverLay(renderer);
        RenderUtils.useLightMap(240, 240, () -> {
            RenderSystem.disableCull();
            int inner = 0;
            for (Map.Entry<MultiblockCapability<?>, Tuple<IO, Direction>> entry : capabilitySettings.entrySet()) {
                if (entry.getValue() != null) {
                    sceneWidget.drawFacingBorder(new MatrixStack(), new BlockPosFace(pos, entry.getValue().getB()), entry.getKey().color, inner);
                    inner++;
                }
            }
            RenderSystem.enableCull();
        });
    });
}
Also used : BlockPosFace(com.lowdragmc.lowdraglib.utils.BlockPosFace) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) MatrixStack(com.mojang.blaze3d.matrix.MatrixStack) HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) Tuple(net.minecraft.util.Tuple) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 2 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class BlockPattern method checkPatternAt.

public boolean checkPatternAt(MultiblockState worldState, boolean savePredicate) {
    ControllerTileEntity controller = worldState.getController();
    if (controller == null) {
        worldState.setError(new PatternStringError("no controller found"));
        return false;
    }
    BlockPos centerPos = controller.getBlockPos();
    Direction frontFacing = controller.getFrontFacing();
    Set<MultiblockCapability<?>> inputCapabilities = controller.getDefinition().recipeMap.inputCapabilities;
    Set<MultiblockCapability<?>> outputCapabilities = controller.getDefinition().recipeMap.outputCapabilities;
    Direction[] facings = controller.getDefinition().allowRotate ? new Direction[] { frontFacing } : new Direction[] { Direction.SOUTH, Direction.NORTH, Direction.EAST, Direction.WEST };
    for (Direction facing : facings) {
        if (checkPatternAt(worldState, centerPos, facing, savePredicate, inputCapabilities, outputCapabilities)) {
            return true;
        }
    }
    return false;
}
Also used : MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) BlockPos(net.minecraft.util.math.BlockPos) PatternStringError(com.lowdragmc.multiblocked.api.pattern.error.PatternStringError) Direction(net.minecraft.util.Direction) RelativeDirection(com.lowdragmc.multiblocked.api.pattern.util.RelativeDirection) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)

Example 3 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class BlockPattern method checkPatternAt.

private boolean checkPatternAt(MultiblockState worldState, BlockPos centerPos, Direction facing, boolean savePredicate, Set<MultiblockCapability<?>> inputCapabilities, Set<MultiblockCapability<?>> outputCapabilities) {
    boolean findFirstAisle = false;
    int minZ = -centerOffset[4];
    worldState.clean();
    PatternMatchContext matchContext = worldState.matchContext;
    Map<SimplePredicate, Integer> globalCount = worldState.globalCount;
    // Checking aisles
    for (int c = 0, z = minZ++, r; c < this.fingerLength; c++) {
        // Checking repeatable slices
        loop: for (r = 0; (findFirstAisle ? r < aisleRepetitions[c][1] : z <= -centerOffset[3]); r++) {
            // Checking single slice
            for (int b = 0, y = -centerOffset[1]; b < this.thumbLength; b++, y++) {
                for (int a = 0, x = -centerOffset[0]; a < this.palmLength; a++, x++) {
                    worldState.setError(null);
                    TraceabilityPredicate predicate = this.blockMatches[c][b][a];
                    BlockPos pos = setActualRelativeOffset(x, y, z, facing).offset(centerPos.getX(), centerPos.getY(), centerPos.getZ());
                    if (!worldState.update(pos, predicate)) {
                        return false;
                    }
                    if (!predicate.isAny()) {
                        worldState.addPosCache(pos);
                        if (savePredicate) {
                            worldState.getMatchContext().getOrCreate("predicates", (Supplier<HashMap<BlockPos, TraceabilityPredicate>>) HashMap::new).put(pos, predicate);
                        }
                    }
                    boolean canPartShared = true;
                    TileEntity tileEntity = worldState.getTileEntity();
                    if (tileEntity instanceof IPartComponent) {
                        // add detected parts
                        if (!predicate.isAny()) {
                            IPartComponent part = (IPartComponent) tileEntity;
                            if (part.isFormed() && !part.canShared() && !part.hasController(worldState.controllerPos)) {
                                // check part can be shared
                                canPartShared = false;
                                worldState.setError(new PatternStringError("multiblocked.pattern.error.share"));
                            } else {
                                worldState.getMatchContext().getOrCreate("parts", LongOpenHashSet::new).add(worldState.getPos().asLong());
                            }
                        }
                    }
                    if (!predicate.test(worldState) || !canPartShared) {
                        // matching failed
                        if (findFirstAisle) {
                            if (r < aisleRepetitions[c][0]) {
                                // retreat to see if the first aisle can start later
                                r = c = 0;
                                z = minZ++;
                                matchContext.reset();
                                findFirstAisle = false;
                            }
                        } else {
                            // continue searching for the first aisle
                            z++;
                        }
                        continue loop;
                    }
                    if (tileEntity != null && !predicate.isAny()) {
                        Map<Long, EnumMap<IO, Set<MultiblockCapability<?>>>> capabilities = worldState.getMatchContext().getOrCreate("capabilities", Long2ObjectOpenHashMap::new);
                        if (!capabilities.containsKey(worldState.getPos().asLong()) && worldState.io != null) {
                            // if predicate has no specific capability requirements. we will check abilities of every blocks
                            Set<MultiblockCapability<?>> bothFound = new HashSet<>();
                            for (MultiblockCapability<?> capability : inputCapabilities) {
                                // IN
                                if (worldState.io == IO.BOTH && outputCapabilities.contains(capability) && capability.isBlockHasCapability(IO.BOTH, tileEntity)) {
                                    bothFound.add(capability);
                                    capabilities.computeIfAbsent(worldState.getPos().asLong(), l -> new EnumMap<>(IO.class)).computeIfAbsent(IO.BOTH, xx -> new HashSet<>()).add(capability);
                                } else if (worldState.io != IO.OUT && capability.isBlockHasCapability(IO.IN, tileEntity)) {
                                    capabilities.computeIfAbsent(worldState.getPos().asLong(), l -> new EnumMap<>(IO.class)).computeIfAbsent(IO.IN, xx -> new HashSet<>()).add(capability);
                                }
                            }
                            if (worldState.io != IO.IN) {
                                for (MultiblockCapability<?> capability : outputCapabilities) {
                                    // OUT
                                    if (!bothFound.contains(capability) && capability.isBlockHasCapability(IO.OUT, tileEntity)) {
                                        capabilities.computeIfAbsent(worldState.getPos().asLong(), l -> new EnumMap<>(IO.class)).computeIfAbsent(IO.OUT, xx -> new HashSet<>()).add(capability);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            findFirstAisle = true;
            z++;
        }
        // Repetitions out of range
        if (r < aisleRepetitions[c][0] || !worldState.isFormed() || !findFirstAisle) {
            if (worldState.isFormed()) {
                worldState.setError(new PatternError());
            }
            return false;
        }
    }
    // Check count matches amount
    for (Map.Entry<SimplePredicate, Integer> entry : globalCount.entrySet()) {
        if (entry.getValue() < entry.getKey().minCount) {
            worldState.setError(new SinglePredicateError(entry.getKey(), 1));
            return false;
        }
    }
    worldState.setError(null);
    return true;
}
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) HashSet(java.util.HashSet) Set(java.util.Set) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) PatternMatchContext(com.lowdragmc.multiblocked.api.pattern.util.PatternMatchContext) ComponentTileEntity(com.lowdragmc.multiblocked.api.tile.ComponentTileEntity) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) BlockPos(net.minecraft.util.math.BlockPos) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) EnumMap(java.util.EnumMap) HashSet(java.util.HashSet) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) IPartComponent(com.lowdragmc.multiblocked.api.tile.part.IPartComponent) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) IO(com.lowdragmc.multiblocked.api.capability.IO) SimplePredicate(com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate) PatternError(com.lowdragmc.multiblocked.api.pattern.error.PatternError) PatternStringError(com.lowdragmc.multiblocked.api.pattern.error.PatternStringError) HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) SinglePredicateError(com.lowdragmc.multiblocked.api.pattern.error.SinglePredicateError)

Example 4 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class RecipeTypeAdapter method deserializeIO.

private ImmutableMap<MultiblockCapability<?>, ImmutableList<Tuple<Object, Float>>> deserializeIO(JsonElement jsonElement) {
    JsonObject json = jsonElement.getAsJsonObject();
    ImmutableMap.Builder<MultiblockCapability<?>, ImmutableList<Tuple<Object, Float>>> builder = new ImmutableMap.Builder<>();
    for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
        MultiblockCapability<?> capability = MbdCapabilities.get(entry.getKey());
        if (capability != null) {
            ImmutableList.Builder<Tuple<Object, Float>> listBuilder = new ImmutableList.Builder<>();
            for (JsonElement element : entry.getValue().getAsJsonArray()) {
                JsonObject recipe = element.getAsJsonObject();
                Object content;
                try {
                    content = capability.deserialize(recipe.get("content"));
                } catch (Exception e) {
                    Multiblocked.LOGGER.error(e);
                    content = null;
                }
                if (content != null) {
                    listBuilder.add(new Tuple<>(content, recipe.get("chance").getAsFloat()));
                }
            }
            builder.put(capability, listBuilder.build());
        }
    }
    return builder.build();
}
Also used : MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) ImmutableList(com.google.common.collect.ImmutableList) JsonObject(com.google.gson.JsonObject) ImmutableMap(com.google.common.collect.ImmutableMap) JsonParseException(com.google.gson.JsonParseException) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Tuple(net.minecraft.util.Tuple)

Example 5 with MultiblockCapability

use of com.lowdragmc.multiblocked.api.capability.MultiblockCapability in project Multiblocked by Low-Drag-MC.

the class ComponentTileEntity method save.

@Nonnull
@Override
public CompoundNBT save(@Nonnull CompoundNBT compound) {
    super.save(compound);
    compound.putString("loc", definition.location.toString());
    if (this.owner != null) {
        compound.putUUID("owner", this.owner);
    }
    compound.putString("mbd_def", definition.location.toString());
    CompoundNBT traitTag = new CompoundNBT();
    for (Map.Entry<MultiblockCapability<?>, CapabilityTrait> entry : traits.entrySet()) {
        CompoundNBT tag = new CompoundNBT();
        entry.getValue().writeToNBT(tag);
        traitTag.put(entry.getKey().name, tag);
    }
    compound.put("trait", traitTag);
    if (persistedData != null) {
        compound.put("persisted", persistedData);
    }
    return compound;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) MultiblockCapability(com.lowdragmc.multiblocked.api.capability.MultiblockCapability) CapabilityTrait(com.lowdragmc.multiblocked.api.capability.trait.CapabilityTrait) Map(java.util.Map) HashMap(java.util.HashMap) Nonnull(javax.annotation.Nonnull)

Aggregations

MultiblockCapability (com.lowdragmc.multiblocked.api.capability.MultiblockCapability)14 Map (java.util.Map)9 IO (com.lowdragmc.multiblocked.api.capability.IO)8 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 EnumMap (java.util.EnumMap)5 Direction (net.minecraft.util.Direction)5 Tuple (net.minecraft.util.Tuple)4 CapabilityProxy (com.lowdragmc.multiblocked.api.capability.proxy.CapabilityProxy)3 PatternStringError (com.lowdragmc.multiblocked.api.pattern.error.PatternStringError)3 ControllerTileEntity (com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 TileEntity (net.minecraft.tileentity.TileEntity)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Table (com.google.common.collect.Table)2 JsonObject (com.google.gson.JsonObject)2