Search in sources :

Example 1 with Chunk

use of org.spongepowered.api.world.Chunk in project SpongeCommon by SpongePowered.

the class MixinWorld method updateEntities.

/**
 ********************* TIMINGS **********************
 */
/**
 * @author blood
 * @author gabizou - Ported to 1.9.4 - replace direct field calls to overriden methods in MixinWorldServer
 *
 * @reason Add timing hooks in various areas. This method shouldn't be touched by mods/forge alike
 */
@Overwrite
public void updateEntities() {
    // this.profiler.startSection("entities"); // Sponge - Don't use the profiler
    // this.profiler.startSection("global"); // Sponge - Don't use the profiler
    // Sponge
    this.startEntityGlobalTimings();
    for (int i = 0; i < this.weatherEffects.size(); ++i) {
        net.minecraft.entity.Entity entity = this.weatherEffects.get(i);
        try {
            ++entity.ticksExisted;
            entity.onUpdate();
        } catch (Throwable throwable2) {
            // Sponge - end the entity timing
            this.stopTimingForWeatherEntityTickCrash(entity);
            CrashReport crashreport = CrashReport.makeCrashReport(throwable2, "Ticking entity");
            CrashReportCategory crashreportcategory = crashreport.makeCategory("Entity being ticked");
            if (entity == null) {
                crashreportcategory.addCrashSection("Entity", "~~NULL~~");
            } else {
                entity.addEntityCrashInfo(crashreportcategory);
            }
            SpongeImplHooks.onEntityError(entity, crashreport);
        }
        if (entity.isDead) {
            this.weatherEffects.remove(i--);
        }
    }
    // Sponge
    this.stopEntityTickTimingStartEntityRemovalTiming();
    // this.profiler.endStartSection("remove"); // Sponge - Don't use the profiler
    this.loadedEntityList.removeAll(this.unloadedEntityList);
    for (int k = 0; k < this.unloadedEntityList.size(); ++k) {
        net.minecraft.entity.Entity entity1 = this.unloadedEntityList.get(k);
        // Sponge start - use cached chunk
        // int j = entity1.chunkCoordX;
        // int k1 = entity1.chunkCoordZ;
        final net.minecraft.world.chunk.Chunk activeChunk = (net.minecraft.world.chunk.Chunk) ((IMixinEntity) entity1).getActiveChunk();
        if (activeChunk != null) {
            activeChunk.removeEntity(entity1);
        }
    // Sponge end
    }
    for (int l = 0; l < this.unloadedEntityList.size(); ++l) {
        this.onEntityRemoved(this.unloadedEntityList.get(l));
    }
    this.unloadedEntityList.clear();
    // Sponge
    this.stopEntityRemovalTiming();
    this.tickPlayers();
    // this.profiler.endStartSection("regular"); // Sponge - Don't use the profiler
    this.entityActivationCheck();
    for (int i1 = 0; i1 < this.loadedEntityList.size(); ++i1) {
        net.minecraft.entity.Entity entity2 = this.loadedEntityList.get(i1);
        net.minecraft.entity.Entity entity3 = entity2.getRidingEntity();
        if (entity3 != null) {
            if (!entity3.isDead && entity3.isPassenger(entity2)) {
                continue;
            }
            entity2.dismountRidingEntity();
        }
        // this.profiler.startSection("tick"); // Sponge - Don't use the profiler
        // Sponge
        this.startEntityTickTiming();
        if (!entity2.isDead && !(entity2 instanceof EntityPlayerMP)) {
            try {
                this.updateEntity(entity2);
            } catch (Throwable throwable1) {
                // Sponge
                this.stopTimingTickEntityCrash(entity2);
                CrashReport crashreport1 = CrashReport.makeCrashReport(throwable1, "Ticking entity");
                CrashReportCategory crashreportcategory1 = crashreport1.makeCategory("Entity being ticked");
                entity2.addEntityCrashInfo(crashreportcategory1);
                SpongeImplHooks.onEntityError(entity2, crashreport1);
            }
        }
        // Sponge
        this.stopEntityTickSectionBeforeRemove();
        // this.profiler.endSection(); // Sponge - Don't use the profiler
        // this.profiler.startSection("remove"); // Sponge - Don't use the profiler
        // Sponge
        this.startEntityRemovalTick();
        if (entity2.isDead) {
            // Sponge start - use cached chunk
            // int l1 = entity2.chunkCoordX;
            // int i2 = entity2.chunkCoordZ;
            final net.minecraft.world.chunk.Chunk activeChunk = (net.minecraft.world.chunk.Chunk) ((IMixinEntity) entity2).getActiveChunk();
            if (activeChunk != null) {
                activeChunk.removeEntity(entity2);
            }
            // Sponge end
            this.loadedEntityList.remove(i1--);
            this.onEntityRemoved(entity2);
        }
        // Sponge
        this.stopEntityRemovalTiming();
    // this.profiler.endSection(); // Sponge - Don't use the profiler
    }
    // this.profiler.endStartSection("blockEntities"); // Sponge - Don't use the profiler
    spongeTileEntityActivation();
    this.processingLoadedTiles = true;
    Iterator<net.minecraft.tileentity.TileEntity> iterator = this.tickableTileEntities.iterator();
    while (iterator.hasNext()) {
        // Sponge
        this.startTileTickTimer();
        net.minecraft.tileentity.TileEntity tileentity = iterator.next();
        if (!tileentity.isInvalid() && tileentity.hasWorld()) {
            BlockPos blockpos = tileentity.getPos();
            if (((IMixinTileEntity) tileentity).shouldTick() && this.worldBorder.contains(blockpos)) {
                // Sponge
                try {
                    // this.profiler.startSection(tileentity.getClass().getSimpleName());
                    ((ITickable) tileentity).update();
                // this.profiler.endSection();
                } catch (Throwable throwable) {
                    // Sponge
                    this.stopTimingTickTileEntityCrash(tileentity);
                    CrashReport crashreport2 = CrashReport.makeCrashReport(throwable, "Ticking block entity");
                    CrashReportCategory crashreportcategory2 = crashreport2.makeCategory("Block entity being ticked");
                    tileentity.addInfoToCrashReport(crashreportcategory2);
                    SpongeImplHooks.onTileEntityError(tileentity, crashreport2);
                }
            }
        }
        // Sponge
        this.stopTileEntityAndStartRemoval();
        if (tileentity.isInvalid()) {
            iterator.remove();
            this.loadedTileEntityList.remove(tileentity);
            // Sponge start - use cached chunk
            final net.minecraft.world.chunk.Chunk activeChunk = (net.minecraft.world.chunk.Chunk) ((IMixinTileEntity) tileentity).getActiveChunk();
            if (activeChunk != null) {
                // Forge: Bugfix: If we set the tile entity it immediately sets it in the chunk, so we could be desynced
                if (activeChunk.getTileEntity(tileentity.getPos(), net.minecraft.world.chunk.Chunk.EnumCreateEntityType.CHECK) == tileentity) {
                    activeChunk.removeTileEntity(tileentity.getPos());
                }
            }
        // Sponge end
        }
        // Sponge
        this.stopTileEntityRemovelInWhile();
    }
    if (!this.tileEntitiesToBeRemoved.isEmpty()) {
        // Sponge start - use forge hook
        for (Object tile : this.tileEntitiesToBeRemoved) {
            SpongeImplHooks.onTileChunkUnload(((net.minecraft.tileentity.TileEntity) tile));
        }
        // Sponge end
        // forge: faster "contains" makes this removal much more efficient
        java.util.Set<net.minecraft.tileentity.TileEntity> remove = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>());
        remove.addAll(this.tileEntitiesToBeRemoved);
        this.tickableTileEntities.removeAll(remove);
        this.loadedTileEntityList.removeAll(remove);
        this.tileEntitiesToBeRemoved.clear();
    }
    if (!this.isRemote) {
        try (final PhaseContext<?> context = BlockPhase.State.TILE_CHUNK_UNLOAD.createPhaseContext().source(this).buildAndSwitch()) {
            // Sponge
            this.startPendingTileEntityTimings();
        }
    }
    // FML Move below remove to prevent CMEs
    this.processingLoadedTiles = false;
    if (!this.addedTileEntityList.isEmpty()) {
        for (int j1 = 0; j1 < this.addedTileEntityList.size(); ++j1) {
            net.minecraft.tileentity.TileEntity tileentity1 = this.addedTileEntityList.get(j1);
            if (!tileentity1.isInvalid()) {
                if (!this.loadedTileEntityList.contains(tileentity1)) {
                    this.addTileEntity(tileentity1);
                }
                if (this.isBlockLoaded(tileentity1.getPos())) {
                    net.minecraft.world.chunk.Chunk chunk = this.getChunkFromBlockCoords(tileentity1.getPos());
                    IBlockState iblockstate = chunk.getBlockState(tileentity1.getPos());
                    chunk.addTileEntity(tileentity1.getPos(), tileentity1);
                    this.notifyBlockUpdate(tileentity1.getPos(), iblockstate, iblockstate, 3);
                }
            }
        }
        this.addedTileEntityList.clear();
    }
    // Sponge
    this.endPendingTileEntities();
// this.profiler.endSection(); // Sponge - Don't use the profiler
// this.profiler.endSection(); // Sponge - Don't use the profiler
}
Also used : CrashReport(net.minecraft.crash.CrashReport) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) BlockPos(net.minecraft.util.math.BlockPos) IMixinBlockPos(org.spongepowered.common.interfaces.util.math.IMixinBlockPos) IBlockState(net.minecraft.block.state.IBlockState) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(org.spongepowered.api.world.Chunk) ITickable(net.minecraft.util.ITickable) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) CrashReportCategory(net.minecraft.crash.CrashReportCategory) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with Chunk

use of org.spongepowered.api.world.Chunk in project LanternServer by LanternPowered.

the class LightningSpawnerAction method run.

@Override
public void run(ScriptContext scriptContext) {
    final LanternWorld world = (LanternWorld) scriptContext.get(Parameters.WORLD).get();
    final Random random = RANDOM.get();
    final Iterable<Chunk> chunks = world.getLoadedChunks();
    final int chance = (int) (1f / Math.max(this.chance, 0.000000000001f));
    for (Chunk chunk : chunks) {
        for (int i = 0; i < this.attemptsPerChunk; i++) {
            final LanternChunk chunk1 = (LanternChunk) chunk;
            if (random.nextInt(chance) != 0) {
                continue;
            }
            final int value = random.nextInt(0x10000);
            final int x = chunk1.getX() << 4 | value & 0xf;
            final int z = chunk1.getZ() << 4 | (value >> 4) & 0xf;
            final Entity entity = world.createEntity(EntityTypes.LIGHTNING, new Vector3d(x, world.getHighestYAt(x, z), z));
            world.spawnEntity(entity);
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Random(java.util.Random) Vector3d(com.flowpowered.math.vector.Vector3d) LanternWorld(org.lanternpowered.server.world.LanternWorld) LanternChunk(org.lanternpowered.server.world.chunk.LanternChunk) Chunk(org.spongepowered.api.world.Chunk) LanternChunk(org.lanternpowered.server.world.chunk.LanternChunk)

Example 3 with Chunk

use of org.spongepowered.api.world.Chunk in project LanternServer by LanternPowered.

the class LanternChunkManager method loadTickets.

public void loadTickets() throws IOException {
    final Multimap<String, LanternLoadingTicket> tickets = LanternLoadingTicketIO.load(this.worldFolder, this, this.chunkLoadService);
    final Iterator<Entry<String, LanternLoadingTicket>> it = tickets.entries().iterator();
    final CauseStack causeStack = CauseStack.current();
    while (it.hasNext()) {
        final LanternLoadingTicket ticket = it.next().getValue();
        if (ticket instanceof LanternEntityLoadingTicket) {
            final LanternEntityLoadingTicket ticket0 = (LanternEntityLoadingTicket) ticket;
            final EntityReference ref = ticket0.getEntityReference().orElse(null);
            if (ref != null) {
                causeStack.pushCause(ticket0);
                final LanternChunk chunk = getOrCreateChunk(ref.getChunkCoords(), causeStack, true, true);
                causeStack.popCause();
                final Entity entity = chunk.getEntity(ref.getUniqueId()).orElse(null);
                if (entity != null) {
                    ticket0.bindToEntity(entity);
                } else {
                    // The entity is gone?
                    it.remove();
                }
            } else {
                // The entity is gone?
                it.remove();
            }
        }
    }
    for (Entry<String, Collection<LanternLoadingTicket>> entry : tickets.asMap().entrySet()) {
        final Collection<ChunkTicketManager.Callback> callbacks = this.chunkLoadService.getCallbacks().get(entry.getKey());
        // These maps will be loaded lazily
        ImmutableListMultimap<UUID, LoadingTicket> playerLoadedTickets = null;
        ImmutableList<LoadingTicket> nonPlayerLoadedTickets = null;
        final Set<LoadingTicket> resultPlayerLoadedTickets = entry.getValue().stream().filter(ticket -> ticket instanceof PlayerLoadingTicket).collect(Collectors.toSet());
        final Set<LoadingTicket> resultNonPlayerLoadedTickets = entry.getValue().stream().filter(ticket -> !(ticket instanceof PlayerLoadingTicket)).collect(Collectors.toSet());
        final int maxTickets = this.chunkLoadService.getMaxTicketsById(entry.getKey());
        for (ChunkTicketManager.Callback callback : callbacks) {
            if (callback instanceof ChunkTicketManager.OrderedCallback) {
                if (nonPlayerLoadedTickets == null) {
                    nonPlayerLoadedTickets = ImmutableList.copyOf(resultNonPlayerLoadedTickets);
                    resultNonPlayerLoadedTickets.clear();
                }
                final List<LoadingTicket> result = ((ChunkTicketManager.OrderedCallback) callback).onLoaded(nonPlayerLoadedTickets, this.world, maxTickets);
                checkNotNull(result, "The OrderedCallback#onLoaded method may not return null, " + "error caused by (plugin=%s, clazz=%s)", entry.getKey(), callback.getClass().getName());
                resultNonPlayerLoadedTickets.addAll(result);
            }
            if (callback instanceof ChunkTicketManager.PlayerOrderedCallback) {
                if (playerLoadedTickets == null) {
                    final ImmutableListMultimap.Builder<UUID, LoadingTicket> mapBuilder = ImmutableListMultimap.builder();
                    resultPlayerLoadedTickets.forEach(ticket -> mapBuilder.put(((PlayerLoadingTicket) ticket).getPlayerUniqueId(), ticket));
                    resultPlayerLoadedTickets.clear();
                    playerLoadedTickets = mapBuilder.build();
                }
                final ListMultimap<UUID, LoadingTicket> result = ((ChunkTicketManager.PlayerOrderedCallback) callback).onPlayerLoaded(playerLoadedTickets, this.world);
                checkNotNull(result, "The PlayerOrderedCallback#onPlayerLoaded method may not return null, " + "error caused by (plugin=%s, clazz=%s)", entry.getKey(), callback.getClass().getName());
                resultPlayerLoadedTickets.addAll(result.values());
            }
        }
        final List<LoadingTicket> resultLoadedTickets = new ArrayList<>();
        resultLoadedTickets.addAll(resultPlayerLoadedTickets);
        resultLoadedTickets.addAll(resultNonPlayerLoadedTickets);
        // Lets see how many plugins attempted to add loading tickets
        final int sizeA = resultLoadedTickets.size();
        resultLoadedTickets.retainAll(entry.getValue());
        final int sizeB = resultLoadedTickets.size();
        if (sizeA != sizeB) {
            Lantern.getLogger().warn("The plugin {} attempted to add LoadingTicket's that were previously not present.", entry.getKey());
        }
        // Remove all the tickets that are already released
        resultLoadedTickets.removeIf(ticket -> ((ChunkLoadingTicket) ticket).isReleased());
        if (resultLoadedTickets.size() > maxTickets) {
            Lantern.getLogger().warn("The plugin {} has too many open chunk loading tickets {}. " + "Excess will be dropped", entry.getKey(), resultLoadedTickets.size());
            resultLoadedTickets.subList(maxTickets, resultLoadedTickets.size()).clear();
        }
        // Release all the tickets that were no longer usable
        final List<LoadingTicket> removedTickets = new ArrayList<>(entry.getValue());
        removedTickets.removeAll(resultLoadedTickets);
        removedTickets.forEach(LoadingTicket::release);
        final ImmutableList<LoadingTicket> loadedTickets = ImmutableList.copyOf(resultLoadedTickets);
        for (ChunkTicketManager.Callback callback : callbacks) {
            callback.onLoaded(loadedTickets, this.world);
        }
    }
}
Also used : ImmutableBlockVolume(org.spongepowered.api.world.extent.ImmutableBlockVolume) GenerationPopulator(org.spongepowered.api.world.gen.GenerationPopulator) Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) ChunkSection(org.lanternpowered.server.world.chunk.LanternChunk.ChunkSection) Inject(com.google.inject.Inject) ChunkTicketManager(org.spongepowered.api.world.ChunkTicketManager) ShortArrayMutableBiomeBuffer(org.lanternpowered.server.util.gen.biome.ShortArrayMutableBiomeBuffer) BiomeGenerationSettings(org.spongepowered.api.world.biome.BiomeGenerationSettings) Random(java.util.Random) BiomeTypes(org.spongepowered.api.world.biome.BiomeTypes) WorldConfig(org.lanternpowered.server.config.world.WorldConfig) BiomeGenerator(org.spongepowered.api.world.gen.BiomeGenerator) EventManager(org.spongepowered.api.event.EventManager) Future(java.util.concurrent.Future) Populator(org.spongepowered.api.world.gen.Populator) DirectoryKeys(org.lanternpowered.server.game.DirectoryKeys) ChunkIOService(org.lanternpowered.server.data.io.ChunkIOService) Map(java.util.Map) BlockRegistryModule(org.lanternpowered.server.game.registry.type.block.BlockRegistryModule) ShortArrayMutableBlockBuffer(org.lanternpowered.server.util.gen.block.ShortArrayMutableBlockBuffer) EntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.EntityLoadingTicket) VirtualBiomeType(org.spongepowered.api.world.biome.VirtualBiomeType) Path(java.nio.file.Path) ObjectArrayImmutableBiomeBuffer(org.lanternpowered.server.util.gen.biome.ObjectArrayImmutableBiomeBuffer) PlayerLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) CHUNK_SECTIONS(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_SECTIONS) ImmutableSet(com.google.common.collect.ImmutableSet) CHUNK_AREA(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_AREA) Extent(org.spongepowered.api.world.extent.Extent) BlockTypes(org.spongepowered.api.block.BlockTypes) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) UUID(java.util.UUID) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) LanternGame(org.lanternpowered.server.game.LanternGame) BlockState(org.spongepowered.api.block.BlockState) FastSoftThreadLocal(org.lanternpowered.server.util.concurrent.FastSoftThreadLocal) WorldGenerator(org.spongepowered.api.world.gen.WorldGenerator) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) SoftBufferExtentViewDownsize(org.lanternpowered.server.world.extent.SoftBufferExtentViewDownsize) Lantern(org.lanternpowered.server.game.Lantern) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Entry(java.util.Map.Entry) Optional(java.util.Optional) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) InternalPluginsInfo(org.lanternpowered.server.plugin.InternalPluginsInfo) AtomicShortArrayMutableBlockBuffer(org.lanternpowered.server.util.gen.block.AtomicShortArrayMutableBlockBuffer) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) AbstractMutableBlockBuffer(org.lanternpowered.server.util.gen.block.AbstractMutableBlockBuffer) MutableBlockVolume(org.spongepowered.api.world.extent.MutableBlockVolume) BiomeType(org.spongepowered.api.world.biome.BiomeType) Callable(java.util.concurrent.Callable) Multimap(com.google.common.collect.Multimap) CHUNK_SECTION_VOLUME(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_SECTION_VOLUME) ImmutableBiomeVolume(org.spongepowered.api.world.extent.ImmutableBiomeVolume) ArrayList(java.util.ArrayList) Vector2i(com.flowpowered.math.vector.Vector2i) HashSet(java.util.HashSet) CHUNK_SECTION_SIZE(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_SECTION_SIZE) ThreadHelper(org.lanternpowered.server.util.ThreadHelper) CauseStack(org.lanternpowered.server.event.CauseStack) ImmutableList(com.google.common.collect.ImmutableList) ShortArrayImmutableBlockBuffer(org.lanternpowered.server.util.gen.block.ShortArrayImmutableBlockBuffer) LanternWorld(org.lanternpowered.server.world.LanternWorld) CHUNK_BIOME_VOLUME(org.lanternpowered.server.world.chunk.LanternChunkLayout.CHUNK_BIOME_VOLUME) LinkedList(java.util.LinkedList) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) ExtentBufferHelper(org.lanternpowered.server.world.extent.ExtentBufferHelper) Iterator(java.util.Iterator) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CHUNK_HEIGHT(org.lanternpowered.server.world.chunk.LanternChunk.CHUNK_HEIGHT) Chunk(org.spongepowered.api.world.Chunk) IOException(java.io.IOException) Entity(org.spongepowered.api.entity.Entity) LoadingTicket(org.spongepowered.api.world.ChunkTicketManager.LoadingTicket) TimeUnit(java.util.concurrent.TimeUnit) StorageType(org.spongepowered.api.world.extent.StorageType) Vector3i(com.flowpowered.math.vector.Vector3i) Named(com.google.inject.name.Named) PlayerEntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerEntityLoadingTicket) MapMaker(com.google.common.collect.MapMaker) Conditions.checkPlugin(org.lanternpowered.server.util.Conditions.checkPlugin) Entity(org.spongepowered.api.entity.Entity) ArrayList(java.util.ArrayList) EntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.EntityLoadingTicket) PlayerLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket) LoadingTicket(org.spongepowered.api.world.ChunkTicketManager.LoadingTicket) PlayerEntityLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerEntityLoadingTicket) Entry(java.util.Map.Entry) PlayerLoadingTicket(org.spongepowered.api.world.ChunkTicketManager.PlayerLoadingTicket) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) UUID(java.util.UUID) CauseStack(org.lanternpowered.server.event.CauseStack) ChunkTicketManager(org.spongepowered.api.world.ChunkTicketManager) Collection(java.util.Collection)

Example 4 with Chunk

use of org.spongepowered.api.world.Chunk in project CatClearLag by Time6628.

the class EntitiesCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    List<Chunk> chunksToSort = (List<Chunk>) ((Player) src).getWorld().getLoadedChunks();
    TreeMap<Chunk, Integer> sortedChunks = new TreeMap<>((o1, o2) -> Integer.compare(o2.getEntities().size(), o1.getEntities().size()));
    for (Chunk chunk : chunksToSort) {
        sortedChunks.put(chunk, chunk.getEntities().size());
    }
    List<Text> texts = new ArrayList<>();
    sortedChunks.forEach(((chunk, integer) -> texts.add(Text.builder().append(Text.of(chunk.getPosition().getX() + "," + chunk.getPosition().getZ() + " contains " + integer + " entities.")).onClick(callback(chunk)).build())));
    plugin.getPaginationService().builder().contents((texts)).title(Text.builder().color(TextColors.LIGHT_PURPLE).append(Text.of("Laggy Chunks")).build()).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Chunk(org.spongepowered.api.world.Chunk) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TreeMap(java.util.TreeMap) LaggyChunksCommand(me.time6628.clag.sponge.commands.LaggyChunksCommand) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) CatClearLag(me.time6628.clag.sponge.CatClearLag) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Text(org.spongepowered.api.text.Text) Chunk(org.spongepowered.api.world.Chunk) TreeMap(java.util.TreeMap)

Example 5 with Chunk

use of org.spongepowered.api.world.Chunk in project CatClearLag by Time6628.

the class TilesCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    List<Chunk> chunksToSort = (List<Chunk>) ((Player) src).getWorld().getLoadedChunks();
    TreeMap<Chunk, Integer> sortedChunks = new TreeMap<>((o1, o2) -> Integer.compare(o2.getTileEntities().size(), o1.getTileEntities().size()));
    for (Chunk chunk : chunksToSort) {
        sortedChunks.put(chunk, chunk.getTileEntities().size());
    }
    List<Text> texts = new ArrayList<>();
    sortedChunks.forEach(((chunk, integer) -> texts.add(Text.builder().append(Text.of(chunk.getPosition().getX() + "," + chunk.getPosition().getZ() + " contains " + integer + " tiles.")).onClick(callback(chunk)).build())));
    plugin.getPaginationService().builder().contents((texts)).title(Text.builder().color(TextColors.LIGHT_PURPLE).append(Text.of("Laggy Chunks")).build()).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Chunk(org.spongepowered.api.world.Chunk) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TreeMap(java.util.TreeMap) LaggyChunksCommand(me.time6628.clag.sponge.commands.LaggyChunksCommand) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) CatClearLag(me.time6628.clag.sponge.CatClearLag) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Text(org.spongepowered.api.text.Text) Chunk(org.spongepowered.api.world.Chunk) TreeMap(java.util.TreeMap)

Aggregations

Chunk (org.spongepowered.api.world.Chunk)12 Vector3i (com.flowpowered.math.vector.Vector3i)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3 PopulateChunkEvent (net.minecraftforge.event.terraingen.PopulateChunkEvent)3 Direction (org.spongepowered.api.util.Direction)3 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)3 Random (java.util.Random)2 TreeMap (java.util.TreeMap)2 CatClearLag (me.time6628.clag.sponge.CatClearLag)2 LaggyChunksCommand (me.time6628.clag.sponge.commands.LaggyChunksCommand)2 ChunkEvent (net.minecraftforge.event.world.ChunkEvent)2 LanternWorld (org.lanternpowered.server.world.LanternWorld)2 CommandException (org.spongepowered.api.command.CommandException)2 CommandResult (org.spongepowered.api.command.CommandResult)2 CommandSource (org.spongepowered.api.command.CommandSource)2 CommandContext (org.spongepowered.api.command.args.CommandContext)2 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)2 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)2 Entity (org.spongepowered.api.entity.Entity)2