Search in sources :

Example 1 with ComponentDefinition

use of com.lowdragmc.multiblocked.api.definition.ComponentDefinition in project Multiblocked by Low-Drag-MC.

the class TemplateBuilderWidget method onSelected.

@OnlyIn(Dist.CLIENT)
public void onSelected(ItemStack itemStack, int slot) {
    if (this.selected != itemStack) {
        this.selected = itemStack;
        this.selectedSlot = slot;
        if (selected != null && isRemote()) {
            this.pos = null;
            this.facing = null;
            templateButton.setVisible(true);
            if (ItemBlueprint.isRaw(itemStack)) {
                BlockPos[] poses = ItemBlueprint.getPos(itemStack);
                World world = table.getLevel();
                sceneWidget.createScene(world);
                if (poses != null && world.hasChunksAt(poses[0], poses[1])) {
                    Set<BlockPos> rendered = new HashSet<>();
                    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++) {
                                if (!world.isEmptyBlock(new BlockPos(x, y, z))) {
                                    rendered.add(new BlockPos(x, y, z));
                                }
                            }
                        }
                    }
                    sceneWidget.setRenderedCore(rendered, null);
                }
            } else if (itemStack.getTagElement("pattern") != null) {
                String json = itemStack.getTagElement("pattern").getString("json");
                JsonBlockPattern pattern = Multiblocked.GSON.fromJson(json, JsonBlockPattern.class);
                int[] centerOffset = pattern.getCenterOffset();
                String[][] patternString = pattern.pattern;
                Set<BlockPos> rendered = new HashSet<>();
                TrackedDummyWorld world = new TrackedDummyWorld();
                sceneWidget.createScene(world);
                int offset = Math.max(patternString.length, Math.max(patternString[0].length, patternString[0][0].length()));
                for (int i = 0; i < patternString.length; i++) {
                    for (int j = 0; j < patternString[0].length; j++) {
                        for (int k = 0; k < patternString[0][0].length(); k++) {
                            char symbol = patternString[i][j].charAt(k);
                            BlockPos pos = pattern.getActualPosOffset(k - centerOffset[2], j - centerOffset[1], i - centerOffset[0], Direction.NORTH).offset(offset, offset, offset);
                            world.addBlock(pos, BlockInfo.fromBlockState(MbdComponents.DummyComponentBlock.defaultBlockState()));
                            DummyComponentTileEntity tileEntity = (DummyComponentTileEntity) world.getBlockEntity(pos);
                            ComponentDefinition definition = null;
                            assert tileEntity != null;
                            if (pattern.symbolMap.containsKey(symbol)) {
                                Set<BlockInfo> candidates = new HashSet<>();
                                for (String s : pattern.symbolMap.get(symbol)) {
                                    SimplePredicate predicate = pattern.predicates.get(s);
                                    if (predicate instanceof PredicateComponent && ((PredicateComponent) predicate).definition != null) {
                                        definition = ((PredicateComponent) predicate).definition;
                                        break;
                                    } else if (predicate != null && predicate.candidates != null) {
                                        candidates.addAll(Arrays.asList(predicate.candidates.get()));
                                    }
                                }
                                definition = getComponentDefinition(definition, candidates);
                            }
                            if (definition != null) {
                                tileEntity.setDefinition(definition);
                            }
                            tileEntity.isFormed = false;
                            tileEntity.setLevelAndPosition(world, pos);
                            rendered.add(pos);
                        }
                    }
                }
                sceneWidget.setRenderedCore(rendered, null);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TrackedDummyWorld(com.lowdragmc.lowdraglib.utils.TrackedDummyWorld) 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) SimplePredicate(com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) BlockPos(net.minecraft.util.math.BlockPos) DummyComponentTileEntity(com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity) HashSet(java.util.HashSet) ComponentDefinition(com.lowdragmc.multiblocked.api.definition.ComponentDefinition) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 2 with ComponentDefinition

use of com.lowdragmc.multiblocked.api.definition.ComponentDefinition in project Multiblocked by Low-Drag-MC.

the class PredicateComponent method getAvailableComponents.

private List<String> getAvailableComponents() {
    Set<String> components = new HashSet<>();
    for (ComponentDefinition value : MbdComponents.DEFINITION_REGISTRY.values()) {
        if (value.baseRenderer instanceof CycleBlockStateRenderer)
            continue;
        if (value instanceof ControllerDefinition)
            continue;
        components.add(value.location.toString());
    }
    File dir = new File(Multiblocked.location, "definition/part");
    if (dir.isDirectory()) {
        File[] files = dir.listFiles();
        if (files != null) {
            Arrays.stream(files).map(file -> {
                try {
                    return FileUtility.loadJson(file).getAsJsonObject().get("location").getAsString();
                } catch (Exception e) {
                    return null;
                }
            }).filter(Objects::nonNull).forEach(components::add);
        }
    }
    return new ArrayList<>(components);
}
Also used : CycleBlockStateRenderer(com.lowdragmc.multiblocked.client.renderer.impl.CycleBlockStateRenderer) ControllerDefinition(com.lowdragmc.multiblocked.api.definition.ControllerDefinition) ArrayList(java.util.ArrayList) File(java.io.File) HashSet(java.util.HashSet) ComponentDefinition(com.lowdragmc.multiblocked.api.definition.ComponentDefinition)

Example 3 with ComponentDefinition

use of com.lowdragmc.multiblocked.api.definition.ComponentDefinition in project Multiblocked by Low-Drag-MC.

the class MbdCapabilities method registerAnyCapabilityBlocks.

public static void registerAnyCapabilityBlocks() {
    for (MultiblockCapability<?> capability : MbdCapabilities.CAPABILITY_REGISTRY.values()) {
        ComponentDefinition definition = new PartDefinition(new ResourceLocation(Multiblocked.MODID, capability.name + ".any"));
        definition.properties.isOpaque = false;
        definition.properties.tabGroup = null;
        definition.allowRotate = false;
        definition.showInJei = false;
        MbdComponents.registerComponent(definition);
    }
}
Also used : ResourceLocation(net.minecraft.util.ResourceLocation) PartDefinition(com.lowdragmc.multiblocked.api.definition.PartDefinition) ComponentDefinition(com.lowdragmc.multiblocked.api.definition.ComponentDefinition)

Example 4 with ComponentDefinition

use of com.lowdragmc.multiblocked.api.definition.ComponentDefinition in project Multiblocked by Low-Drag-MC.

the class JEIPlugin method onRuntimeAvailable.

@Override
public void onRuntimeAvailable(@Nonnull IJeiRuntime jeiRuntime) {
    JEIPlugin.jeiRuntime = jeiRuntime;
    List<ItemStack> removed = new ArrayList<>();
    for (ComponentDefinition definition : MbdComponents.DEFINITION_REGISTRY.values()) {
        if (!definition.showInJei) {
            removed.add(definition.getStackForm());
        }
    }
    jeiRuntime.getIngredientManager().removeIngredientsAtRuntime(VanillaTypes.ITEM, removed);
}
Also used : ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) ComponentDefinition(com.lowdragmc.multiblocked.api.definition.ComponentDefinition)

Example 5 with ComponentDefinition

use of com.lowdragmc.multiblocked.api.definition.ComponentDefinition in project Multiblocked by Low-Drag-MC.

the class ControllerWidget method updateScene.

@OnlyIn(Dist.CLIENT)
private void updateScene(JsonBlockPattern jsonPattern) {
    if (thread != null) {
        thread.interrupt();
        thread = null;
    }
    TrackedDummyWorld world = new TrackedDummyWorld();
    tiles.clear();
    sceneWidget.createScene(world);
    ImageWidget imageWidget;
    sceneWidget.addWidget(imageWidget = new ImageWidget(0, 0, sceneWidget.getSize().width, sceneWidget.getSize().height));
    imageWidget.setVisible(jsonPattern.pattern.length * jsonPattern.pattern[0].length * jsonPattern.pattern[0][0].length() > 1000);
    thread = new Thread(() -> {
        int[] centerOffset = jsonPattern.getCenterOffset();
        String[][] pattern = jsonPattern.pattern;
        Set<BlockPos> posSet = new HashSet<>();
        int offset = Math.max(pattern.length, Math.max(pattern[0].length, pattern[0][0].length()));
        int sum = jsonPattern.pattern.length * jsonPattern.pattern[0].length * jsonPattern.pattern[0][0].length();
        AtomicDouble progress = new AtomicDouble(0);
        imageWidget.setImage(new TextTexture("building scene!").setSupplier(() -> "building scene! " + String.format("%.1f", progress.get()) + "%%").setWidth(sceneWidget.getSize().width));
        int count = 0;
        for (int i = 0; i < pattern.length; i++) {
            for (int j = 0; j < pattern[0].length; j++) {
                for (int k = 0; k < pattern[0][0].length(); k++) {
                    if (Thread.interrupted()) {
                        sceneWidget.waitToRemoved(imageWidget);
                        return;
                    }
                    count++;
                    progress.set(count * 100.0 / sum);
                    char symbol = pattern[i][j].charAt(k);
                    BlockPos pos = jsonPattern.getActualPosOffset(k - centerOffset[2], j - centerOffset[1], i - centerOffset[0], Direction.NORTH).offset(offset, offset, offset);
                    world.addBlock(pos, BlockInfo.fromBlockState(MbdComponents.DummyComponentBlock.defaultBlockState()));
                    DummyComponentTileEntity tileEntity = (DummyComponentTileEntity) world.getBlockEntity(pos);
                    ComponentDefinition definition = null;
                    assert tileEntity != null;
                    boolean disableFormed = false;
                    if (jsonPattern.symbolMap.containsKey(symbol)) {
                        Set<BlockInfo> candidates = new HashSet<>();
                        for (String s : jsonPattern.symbolMap.get(symbol)) {
                            SimplePredicate predicate = jsonPattern.predicates.get(s);
                            if (predicate instanceof PredicateComponent && ((PredicateComponent) predicate).definition != null) {
                                definition = ((PredicateComponent) predicate).definition;
                                disableFormed |= predicate.disableRenderFormed;
                                break;
                            } else if (predicate != null && predicate.candidates != null) {
                                candidates.addAll(Arrays.asList(predicate.candidates.get()));
                                disableFormed |= predicate.disableRenderFormed;
                            }
                        }
                        definition = TemplateBuilderWidget.getComponentDefinition(definition, candidates);
                    }
                    if (definition != null) {
                        tileEntity.setDefinition(definition);
                        if (disableFormed) {
                            definition.formedRenderer = new MBDBlockStateRenderer(Blocks.AIR.defaultBlockState());
                        }
                    }
                    tileEntity.isFormed = isFormed;
                    tileEntity.setLevelAndPosition(world, pos);
                    posSet.add(pos);
                    tiles.add(tileEntity);
                }
            }
        }
        Minecraft.getInstance().execute(() -> {
            sceneWidget.setRenderedCore(posSet, null);
            sceneWidget.waitToRemoved(imageWidget);
        });
        thread = null;
    });
    thread.start();
}
Also used : MBDBlockStateRenderer(com.lowdragmc.multiblocked.client.renderer.impl.MBDBlockStateRenderer) HashSet(java.util.HashSet) Set(java.util.Set) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) TrackedDummyWorld(com.lowdragmc.lowdraglib.utils.TrackedDummyWorld) PredicateComponent(com.lowdragmc.multiblocked.api.pattern.predicates.PredicateComponent) SimplePredicate(com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate) TextTexture(com.lowdragmc.lowdraglib.gui.texture.TextTexture) BlockPos(net.minecraft.util.math.BlockPos) ImageWidget(com.lowdragmc.lowdraglib.gui.widget.ImageWidget) DummyComponentTileEntity(com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity) ComponentDefinition(com.lowdragmc.multiblocked.api.definition.ComponentDefinition) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Aggregations

ComponentDefinition (com.lowdragmc.multiblocked.api.definition.ComponentDefinition)5 HashSet (java.util.HashSet)3 TrackedDummyWorld (com.lowdragmc.lowdraglib.utils.TrackedDummyWorld)2 PredicateComponent (com.lowdragmc.multiblocked.api.pattern.predicates.PredicateComponent)2 SimplePredicate (com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate)2 DummyComponentTileEntity (com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 BlockPos (net.minecraft.util.math.BlockPos)2 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)2 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 TextTexture (com.lowdragmc.lowdraglib.gui.texture.TextTexture)1 ImageWidget (com.lowdragmc.lowdraglib.gui.widget.ImageWidget)1 ControllerDefinition (com.lowdragmc.multiblocked.api.definition.ControllerDefinition)1 PartDefinition (com.lowdragmc.multiblocked.api.definition.PartDefinition)1 ItemBlueprint (com.lowdragmc.multiblocked.api.item.ItemBlueprint)1 JsonBlockPattern (com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern)1 CycleBlockStateRenderer (com.lowdragmc.multiblocked.client.renderer.impl.CycleBlockStateRenderer)1 MBDBlockStateRenderer (com.lowdragmc.multiblocked.client.renderer.impl.MBDBlockStateRenderer)1 File (java.io.File)1