This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Host a HTTP server instead of sending data to third party.
- Loading branch information
Showing
7 changed files
with
152 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package ct.server; | ||
|
||
import net.fabricmc.api.ModInitializer; | ||
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents; | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; | ||
|
||
import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents; | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Instant; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
|
||
public class CtServer implements ModInitializer { | ||
public static final String MOD_ID = "ct-server"; | ||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); | ||
|
||
public static final ct.server.CtServerConfig CONFIG = ct.server.CtServerConfig.createAndLoad(); | ||
|
||
private static float currentTps = 0; | ||
private static float currentMspt = 0; | ||
private static int currentPlayerCount = 0; | ||
|
||
private static CtServer INSTANCE; | ||
|
||
public static CtServer getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public void onInitialize() { | ||
INSTANCE = this; | ||
|
||
LOGGER.info("Starting ct-client"); | ||
|
||
ServerTickEvents.END_SERVER_TICK.register(server -> { | ||
currentMspt = server.getAverageTickTime(); | ||
if (currentMspt != 0) { | ||
currentTps = Math.min(20, 1000 / currentMspt); | ||
} | ||
}); | ||
|
||
ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> { | ||
if(entity instanceof ServerPlayerEntity) { | ||
currentPlayerCount = world.getServer().getCurrentPlayerCount(); | ||
} | ||
}); | ||
|
||
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { | ||
currentPlayerCount = server.getCurrentPlayerCount() - 1; | ||
}); | ||
|
||
try { | ||
var httpServer = new ServiceHttpServer(); | ||
} catch (IOException e) { | ||
LOGGER.error("Unable to start HTTP server", e); | ||
} | ||
} | ||
|
||
public static float getTPS() { | ||
return currentTps; | ||
} | ||
|
||
public static float getMSPT() { | ||
return currentMspt; | ||
} | ||
|
||
public static int getPlayerCount() { | ||
return currentPlayerCount; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ct.server; | ||
|
||
import io.wispforest.owo.config.annotation.Config; | ||
|
||
@Config(name = "ct-server-config", wrapperName = "CtServerConfig") | ||
public class CtServerConfigModel { | ||
public short httpPort = 25581; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ct.server; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
|
||
public class ServiceHttpServer { | ||
private HttpServer server; | ||
|
||
public ServiceHttpServer() throws IOException { | ||
server = HttpServer.create(new InetSocketAddress(CtServer.CONFIG.httpPort()), 0); | ||
server.createContext("/tps", new TPSHandler()); | ||
server.createContext("/mspt", new MSPTHandler()); | ||
server.createContext("/player", new PlayerCountHandler()); | ||
server.setExecutor(null); | ||
|
||
var httpThread = new Thread(() -> server.start()); | ||
httpThread.start(); | ||
} | ||
|
||
static class TPSHandler implements HttpHandler { | ||
@Override | ||
public void handle(HttpExchange t) throws IOException { | ||
var tps = String.valueOf(CtServer.getTPS()); | ||
t.sendResponseHeaders(200, tps.length()); | ||
var body = t.getResponseBody(); | ||
body.write(tps.getBytes()); | ||
body.close(); | ||
} | ||
} | ||
|
||
static class MSPTHandler implements HttpHandler { | ||
@Override | ||
public void handle(HttpExchange t) throws IOException { | ||
var tps = String.valueOf(CtServer.getMSPT()); | ||
t.sendResponseHeaders(200, tps.length()); | ||
var body = t.getResponseBody(); | ||
body.write(tps.getBytes()); | ||
body.close(); | ||
} | ||
} | ||
|
||
static class PlayerCountHandler implements HttpHandler { | ||
@Override | ||
public void handle(HttpExchange t) throws IOException { | ||
var tps = String.valueOf(CtServer.getPlayerCount()); | ||
t.sendResponseHeaders(200, tps.length()); | ||
var body = t.getResponseBody(); | ||
body.write(tps.getBytes()); | ||
body.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters