Make client net handling run on main thread

This commit is contained in:
Sensei authored and gnembon committed 2022-10-10 20:30:25 +02:00
1 parent a50a0d7328
commit 059fda49a4
4 files changed
+34 -58

No files matched your search

@@ -7,6 +7,7 @@ import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket; import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.game.ClientboundLoginPacket; import net.minecraft.network.protocol.game.ClientboundLoginPacket;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@@ -16,9 +17,9 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ClientPacketListener.class) @Mixin(ClientPacketListener.class)
public abstract class ClientPacketListener_customPacketsMixin public abstract class ClientPacketListener_customPacketsMixin
{ {
@Shadow private Minecraft minecraft; @Final @Shadow private Minecraft minecraft;
@Inject(method = "handleCustomPayload", at = @At("HEAD"), cancellable = true) @Inject(method = "handleCustomPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;getIdentifier()Lnet/minecraft/resources/ResourceLocation;"), cancellable = true)
private void onOnCustomPayload(ClientboundCustomPayloadPacket packet, CallbackInfo ci) private void onOnCustomPayload(ClientboundCustomPayloadPacket packet, CallbackInfo ci)
{ {
if (CarpetClient.CARPET_CHANNEL.equals(packet.getIdentifier())) if (CarpetClient.CARPET_CHANNEL.equals(packet.getIdentifier()))
@@ -39,5 +40,4 @@ public abstract class ClientPacketListener_customPacketsMixin
{ {
CarpetClient.disconnect(); CarpetClient.disconnect();
} }
} }
@@ -12,7 +12,6 @@ import net.minecraft.resources.ResourceLocation;
public class CarpetClient public class CarpetClient
{ {
public static final Object sync = new Object();
public static final int HI = 69; public static final int HI = 69;
public static final int HELLO = 420; public static final int HELLO = 420;
public static final int DATA = 1; public static final int DATA = 1;
@@ -25,14 +24,7 @@ public class CarpetClient
public static void gameJoined(LocalPlayer player) public static void gameJoined(LocalPlayer player)
{ {
synchronized (sync) clientPlayer = player;
{
clientPlayer = player;
// client didn't say hi back yet
if (isServerCarpet)
ClientNetworkHandler.respondHello();
}
} }
public static void disconnect() public static void disconnect()
@@ -87,6 +87,7 @@ public class ClientNetworkHandler
}); });
}; };
// Ran on the Main Minecraft Thread
public static void handleData(FriendlyByteBuf data, LocalPlayer player) public static void handleData(FriendlyByteBuf data, LocalPlayer player)
{ {
if (data != null) if (data != null)
@@ -101,22 +102,19 @@ public class ClientNetworkHandler
private static void onHi(FriendlyByteBuf data) private static void onHi(FriendlyByteBuf data)
{ {
synchronized (CarpetClient.sync) CarpetClient.setCarpet();
CarpetClient.serverCarpetVersion = data.readUtf(64);
if (CarpetSettings.carpetVersion.equals(CarpetClient.serverCarpetVersion))
{ {
CarpetClient.setCarpet(); CarpetSettings.LOG.info("Joined carpet server with matching carpet version");
CarpetClient.serverCarpetVersion = data.readUtf(64);
if (CarpetSettings.carpetVersion.equals(CarpetClient.serverCarpetVersion))
{
CarpetSettings.LOG.info("Joined carpet server with matching carpet version");
}
else
{
CarpetSettings.LOG.warn("Joined carpet server with another carpet version: "+CarpetClient.serverCarpetVersion);
}
if (CarpetClient.getPlayer() != null)
respondHello();
} }
else
{
CarpetSettings.LOG.warn("Joined carpet server with another carpet version: "+CarpetClient.serverCarpetVersion);
}
// We can ensure that this packet is
// processed AFTER the player has joined
respondHello();
} }
public static void respondHello() public static void respondHello()
@@ -113,7 +113,7 @@ public class ShapesRenderer
double cameraZ = camera.getPosition().z; double cameraZ = camera.getPosition().z;
boolean entityBoxes = client.getEntityRenderDispatcher().shouldRenderHitBoxes(); boolean entityBoxes = client.getEntityRenderDispatcher().shouldRenderHitBoxes();
if (shapes.size() != 0) { synchronized (shapes) { if (shapes.size() != 0) {
shapes.get(dimensionType).long2ObjectEntrySet().removeIf( shapes.get(dimensionType).long2ObjectEntrySet().removeIf(
entry -> entry.getValue().isExpired(currentTime) entry -> entry.getValue().isExpired(currentTime)
); );
@@ -136,16 +136,15 @@ public class ShapesRenderer
matrixStack.popPose(); matrixStack.popPose();
RenderSystem.applyModelViewMatrix(); RenderSystem.applyModelViewMatrix();
}} }
if (labels.size() != 0) { synchronized (labels) if (labels.size() != 0) {
{
labels.get(dimensionType).long2ObjectEntrySet().removeIf( labels.get(dimensionType).long2ObjectEntrySet().removeIf(
entry -> entry.getValue().isExpired(currentTime) entry -> entry.getValue().isExpired(currentTime)
); );
labels.get(dimensionType).values().forEach(s -> { labels.get(dimensionType).values().forEach(s -> {
if ( (!s.shape.debug || entityBoxes) && s.shouldRender(dimensionType)) s.renderLines(matrices, tessellator, bufferBuilder, cameraX, cameraY, cameraZ, partialTick); if ( (!s.shape.debug || entityBoxes) && s.shouldRender(dimensionType)) s.renderLines(matrices, tessellator, bufferBuilder, cameraX, cameraY, cameraZ, partialTick);
}); });
}} }
RenderSystem.enableCull(); RenderSystem.enableCull();
RenderSystem.depthMask(true); RenderSystem.depthMask(true);
RenderSystem.enableBlend(); RenderSystem.enableBlend();
@@ -181,43 +180,30 @@ public class ShapesRenderer
long key = rshape.key(); long key = rshape.key();
Map<ResourceKey<Level>, Long2ObjectOpenHashMap<RenderedShape<? extends ShapeDispatcher.ExpiringShape>>> container = Map<ResourceKey<Level>, Long2ObjectOpenHashMap<RenderedShape<? extends ShapeDispatcher.ExpiringShape>>> container =
rshape.stageDeux()?labels:shapes; rshape.stageDeux()?labels:shapes;
synchronized (container) RenderedShape<?> existing = container.computeIfAbsent(dim, d -> new Long2ObjectOpenHashMap<>()).get(key);
{ if (existing != null)
RenderedShape<?> existing = container.computeIfAbsent(dim, d -> new Long2ObjectOpenHashMap<>()).get(key); { // promoting previous shape
if (existing != null) existing.promoteWith(rshape);
{ // promoting previous shape
existing.promoteWith(rshape);
}
else
{
container.get(dim).put(key, rshape);
}
} }
else
{
container.get(dim).put(key, rshape);
}
} }
} }
public void reset() public void reset()
{ {
synchronized (shapes) shapes.values().forEach(Long2ObjectOpenHashMap::clear);
{ labels.values().forEach(Long2ObjectOpenHashMap::clear);
shapes.values().forEach(Long2ObjectOpenHashMap::clear);
}
synchronized (labels)
{
labels.values().forEach(Long2ObjectOpenHashMap::clear);
}
} }
public void renewShapes() public void renewShapes()
{ {
CarpetProfiler.ProfilerToken token = CarpetProfiler.start_section(null, "Scarpet client", CarpetProfiler.TYPE.GENERAL); CarpetProfiler.ProfilerToken token = CarpetProfiler.start_section(null, "Scarpet client", CarpetProfiler.TYPE.GENERAL);
synchronized (shapes) shapes.values().forEach(el -> el.values().forEach(shape -> shape.expiryTick++));
{ labels.values().forEach(el -> el.values().forEach(shape -> shape.expiryTick++));
shapes.values().forEach(el -> el.values().forEach(shape -> shape.expiryTick++));
}
synchronized (labels)
{
labels.values().forEach(el -> el.values().forEach(shape -> shape.expiryTick++));
}
CarpetProfiler.end_current_section(token); CarpetProfiler.end_current_section(token);
} }