diff options
author | Luck <git@lucko.me> | 2022-06-25 22:48:55 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2022-06-25 22:49:41 +0100 |
commit | 4d45579d2bf57b417d5d3eca041c2131177183e4 (patch) | |
tree | c51f46d0efb323b8d7a878f383b44dbaac129fb6 /spark-sponge8/src/main | |
parent | 28cf3185c1374c4b5af277ef28482299694209a3 (diff) | |
download | spark-4d45579d2bf57b417d5d3eca041c2131177183e4.tar.gz spark-4d45579d2bf57b417d5d3eca041c2131177183e4.tar.bz2 spark-4d45579d2bf57b417d5d3eca041c2131177183e4.zip |
Add providers for world (entity/chunk) statistics
Diffstat (limited to 'spark-sponge8/src/main')
-rw-r--r-- | spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java | 36 | ||||
-rw-r--r-- | spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8WorldInfoProvider.java | 88 |
2 files changed, 120 insertions, 4 deletions
diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java index e867a75..68e47e3 100644 --- a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java +++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8SparkPlugin.java @@ -27,6 +27,7 @@ import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.command.sender.CommandSender; import me.lucko.spark.common.monitor.ping.PlayerPingProvider; import me.lucko.spark.common.platform.PlatformInfo; +import me.lucko.spark.common.platform.world.WorldInfoProvider; import me.lucko.spark.common.sampler.ThreadDumper; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.util.ClassSourceLookup; @@ -67,6 +68,7 @@ public class Sponge8SparkPlugin implements SparkPlugin { private final Game game; private final Path configDirectory; private final ExecutorService asyncExecutor; + private final ExecutorService syncExecutor; private SparkPlatform platform; private final ThreadDumper.GameThread threadDumper = new ThreadDumper.GameThread(); @@ -78,6 +80,14 @@ public class Sponge8SparkPlugin implements SparkPlugin { this.game = game; this.configDirectory = configDirectory; this.asyncExecutor = game.asyncScheduler().executor(pluginContainer); + + if (game.isServerAvailable()) { + this.syncExecutor = game.server().scheduler().executor(pluginContainer); + } else if (game.isClientAvailable()) { + this.syncExecutor = game.client().scheduler().executor(pluginContainer); + } else { + throw new IllegalStateException("Server and client both unavailable"); + } } @@ -114,10 +124,14 @@ public class Sponge8SparkPlugin implements SparkPlugin { @Override public Stream<CommandSender> getCommandSenders() { - return Stream.concat( - this.game.server().onlinePlayers().stream(), - Stream.of(this.game.systemSubject()) - ).map(Sponge8CommandSender::new); + if (this.game.isServerAvailable()) { + return Stream.concat( + this.game.server().onlinePlayers().stream(), + Stream.of(this.game.systemSubject()) + ).map(Sponge8CommandSender::new); + } else { + return Stream.of(this.game.systemSubject()).map(Sponge8CommandSender::new); + } } @Override @@ -126,6 +140,11 @@ public class Sponge8SparkPlugin implements SparkPlugin { } @Override + public void executeSync(Runnable task) { + this.syncExecutor.execute(task); + } + + @Override public void log(Level level, String msg) { if (level == Level.INFO) { this.logger.info(msg); @@ -163,6 +182,15 @@ public class Sponge8SparkPlugin implements SparkPlugin { } @Override + public WorldInfoProvider createWorldInfoProvider() { + if (this.game.isServerAvailable()) { + return new Sponge8WorldInfoProvider(this.game.server()); + } else { + return WorldInfoProvider.NO_OP; + } + } + + @Override public PlatformInfo getPlatformInfo() { return new Sponge8PlatformInfo(this.game); } diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8WorldInfoProvider.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8WorldInfoProvider.java new file mode 100644 index 0000000..bff4d6e --- /dev/null +++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8WorldInfoProvider.java @@ -0,0 +1,88 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.sponge; + +import com.google.common.collect.Lists; + +import me.lucko.spark.common.platform.world.AbstractChunkInfo; +import me.lucko.spark.common.platform.world.CountMap; +import me.lucko.spark.common.platform.world.WorldInfoProvider; + +import org.spongepowered.api.Server; +import org.spongepowered.api.entity.Entity; +import org.spongepowered.api.entity.EntityType; +import org.spongepowered.api.entity.EntityTypes; +import org.spongepowered.api.world.chunk.WorldChunk; +import org.spongepowered.api.world.server.ServerWorld; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class Sponge8WorldInfoProvider implements WorldInfoProvider { + private final Server server; + + public Sponge8WorldInfoProvider(Server server) { + this.server = server; + } + + @Override + public Result<Sponge7ChunkInfo> poll() { + Result<Sponge7ChunkInfo> data = new Result<>(); + + for (ServerWorld world : this.server.worldManager().worlds()) { + List<WorldChunk> chunks = Lists.newArrayList(world.loadedChunks()); + + List<Sponge7ChunkInfo> list = new ArrayList<>(chunks.size()); + for (WorldChunk chunk : chunks) { + list.add(new Sponge7ChunkInfo(chunk)); + } + + data.put(world.key().value(), list); + } + + return data; + } + + static final class Sponge7ChunkInfo extends AbstractChunkInfo<EntityType<?>> { + private final CountMap<EntityType<?>> entityCounts; + + Sponge7ChunkInfo(WorldChunk chunk) { + super(chunk.chunkPosition().x(), chunk.chunkPosition().z()); + + this.entityCounts = new CountMap.Simple<>(new HashMap<>()); + for (Entity entity : chunk.entities()) { + this.entityCounts.increment(entity.type()); + } + } + + @Override + public CountMap<EntityType<?>> getEntityCounts() { + return this.entityCounts; + } + + @Override + public String entityTypeName(EntityType<?> type) { + return EntityTypes.registry().valueKey(type).value(); + } + + } +} |