Search in sources :

Example 1 with WorldChunk

use of net.minecraft.world.chunk.WorldChunk in project roadrunner by MaxNeedsSnacks.

the class WorldHelper method getEntitiesOfClassGroup.

/**
 * Method that allows getting entities of a class group.
 * [VanillaCopy] but custom combination of: get class filtered entities together with excluding one entity
 */
public static List<Entity> getEntitiesOfClassGroup(World world, Entity excluded, EntityClassGroup type, Box box, Predicate<Entity> predicate) {
    world.getProfiler().visit("getEntities");
    int minChunkX = MathHelper.floor((box.minX - 2.0D) / 16.0D);
    int maxChunkX = MathHelper.ceil((box.maxX + 2.0D) / 16.0D);
    int minChunkZ = MathHelper.floor((box.minZ - 2.0D) / 16.0D);
    int maxChunkZ = MathHelper.ceil((box.maxZ + 2.0D) / 16.0D);
    List<Entity> entities = Lists.newArrayList();
    ChunkManager chunkManager = world.getChunkManager();
    for (int chunkX = minChunkX; chunkX < maxChunkX; chunkX++) {
        for (int chunkZ = minChunkZ; chunkZ < maxChunkZ; chunkZ++) {
            WorldChunk chunk = chunkManager.getWorldChunk(chunkX, chunkZ, false);
            if (chunk != null) {
                WorldHelper.getEntitiesOfClassGroup(chunk, excluded, type, box, entities, predicate);
            }
        }
    }
    return entities;
}
Also used : Entity(net.minecraft.entity.Entity) WorldChunk(net.minecraft.world.chunk.WorldChunk) ChunkManager(net.minecraft.world.chunk.ChunkManager)

Example 2 with WorldChunk

use of net.minecraft.world.chunk.WorldChunk in project MasaGadget by plusls.

the class SearchMobSpawnPointUtil method search.

public static void search() {
    ClientWorld world = MinecraftClient.getInstance().world;
    ClientPlayerEntity player = MinecraftClient.getInstance().player;
    if (world == null || player == null) {
        return;
    }
    ShapeDespawnSphere shapeDespawnSphere = getShapeDespawnSphere();
    if (shapeDespawnSphere == null) {
        return;
    }
    Vec3d centerPos = shapeDespawnSphere.getCenter();
    BlockPos pos = new BlockPos((int) centerPos.x, (int) centerPos.y, (int) centerPos.z);
    ModInfo.LOGGER.warn("shape: {}", shapeDespawnSphere.getCenter());
    BlockPos spawnPos = null;
    int maxX = pos.getX() + 129;
    int maxZ = pos.getZ() + 129;
    BlockPos.Mutable currentPos = new BlockPos.Mutable();
    int maxSpawnLightLevel = fi.dy.masa.minihud.config.Configs.Generic.LIGHT_LEVEL_THRESHOLD_SAFE.getIntegerValue();
    LightingProvider lightingProvider = world.getChunkManager().getLightingProvider();
    EntityType<?> entityType = world.getDimension().isUltrawarm() ? EntityType.ZOMBIFIED_PIGLIN : EntityType.CREEPER;
    EntityType<?> entityType2 = world.getDimension().isUltrawarm() ? null : EntityType.SPIDER;
    for (int x = pos.getX() - 129; x <= maxX; ++x) {
        for (int z = pos.getZ() - 129; z <= maxZ; ++z) {
            WorldChunk chunk = world.getChunk(x >> 4, z >> 4);
            if (chunk == null) {
                continue;
            }
            int maxY = Math.min(pos.getY() + 129, chunk.sampleHeightmap(Heightmap.Type.WORLD_SURFACE, x, z) + 1);
            for (int y = Math.max(pos.getY() - 129, world.getBottomY() + 1); y <= maxY; ++y) {
                if (squaredDistanceTo(x, y, z, centerPos) > 16384) {
                    if (y > centerPos.y) {
                        break;
                    } else {
                        continue;
                    }
                } else if (spawnPos != null && player.squaredDistanceTo(x, y, z) > player.squaredDistanceTo(spawnPos.getX(), spawnPos.getY(), spawnPos.getZ())) {
                    continue;
                }
                currentPos.set(x, y, z);
                if (SpawnHelper.canSpawn(SpawnRestriction.getLocation(entityType), world, currentPos, entityType) && lightingProvider.get(LightType.BLOCK).getLightLevel(currentPos) < maxSpawnLightLevel) {
                    Block block = world.getBlockState(currentPos.down()).getBlock();
                    String blockId = Registry.BLOCK.getId(world.getBlockState(currentPos.down()).getBlock()).toString();
                    String blockName = block.getName().getString();
                    if (Configs.Generic.SEARCH_MOB_SPAWN_POINT_BLACK_LIST.getStrings().stream().noneMatch(s -> blockId.contains(s) || blockName.contains(s))) {
                        if (world.isSpaceEmpty(entityType.createSimpleBoundingBox(currentPos.getX() + 0.5D, currentPos.getY(), currentPos.getZ() + 0.5D))) {
                            spawnPos = currentPos.mutableCopy();
                        } else if (entityType2 != null && world.isSpaceEmpty(entityType2.createSimpleBoundingBox(currentPos.getX() + 0.5D, currentPos.getY(), currentPos.getZ() + 0.5D))) {
                            spawnPos = currentPos.mutableCopy();
                        }
                    }
                }
            }
        }
    }
    Text text;
    if (spawnPos == null) {
        text = new TranslatableText("masa_gadget_mod.message.noBlockCanSpawn").setStyle(Style.EMPTY.withColor(TextColor.fromFormatting(Formatting.GREEN)));
    } else {
        // for ommc parser
        text = new LiteralText(I18n.translate("masa_gadget_mod.message.spawnPos", spawnPos.getX(), spawnPos.getY(), spawnPos.getZ()));
        player.sendChatMessage(String.format("/highlightWaypoint %d %d %d", spawnPos.getX(), spawnPos.getY(), spawnPos.getZ()));
    }
    Objects.requireNonNull(MinecraftClient.getInstance().player).sendMessage(text, false);
}
Also used : Vec3d(net.minecraft.util.math.Vec3d) WorldChunk(net.minecraft.world.chunk.WorldChunk) LightingProvider(net.minecraft.world.chunk.light.LightingProvider) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) ClientWorld(net.minecraft.client.world.ClientWorld) ClientPlayerEntity(net.minecraft.client.network.ClientPlayerEntity) ShapeDespawnSphere(fi.dy.masa.minihud.renderer.shapes.ShapeDespawnSphere)

Example 3 with WorldChunk

use of net.minecraft.world.chunk.WorldChunk in project meteor-rejects by AntiCope.

the class NewChunks method onReadPacket.

@EventHandler
private void onReadPacket(PacketEvent.Receive event) {
    if (event.packet instanceof ChunkDeltaUpdateS2CPacket) {
        ChunkDeltaUpdateS2CPacket packet = (ChunkDeltaUpdateS2CPacket) event.packet;
        packet.visitUpdates((pos, state) -> {
            if (!state.getFluidState().isEmpty() && !state.getFluidState().isStill()) {
                ChunkPos chunkPos = new ChunkPos(pos);
                for (Direction dir : searchDirs) {
                    if (mc.world.getBlockState(pos.offset(dir)).getFluidState().isStill() && !oldChunks.contains(chunkPos)) {
                        newChunks.add(chunkPos);
                        return;
                    }
                }
            }
        });
    } else if (event.packet instanceof BlockUpdateS2CPacket) {
        BlockUpdateS2CPacket packet = (BlockUpdateS2CPacket) event.packet;
        if (!packet.getState().getFluidState().isEmpty() && !packet.getState().getFluidState().isStill()) {
            ChunkPos chunkPos = new ChunkPos(packet.getPos());
            for (Direction dir : searchDirs) {
                if (mc.world.getBlockState(packet.getPos().offset(dir)).getFluidState().isStill() && !oldChunks.contains(chunkPos)) {
                    newChunks.add(chunkPos);
                    return;
                }
            }
        }
    } else if (event.packet instanceof ChunkDataS2CPacket && mc.world != null) {
        ChunkDataS2CPacket packet = (ChunkDataS2CPacket) event.packet;
        ChunkPos pos = new ChunkPos(packet.getX(), packet.getZ());
        if (!newChunks.contains(pos) && mc.world.getChunkManager().getChunk(packet.getX(), packet.getZ()) == null) {
            WorldChunk chunk = new WorldChunk(mc.world, pos);
            try {
                chunk.loadFromPacket(packet.getChunkData().getSectionsDataBuf(), new NbtCompound(), packet.getChunkData().getBlockEntities(packet.getX(), packet.getZ()));
            } catch (ArrayIndexOutOfBoundsException e) {
                return;
            }
            for (int x = 0; x < 16; x++) {
                for (int y = mc.world.getBottomY(); y < mc.world.getTopY(); y++) {
                    for (int z = 0; z < 16; z++) {
                        FluidState fluid = chunk.getFluidState(x, y, z);
                        if (!fluid.isEmpty() && !fluid.isStill()) {
                            oldChunks.add(pos);
                            return;
                        }
                    }
                }
            }
        }
    }
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) WorldChunk(net.minecraft.world.chunk.WorldChunk) FluidState(net.minecraft.fluid.FluidState) EventHandler(meteordevelopment.orbit.EventHandler)

Example 4 with WorldChunk

use of net.minecraft.world.chunk.WorldChunk in project lithium-fabric by CaffeineMC.

the class RedstoneWireBlockMixin method getPowerFromSide.

/**
 * Calculate the redstone power a wire receives from blocks next to it.
 */
private int getPowerFromSide(World world, BlockPos pos, Direction toDir, boolean checkWiresAbove) {
    WorldChunk chunk = world.getWorldChunk(pos);
    BlockState state = chunk.getBlockState(pos);
    if (state.isOf(this)) {
        return state.get(Properties.POWER) - 1;
    }
    int power = state.getWeakRedstonePower(world, pos, toDir);
    if (power >= MAX) {
        return MAX;
    }
    if (state.isSolidBlock(world, pos)) {
        power = Math.max(power, this.getStrongPowerTo(world, pos, toDir.getOpposite()));
        if (power >= MAX) {
            return MAX;
        }
        if (checkWiresAbove && power < MAX_WIRE) {
            BlockPos up = pos.up();
            BlockState aboveState = chunk.getBlockState(up);
            if (aboveState.isOf(this)) {
                power = Math.max(power, aboveState.get(Properties.POWER) - 1);
            }
        }
    } else if (power < MAX_WIRE) {
        BlockPos down = pos.down();
        BlockState belowState = chunk.getBlockState(down);
        if (belowState.isOf(this)) {
            power = Math.max(power, belowState.get(Properties.POWER) - 1);
        }
    }
    return power;
}
Also used : BlockState(net.minecraft.block.BlockState) WorldChunk(net.minecraft.world.chunk.WorldChunk) BlockPos(net.minecraft.util.math.BlockPos)

Example 5 with WorldChunk

use of net.minecraft.world.chunk.WorldChunk in project lithium-fabric by CaffeineMC.

the class WorldMixin method getBlockState.

/**
 * @reason Reduce method size to help the JVM inline, Avoid excess height limit checks
 * @author 2No2Name
 */
@Overwrite
public BlockState getBlockState(BlockPos pos) {
    WorldChunk worldChunk = this.getChunk(ChunkSectionPos.getSectionCoord(pos.getX()), ChunkSectionPos.getSectionCoord(pos.getZ()));
    ChunkSection[] sections = worldChunk.getSectionArray();
    int x = pos.getX();
    int y = pos.getY();
    int z = pos.getZ();
    int chunkY = this.getSectionIndex(y);
    if (chunkY < 0 || chunkY >= sections.length) {
        return OUTSIDE_WORLD_BLOCK;
    }
    ChunkSection section = sections[chunkY];
    if (section == null || section.isEmpty()) {
        return INSIDE_WORLD_DEFAULT_BLOCK;
    }
    return section.getBlockState(x & 15, y & 15, z & 15);
// This code path is slower than with the extra world height limit check. Tradeoff in favor of the default path.
}
Also used : WorldChunk(net.minecraft.world.chunk.WorldChunk) ChunkSection(net.minecraft.world.chunk.ChunkSection) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

WorldChunk (net.minecraft.world.chunk.WorldChunk)28 BlockPos (net.minecraft.util.math.BlockPos)10 BlockState (net.minecraft.block.BlockState)6 ClientPlayerEntity (net.minecraft.client.network.ClientPlayerEntity)6 ChunkPos (net.minecraft.util.math.ChunkPos)5 PlayerUtils (at.haha007.edenclient.utils.PlayerUtils)4 ConfigSubscriber (at.haha007.edenclient.utils.config.ConfigSubscriber)4 PerWorldConfig (at.haha007.edenclient.utils.config.PerWorldConfig)4 Collectors (java.util.stream.Collectors)4 Inject (org.spongepowered.asm.mixin.injection.Inject)4 PlayerTickCallback (at.haha007.edenclient.callbacks.PlayerTickCallback)3 CommandManager (at.haha007.edenclient.command.CommandManager)3 ChatColor (at.haha007.edenclient.utils.ChatColor)3 PlayerUtils.sendModMessage (at.haha007.edenclient.utils.PlayerUtils.sendModMessage)3 LiteralArgumentBuilder (com.mojang.brigadier.builder.LiteralArgumentBuilder)3 CommandContext (com.mojang.brigadier.context.CommandContext)3 java.util (java.util)3 ArrayList (java.util.ArrayList)3 BlockEntity (net.minecraft.block.entity.BlockEntity)3 Direction (net.minecraft.util.math.Direction)3