Search in sources :

Example 1 with IChunk

use of net.minecraft.world.chunk.IChunk in project minecolonies by Minecolonies.

the class Tree method checkIfInColonyAndNotInBuilding.

/**
 * Calculates with a colony if the position is inside the colony and if it is inside a building.
 *
 * @param pos    the position.
 * @param colony the colony.
 * @param world  the world to use
 * @return return false if not inside the colony or if inside a building.
 */
public static boolean checkIfInColonyAndNotInBuilding(final BlockPos pos, final IColony colony, final IWorldReader world) {
    final IChunk chunk = world.getChunk(pos);
    if (!(chunk instanceof Chunk)) {
        return false;
    }
    final IColonyTagCapability cap = ((Chunk) chunk).getCapability(CLOSE_COLONY_CAP, null).resolve().orElse(null);
    if (cap != null && cap.getOwningColony() != colony.getID()) {
        return false;
    }
    // Dynamic tree's are never part of buildings
    if (Compatibility.isDynamicBlock(world.getBlockState(pos).getBlock())) {
        return true;
    }
    for (final IBuilding building : colony.getBuildingManager().getBuildings().values()) {
        if (building.isInBuilding(pos)) {
            return false;
        }
    }
    return true;
}
Also used : IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IChunk(net.minecraft.world.chunk.IChunk) Chunk(net.minecraft.world.chunk.Chunk) IChunk(net.minecraft.world.chunk.IChunk) IColonyTagCapability(com.minecolonies.api.colony.IColonyTagCapability)

Example 2 with IChunk

use of net.minecraft.world.chunk.IChunk in project Mekanism by mekanism.

the class TurbineValidator method postcheck.

@Override
public FormationResult postcheck(TurbineMultiblockData structure, Set<BlockPos> innerNodes, Long2ObjectMap<IChunk> chunkMap) {
    if (structure.length() % 2 != 1 || structure.width() % 2 != 1) {
        return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_EVEN_LENGTH);
    }
    int centerX = structure.getMinPos().getX() + (structure.length() - 1) / 2;
    int centerZ = structure.getMinPos().getZ() + (structure.width() - 1) / 2;
    BlockPos complex = null;
    Set<BlockPos> turbines = new ObjectOpenHashSet<>();
    Set<BlockPos> dispersers = new ObjectOpenHashSet<>();
    Set<BlockPos> coils = new ObjectOpenHashSet<>();
    Set<BlockPos> condensers = new ObjectOpenHashSet<>();
    // Scan for complex
    for (BlockPos pos : innerNodes) {
        TileEntity tile = WorldUtils.getTileEntity(world, chunkMap, pos);
        if (tile instanceof TileEntityRotationalComplex) {
            if (complex != null || pos.getX() != centerX || pos.getZ() != centerZ) {
                return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_BAD_COMPLEX, pos);
            }
            structure.internalLocations.add(pos);
            complex = pos;
        } else if (tile instanceof TileEntityTurbineRotor) {
            if (pos.getX() != centerX || pos.getZ() != centerZ) {
                return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_BAD_ROTOR, pos);
            }
            turbines.add(pos);
        } else if (tile instanceof TileEntityPressureDisperser) {
            dispersers.add(pos);
        } else if (tile instanceof TileEntityElectromagneticCoil) {
            coils.add(pos);
        } else if (tile instanceof TileEntitySaturatingCondenser) {
            condensers.add(pos);
        }
    }
    // Terminate if complex doesn't exist
    if (complex == null) {
        return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_MISSING_COMPLEX);
    }
    int rotors = complex.getY() - structure.getMinPos().getY() + 1;
    int innerRadius = (Math.min(structure.length(), structure.width()) - 3) / 2;
    if (innerRadius < rotors / 4) {
        return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_TOO_NARROW);
    }
    // Terminate if coils don't exist
    if (coils.isEmpty()) {
        return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_MISSING_COILS);
    }
    BlockPos.Mutable mutablePos = new BlockPos.Mutable();
    // Make sure a flat, horizontal plane of dispersers exists within the multiblock around the complex
    for (int x = complex.getX() - innerRadius; x <= complex.getX() + innerRadius; x++) {
        for (int z = complex.getZ() - innerRadius; z <= complex.getZ() + innerRadius; z++) {
            if (x != centerX || z != centerZ) {
                mutablePos.set(x, complex.getY(), z);
                TileEntityPressureDisperser tile = WorldUtils.getTileEntity(TileEntityPressureDisperser.class, world, chunkMap, mutablePos);
                if (tile == null) {
                    return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_MISSING_DISPERSER, mutablePos);
                }
                dispersers.remove(mutablePos);
            }
        }
    }
    // If any dispersers were not processed, they're in the wrong place
    if (!dispersers.isEmpty()) {
        return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_MALFORMED_DISPERSERS);
    }
    // Make sure all condensers are in proper locations
    for (BlockPos coord : condensers) {
        if (coord.getY() <= complex.getY()) {
            return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_CONDENSER_BELOW_COMPLEX, coord);
        }
    }
    structure.condensers = condensers.size();
    int turbineHeight = 0;
    int blades = 0;
    // Starting from the complex, walk down and count the number of rotors/blades in the structure
    for (int y = complex.getY() - 1; y > structure.getMinPos().getY(); y--) {
        mutablePos.set(centerX, y, centerZ);
        TileEntityTurbineRotor rotor = WorldUtils.getTileEntity(TileEntityTurbineRotor.class, world, chunkMap, mutablePos);
        if (rotor == null) {
            // Not a contiguous set of rotors
            return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_ROTORS_NOT_CONTIGUOUS);
        }
        turbineHeight++;
        blades += rotor.getHousedBlades();
        structure.internalLocations.add(rotor.getBlockPos());
        turbines.remove(mutablePos);
    }
    // If there are any rotors left over, they are in the wrong place in the structure
    if (!turbines.isEmpty()) {
        return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_BAD_ROTORS);
    }
    // Update the structure with number of blades found on rotors
    structure.blades = blades;
    BlockPos startCoord = complex.relative(Direction.UP);
    if (WorldUtils.getTileEntity(TileEntityElectromagneticCoil.class, world, chunkMap, startCoord) != null) {
        structure.coils = FormationProtocol.explore(startCoord, coord -> WorldUtils.getTileEntity(TileEntityElectromagneticCoil.class, world, chunkMap, coord) != null);
    }
    if (coils.size() > structure.coils) {
        return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_MALFORMED_COILS);
    }
    for (BlockPos coord : structure.locations) {
        if (WorldUtils.getTileEntity(TileEntityTurbineVent.class, world, chunkMap, coord) != null) {
            if (coord.getY() < complex.getY()) {
                return FormationResult.fail(GeneratorsLang.TURBINE_INVALID_VENT_BELOW_COMPLEX, coord);
            }
            structure.vents++;
        }
    }
    structure.lowerVolume = structure.length() * structure.width() * turbineHeight;
    structure.complex = complex;
    return FormationResult.SUCCESS;
}
Also used : VoxelCuboid(mekanism.common.lib.math.voxel.VoxelCuboid) TileEntityTurbineRotor(mekanism.generators.common.tile.turbine.TileEntityTurbineRotor) TileEntitySaturatingCondenser(mekanism.generators.common.tile.turbine.TileEntitySaturatingCondenser) Direction(net.minecraft.util.Direction) CasingType(mekanism.common.lib.multiblock.FormationProtocol.CasingType) MekanismBlockTypes(mekanism.common.registries.MekanismBlockTypes) Block(net.minecraft.block.Block) BlockType(mekanism.common.content.blocktype.BlockType) FormationResult(mekanism.common.lib.multiblock.FormationProtocol.FormationResult) BlockState(net.minecraft.block.BlockState) TileEntityElectromagneticCoil(mekanism.generators.common.tile.turbine.TileEntityElectromagneticCoil) TileEntityRotationalComplex(mekanism.generators.common.tile.turbine.TileEntityRotationalComplex) TileEntityTurbineVent(mekanism.generators.common.tile.turbine.TileEntityTurbineVent) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) FormationProtocol(mekanism.common.lib.multiblock.FormationProtocol) CuboidStructureValidator(mekanism.common.lib.multiblock.CuboidStructureValidator) ObjectOpenHashSet(it.unimi.dsi.fastutil.objects.ObjectOpenHashSet) GeneratorsLang(mekanism.generators.common.GeneratorsLang) TileEntityPressureDisperser(mekanism.common.tile.TileEntityPressureDisperser) WorldUtils(mekanism.common.util.WorldUtils) IChunk(net.minecraft.world.chunk.IChunk) TileEntity(net.minecraft.tileentity.TileEntity) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) GeneratorsBlockTypes(mekanism.generators.common.registries.GeneratorsBlockTypes) TileEntityPressureDisperser(mekanism.common.tile.TileEntityPressureDisperser) ObjectOpenHashSet(it.unimi.dsi.fastutil.objects.ObjectOpenHashSet) TileEntityTurbineVent(mekanism.generators.common.tile.turbine.TileEntityTurbineVent) TileEntityElectromagneticCoil(mekanism.generators.common.tile.turbine.TileEntityElectromagneticCoil) TileEntity(net.minecraft.tileentity.TileEntity) TileEntitySaturatingCondenser(mekanism.generators.common.tile.turbine.TileEntitySaturatingCondenser) TileEntityRotationalComplex(mekanism.generators.common.tile.turbine.TileEntityRotationalComplex) BlockPos(net.minecraft.util.math.BlockPos) TileEntityTurbineRotor(mekanism.generators.common.tile.turbine.TileEntityTurbineRotor)

Example 3 with IChunk

use of net.minecraft.world.chunk.IChunk in project Mekanism by mekanism.

the class BuildCommand method destroy.

private static void destroy(World world, BlockPos pos) throws CommandSyntaxException {
    Long2ObjectMap<IChunk> chunkMap = new Long2ObjectOpenHashMap<>();
    if (!isMekanismBlock(world, chunkMap, pos)) {
        // If we didn't hit a mekanism block throw an error that we missed
        throw MISS.create();
    }
    Set<BlockPos> traversed = new HashSet<>();
    Queue<BlockPos> openSet = new ArrayDeque<>();
    openSet.add(pos);
    traversed.add(pos);
    while (!openSet.isEmpty()) {
        BlockPos ptr = openSet.poll();
        if (isMekanismBlock(world, chunkMap, ptr)) {
            world.removeBlock(ptr, false);
            for (Direction side : EnumUtils.DIRECTIONS) {
                BlockPos offset = ptr.relative(side);
                if (traversed.add(offset)) {
                    openSet.add(offset);
                }
            }
        }
    }
}
Also used : IChunk(net.minecraft.world.chunk.IChunk) BlockPos(net.minecraft.util.math.BlockPos) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Direction(net.minecraft.util.Direction) ArrayDeque(java.util.ArrayDeque) HashSet(java.util.HashSet)

Example 4 with IChunk

use of net.minecraft.world.chunk.IChunk in project Mekanism by mekanism.

the class WorldUtils method getChunkForPos.

/**
 * Gets the chunk in a given position or {@code null} if there is no world, the position is out of bounds or the chunk isn't loaded. Tries to retrieve it from our
 * cache and if it isn't found, tries to get it from the world and adds it to our cache.
 *
 * @param world    world
 * @param chunkMap cached chunk map
 * @param pos      position
 *
 * @return The chunk in a given position or {@code null} if there is no world, the position is out of bounds or the chunk isn't loaded
 */
@Nullable
@Contract("null, _, _ -> null")
private static IChunk getChunkForPos(@Nullable IWorld world, @Nonnull Long2ObjectMap<IChunk> chunkMap, @Nonnull BlockPos pos) {
    if (world == null || !World.isInWorldBounds(pos)) {
        // Also short circuit to check if the position is out of bounds before bothering to look up the chunk
        return null;
    }
    int chunkX = pos.getX() >> 4;
    int chunkZ = pos.getZ() >> 4;
    long combinedChunk = (((long) chunkX) << 32) | (chunkZ & 0xFFFFFFFFL);
    // We get the chunk rather than the world, so we can cache the chunk improving the overall
    // performance for retrieving a bunch of chunks in the general vicinity
    IChunk chunk = chunkMap.get(combinedChunk);
    if (chunk == null) {
        // Get the chunk but don't force load it
        chunk = world.getChunk(chunkX, chunkZ, ChunkStatus.FULL, false);
        if (chunk != null) {
            chunkMap.put(combinedChunk, chunk);
        }
    }
    return chunk;
}
Also used : IChunk(net.minecraft.world.chunk.IChunk) Contract(org.jetbrains.annotations.Contract) Nullable(javax.annotation.Nullable)

Example 5 with IChunk

use of net.minecraft.world.chunk.IChunk in project MCMOD-Industria by M-Marvin.

the class ScreenMChunkLoader method mapChunk.

private void mapChunk(ChunkPos pos, World world, int chunkX, int chunkZ) {
    IChunk chunk = world.getChunk(pos.x, pos.z);
    for (int x = 0; x < 16; x++) {
        for (int z = 0; z < 16; z++) {
            int y = chunk.getHeight(Type.WORLD_SURFACE, (pos.x * 16) + x, (pos.z * 16) + z);
            BlockPos blockToMap = new BlockPos((pos.x * 16) + x, y, (pos.z * 16) + z);
            BlockState stateToMap = world.getBlockState(blockToMap);
            MaterialColor color = stateToMap.getMapColor(world, blockToMap);
            this.putColorOnMap(color, chunkX * 16 + x, chunkZ * 16 + z);
        }
    }
}
Also used : BlockState(net.minecraft.block.BlockState) IChunk(net.minecraft.world.chunk.IChunk) MaterialColor(net.minecraft.block.material.MaterialColor) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

IChunk (net.minecraft.world.chunk.IChunk)18 BlockPos (net.minecraft.util.math.BlockPos)5 ChunkPos (net.minecraft.util.math.ChunkPos)5 JsonParseException (com.google.gson.JsonParseException)3 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)3 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)3 CancellationException (java.util.concurrent.CancellationException)3 ExecutionException (java.util.concurrent.ExecutionException)3 BlockState (net.minecraft.block.BlockState)3 CommandException (net.minecraft.command.CommandException)3 Direction (net.minecraft.util.Direction)3 Chunk (net.minecraft.world.chunk.Chunk)3 ChunkStatus (net.minecraft.world.chunk.ChunkStatus)3 ChunkHolder (net.minecraft.world.server.ChunkHolder)3 ServerWorld (net.minecraft.world.server.ServerWorld)3 IColonyTagCapability (com.minecolonies.api.colony.IColonyTagCapability)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 Long2ObjectMap (it.unimi.dsi.fastutil.longs.Long2ObjectMap)2 ObjectOpenHashSet (it.unimi.dsi.fastutil.objects.ObjectOpenHashSet)2 ArrayList (java.util.ArrayList)2