diff options
Diffstat (limited to 'spark-forge1710/src')
4 files changed, 218 insertions, 0 deletions
diff --git a/spark-forge1710/src/main/java/me/lucko/spark/forge/Forge1710PlayerPingProvider.java b/spark-forge1710/src/main/java/me/lucko/spark/forge/Forge1710PlayerPingProvider.java new file mode 100644 index 0000000..11ee45a --- /dev/null +++ b/spark-forge1710/src/main/java/me/lucko/spark/forge/Forge1710PlayerPingProvider.java @@ -0,0 +1,48 @@ +/* + * 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.forge; + +import com.google.common.collect.ImmutableMap; + +import me.lucko.spark.common.monitor.ping.PlayerPingProvider; + +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.server.MinecraftServer; + +import java.util.List; +import java.util.Map; + +public class Forge1710PlayerPingProvider implements PlayerPingProvider { + private final MinecraftServer server; + + public Forge1710PlayerPingProvider(MinecraftServer server) { + this.server = server; + } + + @Override + public Map<String, Integer> poll() { + ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); + for (EntityPlayerMP player : (List<EntityPlayerMP>)this.server.getConfigurationManager().playerEntityList) { + builder.put(player.getGameProfile().getName(), player.ping); + } + return builder.build(); + } +} diff --git a/spark-forge1710/src/main/java/me/lucko/spark/forge/Forge1710WorldInfoProvider.java b/spark-forge1710/src/main/java/me/lucko/spark/forge/Forge1710WorldInfoProvider.java new file mode 100644 index 0000000..86ef8ec --- /dev/null +++ b/spark-forge1710/src/main/java/me/lucko/spark/forge/Forge1710WorldInfoProvider.java @@ -0,0 +1,126 @@ +/* + * 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.forge; + +import cpw.mods.fml.relauncher.ReflectionHelper; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +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 net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ChunkProviderClient; +import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityList; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.WorldServer; +import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.IChunkProvider; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public abstract class Forge1710WorldInfoProvider implements WorldInfoProvider { + public static final class Server extends Forge1710WorldInfoProvider { + private final MinecraftServer server; + + public Server(MinecraftServer server) { + this.server = server; + } + + @Override + public Result<ForgeChunkInfo> poll() { + Result<ForgeChunkInfo> data = new Result<>(); + + for (WorldServer level : this.server.worldServers) { + ArrayList<ForgeChunkInfo> list = new ArrayList<>(); + for(Chunk chunk : (List<Chunk>)level.theChunkProviderServer.loadedChunks) { + list.add(new ForgeChunkInfo(chunk)); + } + data.put(level.provider.getDimensionName(), list); + } + + return data; + } + } + + @SideOnly(Side.CLIENT) + public static final class Client extends Forge1710WorldInfoProvider { + private final Minecraft client; + + public Client(Minecraft client) { + this.client = client; + } + + @Override + public Result<ForgeChunkInfo> poll() { + Result<ForgeChunkInfo> data = new Result<>(); + + WorldClient level = this.client.theWorld; + if (level == null) { + return null; + } + + ArrayList<ForgeChunkInfo> list = new ArrayList<>(); + IChunkProvider provider = level.getChunkProvider(); + if(provider instanceof ChunkProviderClient) { + List<Chunk> chunks = ReflectionHelper.getPrivateValue(ChunkProviderClient.class, (ChunkProviderClient)provider, "chunkMapping", "field_73236_b"); + for(Chunk chunk : chunks) { + list.add(new ForgeChunkInfo(chunk)); + } + } + + data.put(level.provider.getDimensionName(), list); + + return data; + } + } + + static final class ForgeChunkInfo extends AbstractChunkInfo<Class<? extends Entity>> { + private final CountMap<Class<? extends Entity>> entityCounts; + + ForgeChunkInfo(Chunk chunk) { + super(chunk.xPosition, chunk.zPosition); + + this.entityCounts = new CountMap.Simple<>(new HashMap<>()); + for(List<Entity> entityList : chunk.entityLists) { + entityList.forEach(entity -> { + this.entityCounts.increment(entity.getClass()); + }); + } + } + + @Override + public CountMap<Class<? extends Entity>> getEntityCounts() { + return this.entityCounts; + } + + @Override + public String entityTypeName(Class<? extends Entity> type) { + return (String)EntityList.classToStringMapping.get(type); + } + } + + +} diff --git a/spark-forge1710/src/main/java/me/lucko/spark/forge/plugin/Forge1710ClientSparkPlugin.java b/spark-forge1710/src/main/java/me/lucko/spark/forge/plugin/Forge1710ClientSparkPlugin.java index 305b06e..d0ae1ed 100644 --- a/spark-forge1710/src/main/java/me/lucko/spark/forge/plugin/Forge1710ClientSparkPlugin.java +++ b/spark-forge1710/src/main/java/me/lucko/spark/forge/plugin/Forge1710ClientSparkPlugin.java @@ -22,6 +22,7 @@ package me.lucko.spark.forge.plugin; import cpw.mods.fml.common.gameevent.TickEvent; import me.lucko.spark.common.platform.PlatformInfo; +import me.lucko.spark.common.platform.world.WorldInfoProvider; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; import me.lucko.spark.forge.*; @@ -73,6 +74,16 @@ public class Forge1710ClientSparkPlugin extends Forge1710SparkPlugin { } @Override + public WorldInfoProvider createWorldInfoProvider() { + return new Forge1710WorldInfoProvider.Client(Minecraft.getMinecraft()); + } + + @Override + public void executeSync(Runnable task) { + this.minecraft.addScheduledTask(task); + } + + @Override public PlatformInfo getPlatformInfo() { return new Forge1710PlatformInfo(PlatformInfo.Type.CLIENT); } diff --git a/spark-forge1710/src/main/java/me/lucko/spark/forge/plugin/Forge1710ServerSparkPlugin.java b/spark-forge1710/src/main/java/me/lucko/spark/forge/plugin/Forge1710ServerSparkPlugin.java index 303739f..8dff67e 100644 --- a/spark-forge1710/src/main/java/me/lucko/spark/forge/plugin/Forge1710ServerSparkPlugin.java +++ b/spark-forge1710/src/main/java/me/lucko/spark/forge/plugin/Forge1710ServerSparkPlugin.java @@ -20,9 +20,13 @@ package me.lucko.spark.forge.plugin; +import com.google.common.collect.Queues; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.event.FMLServerStartingEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import me.lucko.spark.common.platform.PlatformInfo; +import me.lucko.spark.common.platform.world.WorldInfoProvider; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; import me.lucko.spark.forge.*; @@ -32,20 +36,37 @@ import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.server.MinecraftServer; import java.util.List; +import java.util.Queue; +import java.util.concurrent.FutureTask; import java.util.stream.Stream; public class Forge1710ServerSparkPlugin extends Forge1710SparkPlugin { + private final Queue<Runnable> scheduledServerTasks = Queues.newArrayDeque(); public static Forge1710ServerSparkPlugin register(Forge1710SparkMod mod, FMLServerStartingEvent event) { Forge1710ServerSparkPlugin plugin = new Forge1710ServerSparkPlugin(mod, event.getServer()); plugin.enable(); + FMLCommonHandler.instance().bus().register(plugin); + // register commands & permissions event.registerServerCommand(plugin); return plugin; } + @SubscribeEvent + public void onServerTickEnd(TickEvent.ServerTickEvent event) { + if(event.phase == TickEvent.Phase.START) { + synchronized(scheduledServerTasks) { + while (!scheduledServerTasks.isEmpty()) + { + scheduledServerTasks.poll().run(); + } + } + } + } + private final MinecraftServer server; public Forge1710ServerSparkPlugin(Forge1710SparkMod mod, MinecraftServer server) { @@ -82,6 +103,18 @@ public class Forge1710ServerSparkPlugin extends Forge1710SparkPlugin { } @Override + public WorldInfoProvider createWorldInfoProvider() { + return new Forge1710WorldInfoProvider.Server(FMLCommonHandler.instance().getMinecraftServerInstance()); + } + + @Override + public void executeSync(Runnable task) { + synchronized (scheduledServerTasks) { + scheduledServerTasks.add(task); + } + } + + @Override public PlatformInfo getPlatformInfo() { return new Forge1710PlatformInfo(PlatformInfo.Type.SERVER); } |