Search in sources :

Example 36 with Chunk

use of org.bukkit.Chunk in project askyblock by tastybento.

the class GridManager method removeMobs.

/**
 * Removes monsters around location l
 *
 * @param l location
 */
public void removeMobs(final Location l) {
    if (!inWorld(l)) {
        return;
    }
    // Don't remove mobs if at spawn
    if (this.isAtSpawn(l)) {
        // plugin.getLogger().info("DEBUG: at spawn!");
        return;
    }
    final int px = l.getBlockX();
    final int py = l.getBlockY();
    final int pz = l.getBlockZ();
    for (int x = -1; x <= 1; x++) {
        for (int z = -1; z <= 1; z++) {
            final Chunk c = l.getWorld().getChunkAt(new Location(l.getWorld(), px + x * 16, py, pz + z * 16));
            if (c.isLoaded()) {
                for (final Entity e : c.getEntities()) {
                    // Don't remove if the entity is an NPC or has a name tag
                    if (e.getCustomName() != null || e.hasMetadata("NPC"))
                        continue;
                    if (e instanceof Monster && !Settings.mobWhiteList.contains(e.getType())) {
                        e.remove();
                    }
                }
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Monster(org.bukkit.entity.Monster) Chunk(org.bukkit.Chunk) Location(org.bukkit.Location)

Example 37 with Chunk

use of org.bukkit.Chunk in project BKCommonLib by bergerhealer.

the class RegionHandler_Vanilla_1_14 method getRegions3.

@Override
public Set<IntVector3> getRegions3(World world) {
    HashSet<IntVector3> regionIndices = new HashSet<IntVector3>();
    // Add all RegionFile instances in the cache
    Object regionFileCache = findRegionFileCache.invoke(null, HandleConversion.toWorldHandle(world));
    regionIndices.addAll(findCacheRegionFileCoordinates.invoke(null, regionFileCache));
    // Obtain the region coordinates from all files in regions folder
    File regionFolder = Common.SERVER.getWorldRegionFolder(world.getName());
    if (regionFolder != null) {
        String[] regionFileNames = regionFolder.list();
        for (String regionFileName : regionFileNames) {
            File file = new File(regionFolder, regionFileName);
            if (file.isFile() && file.exists() && file.length() >= 4096) {
                IntVector3 coords = getRegionFileCoordinates(file).toIntVector3(0);
                if (coords != null && !regionIndices.contains(coords)) {
                    regionIndices.add(coords);
                }
            }
        }
    }
    // Look at all loaded chunks of the world and add the regions they are inside of
    for (Chunk chunk : world.getLoadedChunks()) {
        IntVector3 coords = new IntVector3(chunk.getX() >> 5, 0, chunk.getZ() >> 5);
        regionIndices.add(coords);
    }
    return regionIndices;
}
Also used : Chunk(org.bukkit.Chunk) IntVector3(com.bergerkiller.bukkit.common.bases.IntVector3) File(java.io.File) HashSet(java.util.HashSet)

Example 38 with Chunk

use of org.bukkit.Chunk in project BKCommonLib by bergerhealer.

the class LightingHandler_1_16_4_StarLightEngine method setSectionSkyLightAsync.

@Override
public CompletableFuture<Void> setSectionSkyLightAsync(World world, int cx, int cy, int cz, byte[] data) {
    final CompletableFuture<Void> future = new CompletableFuture<Void>();
    final Chunk chunk = WorldUtil.getChunk(world, cx, cz);
    scheduleUpdate(world, () -> {
        try {
            handle.setSkyLightData(HandleConversion.toChunkHandle(chunk), cx, cy, cz, data);
            future.complete(null);
        } catch (Throwable t) {
            future.completeExceptionally(t);
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Chunk(org.bukkit.Chunk)

Example 39 with Chunk

use of org.bukkit.Chunk in project BKCommonLib by bergerhealer.

the class BlockStateConversion_1_12_2 method tileEntityToBlockState.

public synchronized BlockState tileEntityToBlockState(Block block, Object nmsTileEntity) {
    // Store and restore old state in case of recursive calls to this function
    // This could happen if inside BlockState construction a chunk is loaded anyway
    // Would be bad, but its best to assume the worst
    TileState old_state = input_state;
    try {
        input_state = new TileState(block, nmsTileEntity);
        BlockState result = proxy_block.getState();
        // Internal BlockState needs to have all proxy field instances replaced with what it should be
        BlockStateCache cache = BlockStateCache.get(result.getClass());
        for (SafeField<World> worldField : cache.worldFields) {
            worldField.set(result, input_state.block.getWorld());
        }
        for (SafeField<Chunk> chunkField : cache.chunkFields) {
            chunkField.set(result, input_state.block.getChunk());
        }
        // All done!
        return result;
    } catch (Throwable t) {
        Logging.LOGGER_CONVERSION.once(Level.SEVERE, "Failed to convert " + nmsTileEntity.getClass().getName() + " to CraftBlockState", t);
        return CraftBlockStateHandle.createNew(input_state.block);
    } finally {
        input_state = old_state;
    }
}
Also used : BlockState(org.bukkit.block.BlockState) World(org.bukkit.World) Chunk(org.bukkit.Chunk)

Example 40 with Chunk

use of org.bukkit.Chunk in project Glowstone by GlowstoneMC.

the class AnvilChunkIoService method write.

@Override
public void write(GlowChunk chunk) throws IOException {
    int x = chunk.getX();
    int z = chunk.getZ();
    RegionFile region = cache.getRegionFile(x, z);
    int regionX = x & REGION_SIZE - 1;
    int regionZ = z & REGION_SIZE - 1;
    CompoundTag levelTags = new CompoundTag();
    // core properties
    // NON-NLS
    levelTags.putInt("xPos", chunk.getX());
    // NON-NLS
    levelTags.putInt("zPos", chunk.getZ());
    // NON-NLS
    levelTags.putLong("LastUpdate", 0);
    // NON-NLS
    levelTags.putLong("InhabitedTime", chunk.getInhabitedTime());
    // NON-NLS
    levelTags.putBool("TerrainPopulated", chunk.isPopulated());
    // chunk sections
    List<CompoundTag> sectionTags = new ArrayList<>();
    GlowChunkSnapshot snapshot = chunk.getChunkSnapshot(true, true, false);
    ChunkSection[] sections = snapshot.getRawSections();
    for (byte i = 0; i < sections.length; ++i) {
        ChunkSection sec = sections[i];
        if (sec == null) {
            continue;
        }
        CompoundTag sectionTag = new CompoundTag();
        // NON-NLS
        sectionTag.putByte("Y", i);
        sec.recount();
        sec.writeToNbt(sectionTag);
        sectionTags.add(sectionTag);
    }
    // NON-NLS
    levelTags.putCompoundList("Sections", sectionTags);
    // height map and biomes
    // NON-NLS
    levelTags.putIntArray("HeightMap", snapshot.getRawHeightmap());
    // NON-NLS
    levelTags.putByteArray("Biomes", snapshot.getRawBiomes());
    // Save Slime Chunk
    // NON-NLS
    levelTags.putByte("isSlimeChunk", snapshot.isSlimeChunk() ? 1 : 0);
    // entities
    List<CompoundTag> entities = new ArrayList<>();
    for (GlowEntity entity : chunk.getRawEntities()) {
        if (!entity.shouldSave()) {
            continue;
        }
        // passengers will be saved as part of the vehicle
        if (entity.isInsideVehicle()) {
            continue;
        }
        try {
            CompoundTag tag = new CompoundTag();
            EntityStorage.save(entity, tag);
            entities.add(tag);
        } catch (Exception e) {
            ConsoleMessages.Warn.Entity.SAVE_FAILED.log(e, entity, chunk);
        }
    }
    levelTags.putCompoundList("Entities", entities);
    // block entities
    List<CompoundTag> blockEntities = new ArrayList<>();
    for (BlockEntity entity : chunk.getRawBlockEntities()) {
        try {
            CompoundTag tag = new CompoundTag();
            entity.saveNbt(tag);
            blockEntities.add(tag);
        } catch (Exception ex) {
            ConsoleMessages.Error.BlockEntity.SAVE_FAILED.log(ex, entity.getBlock());
        }
    }
    levelTags.putCompoundList("TileEntities", blockEntities);
    List<CompoundTag> tileTicks = new ArrayList<>();
    for (Location location : chunk.getWorld().getTickMap()) {
        Chunk locationChunk = location.getChunk();
        if (locationChunk.getX() == chunk.getX() && locationChunk.getZ() == chunk.getZ()) {
            int tileX = location.getBlockX();
            int tileY = location.getBlockY();
            int tileZ = location.getBlockZ();
            String type = ItemIds.getName(location.getBlock().getType());
            CompoundTag tag = new CompoundTag();
            tag.putInt("x", tileX);
            tag.putInt("y", tileY);
            tag.putInt("z", tileZ);
            tag.putString("i", type);
            tileTicks.add(tag);
        }
    }
    levelTags.putCompoundList("TileTicks", tileTicks);
    CompoundTag levelOut = new CompoundTag();
    levelOut.putCompound("Level", levelTags);
    try (NbtOutputStream nbt = new NbtOutputStream(region.getChunkDataOutputStream(regionX, regionZ), false)) {
        nbt.writeTag(levelOut);
    }
}
Also used : NbtOutputStream(net.glowstone.util.nbt.NbtOutputStream) ArrayList(java.util.ArrayList) GlowChunk(net.glowstone.chunk.GlowChunk) Chunk(org.bukkit.Chunk) UnknownEntityTypeException(net.glowstone.io.entity.UnknownEntityTypeException) IOException(java.io.IOException) GlowEntity(net.glowstone.entity.GlowEntity) ChunkSection(net.glowstone.chunk.ChunkSection) CompoundTag(net.glowstone.util.nbt.CompoundTag) GlowChunkSnapshot(net.glowstone.chunk.GlowChunkSnapshot) BlockEntity(net.glowstone.block.entity.BlockEntity) Location(org.bukkit.Location)

Aggregations

Chunk (org.bukkit.Chunk)52 Location (org.bukkit.Location)16 World (org.bukkit.World)10 OwnedLand (biz.princeps.landlord.util.OwnedLand)9 Entity (org.bukkit.entity.Entity)9 ArrayList (java.util.ArrayList)6 IntVector2 (com.bergerkiller.bukkit.common.bases.IntVector2)5 IntVector3 (com.bergerkiller.bukkit.common.bases.IntVector3)5 Block (org.bukkit.block.Block)5 BlockState (org.bukkit.block.BlockState)5 Player (org.bukkit.entity.Player)5 Offers (biz.princeps.landlord.persistent.Offers)4 HashSet (java.util.HashSet)4 EventHandler (org.bukkit.event.EventHandler)3 Plugin (org.bukkit.plugin.Plugin)3 LPlayer (biz.princeps.landlord.persistent.LPlayer)2 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)2 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)2 ListTag (com.denizenscript.denizencore.objects.core.ListTag)2 Mob (com.earth2me.essentials.Mob)2