Search in sources :

Example 6 with StructureName

use of com.ldtteam.structurize.management.StructureName in project Structurize by ldtteam.

the class ItemScanTool method saveStructureOnServer.

/**
 * Save a structure on the server.
 *
 * @param world        the world.
 * @param from         the start position.
 * @param to           the end position.
 * @param name         the name.
 * @param saveEntities whether to scan in entities
 * @return true if succesful.
 */
public static boolean saveStructureOnServer(@NotNull final World world, @NotNull final BlockPos from, @NotNull final BlockPos to, final String name, final boolean saveEntities) {
    final BlockPos blockpos = new BlockPos(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ()));
    final BlockPos blockpos1 = new BlockPos(Math.max(from.getX(), to.getX()), Math.max(from.getY(), to.getY()), Math.max(from.getZ(), to.getZ()));
    final BlockPos size = blockpos1.subtract(blockpos).offset(1, 1, 1);
    if (size.getX() * size.getY() * size.getZ() > Structurize.getConfig().getServer().schematicBlockLimit.get()) {
        Log.getLogger().warn("Saving too large schematic for:" + name);
    }
    final String prefix = "cache";
    final String fileName;
    if (name == null || name.isEmpty()) {
        fileName = LanguageHandler.format("item.sceptersteel.scanformat");
    } else {
        fileName = name;
    }
    final StructureName structureName = new StructureName(prefix, "backup", fileName);
    final List<File> folder = StructureLoadingUtils.getCachedSchematicsFolders();
    if (folder == null || folder.isEmpty()) {
        Log.getLogger().warn("Unable to save schematic in cache since no folder was found.");
        return false;
    }
    final Blueprint bp = BlueprintUtil.createBlueprint(world, blockpos, saveEntities, (short) size.getX(), (short) size.getY(), (short) size.getZ(), name, Optional.empty());
    final File file = new File(folder.get(0), structureName.toString() + Structures.SCHEMATIC_EXTENSION_NEW);
    Utils.checkDirectory(file.getParentFile());
    try (OutputStream outputstream = new FileOutputStream(file)) {
        CompressedStreamTools.writeCompressed(BlueprintUtil.writeBlueprintToNBT(bp), outputstream);
    } catch (final Exception e) {
        return false;
    }
    return true;
}
Also used : Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) StructureName(com.ldtteam.structurize.management.StructureName) BlockPos(net.minecraft.util.math.BlockPos) File(java.io.File)

Example 7 with StructureName

use of com.ldtteam.structurize.management.StructureName in project Structurize by ldtteam.

the class StructureLoadingUtils method getStream.

/**
 * get a InputStream for a give structureName.
 * <p>
 * Look into the following director (in order):
 * - scan
 * - cache
 * - schematics folder
 * - jar
 * It should be the exact opposite that the way used to build the list.
 * <p>
 * Suppressing Sonar Rule squid:S2095
 * This rule enforces "Close this InputStream"
 * But in this case the rule does not apply because
 * We are returning the stream and that is reasonable
 *
 * @param structureName name of the structure to load
 * @return the input stream or null
 */
@SuppressWarnings(RESOURCES_SHOULD_BE_CLOSED)
@Nullable
public static InputStream getStream(final String structureName) {
    final StructureName sn = new StructureName(structureName);
    InputStream inputstream = null;
    if (Structures.SCHEMATICS_CACHE.equals(sn.getPrefix())) {
        for (final File cachedFile : StructureLoadingUtils.getCachedSchematicsFolders()) {
            final InputStream stream = StructureLoadingUtils.getStreamFromFolder(cachedFile, structureName);
            if (stream != null) {
                return stream;
            }
        }
    } else if (Structures.SCHEMATICS_SCAN.equals(sn.getPrefix()) && FMLEnvironment.dist == Dist.CLIENT) {
        for (final File cachedFile : StructureLoadingUtils.getClientSchematicsFolders()) {
            final InputStream stream = StructureLoadingUtils.getStreamFromFolder(cachedFile, structureName);
            if (stream != null) {
                return stream;
            }
        }
    } else if (!Structures.SCHEMATICS_PREFIX.equals(sn.getPrefix())) {
        return null;
    } else {
        // Look in the folder first
        inputstream = StructureLoadingUtils.getStreamFromFolder(Structurize.proxy.getSchematicsFolder(), structureName);
        if (inputstream == null && !Structurize.getConfig().getServer().ignoreSchematicsFromJar.get()) {
            inputstream = getStreamFromJar(structureName);
        }
    }
    return inputstream;
}
Also used : StructureName(com.ldtteam.structurize.management.StructureName) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with StructureName

use of com.ldtteam.structurize.management.StructureName in project minecolonies by ldtteam.

the class EventStructureManager method loadBackupForEvent.

@Override
public void loadBackupForEvent(final int eventID) {
    final Iterator<Map.Entry<BlockPos, Integer>> iterator = backupSchematics.entrySet().iterator();
    while (iterator.hasNext()) {
        final Map.Entry<BlockPos, Integer> entry = iterator.next();
        if (entry.getValue() == eventID) {
            final String oldBackupPath = String.valueOf(colony.getID()) + colony.getDimension() + entry.getKey();
            String fileName = new StructureName("cache", "backup", Structures.SCHEMATICS_PREFIX + "/" + STRUCTURE_BACKUP_FOLDER).toString() + "/" + String.valueOf(colony.getID()) + "/" + colony.getDimension().location().getNamespace() + colony.getDimension().location().getPath() + "/" + entry.getKey();
            // TODO: remove compat for colony.getDimension()-based file names after sufficient time has passed from PR#6305
            if (CreativeBuildingStructureHandler.loadAndPlaceStructureWithRotation(colony.getWorld(), fileName, entry.getKey(), Rotation.NONE, Mirror.NONE, true, null) == null) {
                fileName = new StructureName("cache", "backup", Structures.SCHEMATICS_PREFIX + STRUCTURE_BACKUP_FOLDER).toString() + oldBackupPath;
                CreativeBuildingStructureHandler.loadAndPlaceStructureWithRotation(colony.getWorld(), fileName, entry.getKey(), Rotation.NONE, Mirror.NONE, true, null);
            }
            try {
                Structurize.proxy.getSchematicsFolder().toPath().resolve(fileName + SCHEMATIC_EXTENSION_NEW).toFile().delete();
            } catch (Exception e) {
                Log.getLogger().info("Minor issue: Failed at deleting a backup schematic at " + fileName, e);
            }
            iterator.remove();
        }
    }
}
Also used : StructureName(com.ldtteam.structurize.management.StructureName) BlockPos(net.minecraft.util.math.BlockPos) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with StructureName

use of com.ldtteam.structurize.management.StructureName in project minecolonies by ldtteam.

the class WorkOrderBuildDecoration method read.

/**
 * Read the WorkOrder data from the CompoundNBT.
 *
 * @param compound NBT Tag compound.
 */
@Override
public void read(@NotNull final CompoundNBT compound, final IWorkManager manager) {
    super.read(compound, manager);
    final StructureName sn = new StructureName(compound.getString(TAG_SCHEMATIC_NAME));
    structureName = sn.toString();
    workOrderName = compound.getString(TAG_WORKORDER_NAME);
    cleared = compound.getBoolean(TAG_IS_CLEARED);
    buildingRotation = compound.getInt(TAG_BUILDING_ROTATION);
    requested = compound.getBoolean(TAG_IS_REQUESTED);
    isBuildingMirrored = compound.getBoolean(TAG_IS_MIRRORED);
    amountOfRes = compound.getInt(TAG_AMOUNT_OF_RES);
    levelUp = compound.getBoolean(TAG_LEVEL);
}
Also used : StructureName(com.ldtteam.structurize.management.StructureName)

Example 10 with StructureName

use of com.ldtteam.structurize.management.StructureName in project minecolonies by ldtteam.

the class WindowBuildBuilding method updateResources.

/**
 * Clears and resets/updates all resources.
 */
private void updateResources() {
    if (stylesDropDownList.getSelectedIndex() == -1) {
        return;
    }
    final World world = Minecraft.getInstance().level;
    resources.clear();
    final IBuildingView parentBuilding = building.getColony().getBuilding(building.getParent());
    int nextLevel = building.getBuildingLevel();
    if (building.getBuildingLevel() < building.getBuildingMaxLevel() && (parentBuilding == null || building.getBuildingLevel() < parentBuilding.getBuildingLevel())) {
        nextLevel = building.getBuildingLevel() + 1;
    }
    final TileEntity tile = world.getBlockEntity(building.getID());
    String schematicName = building.getSchematicName();
    if (tile instanceof IBlueprintDataProvider) {
        if (!((IBlueprintDataProvider) tile).getSchematicName().isEmpty()) {
            schematicName = ((IBlueprintDataProvider) tile).getSchematicName().replaceAll("\\d$", "");
        }
    }
    final StructureName sn = new StructureName(Structures.SCHEMATICS_PREFIX, styles.get(stylesDropDownList.getSelectedIndex()), schematicName + nextLevel);
    final LoadOnlyStructureHandler structure = new LoadOnlyStructureHandler(world, building.getPosition(), sn.toString(), new PlacementSettings(), true);
    final String md5 = Structures.getMD5(sn.toString());
    if (!structure.hasBluePrint() || !structure.isCorrectMD5(md5)) {
        if (!structure.hasBluePrint()) {
            Log.getLogger().info("Template structure " + sn + " missing");
        } else {
            Log.getLogger().info("structure " + sn + " md5 error");
        }
        Log.getLogger().info("Request To Server for structure " + sn);
        if (ServerLifecycleHooks.getCurrentServer() == null) {
            com.ldtteam.structurize.Network.getNetwork().sendToServer(new SchematicRequestMessage(sn.toString()));
            return;
        } else {
            Log.getLogger().error("WindowMinecoloniesBuildTool: Need to download schematic on a standalone client/server. This should never happen", new Exception());
        }
    }
    if (!structure.hasBluePrint()) {
        findPaneOfTypeByID(BUTTON_BUILD, Button.class).hide();
        findPaneOfTypeByID(BUTTON_REPAIR, Button.class).hide();
        findPaneOfTypeByID(BUTTON_PICKUP_BUILDING, Button.class).show();
        return;
    }
    structure.getBluePrint().rotateWithMirror(BlockPosUtil.getRotationFromRotations(building.getRotation()), building.isMirrored() ? Mirror.FRONT_BACK : Mirror.NONE, world);
    StructurePlacer placer = new StructurePlacer(structure);
    StructurePhasePlacementResult result;
    BlockPos progressPos = NULL_POS;
    do {
        result = placer.executeStructureStep(world, null, progressPos, StructurePlacer.Operation.GET_RES_REQUIREMENTS, () -> placer.getIterator().increment(DONT_TOUCH_PREDICATE.and((info, pos, handler) -> false)), true);
        progressPos = result.getIteratorPos();
        for (final ItemStack stack : result.getBlockResult().getRequiredItems()) {
            addNeededResource(stack, stack.getCount());
        }
    } while (result != null && result.getBlockResult().getResult() != BlockPlacementResult.Result.FINISHED);
    window.findPaneOfTypeByID(LIST_RESOURCES, ScrollingList.class).refreshElementPanes();
    updateResourceList();
}
Also used : LanguageHandler(com.ldtteam.structurize.util.LanguageHandler) DropDownList(com.ldtteam.blockout.views.DropDownList) Structures(com.ldtteam.structurize.management.Structures) IBuilderUndestroyable(com.minecolonies.api.entity.ai.citizen.builder.IBuilderUndestroyable) ScrollingList(com.ldtteam.blockout.views.ScrollingList) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) TriPredicate(net.minecraftforge.common.util.TriPredicate) ServerLifecycleHooks(net.minecraftforge.fml.server.ServerLifecycleHooks) Settings(com.ldtteam.structures.helpers.Settings) Network(com.minecolonies.coremod.Network) Log(com.minecolonies.api.util.Log) BlockState(net.minecraft.block.BlockState) Constants(com.minecolonies.api.util.constant.Constants) Color(com.ldtteam.blockout.Color) BuildPickUpMessage(com.minecolonies.coremod.network.messages.server.colony.building.BuildPickUpMessage) StructureName(com.ldtteam.structurize.management.StructureName) BuildingSetStyleMessage(com.minecolonies.coremod.network.messages.server.colony.building.BuildingSetStyleMessage) BlueprintPositionInfo(com.ldtteam.structurize.util.BlueprintPositionInfo) PlacementSettings(com.ldtteam.structurize.util.PlacementSettings) Collectors(java.util.stream.Collectors) Text(com.ldtteam.blockout.controls.Text) Nullable(org.jetbrains.annotations.Nullable) IStructureHandler(com.ldtteam.structurize.placement.structure.IStructureHandler) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) StructurePlacer(com.ldtteam.structurize.placement.StructurePlacer) java.util(java.util) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) LoadOnlyStructureHandler(com.minecolonies.api.util.LoadOnlyStructureHandler) Mirror(net.minecraft.util.Mirror) ItemIcon(com.ldtteam.blockout.controls.ItemIcon) ItemStack(net.minecraft.item.ItemStack) Minecraft(net.minecraft.client.Minecraft) BuildRequestMessage(com.minecolonies.coremod.network.messages.server.colony.building.BuildRequestMessage) ModJobs(com.minecolonies.api.colony.jobs.ModJobs) SchematicRequestMessage(com.ldtteam.structurize.network.messages.SchematicRequestMessage) StructurePhasePlacementResult(com.ldtteam.structurize.placement.StructurePhasePlacementResult) IBlueprintDataProvider(com.ldtteam.structurize.blocks.interfaces.IBlueprintDataProvider) IColonyView(com.minecolonies.api.colony.IColonyView) AbstractBuildingBuilderView(com.minecolonies.coremod.colony.buildings.views.AbstractBuildingBuilderView) World(net.minecraft.world.World) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) Tuple(net.minecraft.util.Tuple) BlockPos(net.minecraft.util.math.BlockPos) NULL_POS(com.ldtteam.structurize.placement.AbstractBlueprintIterator.NULL_POS) Pane(com.ldtteam.blockout.Pane) BlockPosUtil(com.minecolonies.api.util.BlockPosUtil) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) Blocks(net.minecraft.block.Blocks) ModBuildings(com.minecolonies.api.colony.buildings.ModBuildings) Button(com.ldtteam.blockout.controls.Button) WindowConstants(com.minecolonies.api.util.constant.WindowConstants) BlockPlacementResult(com.ldtteam.structurize.placement.BlockPlacementResult) TileEntity(net.minecraft.tileentity.TileEntity) IBlueprintDataProvider(com.ldtteam.structurize.blocks.interfaces.IBlueprintDataProvider) StructureName(com.ldtteam.structurize.management.StructureName) World(net.minecraft.world.World) PlacementSettings(com.ldtteam.structurize.util.PlacementSettings) TileEntity(net.minecraft.tileentity.TileEntity) SchematicRequestMessage(com.ldtteam.structurize.network.messages.SchematicRequestMessage) Button(com.ldtteam.blockout.controls.Button) StructurePhasePlacementResult(com.ldtteam.structurize.placement.StructurePhasePlacementResult) StructurePlacer(com.ldtteam.structurize.placement.StructurePlacer) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) BlockPos(net.minecraft.util.math.BlockPos) LoadOnlyStructureHandler(com.minecolonies.api.util.LoadOnlyStructureHandler) ItemStack(net.minecraft.item.ItemStack) ScrollingList(com.ldtteam.blockout.views.ScrollingList)

Aggregations

StructureName (com.ldtteam.structurize.management.StructureName)43 Blueprint (com.ldtteam.structures.blueprints.v1.Blueprint)14 PlacementSettings (com.ldtteam.structurize.util.PlacementSettings)10 BlockPos (net.minecraft.util.math.BlockPos)9 LoadOnlyStructureHandler (com.minecolonies.api.util.LoadOnlyStructureHandler)7 Nullable (org.jetbrains.annotations.Nullable)6 SchematicRequestMessage (com.ldtteam.structurize.network.messages.SchematicRequestMessage)5 IStructureHandler (com.ldtteam.structurize.placement.structure.IStructureHandler)5 TileEntity (net.minecraft.tileentity.TileEntity)5 DropDownList (com.ldtteam.blockout.views.DropDownList)4 IBlueprintDataProvider (com.ldtteam.structurize.blocks.interfaces.IBlueprintDataProvider)4 AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)4 IColonyView (com.minecolonies.api.colony.IColonyView)4 IBuildingView (com.minecolonies.api.colony.buildings.views.IBuildingView)4 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)4 StringTextComponent (net.minecraft.util.text.StringTextComponent)4 Button (com.ldtteam.blockout.controls.Button)3 IAltersBuildingFootprint (com.minecolonies.api.colony.buildings.modules.IAltersBuildingFootprint)3 Log (com.minecolonies.api.util.Log)3 Predicate (java.util.function.Predicate)3