Search in sources :

Example 1 with ControllerDefinition

use of com.lowdragmc.multiblocked.api.definition.ControllerDefinition 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 2 with ControllerDefinition

use of com.lowdragmc.multiblocked.api.definition.ControllerDefinition 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 3 with ControllerDefinition

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

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

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).setHoverTooltips("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 : Arrays(java.util.Arrays) PredicateComponent(com.lowdragmc.multiblocked.api.pattern.predicates.PredicateComponent) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) ImageWidget(com.lowdragmc.lowdraglib.gui.widget.ImageWidget) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) Direction(net.minecraft.util.Direction) ItemBlueprint(com.lowdragmc.multiblocked.api.item.ItemBlueprint) BlueprintTableTileEntity(com.lowdragmc.multiblocked.api.tile.BlueprintTableTileEntity) Dist(net.minecraftforge.api.distmarker.Dist) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) ClickData(com.lowdragmc.lowdraglib.gui.util.ClickData) MBDBlockStateRenderer(com.lowdragmc.multiblocked.client.renderer.impl.MBDBlockStateRenderer) I18n(net.minecraft.client.resources.I18n) HashSet(java.util.HashSet) ItemStack(net.minecraft.item.ItemStack) BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) Minecraft(net.minecraft.client.Minecraft) Map(java.util.Map) DummyComponentTileEntity(com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity) TextTexture(com.lowdragmc.lowdraglib.gui.texture.TextTexture) SelectableWidgetGroup(com.lowdragmc.lowdraglib.gui.widget.SelectableWidgetGroup) Desktop(java.awt.Desktop) MbdComponents(com.lowdragmc.multiblocked.api.registry.MbdComponents) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ItemStackTexture(com.lowdragmc.lowdraglib.gui.texture.ItemStackTexture) TrackedDummyWorld(com.lowdragmc.lowdraglib.utils.TrackedDummyWorld) World(net.minecraft.world.World) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) Items(net.minecraft.item.Items) ButtonWidget(com.lowdragmc.lowdraglib.gui.widget.ButtonWidget) ControllerWidget(com.lowdragmc.multiblocked.api.gui.blueprint_table.components.ControllerWidget) File(java.io.File) List(java.util.List) ColorRectTexture(com.lowdragmc.lowdraglib.gui.texture.ColorRectTexture) ControllerDefinition(com.lowdragmc.multiblocked.api.definition.ControllerDefinition) ResourceTexture(com.lowdragmc.lowdraglib.gui.texture.ResourceTexture) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) ResourceLocation(net.minecraft.util.ResourceLocation) SimplePredicate(com.lowdragmc.multiblocked.api.pattern.predicates.SimplePredicate) Optional(java.util.Optional) FileUtility(com.lowdragmc.lowdraglib.utils.FileUtility) TileEntity(net.minecraft.tileentity.TileEntity) Multiblocked(com.lowdragmc.multiblocked.Multiblocked) PacketBuffer(net.minecraft.network.PacketBuffer) ItemStackTexture(com.lowdragmc.lowdraglib.gui.texture.ItemStackTexture) ResourceTexture(com.lowdragmc.lowdraglib.gui.texture.ResourceTexture) ControllerWidget(com.lowdragmc.multiblocked.api.gui.blueprint_table.components.ControllerWidget) SelectableWidgetGroup(com.lowdragmc.lowdraglib.gui.widget.SelectableWidgetGroup) ColorRectTexture(com.lowdragmc.lowdraglib.gui.texture.ColorRectTexture) ItemBlueprint(com.lowdragmc.multiblocked.api.item.ItemBlueprint) ButtonWidget(com.lowdragmc.lowdraglib.gui.widget.ButtonWidget) JsonBlockPattern(com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern) ControllerDefinition(com.lowdragmc.multiblocked.api.definition.ControllerDefinition) JsonElement(com.google.gson.JsonElement) TextTexture(com.lowdragmc.lowdraglib.gui.texture.TextTexture) ImageWidget(com.lowdragmc.lowdraglib.gui.widget.ImageWidget) File(java.io.File)

Example 5 with ControllerDefinition

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

the class PredicateComponent method buildPredicate.

@Override
public SimplePredicate buildPredicate() {
    predicate = state -> {
        TileEntity tileEntity = state.getTileEntity();
        if (tileEntity instanceof IComponent) {
            return ((IComponent) tileEntity).getDefinition().location.equals(location);
        }
        return false;
    };
    candidates = () -> {
        if (MbdComponents.COMPONENT_BLOCKS_REGISTRY.containsKey(location)) {
            return new BlockInfo[] { new BlockInfo(MbdComponents.COMPONENT_BLOCKS_REGISTRY.get(location).defaultBlockState(), MbdComponents.DEFINITION_REGISTRY.get(location).createNewTileEntity()) };
        } else {
            if (definition == null)
                return new BlockInfo[0];
            if (definition instanceof ControllerDefinition) {
                ControllerTileTesterEntity te = new ControllerTileTesterEntity(ControllerTileTesterEntity.DEFAULT_DEFINITION);
                te.setDefinition((ControllerDefinition) definition);
                return new BlockInfo[] { new BlockInfo(MbdComponents.COMPONENT_BLOCKS_REGISTRY.get(ControllerTileTesterEntity.DEFAULT_DEFINITION.location).defaultBlockState(), te) };
            } else {
                DummyComponentTileEntity te = new DummyComponentTileEntity(MbdComponents.DummyComponentBlock.definition);
                te.setDefinition(definition);
                return new BlockInfo[] { new BlockInfo(MbdComponents.DummyComponentBlock.defaultBlockState(), te) };
            }
        }
    };
    return this;
}
Also used : DummyComponentTileEntity(com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ControllerDefinition(com.lowdragmc.multiblocked.api.definition.ControllerDefinition) IComponent(com.lowdragmc.multiblocked.api.tile.IComponent) BlockInfo(com.lowdragmc.lowdraglib.utils.BlockInfo) ControllerTileTesterEntity(com.lowdragmc.multiblocked.api.tile.ControllerTileTesterEntity) DummyComponentTileEntity(com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity)

Aggregations

ControllerDefinition (com.lowdragmc.multiblocked.api.definition.ControllerDefinition)5 File (java.io.File)4 JsonBlockPattern (com.lowdragmc.multiblocked.api.pattern.JsonBlockPattern)3 JsonElement (com.google.gson.JsonElement)2 BlockInfo (com.lowdragmc.lowdraglib.utils.BlockInfo)2 TrackedDummyWorld (com.lowdragmc.lowdraglib.utils.TrackedDummyWorld)2 ControllerWidget (com.lowdragmc.multiblocked.api.gui.blueprint_table.components.ControllerWidget)2 PredicateComponent (com.lowdragmc.multiblocked.api.pattern.predicates.PredicateComponent)2 DummyComponentTileEntity (com.lowdragmc.multiblocked.api.tile.DummyComponentTileEntity)2 MBDBlockStateRenderer (com.lowdragmc.multiblocked.client.renderer.impl.MBDBlockStateRenderer)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 TileEntity (net.minecraft.tileentity.TileEntity)2 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 JsonObject (com.google.gson.JsonObject)1 ColorRectTexture (com.lowdragmc.lowdraglib.gui.texture.ColorRectTexture)1 ItemStackTexture (com.lowdragmc.lowdraglib.gui.texture.ItemStackTexture)1 ResourceTexture (com.lowdragmc.lowdraglib.gui.texture.ResourceTexture)1 TextTexture (com.lowdragmc.lowdraglib.gui.texture.TextTexture)1 ClickData (com.lowdragmc.lowdraglib.gui.util.ClickData)1