diff options
author | lucko <git@lucko.me> | 2022-12-27 09:17:54 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-27 09:17:54 +0000 |
commit | e5b278047ccb7bc6b301d787474c51d162911867 (patch) | |
tree | 11bba64e8f28ce8b83adc05252b75f17e2ccbf6a /spark-sponge7/src/main/java | |
parent | 4a16a1a2f4eb09f706b4a541e3d31618de29420b (diff) | |
parent | 1075665def4a41cf0064255a6da1d1a652f5d473 (diff) | |
download | spark-e5b278047ccb7bc6b301d787474c51d162911867.tar.gz spark-e5b278047ccb7bc6b301d787474c51d162911867.tar.bz2 spark-e5b278047ccb7bc6b301d787474c51d162911867.zip |
Merge pull request #284 from embeddedt/forge-1.7.10
Align 1.7.10 with master
Diffstat (limited to 'spark-sponge7/src/main/java')
3 files changed, 137 insertions, 10 deletions
diff --git a/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7ClassSourceLookup.java b/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7ClassSourceLookup.java index 90f3b8f..899ce58 100644 --- a/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7ClassSourceLookup.java +++ b/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7ClassSourceLookup.java @@ -20,7 +20,7 @@ package me.lucko.spark.sponge; -import me.lucko.spark.common.util.ClassSourceLookup; +import me.lucko.spark.common.sampler.source.ClassSourceLookup; import org.spongepowered.api.Game; diff --git a/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7SparkPlugin.java b/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7SparkPlugin.java index 670e0c5..0e3f4eb 100644 --- a/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7SparkPlugin.java +++ b/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7SparkPlugin.java @@ -27,9 +27,10 @@ import me.lucko.spark.common.SparkPlatform; 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.world.WorldInfoProvider; import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.common.sampler.source.ClassSourceLookup; import me.lucko.spark.common.tick.TickHook; -import me.lucko.spark.common.util.ClassSourceLookup; import org.slf4j.Logger; import org.spongepowered.api.Game; @@ -44,6 +45,7 @@ import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.scheduler.AsynchronousExecutor; import org.spongepowered.api.scheduler.SpongeExecutorService; +import org.spongepowered.api.scheduler.SynchronousExecutor; import org.spongepowered.api.text.Text; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -70,17 +72,21 @@ public class Sponge7SparkPlugin implements SparkPlugin { private final Game game; private final Path configDirectory; private final SpongeExecutorService asyncExecutor; + private final SpongeExecutorService syncExecutor; + private final ThreadDumper.GameThread gameThreadDumper = new ThreadDumper.GameThread(); private SparkPlatform platform; - private final ThreadDumper.GameThread threadDumper = new ThreadDumper.GameThread(); @Inject - public Sponge7SparkPlugin(PluginContainer pluginContainer, Logger logger, Game game, @ConfigDir(sharedRoot = false) Path configDirectory, @AsynchronousExecutor SpongeExecutorService asyncExecutor) { + public Sponge7SparkPlugin(PluginContainer pluginContainer, Logger logger, Game game, @ConfigDir(sharedRoot = false) Path configDirectory, @AsynchronousExecutor SpongeExecutorService asyncExecutor, @SynchronousExecutor SpongeExecutorService syncExecutor) { this.pluginContainer = pluginContainer; this.logger = logger; this.game = game; this.configDirectory = configDirectory; this.asyncExecutor = asyncExecutor; + this.syncExecutor = syncExecutor; + + this.syncExecutor.execute(() -> this.gameThreadDumper.setThread(Thread.currentThread())); } @Listener @@ -112,10 +118,14 @@ public class Sponge7SparkPlugin implements SparkPlugin { @Override public Stream<Sponge7CommandSender> getCommandSenders() { - return Stream.concat( - this.game.getServer().getOnlinePlayers().stream(), - Stream.of(this.game.getServer().getConsole()) - ).map(Sponge7CommandSender::new); + if (this.game.isServerAvailable()) { + return Stream.concat( + this.game.getServer().getOnlinePlayers().stream(), + Stream.of(this.game.getServer().getConsole()) + ).map(Sponge7CommandSender::new); + } else { + return Stream.of(this.game.getServer().getConsole()).map(Sponge7CommandSender::new); + } } @Override @@ -124,6 +134,11 @@ public class Sponge7SparkPlugin 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); @@ -138,7 +153,7 @@ public class Sponge7SparkPlugin implements SparkPlugin { @Override public ThreadDumper getDefaultThreadDumper() { - return this.threadDumper.get(); + return this.gameThreadDumper.get(); } @Override @@ -161,6 +176,15 @@ public class Sponge7SparkPlugin implements SparkPlugin { } @Override + public WorldInfoProvider createWorldInfoProvider() { + if (this.game.isServerAvailable()) { + return new Sponge7WorldInfoProvider(this.game.getServer()); + } else { + return WorldInfoProvider.NO_OP; + } + } + + @Override public PlatformInfo getPlatformInfo() { return new Sponge7PlatformInfo(this.game); } @@ -179,7 +203,6 @@ public class Sponge7SparkPlugin implements SparkPlugin { @Override public CommandResult process(CommandSource source, String arguments) { - this.plugin.threadDumper.ensureSetup(); this.plugin.platform.executeCommand(new Sponge7CommandSender(source), arguments.split(" ")); return CommandResult.empty(); } diff --git a/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7WorldInfoProvider.java b/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7WorldInfoProvider.java new file mode 100644 index 0000000..df58028 --- /dev/null +++ b/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7WorldInfoProvider.java @@ -0,0 +1,104 @@ +/* + * 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.Iterables; +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.world.Chunk; +import org.spongepowered.api.world.World; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class Sponge7WorldInfoProvider implements WorldInfoProvider { + private final Server server; + + public Sponge7WorldInfoProvider(Server server) { + this.server = server; + } + + @Override + public CountsResult pollCounts() { + int players = this.server.getOnlinePlayers().size(); + int entities = 0; + int tileEntities = 0; + int chunks = 0; + + for (World world : this.server.getWorlds()) { + entities += world.getEntities().size(); + tileEntities += world.getTileEntities().size(); + chunks += Iterables.size(world.getLoadedChunks()); + } + + return new CountsResult(players, entities, tileEntities, chunks); + } + + @Override + public ChunksResult<Sponge7ChunkInfo> pollChunks() { + ChunksResult<Sponge7ChunkInfo> data = new ChunksResult<>(); + + for (World world : this.server.getWorlds()) { + List<Chunk> chunks = Lists.newArrayList(world.getLoadedChunks()); + + List<Sponge7ChunkInfo> list = new ArrayList<>(chunks.size()); + for (Chunk chunk : chunks) { + list.add(new Sponge7ChunkInfo(chunk)); + } + + data.put(world.getName(), list); + } + + return data; + } + + static final class Sponge7ChunkInfo extends AbstractChunkInfo<EntityType> { + private final CountMap<EntityType> entityCounts; + + Sponge7ChunkInfo(Chunk chunk) { + super(chunk.getPosition().getX(), chunk.getPosition().getZ()); + + this.entityCounts = new CountMap.Simple<>(new HashMap<>()); + for (Entity entity : chunk.getEntities()) { + this.entityCounts.increment(entity.getType()); + } + } + + @Override + public CountMap<EntityType> getEntityCounts() { + return this.entityCounts; + } + + @Override + public String entityTypeName(EntityType type) { + return type.getName(); + } + + } +} |