Search in sources :

Example 1 with JsonBlockPattern

use of com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern in project Multiblocked by Low-Drag-MC.

the class ControllerBuilderWidget method onJsonSelected.

@OnlyIn(Dist.CLIENT)
public void onJsonSelected(File file) {
    JsonElement jsonElement = FileUtility.loadJson(file);
    if (jsonElement != null) {
        JsonBlockPattern pattern = Multiblocked.GSON.fromJson(jsonElement.getAsJsonObject().get("basePattern"), JsonBlockPattern.class);
        updateScene(pattern);
    }
}
Also used : JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) JsonElement(com.google.gson.JsonElement) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn)

Example 2 with JsonBlockPattern

use of com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern in project Multiblocked by Low-Drag-MC.

the class ControllerBuilderWidget method onBuildTemplate.

@Override
protected void onBuildTemplate(ClickData clickData) {
    if (clickData.isRemote) {
        if (pos != null && facing != null && selected != null && facing.getAxis() != Direction.Axis.Y) {
            BlockPos[] poses = ItemBlueprint.getPos(selected);
            if (poses != null) {
                World world = table.getLevel();
                ResourceLocation location = new ResourceLocation("mod_id:component_id");
                ControllerDefinition controllerDefinition = new ControllerDefinition(location);
                controllerDefinition.baseRenderer = new MBDBlockStateRenderer(world.getBlockState(pos));
                new ControllerWidget(this, controllerDefinition, new JsonBlockPattern(world, location, pos, facing, poses[0].getX(), poses[0].getY(), poses[0].getZ(), poses[1].getX(), poses[1].getY(), poses[1].getZ()), "empty", jsonObject -> {
                    if (jsonObject != null) {
                        FileUtility.saveJson(new File(Multiblocked.location, "definition/controller/" + jsonObject.get("location").getAsString().replace(":", "_") + ".json"), jsonObject);
                        updateList();
                    }
                });
            }
        } else {
        // TODO
        }
    }
}
Also used : MBDBlockStateRenderer(com.lowdragmc.multiblocked.client.renderer.impl.MBDBlockStateRenderer) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) ControllerDefinition(com.lowdragmc.multiblocked.api.definition.ControllerDefinition) ControllerWidget(com.lowdragmc.multiblocked.api.gui.blueprint_table.components.ControllerWidget) ResourceLocation(net.minecraft.util.ResourceLocation) BlockPos(net.minecraft.util.math.BlockPos) TrackedDummyWorld(com.lowdragmc.lowdraglib.utils.TrackedDummyWorld) World(net.minecraft.world.World) File(java.io.File)

Example 3 with JsonBlockPattern

use of com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern 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 4 with JsonBlockPattern

use of com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern in project Multiblocked by Low-Drag-MC.

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.TEST_DEFINITION_REGISTRY.put(definition.location, definition);
                writeClientAction(-1, buffer -> buffer.writeUtf(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.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) ControllerDefinition(com.lowdragmc.multiblocked.api.definition.ControllerDefinition) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) PredicateComponent(com.lowdragmc.multiblocked.api.pattern.predicates.PredicateComponent) File(java.io.File) IOException(java.io.IOException) RecipeMap(com.lowdragmc.multiblocked.api.recipe.RecipeMap)

Example 5 with JsonBlockPattern

use of com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern in project Multiblocked by Low-Drag-MC.

the class ItemMultiblockBuilder method onItemUseFirst.

@Override
public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    if (player != null) {
        if (!player.level.isClientSide) {
            TileEntity tileEntity = player.level.getBlockEntity(context.getClickedPos());
            ItemStack hold = player.getItemInHand(context.getHand());
            if (isItemMultiblockBuilder(hold) && tileEntity instanceof ControllerTileEntity) {
                if (isRaw(hold)) {
                    BlockPattern pattern = ((ControllerTileEntity) tileEntity).getPattern();
                    if (pattern != null) {
                        pattern.autoBuild(player, new MultiblockState(player.level, context.getClickedPos()));
                    }
                    return ActionResultType.SUCCESS;
                } else {
                    String json = hold.getOrCreateTagElement("pattern").getString("json");
                    String controller = hold.getOrCreateTagElement("pattern").getString("controller");
                    if (!json.isEmpty() && !controller.isEmpty()) {
                        if (controller.equals(((ControllerTileEntity) tileEntity).getDefinition().location.toString())) {
                            JsonBlockPattern jsonBlockPattern = Multiblocked.GSON.fromJson(json, JsonBlockPattern.class);
                            jsonBlockPattern.build().autoBuild(player, new MultiblockState(player.level, context.getClickedPos()));
                            return ActionResultType.SUCCESS;
                        }
                    }
                }
            }
        }
    }
    return ActionResultType.PASS;
}
Also used : ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) BlockPattern(com.lowdragmc.multiblocked.api.pattern.BlockPattern) MultiblockState(com.lowdragmc.multiblocked.api.pattern.MultiblockState) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ControllerTileEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)

Aggregations

JsonBlockPattern (com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern)7 TrackedDummyWorld (com.lowdragmc.lowdraglib.utils.TrackedDummyWorld)4 PredicateComponent (com.lowdragmc.multiblocked.api.pattern.predicates.PredicateComponent)4 BlockPos (net.minecraft.util.math.BlockPos)4 World (net.minecraft.world.World)4 JsonElement (com.google.gson.JsonElement)3 ControllerDefinition (com.lowdragmc.multiblocked.api.definition.ControllerDefinition)3 ItemBlueprint (com.lowdragmc.multiblocked.api.item.ItemBlueprint)3 DummyComponentTileEntity (com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity)3 File (java.io.File)3 TileEntity (net.minecraft.tileentity.TileEntity)3 ControllerWidget (com.lowdragmc.multiblocked.api.gui.blueprint_table.components.ControllerWidget)2 SimplePredicate (com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate)2 BlueprintTableTileEntity (com.lowdragmc.multiblocked.api.tile.BlueprintTableTileEntity)2 ControllerTileEntity (com.lowdragmc.multiblocked.api.tile.ControllerTileEntity)2 MBDBlockStateRenderer (com.lowdragmc.multiblocked.client.renderer.impl.MBDBlockStateRenderer)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)2 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1