Search in sources :

Example 1 with JsonBlockPattern

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

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

the class ControllerBuilderWidget method updateList.

protected void updateList() {
    int size = files.size();
    files.forEach(containers::waitToRemoved);
    files.clear();
    File path = new File(Multiblocked.location, "definition/controller");
    if (!path.isDirectory()) {
        if (!path.mkdirs()) {
            return;
        }
    }
    for (File file : Optional.ofNullable(path.listFiles((s, name) -> name.endsWith(".json"))).orElse(new File[0])) {
        SelectableWidgetGroup widgetGroup = (SelectableWidgetGroup) new SelectableWidgetGroup(0, (containers.widgets.size() - size) * 22, containers.getSize().width, 20).setSelectedTexture(-2, 0xff00aa00).setOnSelected(W -> {
            templateButton.setVisible(false);
            selected = null;
            onJsonSelected(file);
        }).addWidget(new ImageWidget(0, 0, 150, 20, new ColorRectTexture(0x4faaaaaa))).addWidget(new ButtonWidget(134, 4, 12, 12, new ResourceTexture("multiblocked:textures/gui/option.png"), cd -> {
            JsonElement jsonElement = FileUtility.loadJson(file);
            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);
                    new ControllerWidget(this, definition, pattern, recipeMap, jsonObject -> {
                        if (jsonObject != null) {
                            FileUtility.saveJson(file, jsonObject);
                        }
                    });
                } catch (Exception ignored) {
                }
            }
        }).setHoverBorderTexture(1, -1).setHoverTooltip("multiblocked.gui.tips.settings")).addWidget(new ImageWidget(32, 0, 100, 20, new TextTexture(file.getName().replace(".json", "")).setWidth(100).setType(TextTexture.TextType.ROLL))).addWidget(new ImageWidget(4, 2, 18, 18, new ItemStackTexture(Items.PAPER)));
        files.add(widgetGroup);
        containers.addWidget(widgetGroup);
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) Arrays(java.util.Arrays) ControllerDefinition(com.cleanroommc.multiblocked.api.definition.ControllerDefinition) Multiblocked(com.cleanroommc.multiblocked.Multiblocked) SelectableWidgetGroup(com.cleanroommc.multiblocked.api.gui.widget.imp.SelectableWidgetGroup) TextTexture(com.cleanroommc.multiblocked.api.gui.texture.TextTexture) DummyComponentTileEntity(com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) I18n(net.minecraft.client.resources.I18n) Side(net.minecraftforge.fml.relauncher.Side) Map(java.util.Map) FileUtility(com.cleanroommc.multiblocked.util.FileUtility) PredicateComponent(com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ImageWidget(com.cleanroommc.multiblocked.api.gui.widget.imp.ImageWidget) Set(java.util.Set) List(java.util.List) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) ColorRectTexture(com.cleanroommc.multiblocked.api.gui.texture.ColorRectTexture) MbdComponents(com.cleanroommc.multiblocked.api.registry.MbdComponents) Optional(java.util.Optional) ControllerWidget(com.cleanroommc.multiblocked.api.gui.widget.imp.blueprint_table.components.ControllerWidget) BlueprintTableTileEntity(com.cleanroommc.multiblocked.api.tile.BlueprintTableTileEntity) BlockStateRenderer(com.cleanroommc.multiblocked.client.renderer.impl.BlockStateRenderer) Blocks(net.minecraft.init.Blocks) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ButtonWidget(com.cleanroommc.multiblocked.api.gui.widget.imp.ButtonWidget) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet) ClickData(com.cleanroommc.multiblocked.api.gui.util.ClickData) ItemStack(net.minecraft.item.ItemStack) Widget(com.cleanroommc.multiblocked.api.gui.widget.Widget) Minecraft(net.minecraft.client.Minecraft) JsonBlockPattern(com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) ItemStackTexture(com.cleanroommc.multiblocked.api.gui.texture.ItemStackTexture) Desktop(java.awt.Desktop) Items(net.minecraft.init.Items) World(net.minecraft.world.World) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) IOException(java.io.IOException) File(java.io.File) ItemBlueprint(com.cleanroommc.multiblocked.api.item.ItemBlueprint) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) ComponentDefinition(com.cleanroommc.multiblocked.api.definition.ComponentDefinition) ResourceLocation(net.minecraft.util.ResourceLocation) TileEntity(net.minecraft.tileentity.TileEntity) ResourceTexture(com.cleanroommc.multiblocked.api.gui.texture.ResourceTexture) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) PacketBuffer(net.minecraft.network.PacketBuffer) ItemStackTexture(com.cleanroommc.multiblocked.api.gui.texture.ItemStackTexture) ResourceTexture(com.cleanroommc.multiblocked.api.gui.texture.ResourceTexture) ControllerWidget(com.cleanroommc.multiblocked.api.gui.widget.imp.blueprint_table.components.ControllerWidget) SelectableWidgetGroup(com.cleanroommc.multiblocked.api.gui.widget.imp.SelectableWidgetGroup) ColorRectTexture(com.cleanroommc.multiblocked.api.gui.texture.ColorRectTexture) ItemBlueprint(com.cleanroommc.multiblocked.api.item.ItemBlueprint) ButtonWidget(com.cleanroommc.multiblocked.api.gui.widget.imp.ButtonWidget) IOException(java.io.IOException) JsonBlockPattern(com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern) ControllerDefinition(com.cleanroommc.multiblocked.api.definition.ControllerDefinition) JsonElement(com.google.gson.JsonElement) TextTexture(com.cleanroommc.multiblocked.api.gui.texture.TextTexture) ImageWidget(com.cleanroommc.multiblocked.api.gui.widget.imp.ImageWidget) File(java.io.File)

Example 3 with JsonBlockPattern

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

the class ControllerBuilderWidget method onJsonSelected.

@SideOnly(Side.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.cleanroommc.multiblocked.api.pattern.JsonBlockPattern) JsonElement(com.google.gson.JsonElement) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 4 with JsonBlockPattern

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

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

the class ItemMultiblockBuilder method onItemUseFirst.

@Override
@Nonnull
@ParametersAreNonnullByDefault
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    if (!world.isRemote) {
        TileEntity tileEntity = world.getTileEntity(pos);
        ItemStack hold = player.getHeldItem(hand);
        if (isItemMultiblockBuilder(hold) && tileEntity instanceof ControllerTileEntity) {
            if (isRaw(hold)) {
                ((ControllerTileEntity) tileEntity).getPattern().autoBuild(player, new MultiblockState(world, pos));
                return EnumActionResult.SUCCESS;
            } else {
                String json = hold.getOrCreateSubCompound("pattern").getString("json");
                String controller = hold.getOrCreateSubCompound("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(world, pos));
                        return EnumActionResult.SUCCESS;
                    }
                }
            }
        }
    }
    return EnumActionResult.PASS;
}
Also used : ControllerTileEntity(com.cleanroommc.multiblocked.api.tile.ControllerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) JsonBlockPattern(com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern) MultiblockState(com.cleanroommc.multiblocked.api.pattern.MultiblockState) ItemStack(net.minecraft.item.ItemStack) ControllerTileEntity(com.cleanroommc.multiblocked.api.tile.ControllerTileEntity) Nonnull(javax.annotation.Nonnull) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault)

Aggregations

JsonBlockPattern (com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern)8 BlockPos (net.minecraft.util.math.BlockPos)5 PredicateComponent (com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent)4 TrackedDummyWorld (com.cleanroommc.multiblocked.client.util.TrackedDummyWorld)4 World (net.minecraft.world.World)4 ControllerDefinition (com.cleanroommc.multiblocked.api.definition.ControllerDefinition)3 ItemBlueprint (com.cleanroommc.multiblocked.api.item.ItemBlueprint)3 DummyComponentTileEntity (com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity)3 JsonElement (com.google.gson.JsonElement)3 File (java.io.File)3 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)3 ComponentDefinition (com.cleanroommc.multiblocked.api.definition.ComponentDefinition)2 ControllerWidget (com.cleanroommc.multiblocked.api.gui.widget.imp.blueprint_table.components.ControllerWidget)2 SimplePredicate (com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate)2 BlockInfo (com.cleanroommc.multiblocked.api.pattern.util.BlockInfo)2 BlueprintTableTileEntity (com.cleanroommc.multiblocked.api.tile.BlueprintTableTileEntity)2 ControllerTileEntity (com.cleanroommc.multiblocked.api.tile.ControllerTileEntity)2 BlockStateRenderer (com.cleanroommc.multiblocked.client.renderer.impl.BlockStateRenderer)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2