Search in sources :

Example 31 with Blueprint

use of com.ldtteam.structures.blueprints.v1.Blueprint in project Structurize by ldtteam.

the class GenerateAndSaveMessage method onExecute.

@Override
public void onExecute(NetworkEvent.Context ctxIn, boolean isLogicalServer) {
    if (isLogicalServer) {
        final Blueprint blueprint = Manager.getStructureFromFormula(width, length, height, frequency, equation, shape, block, block2, hollow);
        blueprint.rotateWithMirror(BlockPosUtil.getRotationFromRotations(rotation), mirror ? Mirror.FRONT_BACK : Mirror.NONE, ctxIn.getSender().level);
        // in an ideal world, we'd save the original shape and rotate only after the fact.
        // but the client only has a pre-rotated blueprint to calculate the MD5 from...
        final ByteArrayOutputStream stream = new ByteArrayOutputStream();
        BlueprintUtil.writeToStream(stream, blueprint);
        Structures.handleSaveSchematicMessage(stream.toByteArray(), true);
    }
}
Also used : Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 32 with Blueprint

use of com.ldtteam.structures.blueprints.v1.Blueprint in project minecolonies by ldtteam.

the class WorkerUtil method findFirstLevelSign.

/**
 * Find the first level in a structure and return it.
 *
 * @param structure the structure to scan.
 * @return the position of the sign.
 */
@Nullable
public static BlockPos findFirstLevelSign(final Blueprint structure, final BlockPos pos) {
    for (int j = 0; j < structure.getSizeY(); j++) {
        for (int k = 0; k < structure.getSizeZ(); k++) {
            for (int i = 0; i < structure.getSizeX(); i++) {
                @NotNull final BlockPos localPos = new BlockPos(i, j, k);
                final BlockInfo te = structure.getBlockInfoAsMap().get(localPos);
                if (te != null) {
                    final CompoundNBT teData = te.getTileEntityData();
                    if (teData != null && teData.getString(LEVEL_SIGN_FIRST_ROW).equals(LEVEL_SIGN_TEXT)) {
                        // try to make an anchor in 0,0,0 instead of the middle of the structure
                        return pos.subtract(structure.getPrimaryBlockOffset()).offset(localPos);
                    }
                }
            }
        }
    }
    return null;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) BlockInfo(com.ldtteam.structurize.util.BlockInfo) BlockPos(net.minecraft.util.math.BlockPos) NotNull(org.jetbrains.annotations.NotNull) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with Blueprint

use of com.ldtteam.structures.blueprints.v1.Blueprint in project minecolonies by ldtteam.

the class EventStructureManager method spawnTemporaryStructure.

/**
 * Spawns the given structure at the blockpos and saves a backup for the previous blocks.
 *
 * @param structure the structure to spawn.
 * @param eventID   the id of the event.
 */
@Override
public boolean spawnTemporaryStructure(final Blueprint structure, final String schematicPath, final BlockPos targetSpawnPoint, final int eventID, final int rotations, final Mirror mirror) {
    if (eventManager.getEventByID(eventID) == null) {
        return false;
    }
    final World world = colony.getWorld();
    final int y = BlueprintTagUtils.getNumberOfGroundLevels(structure, 4) - 1;
    final BlockPos spawnPos = targetSpawnPoint.offset(0, -y, 0).offset(structure.getPrimaryBlockOffset());
    final BlockPos zeroPos = spawnPos.subtract(structure.getPrimaryBlockOffset());
    final BlockPos cornerPos = new BlockPos(zeroPos.getX() + structure.getSizeX() - 1, zeroPos.getY() + structure.getSizeY(), zeroPos.getZ() + structure.getSizeZ() - 1);
    final BlockPos anchor = new BlockPos(zeroPos.getX() + structure.getSizeX() / 2, zeroPos.getY(), zeroPos.getZ() + structure.getSizeZ() / 2);
    final String backupPath = Structures.SCHEMATICS_PREFIX + "/" + STRUCTURE_BACKUP_FOLDER + "/" + colony.getID() + "/" + colony.getDimension().location().getNamespace() + colony.getDimension().location().getPath() + "/" + anchor;
    if (!ItemScanTool.saveStructureOnServer(world, zeroPos, cornerPos, backupPath, false)) {
        // No structure spawn if we didn't successfully save the surroundings before
        Log.getLogger().info("Failed to save schematics for event");
        return false;
    }
    backupSchematics.put(anchor, eventID);
    CreativeRaiderStructureHandler.loadAndPlaceStructureWithRotation(world, schematicPath, spawnPos, BlockPosUtil.getRotationFromRotations(rotations), mirror, true, colony.getID(), (IColonyRaidEvent) eventManager.getEventByID(eventID), null);
    return true;
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint)

Example 34 with Blueprint

use of com.ldtteam.structures.blueprints.v1.Blueprint in project minecolonies by ldtteam.

the class ClientEventHandler method handleRenderBuildTool.

/**
 * Renders building bounding boxes into the client
 *
 * @param event  The caught event
 * @param world  The world in which to render
 * @param player The player for which to render
 */
private static void handleRenderBuildTool(@NotNull final RenderWorldLastEvent event, final ClientWorld world, final PlayerEntity player) {
    if (Settings.instance.getActiveStructure() == null) {
        return;
    }
    final IColonyView colony = IColonyManager.getInstance().getClosestColonyView(world, new BlockPos(player.position()));
    if (colony == null) {
        return;
    }
    final BlockPos activePosition = Settings.instance.getPosition();
    final Map<BlockPos, Triple<Blueprint, BlockPos, BlockPos>> newCache = new HashMap<>();
    for (final IBuildingView buildingView : colony.getBuildings()) {
        if (MinecoloniesAPIProxy.getInstance().getConfig().getClient().neighborbuildingrendering.get()) {
            if (buildingView.getBuildingType() == ModBuildings.postBox || buildingView.getBuildingType() == ModBuildings.stash) {
                continue;
            }
            final BlockPos currentPosition = buildingView.getPosition();
            if (activePosition.closerThan(currentPosition, PREVIEW_RANGE)) {
                if (blueprintCache.containsKey(currentPosition)) {
                    newCache.put(currentPosition, blueprintCache.get(currentPosition));
                    continue;
                }
                final TileEntity tile = world.getBlockEntity(buildingView.getID());
                String schematicName = buildingView.getSchematicName();
                if (tile instanceof IBlueprintDataProvider) {
                    if (!((IBlueprintDataProvider) tile).getSchematicName().isEmpty()) {
                        schematicName = ((IBlueprintDataProvider) tile).getSchematicName().replaceAll("\\d$", "");
                    }
                }
                final StructureName sn = new StructureName(Structures.SCHEMATICS_PREFIX, buildingView.getStyle(), schematicName + buildingView.getBuildingMaxLevel());
                final String structureName = sn.toString();
                final String md5 = Structures.getMD5(structureName);
                final IStructureHandler wrapper = new LoadOnlyStructureHandler(world, buildingView.getID(), structureName, new PlacementSettings(), true);
                if (!wrapper.hasBluePrint() || !wrapper.isCorrectMD5(md5)) {
                    if (alreadyRequestedStructures.contains(structureName)) {
                        continue;
                    }
                    alreadyRequestedStructures.add(structureName);
                    Log.getLogger().error("Couldn't find schematic: " + structureName + " requesting to server if possible.");
                    if (ServerLifecycleHooks.getCurrentServer() == null) {
                        Network.getNetwork().sendToServer(new SchematicRequestMessage(structureName));
                    }
                    continue;
                }
                final Blueprint blueprint = wrapper.getBluePrint();
                final Mirror mirror = buildingView.isMirrored() ? Mirror.FRONT_BACK : Mirror.NONE;
                blueprint.rotateWithMirror(BlockPosUtil.getRotationFromRotations(buildingView.getRotation()), mirror, world);
                final BlockPos primaryOffset = blueprint.getPrimaryBlockOffset();
                final BlockPos boxStartPos = currentPosition.subtract(primaryOffset);
                final BlockPos size = new BlockPos(blueprint.getSizeX(), blueprint.getSizeY(), blueprint.getSizeZ());
                final BlockPos boxEndPos = boxStartPos.offset(size).subtract(new BlockPos(1, 1, 1));
                blueprint.setRenderSource(buildingView.getID());
                if (buildingView.getBuildingLevel() < buildingView.getBuildingMaxLevel()) {
                    newCache.put(currentPosition, new Triple(blueprint, boxStartPos, boxEndPos));
                } else {
                    newCache.put(currentPosition, new Triple<>(null, boxStartPos, boxEndPos));
                }
            }
        }
    }
    blueprintCache = newCache;
    for (final Map.Entry<BlockPos, Triple<Blueprint, BlockPos, BlockPos>> nearbyBuilding : blueprintCache.entrySet()) {
        final Triple<Blueprint, BlockPos, BlockPos> buildingData = nearbyBuilding.getValue();
        final BlockPos position = nearbyBuilding.getKey();
        if (buildingData.a != null) {
            StructureClientHandler.renderStructureAtPos(buildingData.a, event.getPartialTicks(), position, event.getMatrixStack());
        }
        RenderUtils.renderBox(buildingData.b, buildingData.c, 0, 0, 1, 1.0F, 0.002D, event.getMatrixStack(), linesWithCullAndDepth.get());
    }
}
Also used : IBlueprintDataProvider(com.ldtteam.structurize.blocks.interfaces.IBlueprintDataProvider) IStructureHandler(com.ldtteam.structurize.placement.structure.IStructureHandler) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) StructureName(com.ldtteam.structurize.management.StructureName) PlacementSettings(com.ldtteam.structurize.util.PlacementSettings) Triple(org.antlr.v4.runtime.misc.Triple) TileEntity(net.minecraft.tileentity.TileEntity) SchematicRequestMessage(com.ldtteam.structurize.network.messages.SchematicRequestMessage) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) BlockPos(net.minecraft.util.math.BlockPos) LoadOnlyStructureHandler(com.minecolonies.api.util.LoadOnlyStructureHandler) IColonyView(com.minecolonies.api.colony.IColonyView) Mirror(net.minecraft.util.Mirror) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 35 with Blueprint

use of com.ldtteam.structures.blueprints.v1.Blueprint in project minecolonies by ldtteam.

the class AbstractSchematicProvider method getRotation.

@Override
public int getRotation() {
    if (cachedRotation != -1) {
        return cachedRotation;
    }
    final StructureName structureName = new StructureName(Structures.SCHEMATICS_PREFIX, style, this.getSchematicName() + Math.max(1, buildingLevel));
    try {
        final LoadOnlyStructureHandler structure = new LoadOnlyStructureHandler(colony.getWorld(), getPosition(), structureName.toString(), new PlacementSettings(), true);
        final Blueprint blueprint = structure.getBluePrint();
        if (blueprint != null) {
            final BlockState structureState = structure.getBluePrint().getBlockInfoAsMap().get(structure.getBluePrint().getPrimaryBlockOffset()).getState();
            if (structureState != null) {
                if (!(structureState.getBlock() instanceof AbstractBlockHut) || !(colony.getWorld().getBlockState(this.location).getBlock() instanceof AbstractBlockHut)) {
                    Log.getLogger().error(String.format("Schematic %s doesn't have a correct Primary Offset", structureName.toString()));
                    return 0;
                }
                final int structureRotation = structureState.getValue(AbstractBlockHut.FACING).get2DDataValue();
                final int worldRotation = colony.getWorld().getBlockState(this.location).getValue(AbstractBlockHut.FACING).get2DDataValue();
                if (structureRotation <= worldRotation) {
                    cachedRotation = worldRotation - structureRotation;
                } else {
                    cachedRotation = 4 + worldRotation - structureRotation;
                }
                return cachedRotation;
            }
        }
    } catch (Exception e) {
        Log.getLogger().error(String.format("Failed to get rotation for %s: ", structureName.toString()), e);
        return 0;
    }
    return 0;
}
Also used : BlockState(net.minecraft.block.BlockState) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) StructureName(com.ldtteam.structurize.management.StructureName) LoadOnlyStructureHandler(com.minecolonies.api.util.LoadOnlyStructureHandler) PlacementSettings(com.ldtteam.structurize.util.PlacementSettings) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) IAltersBuildingFootprint(com.minecolonies.api.colony.buildings.modules.IAltersBuildingFootprint) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint)

Aggregations

Blueprint (com.ldtteam.structures.blueprints.v1.Blueprint)37 BlockPos (net.minecraft.util.math.BlockPos)23 BlockState (net.minecraft.block.BlockState)13 StructureName (com.ldtteam.structurize.management.StructureName)8 PlacementSettings (com.ldtteam.structurize.util.PlacementSettings)8 LoadOnlyStructureHandler (com.minecolonies.api.util.LoadOnlyStructureHandler)6 PlayerEntity (net.minecraft.entity.player.PlayerEntity)6 TileEntity (net.minecraft.tileentity.TileEntity)6 Nullable (org.jetbrains.annotations.Nullable)6 BlockInfo (com.ldtteam.structurize.util.BlockInfo)5 NotNull (org.jetbrains.annotations.NotNull)5 IBlueprintDataProvider (com.ldtteam.structurize.blocks.interfaces.IBlueprintDataProvider)4 World (net.minecraft.world.World)4 Optional (java.util.Optional)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 SchematicRequestMessage (com.ldtteam.structurize.network.messages.SchematicRequestMessage)2 PlacementError (com.ldtteam.structurize.placement.handlers.placement.PlacementError)2 IStructureHandler (com.ldtteam.structurize.placement.structure.IStructureHandler)2 AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)2 IColony (com.minecolonies.api.colony.IColony)2