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-bukkit | |
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-bukkit')
-rw-r--r-- | spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java | 13 | ||||
-rw-r--r-- | spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitWorldInfoProvider.java | 87 |
2 files changed, 99 insertions, 1 deletions
diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java index 9727277..fddd66b 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java @@ -28,6 +28,7 @@ import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.monitor.ping.PlayerPingProvider; import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.platform.serverconfig.ServerConfigProvider; +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.tick.TickReporter; @@ -136,7 +137,12 @@ public class BukkitSparkPlugin extends JavaPlugin implements SparkPlugin { @Override public void executeAsync(Runnable task) { - getServer().getScheduler().runTaskAsynchronously(BukkitSparkPlugin.this, task); + getServer().getScheduler().runTaskAsynchronously(this, task); + } + + @Override + public void executeSync(Runnable task) { + getServer().getScheduler().runTask(this, task); } @Override @@ -188,6 +194,11 @@ public class BukkitSparkPlugin extends JavaPlugin implements SparkPlugin { } @Override + public WorldInfoProvider createWorldInfoProvider() { + return new BukkitWorldInfoProvider(getServer()); + } + + @Override public PlatformInfo getPlatformInfo() { return new BukkitPlatformInfo(getServer()); } diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitWorldInfoProvider.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitWorldInfoProvider.java new file mode 100644 index 0000000..f34899b --- /dev/null +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitWorldInfoProvider.java @@ -0,0 +1,87 @@ +/* + * 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.bukkit; + +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.bukkit.Chunk; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class BukkitWorldInfoProvider implements WorldInfoProvider { + private final Server server; + + public BukkitWorldInfoProvider(Server server) { + this.server = server; + } + + @Override + public Result<BukkitChunkInfo> poll() { + Result<BukkitChunkInfo> data = new Result<>(); + + for (World world : this.server.getWorlds()) { + Chunk[] chunks = world.getLoadedChunks(); + + List<BukkitChunkInfo> list = new ArrayList<>(chunks.length); + for (Chunk chunk : chunks) { + list.add(new BukkitChunkInfo(chunk)); + } + + data.put(world.getName(), list); + } + + return data; + } + + static final class BukkitChunkInfo extends AbstractChunkInfo<EntityType> { + private final CountMap<EntityType> entityCounts; + + BukkitChunkInfo(Chunk chunk) { + super(chunk.getX(), chunk.getZ()); + + this.entityCounts = new CountMap.EnumKeyed<>(EntityType.class); + for (Entity entity : chunk.getEntities()) { + this.entityCounts.increment(entity.getType()); + } + } + + @Override + public CountMap<EntityType> getEntityCounts() { + return this.entityCounts; + } + + @SuppressWarnings("deprecation") + @Override + public String entityTypeName(EntityType type) { + return type.getName(); + } + + } + +} |