Search in sources :

Example 1 with BlueprintEntity

use of com.demod.fbsr.BlueprintEntity in project Factorio-FBSR by demodude4u.

the class BlueprintBotDiscordService method handleBlueprintUpgradeBeltsCommand.

private void handleBlueprintUpgradeBeltsCommand(SlashCommandEvent event) {
    String content = event.getCommandString();
    Optional<Attachment> attachment = event.optParamAttachment("file");
    if (attachment.isPresent()) {
        content += " " + attachment.get().getUrl();
    }
    List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
    int upgradedCount = 0;
    for (BlueprintStringData blueprintStringData : blueprintStringDatas) {
        for (Blueprint blueprint : blueprintStringData.getBlueprints()) {
            for (BlueprintEntity blueprintEntity : blueprint.getEntities()) {
                String upgradeName = upgradeBeltsEntityMapping.get(blueprintEntity.getName());
                if (upgradeName != null) {
                    blueprintEntity.json().put("name", upgradeName);
                    upgradedCount++;
                }
            }
        }
    }
    if (upgradedCount > 0) {
        for (BlueprintStringData blueprintStringData : blueprintStringDatas) {
            try {
                event.replyFile(BlueprintStringData.encode(blueprintStringData.json()).getBytes(), "blueprint.txt");
            } catch (IOException e) {
                event.getReporting().addException(e);
            }
        }
        event.reply("Upgraded " + upgradedCount + " entities.");
    } else if (blueprintStringDatas.stream().anyMatch(d -> !d.getBlueprints().isEmpty())) {
        event.replyIfNoException("I couldn't find anything to upgrade!");
    } else {
        event.replyIfNoException("No blueprint found!");
    }
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Arrays(java.util.Arrays) EventReply(com.demod.dcba.EventReply) URL(java.net.URL) FBSR(com.demod.fbsr.FBSR) Permission(net.dv8tion.jda.api.Permission) SlashCommandHandler(com.demod.dcba.SlashCommandHandler) Field(net.dv8tion.jda.api.entities.MessageEmbed.Field) TextChannel(net.dv8tion.jda.api.entities.TextChannel) DataTable(com.demod.factorio.DataTable) RenderUtils(com.demod.fbsr.RenderUtils) JSONException(org.json.JSONException) Matcher(java.util.regex.Matcher) JSONObject(org.json.JSONObject) Map(java.util.Map) ZipEntry(java.util.zip.ZipEntry) WebUtils(com.demod.fbsr.WebUtils) Message(net.dv8tion.jda.api.entities.Message) BufferedImage(java.awt.image.BufferedImage) DataPrototype(com.demod.factorio.prototype.DataPrototype) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance) Collectors(java.util.stream.Collectors) Config(com.demod.factorio.Config) BlueprintEntity(com.demod.fbsr.BlueprintEntity) List(java.util.List) FactorioData(com.demod.factorio.FactorioData) MessageCommandEvent(com.demod.dcba.MessageCommandEvent) Entry(java.util.Map.Entry) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) BlueprintStringData(com.demod.fbsr.BlueprintStringData) ZipOutputStream(java.util.zip.ZipOutputStream) Blueprint(com.demod.fbsr.Blueprint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WatchdogReporter(com.demod.fbsr.app.WatchdogService.WatchdogReporter) ImmutableList(com.google.common.collect.ImmutableList) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) SlashCommandEvent(com.demod.dcba.SlashCommandEvent) SimpleEntry(java.util.AbstractMap.SimpleEntry) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) MapVersion(com.demod.fbsr.MapVersion) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandReporting(com.demod.dcba.CommandReporting) DCBA(com.demod.dcba.DCBA) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Utils(com.demod.factorio.Utils) DiscordBot(com.demod.dcba.DiscordBot) LuaValue(org.luaj.vm2.LuaValue) AutoCompleteHandler(com.demod.dcba.AutoCompleteHandler) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ArrayDeque(java.util.ArrayDeque) BlueprintFinder(com.demod.fbsr.BlueprintFinder) ChunkingFilter(net.dv8tion.jda.api.utils.ChunkingFilter) JSONArray(org.json.JSONArray) BlueprintEntity(com.demod.fbsr.BlueprintEntity) Blueprint(com.demod.fbsr.Blueprint) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) BlueprintStringData(com.demod.fbsr.BlueprintStringData) IOException(java.io.IOException) Blueprint(com.demod.fbsr.Blueprint)

Example 2 with BlueprintEntity

use of com.demod.fbsr.BlueprintEntity in project Factorio-FBSR by demodude4u.

the class AssemblingMachineRendering method populateWorldMap.

@Override
public void populateWorldMap(WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    String recipeName = entity.json().optString("recipe", null);
    boolean hasFluid = false;
    if (recipeName != null) {
        Optional<RecipePrototype> optRecipe = dataTable.getRecipe(recipeName);
        if (optRecipe.isPresent()) {
            RecipePrototype protoRecipe = optRecipe.get();
            List<LuaValue> items = new ArrayList<>();
            Utils.forEach(protoRecipe.lua().get("ingredients"), (Consumer<LuaValue>) items::add);
            LuaValue resultsLua = protoRecipe.lua().get("results");
            if (resultsLua != LuaValue.NIL) {
                items.add(resultsLua);
            }
            hasFluid = items.stream().anyMatch(lua -> {
                LuaValue typeLua = lua.get("type");
                return typeLua != LuaValue.NIL && typeLua.toString().equals("fluid");
            });
        }
    }
    LuaValue fluidBoxesLua = prototype.lua().get("fluid_boxes");
    boolean offWhenNoFluidRecipe = fluidBoxesLua.isnil() ? true : fluidBoxesLua.get("off_when_no_fluid_recipe").optboolean(false);
    if (!fluidBoxesLua.isnil() && (!offWhenNoFluidRecipe || hasFluid)) {
        Utils.forEach(fluidBoxesLua, fluidBoxLua -> {
            if (!fluidBoxLua.istable()) {
                return;
            }
            Utils.forEach(fluidBoxLua.get("pipe_connections"), pipeConnectionLua -> {
                Point2D.Double offset = Utils.parsePoint2D(pipeConnectionLua.get("position"));
                if (Math.abs(offset.y) > Math.abs(offset.x)) {
                    offset.y += -Math.signum(offset.y);
                } else {
                    offset.x += -Math.signum(offset.x);
                }
                Point2D.Double pos = entity.getDirection().left().offset(entity.getDirection().back().offset(entity.getPosition(), offset.y), offset.x);
                Direction direction = offset.y > 0 ? entity.getDirection().back() : entity.getDirection();
                map.setPipe(pos, direction);
            });
        });
    }
}
Also used : Color(java.awt.Color) Rectangle(java.awt.Rectangle) Point2D(java.awt.geom.Point2D) DataPrototype(com.demod.factorio.prototype.DataPrototype) Rectangle2D(java.awt.geom.Rectangle2D) RecipePrototype(com.demod.factorio.prototype.RecipePrototype) BlueprintEntity(com.demod.fbsr.BlueprintEntity) DataTable(com.demod.factorio.DataTable) ArrayList(java.util.ArrayList) RenderUtils(com.demod.fbsr.RenderUtils) Consumer(java.util.function.Consumer) Renderer(com.demod.fbsr.Renderer) Sprite(com.demod.fbsr.Sprite) WorldMap(com.demod.fbsr.WorldMap) EntityRendererFactory(com.demod.fbsr.EntityRendererFactory) List(java.util.List) Utils(com.demod.factorio.Utils) FactorioData(com.demod.factorio.FactorioData) Direction(com.demod.fbsr.Direction) Graphics2D(java.awt.Graphics2D) LuaValue(org.luaj.vm2.LuaValue) Optional(java.util.Optional) EntityPrototype(com.demod.factorio.prototype.EntityPrototype) Layer(com.demod.fbsr.Renderer.Layer) Point2D(java.awt.geom.Point2D) ArrayList(java.util.ArrayList) LuaValue(org.luaj.vm2.LuaValue) Direction(com.demod.fbsr.Direction) RecipePrototype(com.demod.factorio.prototype.RecipePrototype)

Aggregations

DataTable (com.demod.factorio.DataTable)2 FactorioData (com.demod.factorio.FactorioData)2 Utils (com.demod.factorio.Utils)2 DataPrototype (com.demod.factorio.prototype.DataPrototype)2 BlueprintEntity (com.demod.fbsr.BlueprintEntity)2 RenderUtils (com.demod.fbsr.RenderUtils)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 LuaValue (org.luaj.vm2.LuaValue)2 AutoCompleteHandler (com.demod.dcba.AutoCompleteHandler)1 CommandReporting (com.demod.dcba.CommandReporting)1 DCBA (com.demod.dcba.DCBA)1 DiscordBot (com.demod.dcba.DiscordBot)1 EventReply (com.demod.dcba.EventReply)1 MessageCommandEvent (com.demod.dcba.MessageCommandEvent)1 SlashCommandEvent (com.demod.dcba.SlashCommandEvent)1 SlashCommandHandler (com.demod.dcba.SlashCommandHandler)1 Config (com.demod.factorio.Config)1 EntityPrototype (com.demod.factorio.prototype.EntityPrototype)1