Java Code Examples for org.bukkit.Chunk#getTileEntities()

The following examples show how to use org.bukkit.Chunk#getTileEntities() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TPContainerListener.java    From Transport-Pipes with MIT License 6 votes vote down vote up
public void handleChunkLoadSync(Chunk loadedChunk, boolean onServerStart) {
    PipeManager pipeManager = (PipeManager) (DuctManager<? extends Duct>) ductRegister.baseDuctTypeOf("pipe").getDuctManager();

    if (loadedChunk.getTileEntities() != null) {
        for (BlockState bs : loadedChunk.getTileEntities()) {
            if (WorldUtils.isContainerBlock(bs.getType())) {

                //automatically ignores this block if it is already registered as container block
                updateContainerBlock(bs.getBlock(), true, !onServerStart);

                //if this block is already registered, update the block, because the blockState object changes after a chunk unload and load
                TransportPipesContainer container = pipeManager.getContainerAtLoc(bs.getLocation());
                if (container instanceof BlockContainer) {
                    ((BlockContainer) container).updateBlock();
                }
            }
        }
    }
}
 
Example 2
Source File: WorthManager.java    From factions-top with MIT License 6 votes vote down vote up
/**
 * Calculates the spawner worth of a chunk.
 *
 * @param chunk    the chunk.
 * @param spawners the spawner totals to add to.
 * @return the chunk worth in spawners.
 */
private double getSpawnerWorth(Chunk chunk, Map<EntityType, Integer> spawners) {
    int count;
    double worth = 0;

    for (BlockState blockState : chunk.getTileEntities()) {
        if (!(blockState instanceof CreatureSpawner)) {
            continue;
        }

        CreatureSpawner spawner = (CreatureSpawner) blockState;
        EntityType spawnType = spawner.getSpawnedType();
        int stackSize = plugin.getSpawnerStackerHook().getStackSize(spawner);
        double blockPrice = plugin.getSettings().getSpawnerPrice(spawnType) * stackSize;
        worth += blockPrice;

        if (blockPrice != 0) {
            count = spawners.getOrDefault(spawnType, 0);
            spawners.put(spawnType, count + stackSize);
        }
    }

    return worth;
}
 
Example 3
Source File: WorldProblemMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private void checkChunk(ChunkPosition pos, @Nullable Chunk chunk) {
    if(repairedChunks.add(pos)) {
        if(chunk == null) {
            chunk = pos.getChunk(match.getWorld());
        }

        for(BlockState state : chunk.getTileEntities()) {
            if(state instanceof Skull) {
                if(!NMSHacks.isSkullCached((Skull) state)) {
                    Location loc = state.getLocation();
                    broadcastDeveloperWarning("Uncached skull \"" + ((Skull) state).getOwner() + "\" at " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
                }
            }
        }

        // Replace formerly invisible half-iron-door blocks with barriers
        for(Block ironDoor : chunk.getBlocks(Material.IRON_DOOR_BLOCK)) {
            BlockFace half = (ironDoor.getData() & 8) == 0 ? BlockFace.DOWN : BlockFace.UP;
            if(ironDoor.getRelative(half.getOppositeFace()).getType() != Material.IRON_DOOR_BLOCK) {
                ironDoor.setType(Material.BARRIER, false);
            }
        }

        // Remove all block 36 and remember the ones at y=0 so VoidFilter can check them
        for(Block block36 : chunk.getBlocks(Material.PISTON_MOVING_PIECE)) {
            if(block36.getY() == 0) {
                block36Locations.add(block36.getX(), block36.getY(), block36.getZ());
            }
            block36.setType(Material.AIR, false);
        }
    }
}
 
Example 4
Source File: SignUpdater.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
void load(Chunk chunk) {
    if(chunks.add(ChunkLocation.of(chunk))) {
        for(BlockState blockState : chunk.getTileEntities()) {
            if(blockState instanceof Sign) {
                final SignHandle sign = createSign((Sign) blockState);
                if(sign != null) {
                    signs.put(blockState.getLocation(), sign);
                }
            }
        }

        logger.fine(() -> "Loaded chunk " + chunk.getX() + "," + chunk.getZ() +" with " + signs.size());
    }
}
 
Example 5
Source File: WorldData.java    From LagMonitor with MIT License 5 votes vote down vote up
public static WorldData fromWorld(World world) {
    String worldName = world.getName();
    int tileEntities = 0;
    for (Chunk loadedChunk : world.getLoadedChunks()) {
        tileEntities += loadedChunk.getTileEntities().length;
    }

    int entities = world.getEntities().size();
    int chunks = world.getLoadedChunks().length;

    return new WorldData(worldName, chunks, tileEntities, entities);
}
 
Example 6
Source File: SystemCommand.java    From LagMonitor with MIT License 5 votes vote down vote up
private void displayWorldInfo(CommandSender sender) {
    int entities = 0;
    int chunks = 0;
    int livingEntities = 0;
    int tileEntities = 0;

    long usedWorldSize = 0;

    List<World> worlds = Bukkit.getWorlds();
    for (World world : worlds) {
        for (Chunk loadedChunk : world.getLoadedChunks()) {
            tileEntities += loadedChunk.getTileEntities().length;
        }

        livingEntities += world.getLivingEntities().size();
        entities += world.getEntities().size();
        chunks += world.getLoadedChunks().length;

        File worldFolder = Bukkit.getWorld(world.getUID()).getWorldFolder();
        usedWorldSize += LagUtils.getFolderSize(plugin.getLogger(), worldFolder.toPath());
    }

    sendMessage(sender, "Entities", String.format("%d/%d", livingEntities, entities));
    sendMessage(sender, "Tile Entities", String.valueOf(tileEntities));
    sendMessage(sender, "Loaded Chunks", String.valueOf(chunks));
    sendMessage(sender, "Worlds", String.valueOf(worlds.size()));
    sendMessage(sender, "World Size", readableBytes(usedWorldSize));
}
 
Example 7
Source File: WorldData.java    From LagMonitor with MIT License 5 votes vote down vote up
public static WorldData fromWorld(World world) {
    String worldName = world.getName();
    int tileEntities = 0;
    for (Chunk loadedChunk : world.getLoadedChunks()) {
        tileEntities += loadedChunk.getTileEntities().length;
    }

    int entities = world.getEntities().size();
    int chunks = world.getLoadedChunks().length;

    return new WorldData(worldName, chunks, tileEntities, entities);
}
 
Example 8
Source File: SystemCommand.java    From LagMonitor with MIT License 5 votes vote down vote up
private void displayWorldInfo(CommandSender sender) {
    int entities = 0;
    int chunks = 0;
    int livingEntities = 0;
    int tileEntities = 0;

    long usedWorldSize = 0;

    List<World> worlds = Bukkit.getWorlds();
    for (World world : worlds) {
        for (Chunk loadedChunk : world.getLoadedChunks()) {
            tileEntities += loadedChunk.getTileEntities().length;
        }

        livingEntities += world.getLivingEntities().size();
        entities += world.getEntities().size();
        chunks += world.getLoadedChunks().length;

        File worldFolder = Bukkit.getWorld(world.getUID()).getWorldFolder();
        usedWorldSize += LagUtils.getFolderSize(plugin.getLogger(), worldFolder.toPath());
    }

    sendMessage(sender, "Entities", String.format("%d/%d", livingEntities, entities));
    sendMessage(sender, "Tile Entities", String.valueOf(tileEntities));
    sendMessage(sender, "Loaded Chunks", String.valueOf(chunks));
    sendMessage(sender, "Worlds", String.valueOf(worlds.size()));
    sendMessage(sender, "World Size", readableBytes(usedWorldSize));
}
 
Example 9
Source File: MapScanner.java    From VoxelGamesLibv2 with MIT License 4 votes vote down vote up
/**
 * Searches the map for "markers". Most of the time these are implemented as tile entities (skulls)
 *
 * @param map    the map to scan
 * @param center the center location
 * @param range  the range in where to scan
 */
public void searchForMarkers(@Nonnull Map map, @Nonnull Vector3D center, int range, @Nonnull UUID gameid) {
    World world = Bukkit.getWorld(map.getLoadedName(gameid));
    if (world == null) {
        throw new MapException("Could not find world " + map.getLoadedName(gameid) + "(" + map.getInfo().getDisplayName() + ")" + ". Is it loaded?");
    }

    List<Marker> markers = new ArrayList<>();
    List<ChestMarker> chestMarkers = new ArrayList<>();

    int startX = (int) center.getX();
    int startY = (int) center.getZ();

    int minX = Math.min(startX - range, startX + range);
    int minZ = Math.min(startY - range, startY + range);

    int maxX = Math.max(startX - range, startX + range);
    int maxZ = Math.max(startY - range, startY + range);

    for (int x = minX; x <= maxX; x += 16) {
        for (int z = minZ; z <= maxZ; z += 16) {
            Chunk chunk = world.getChunkAt(x >> 4, z >> 4);
            for (BlockState te : chunk.getTileEntities()) {
                if (te.getType() == Material.PLAYER_HEAD) {
                    Skull skull = (Skull) te;
                    String markerData = getMarkerData(skull);
                    if (markerData == null) continue;
                    MarkerDefinition markerDefinition = mapHandler.createMarkerDefinition(markerData);
                    markers.add(new Marker(new Vector3D(skull.getX(), skull.getY(), skull.getZ()),
                            DirectionUtil.directionToYaw(skull.getRotation()),
                            markerData, markerDefinition));
                } else if (te.getType() == Material.CHEST) {
                    Chest chest = (Chest) te;
                    String name = chest.getBlockInventory().getName();
                    ItemStack[] items = new ItemStack[chest.getBlockInventory()
                            .getStorageContents().length];
                    for (int i = 0; i < items.length; i++) {
                        ItemStack is = chest.getBlockInventory().getItem(i);
                        if (is == null) {
                            items[i] = new ItemStack(Material.AIR);
                        } else {
                            items[i] = is;
                        }
                    }
                    chestMarkers
                            .add(new ChestMarker(new Vector3D(chest.getX(), chest.getY(), chest.getZ()), name,
                                    items));
                }
            }
        }
    }

    map.setMarkers(markers);
    map.setChestMarkers(chestMarkers);
}
 
Example 10
Source File: WorthManager.java    From factions-top with MIT License 4 votes vote down vote up
/**
 * Calculates the chest worth of a chunk.
 *
 * @param chunk     the chunk.
 * @param materials the material totals to add to.
 * @param spawners  the spawner totals to add to.
 * @return the chunk worth in materials.
 */
private double getChestWorth(Chunk chunk, Map<Material, Integer> materials, Map<EntityType, Integer> spawners) {
    int count;
    double worth = 0;
    double materialPrice;
    EntityType spawnerType;

    for (BlockState blockState : chunk.getTileEntities()) {
        if (!(blockState instanceof Chest)) {
            continue;
        }

        Chest chest = (Chest) blockState;

        for (ItemStack item : chest.getBlockInventory()) {
            if (item == null) continue;

            int stackSize = item.getAmount();

            switch (item.getType()) {
                case MOB_SPAWNER:
                    stackSize *= plugin.getSpawnerStackerHook().getStackSize(item);
                    spawnerType = plugin.getSpawnerStackerHook().getSpawnedType(item);
                    double price = plugin.getSettings().getSpawnerPrice(spawnerType);
                    materialPrice = price * stackSize;
                    break;
                default:
                    materialPrice = plugin.getSettings().getBlockPrice(item.getType()) * stackSize;
                    spawnerType = null;
                    break;
            }

            worth += materialPrice;

            if (materialPrice != 0) {
                if (spawnerType == null) {
                    count = materials.getOrDefault(item.getType(), 0);
                    materials.put(item.getType(), count + stackSize);
                } else {
                    count = spawners.getOrDefault(spawnerType, 0);
                    spawners.put(spawnerType, count + stackSize);
                }
            }
        }
    }

    return worth;
}