Search in sources :

Example 1 with PredicateComponent

use of com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent in project Multiblocked by CleanroomMC.

the class TemplateBuilderWidget method onSelected.

@SideOnly(Side.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.getWorld();
                sceneWidget.createScene(world);
                if (poses != null && world.isAreaLoaded(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.isAirBlock(new BlockPos(x, y, z))) {
                                    rendered.add(new BlockPos(x, y, z));
                                }
                            }
                        }
                    }
                    sceneWidget.setRenderedCore(rendered, null);
                }
            } else if (itemStack.getSubCompound("pattern") != null) {
                String json = itemStack.getSubCompound("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], EnumFacing.NORTH).add(offset, offset, offset);
                            world.addBlock(pos, new BlockInfo(MbdComponents.DummyComponentBlock));
                            DummyComponentTileEntity tileEntity = (DummyComponentTileEntity) world.getTileEntity(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.setWorld(world);
                            tileEntity.validate();
                            rendered.add(pos);
                        }
                    }
                }
                sceneWidget.setRenderedCore(rendered, null);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) PredicateComponent(com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) World(net.minecraft.world.World) ItemBlueprint(com.cleanroommc.multiblocked.api.item.ItemBlueprint) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) JsonBlockPattern(com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) BlockPos(net.minecraft.util.math.BlockPos) DummyComponentTileEntity(com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity) HashSet(java.util.HashSet) ComponentDefinition(com.cleanroommc.multiblocked.api.definition.ComponentDefinition) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 2 with PredicateComponent

use of com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent in project Multiblocked by CleanroomMC.

the class ControllerWidget method updateScene.

@SideOnly(Side.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], EnumFacing.NORTH).add(offset, offset, offset);
                    world.addBlock(pos, new BlockInfo(MbdComponents.DummyComponentBlock));
                    DummyComponentTileEntity tileEntity = (DummyComponentTileEntity) world.getTileEntity(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 BlockStateRenderer(Blocks.AIR.getDefaultState());
                        }
                    }
                    tileEntity.isFormed = isFormed;
                    tileEntity.setWorld(world);
                    tileEntity.validate();
                    posSet.add(pos);
                    tiles.add(tileEntity);
                }
            }
        }
        Minecraft.getMinecraft().addScheduledTask(() -> {
            sceneWidget.setRenderedCore(posSet, null);
            sceneWidget.waitToRemoved(imageWidget);
        });
        thread = null;
    });
    thread.start();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) PredicateComponent(com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) BlockStateRenderer(com.cleanroommc.multiblocked.client.renderer.impl.BlockStateRenderer) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) BlockPos(net.minecraft.util.math.BlockPos) DummyComponentTileEntity(com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity) ComponentDefinition(com.cleanroommc.multiblocked.api.definition.ComponentDefinition) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 3 with PredicateComponent

use of com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent in project Multiblocked by CleanroomMC.

the class ControllerBuilderWidget method updateScene.

@SideOnly(Side.CLIENT)
private void updateScene(JsonBlockPattern jsonPattern) {
    if (thread != null) {
        thread.interrupt();
        thread = null;
    }
    TrackedDummyWorld world = new TrackedDummyWorld();
    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("multiblocked.gui.tips.building_scene").setSupplier(() -> I18n.format("multiblocked.gui.tips.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], EnumFacing.NORTH).add(offset, offset, offset);
                    if (jsonPattern.symbolMap.containsKey(symbol)) {
                        List<BlockInfo> candidates = new ArrayList<>();
                        for (String s : jsonPattern.symbolMap.get(symbol)) {
                            SimplePredicate predicate = jsonPattern.predicates.get(s);
                            if (predicate instanceof PredicateComponent && ((PredicateComponent) predicate).definition != null) {
                                world.addBlock(pos, new BlockInfo(MbdComponents.DummyComponentBlock));
                                DummyComponentTileEntity tileEntity = (DummyComponentTileEntity) world.getTileEntity(pos);
                                assert tileEntity != null;
                                tileEntity.setDefinition(((PredicateComponent) predicate).definition);
                                tileEntity.isFormed = false;
                                tileEntity.setWorld(world);
                                tileEntity.validate();
                                posSet.add(pos);
                                break;
                            } else if (predicate != null && predicate.candidates != null) {
                                candidates.addAll(Arrays.asList(predicate.candidates.get()));
                            }
                        }
                        if (candidates.size() > 0) {
                            world.addBlock(pos, candidates.get(0));
                            posSet.add(pos);
                        }
                    }
                }
            }
        }
        Minecraft.getMinecraft().addScheduledTask(() -> {
            sceneWidget.setRenderedCore(posSet, null);
            sceneWidget.waitToRemoved(imageWidget);
        });
        thread = null;
    });
    thread.start();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) PredicateComponent(com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) TextTexture(com.cleanroommc.multiblocked.api.gui.texture.TextTexture) BlockPos(net.minecraft.util.math.BlockPos) List(java.util.List) ArrayList(java.util.ArrayList) ImageWidget(com.cleanroommc.multiblocked.api.gui.widget.imp.ImageWidget) DummyComponentTileEntity(com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 4 with PredicateComponent

use of com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent in project Multiblocked by CleanroomMC.

the class ControllerScriptWidget method loadJson.

private void loadJson(ClickData clickData) {
    if (selected != null && clickData.isRemote) {
        JsonElement jsonElement = FileUtility.loadJson(selected);
        if (jsonElement != null) {
            try {
                String recipeMap = jsonElement.getAsJsonObject().get("recipeMap").getAsString();
                JsonBlockPattern pattern = Multiblocked.GSON.fromJson(jsonElement.getAsJsonObject().get("basePattern"), JsonBlockPattern.class);
                ControllerDefinition definition = Multiblocked.GSON.fromJson(jsonElement, ControllerDefinition.class);
                pattern.predicates.put("controller", new PredicateComponent(definition));
                definition.basePattern = pattern.build();
                for (File file : Optional.ofNullable(new File(Multiblocked.location, "recipe_map").listFiles((f, n) -> n.endsWith(".json"))).orElse(new File[0])) {
                    JsonObject config = (JsonObject) FileUtility.loadJson(file);
                    if (config != null && config.get("name").getAsString().equals(recipeMap)) {
                        definition.recipeMap = Multiblocked.GSON.fromJson(config, RecipeMap.class);
                        break;
                    }
                }
                controller.setDefinition(definition);
                MbdComponents.DEFINITION_REGISTRY.put(definition.location, definition);
                writeClientAction(-1, buffer -> buffer.writeString(definition.location.toString()));
            } catch (Exception e) {
                Multiblocked.LOGGER.error("tester: error while loading the controller json {}", selected.getName(), e);
            }
            textBox.setContent(Collections.singletonList(Multiblocked.GSON_PRETTY.toJson(jsonElement)));
            tfGroup.computeMax();
        }
    }
}
Also used : JsonBlockPattern(com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern) ControllerDefinition(com.cleanroommc.multiblocked.api.definition.ControllerDefinition) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) PredicateComponent(com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent) File(java.io.File) IOException(java.io.IOException) RecipeMap(com.cleanroommc.multiblocked.api.recipe.RecipeMap)

Example 5 with PredicateComponent

use of com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent in project Multiblocked by CleanroomMC.

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.getWorld();
            if (poses != null && world.isAreaLoaded(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.getTileEntity(new BlockPos(x, y, z));
                            if (te instanceof ControllerTileEntity) {
                                controller = (ControllerTileEntity) te;
                            }
                        }
                    }
                }
                if (controller != null) {
                    pattern = new JsonBlockPattern(table.getWorld(), controller.getLocation(), controller.getPos(), 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.getSubCompound("pattern") != null) {
            String json = selected.getSubCompound("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.getOrCreateSubCompound("pattern").setString("json", json);
                        selected.getOrCreateSubCompound("pattern").setString("controller", controller);
                        writeClientAction(-1, buffer -> {
                            buffer.writeVarInt(selectedSlot);
                            buffer.writeString(json);
                            buffer.writeString(controller);
                        });
                    }
                }
            });
        }
    }
}
Also used : DummyComponentTileEntity(com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity) BlueprintTableTileEntity(com.cleanroommc.multiblocked.api.tile.BlueprintTableTileEntity) ControllerTileEntity(com.cleanroommc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) JsonBlockPattern(com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern) JsonBlockPatternWidget(com.cleanroommc.multiblocked.api.gui.widget.imp.blueprint_table.dialogs.JsonBlockPatternWidget) BlockPos(net.minecraft.util.math.BlockPos) PredicateComponent(com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) World(net.minecraft.world.World) ItemBlueprint(com.cleanroommc.multiblocked.api.item.ItemBlueprint) ControllerTileEntity(com.cleanroommc.multiblocked.api.tile.ControllerTileEntity)

Aggregations

PredicateComponent (com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent)6 SimplePredicate (com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate)4 DummyComponentTileEntity (com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity)4 TrackedDummyWorld (com.cleanroommc.multiblocked.client.util.TrackedDummyWorld)4 BlockPos (net.minecraft.util.math.BlockPos)4 JsonBlockPattern (com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern)3 BlockInfo (com.cleanroommc.multiblocked.api.pattern.util.BlockInfo)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)3 ComponentDefinition (com.cleanroommc.multiblocked.api.definition.ComponentDefinition)2 ItemBlueprint (com.cleanroommc.multiblocked.api.item.ItemBlueprint)2 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)2 World (net.minecraft.world.World)2 ControllerDefinition (com.cleanroommc.multiblocked.api.definition.ControllerDefinition)1 TextTexture (com.cleanroommc.multiblocked.api.gui.texture.TextTexture)1 ImageWidget (com.cleanroommc.multiblocked.api.gui.widget.imp.ImageWidget)1 JsonBlockPatternWidget (com.cleanroommc.multiblocked.api.gui.widget.imp.blueprint_table.dialogs.JsonBlockPatternWidget)1 RecipeMap (com.cleanroommc.multiblocked.api.recipe.RecipeMap)1 BlueprintTableTileEntity (com.cleanroommc.multiblocked.api.tile.BlueprintTableTileEntity)1