-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8b81a46
Showing
18 changed files
with
539 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
gradlew | ||
gradlew.bat | ||
|
||
# gradle | ||
|
||
.gradle/ | ||
gradle/ | ||
build/ | ||
out/ | ||
classes/ | ||
|
||
# eclipse | ||
|
||
*.launch | ||
|
||
# idea | ||
|
||
.idea/ | ||
*.iml | ||
*.ipr | ||
*.iws | ||
|
||
# vscode | ||
|
||
.settings/ | ||
.vscode/ | ||
bin/ | ||
.classpath | ||
.project | ||
|
||
# fabric | ||
|
||
run/ | ||
logs/ |
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,22 @@ | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 Samuel Čavoj | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,75 @@ | ||
plugins { | ||
id 'fabric-loom' version '0.4-SNAPSHOT' | ||
id 'maven-publish' | ||
} | ||
|
||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
|
||
archivesBaseName = project.archives_base_name | ||
version = project.mod_version | ||
group = project.maven_group | ||
|
||
dependencies { | ||
//to change the versions see the gradle.properties file | ||
minecraft "com.mojang:minecraft:${project.minecraft_version}" | ||
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" | ||
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" | ||
|
||
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" | ||
} | ||
|
||
processResources { | ||
inputs.property "version", project.version | ||
|
||
from(sourceSets.main.resources.srcDirs) { | ||
include "fabric.mod.json" | ||
expand "version": project.version | ||
} | ||
|
||
from(sourceSets.main.resources.srcDirs) { | ||
exclude "fabric.mod.json" | ||
} | ||
} | ||
|
||
// ensure that the encoding is set to UTF-8, no matter what the system default is | ||
// this fixes some edge cases with special characters not displaying correctly | ||
// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html | ||
tasks.withType(JavaCompile) { | ||
options.encoding = "UTF-8" | ||
} | ||
|
||
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task | ||
// if it is present. | ||
// If you remove this task, sources will not be generated. | ||
task sourcesJar(type: Jar, dependsOn: classes) { | ||
classifier = "sources" | ||
from sourceSets.main.allSource | ||
} | ||
|
||
jar { | ||
from "LICENSE" | ||
} | ||
|
||
publishing { | ||
publications { | ||
mavenJava(MavenPublication) { | ||
artifact(remapJar) { | ||
builtBy remapJar | ||
} | ||
artifact(sourcesJar) { | ||
builtBy remapSourcesJar | ||
} | ||
} | ||
} | ||
|
||
repositories { | ||
maven { | ||
url = mavenUrl | ||
credentials { | ||
username mavenUser | ||
password mavenPassword | ||
} | ||
} | ||
} | ||
} |
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,14 @@ | ||
# Done to increase the memory available to gradle. | ||
org.gradle.jvmargs=-Xmx1G | ||
|
||
# Fabric Properties | ||
minecraft_version=1.16.2 | ||
yarn_mappings=1.16.2+build.6 | ||
loader_version=0.9.1+build.205 | ||
|
||
# Mod Properties | ||
mod_version = 1.0 | ||
maven_group = net.cavoj | ||
archives_base_name = servertick | ||
|
||
fabric_version=0.17.2+build.396-1.16 |
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,10 @@ | ||
pluginManagement { | ||
repositories { | ||
jcenter() | ||
maven { | ||
name = 'Fabric' | ||
url = 'https://maven.fabricmc.net/' | ||
} | ||
gradlePluginPortal() | ||
} | ||
} |
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,9 @@ | ||
package net.cavoj.servertick; | ||
|
||
import net.minecraft.util.Identifier; | ||
|
||
public class Packets { | ||
public static final Identifier PACKET_TOGGLE_DEBUG_SCREEN = new Identifier("servertick", "test"); | ||
public static final Identifier PACKET_FULL_METRICS = new Identifier("servertick", "metrics/full"); | ||
public static final Identifier PACKET_SAMPLE_METRICS = new Identifier("servertick", "metrics/sample"); | ||
} |
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,73 @@ | ||
package net.cavoj.servertick; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import net.fabricmc.api.ClientModInitializer; | ||
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry; | ||
import net.fabricmc.fabric.api.network.PacketContext; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.util.MetricsData; | ||
|
||
public class STClient implements ClientModInitializer { | ||
private static STClient _instance; | ||
|
||
public STClient() { | ||
if (_instance != null) { | ||
throw new RuntimeException("Cannot have multiple instances"); | ||
} | ||
_instance = this; | ||
} | ||
|
||
public static STClient getInstance() { | ||
return _instance; | ||
} | ||
|
||
@Override | ||
public void onInitializeClient() { | ||
ClientSidePacketRegistry.INSTANCE.register(Packets.PACKET_FULL_METRICS, this::processMetricsFullPacket); | ||
ClientSidePacketRegistry.INSTANCE.register(Packets.PACKET_SAMPLE_METRICS, this::processMetricsSamplePacket); | ||
} | ||
|
||
private void processMetricsFullPacket(PacketContext ctx, PacketByteBuf data) { | ||
// not sure if I can do this on the network thread | ||
if (this.metrics == null) | ||
this.metrics = new MetricsData(); | ||
((SerializableMetricsData)this.metrics).deserialize(data); | ||
} | ||
|
||
private void processMetricsSamplePacket(PacketContext ctx, PacketByteBuf data) { | ||
long time = data.readLong(); | ||
ctx.getTaskQueue().execute(() -> { | ||
if (this.metrics != null) | ||
this.metrics.pushSample(time); | ||
}); | ||
} | ||
|
||
private boolean debugTpsEnabled; | ||
private MetricsData metrics; | ||
|
||
public void setTpsEnabled(boolean enabled) { | ||
if (this.debugTpsEnabled != enabled) { | ||
this.debugTpsEnabled = enabled; | ||
updateTpsEnabled(); | ||
} | ||
} | ||
|
||
private void updateTpsEnabled() { | ||
PacketByteBuf data = new PacketByteBuf(Unpooled.buffer()); | ||
data.writeBoolean(this.debugTpsEnabled); | ||
ClientSidePacketRegistry.INSTANCE.sendToServer(Packets.PACKET_TOGGLE_DEBUG_SCREEN, data); | ||
} | ||
|
||
public void setMetricsData(MetricsData data) { | ||
this.metrics = data; | ||
} | ||
|
||
public MetricsData getMetricsData() { | ||
return this.metrics; | ||
} | ||
|
||
public void joined() { | ||
setMetricsData(null); | ||
setTpsEnabled(false); | ||
} | ||
} |
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 net.cavoj.servertick; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import net.cavoj.servertick.mixin.server.MinecraftServerAccessor; | ||
import net.fabricmc.api.DedicatedServerModInitializer; | ||
import net.fabricmc.fabric.api.network.PacketContext; | ||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.MetricsData; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class STServer implements DedicatedServerModInitializer { | ||
private final Set<PlayerEntity> listeners = new HashSet<>(); | ||
private static STServer _instance; | ||
|
||
public STServer() { | ||
if (_instance != null) { | ||
throw new RuntimeException("Cannot have multiple instances"); | ||
} | ||
_instance = this; | ||
} | ||
|
||
@Override | ||
public void onInitializeServer() { | ||
ServerSidePacketRegistry.INSTANCE.register(Packets.PACKET_TOGGLE_DEBUG_SCREEN, this::processTogglePacket); | ||
// ServerTickEvents.END_SERVER_TICK.register(this::onTick); | ||
} | ||
|
||
public static STServer getInstance() { | ||
return _instance; | ||
} | ||
|
||
// private int tickCounter = 0; | ||
// private void onTick(MinecraftServer server) { | ||
// tickCounter++; | ||
// if (tickCounter > 20) { | ||
// tickCounter = 0; | ||
// // Send full metrics every so often to prevent desync | ||
// listeners.forEach(player -> sendMetrics(server, player)); | ||
// } | ||
// } | ||
|
||
private void sendMetrics(MinecraftServer server, PlayerEntity player) { | ||
MetricsData metrics = ((MinecraftServerAccessor)server).getMetricsData(); | ||
PacketByteBuf data = new PacketByteBuf(Unpooled.buffer()); | ||
((SerializableMetricsData)metrics).serialize(data); | ||
ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, Packets.PACKET_FULL_METRICS, data); | ||
} | ||
|
||
private void processTogglePacket(PacketContext ctx, PacketByteBuf data) { | ||
boolean state = data.readBoolean(); | ||
PlayerEntity player = ctx.getPlayer(); | ||
MinecraftServer server = ctx.getPlayer().getServer(); | ||
ctx.getTaskQueue().execute(() -> { | ||
if (state) { | ||
if (player.hasPermissionLevel(4)) { | ||
listeners.add(player); | ||
sendMetrics(server, player); | ||
} | ||
} else { | ||
listeners.remove(player); | ||
} | ||
}); | ||
} | ||
|
||
public void pushSample(long time) { | ||
PacketByteBuf data = new PacketByteBuf(Unpooled.buffer()); | ||
data.writeLong(time); | ||
listeners.forEach(player -> ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, Packets.PACKET_SAMPLE_METRICS, data)); | ||
} | ||
|
||
public void onPlayerDisconnected(ServerPlayerEntity player) { | ||
this.listeners.remove(player); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/net/cavoj/servertick/SerializableMetricsData.java
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 net.cavoj.servertick; | ||
|
||
import net.minecraft.network.PacketByteBuf; | ||
|
||
public interface SerializableMetricsData { | ||
void deserialize(PacketByteBuf data); | ||
void serialize(PacketByteBuf data); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/net/cavoj/servertick/mixin/MetricsDataMixin.java
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,43 @@ | ||
package net.cavoj.servertick.mixin; | ||
|
||
import net.cavoj.servertick.STServer; | ||
import net.cavoj.servertick.SerializableMetricsData; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.util.MetricsData; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(MetricsData.class) | ||
public abstract class MetricsDataMixin implements SerializableMetricsData { | ||
|
||
@Shadow @Final private long[] samples; | ||
|
||
@Shadow private int writeIndex; | ||
|
||
@Shadow private int sampleCount; | ||
|
||
@Shadow private int startIndex; | ||
|
||
@Override | ||
public void deserialize(PacketByteBuf data) { | ||
this.writeIndex = data.readInt(); | ||
this.sampleCount = data.readInt(); | ||
this.startIndex = data.readInt(); | ||
for (int i = 0; i < this.samples.length; i++) | ||
this.samples[i] = data.readLong(); | ||
} | ||
|
||
@Override | ||
public void serialize(PacketByteBuf data) { | ||
data.writeInt(this.writeIndex); | ||
data.writeInt(this.sampleCount); | ||
data.writeInt(this.startIndex); | ||
for (int i = 0; i < this.samples.length; i++) | ||
data.writeLong(this.samples[i]); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/net/cavoj/servertick/mixin/client/ClientPlayNetworkHandlerMixin.java
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,16 @@ | ||
package net.cavoj.servertick.mixin.client; | ||
|
||
import net.cavoj.servertick.STClient; | ||
import net.minecraft.client.network.ClientPlayNetworkHandler; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
@Mixin(ClientPlayNetworkHandler.class) | ||
public abstract class ClientPlayNetworkHandlerMixin { | ||
@Inject(method = "onGameJoin", at = @At("HEAD")) | ||
public void onGameJoin(CallbackInfo ci) { | ||
STClient.getInstance().joined(); | ||
} | ||
} |
Oops, something went wrong.