-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: add ext_render_distance_v1 protocol for 127+ RD multiplayer
- Loading branch information
Showing
23 changed files
with
360 additions
and
4 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
8 changes: 8 additions & 0 deletions
8
c2me-base/src/main/java/com/ishland/c2me/base/common/C2MEConstants.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 com.ishland.c2me.base.common; | ||
|
||
public class C2MEConstants { | ||
|
||
public static final String MODID = "c2me"; | ||
public static final String EXT_RENDER_DISTANCE_ID = "ext_render_distance_v1"; // consists of one VarInt | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
c2me-base/src/main/java/com/ishland/c2me/base/common/config/ModStatuses.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,9 @@ | ||
package com.ishland.c2me.base.common.config; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
|
||
public class ModStatuses { | ||
|
||
public static final boolean fabric_networking_api_v1 = FabricLoader.getInstance().isModLoaded("fabric-networking-api-v1"); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
c2me-base/src/main/java/com/ishland/c2me/base/common/network/ExtRenderDistance.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,29 @@ | ||
package com.ishland.c2me.base.common.network; | ||
|
||
import com.ishland.c2me.base.common.C2MEConstants; | ||
import net.fabricmc.fabric.api.networking.v1.FabricPacket; | ||
import net.fabricmc.fabric.api.networking.v1.PacketType; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.util.Identifier; | ||
|
||
public record ExtRenderDistance(int renderDistance) implements FabricPacket { | ||
|
||
public static PacketType<ExtRenderDistance> TYPE = PacketType.create( | ||
new Identifier(C2MEConstants.MODID, C2MEConstants.EXT_RENDER_DISTANCE_ID), | ||
ExtRenderDistance::new | ||
); | ||
|
||
public ExtRenderDistance(PacketByteBuf buf) { | ||
this(buf.readVarInt()); | ||
} | ||
|
||
@Override | ||
public void write(PacketByteBuf buf) { | ||
buf.writeVarInt(renderDistance); | ||
} | ||
|
||
@Override | ||
public PacketType<?> getType() { | ||
return TYPE; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
c2me-base/src/main/java/com/ishland/c2me/base/mixin/access/ISyncedClientOptions.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,15 @@ | ||
package com.ishland.c2me.base.mixin.access; | ||
|
||
import net.minecraft.network.packet.c2s.common.SyncedClientOptions; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Mutable; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
@Mixin(SyncedClientOptions.class) | ||
public interface ISyncedClientOptions { | ||
|
||
@Mutable | ||
@Accessor | ||
void setViewDistance(int viewDistance); | ||
|
||
} |
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
55 changes: 55 additions & 0 deletions
55
...ent-uncapvd/src/main/java/com/ishland/c2me/client/uncapvd/common/ClientExtNetworking.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,55 @@ | ||
package com.ishland.c2me.client.uncapvd.common; | ||
|
||
import com.ishland.c2me.base.common.network.ExtRenderDistance; | ||
import net.fabricmc.fabric.api.client.networking.v1.C2SPlayChannelEvents; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | ||
import net.minecraft.client.MinecraftClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.function.BooleanSupplier; | ||
|
||
public class ClientExtNetworking { | ||
|
||
public static final Logger LOGGER = LoggerFactory.getLogger("C2ME ClientExtNetworking"); | ||
|
||
public static void sendViewDistance(int viewDistance) { | ||
if (!MinecraftClient.getInstance().isOnThread()) { | ||
new IllegalStateException("Tried to send packet not on client thread!").printStackTrace(); | ||
return; | ||
} | ||
if (catchExceptions(() -> ClientPlayNetworking.canSend(ExtRenderDistance.TYPE))) { | ||
LOGGER.info("Changing view distance to {} (play stage)", viewDistance); | ||
ClientPlayNetworking.send(new ExtRenderDistance(viewDistance)); | ||
return; | ||
} | ||
if (catchExceptions(() -> ClientConfigurationNetworking.canSend(ExtRenderDistance.TYPE))) { | ||
LOGGER.info("Changing view distance to {} (config stage)", viewDistance); | ||
ClientConfigurationNetworking.send(new ExtRenderDistance(viewDistance)); | ||
return; | ||
} | ||
} | ||
|
||
private static boolean catchExceptions(BooleanSupplier supplier) { | ||
try { | ||
return supplier.getAsBoolean(); | ||
} catch (IllegalStateException e) { | ||
return false; // not connected | ||
} | ||
} | ||
|
||
public static void registerListeners() { | ||
C2SPlayChannelEvents.REGISTER.register((handler, sender, client, channels) -> { | ||
if (channels.contains(ExtRenderDistance.TYPE.getId())) { | ||
if (Config.enableExtRenderDistanceProtocol) { | ||
LOGGER.info("Joined server with {} support", ExtRenderDistance.TYPE.getId()); | ||
MinecraftClient.getInstance().execute(() -> MinecraftClient.getInstance().options.sendClientSettings()); | ||
} else { | ||
LOGGER.info("Server supports {} but it is disabled in config", ExtRenderDistance.TYPE.getId()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} |
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
15 changes: 15 additions & 0 deletions
15
...ient-uncapvd/src/main/java/com/ishland/c2me/client/uncapvd/common/UncapVDInitializer.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,15 @@ | ||
package com.ishland.c2me.client.uncapvd.common; | ||
|
||
import com.ishland.c2me.base.common.config.ModStatuses; | ||
import net.fabricmc.api.ClientModInitializer; | ||
|
||
public class UncapVDInitializer implements ClientModInitializer { | ||
|
||
@Override | ||
public void onInitializeClient() { | ||
if (ModStatuses.fabric_networking_api_v1) { | ||
ClientExtNetworking.registerListeners(); | ||
} | ||
} | ||
|
||
} |
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
19 changes: 19 additions & 0 deletions
19
...uncapvd/src/main/java/com/ishland/c2me/client/uncapvd/mixin/MixinSyncedClientOptions.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,19 @@ | ||
package com.ishland.c2me.client.uncapvd.mixin; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import net.minecraft.network.packet.c2s.common.SyncedClientOptions; | ||
import net.minecraft.util.math.MathHelper; | ||
import org.objectweb.asm.Opcodes; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(SyncedClientOptions.class) | ||
public class MixinSyncedClientOptions { | ||
|
||
@WrapOperation(method = "write", at = @At(value = "FIELD", target = "Lnet/minecraft/network/packet/c2s/common/SyncedClientOptions;viewDistance:I", opcode = Opcodes.GETFIELD)) | ||
private int wrapRenderDistance(SyncedClientOptions instance, Operation<Integer> original) { | ||
return MathHelper.clamp(instance.viewDistance(), 2, Byte.MAX_VALUE); | ||
} | ||
|
||
} |
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
18 changes: 18 additions & 0 deletions
18
c2me-notickvd/src/main/java/com/ishland/c2me/notickvd/MixinPlugin.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,18 @@ | ||
package com.ishland.c2me.notickvd; | ||
|
||
import com.ishland.c2me.base.common.ModuleMixinPlugin; | ||
import com.ishland.c2me.notickvd.common.Config; | ||
|
||
public class MixinPlugin extends ModuleMixinPlugin { | ||
|
||
@Override | ||
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { | ||
if (!super.shouldApplyMixin(targetClassName, mixinClassName)) return false; | ||
|
||
if (mixinClassName.startsWith("com.ishland.c2me.notickvd.mixin.ext_render_distance.")) { | ||
return Config.enableExtRenderDistanceProtocol; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
c2me-notickvd/src/main/java/com/ishland/c2me/notickvd/common/IRenderDistanceOverride.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,7 @@ | ||
package com.ishland.c2me.notickvd.common; | ||
|
||
public interface IRenderDistanceOverride { | ||
|
||
void c2me_notickvd$setRenderDistance(int renderDistance); | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
c2me-notickvd/src/main/java/com/ishland/c2me/notickvd/common/NoTickVDInitializer.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,13 @@ | ||
package com.ishland.c2me.notickvd.common; | ||
|
||
import com.ishland.c2me.base.common.config.ModStatuses; | ||
import net.fabricmc.api.ModInitializer; | ||
|
||
public class NoTickVDInitializer implements ModInitializer { | ||
@Override | ||
public void onInitialize() { | ||
if (Config.enableExtRenderDistanceProtocol && ModStatuses.fabric_networking_api_v1) { | ||
ServerExtNetworking.registerListeners(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
c2me-notickvd/src/main/java/com/ishland/c2me/notickvd/common/ServerExtNetworking.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,22 @@ | ||
package com.ishland.c2me.notickvd.common; | ||
|
||
import com.ishland.c2me.base.common.network.ExtRenderDistance; | ||
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking; | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; | ||
|
||
public class ServerExtNetworking { | ||
|
||
public static void registerListeners() { | ||
ServerPlayNetworking.registerGlobalReceiver( | ||
ExtRenderDistance.TYPE, | ||
(packet, player, responseSender) -> | ||
((IRenderDistanceOverride) player.networkHandler).c2me_notickvd$setRenderDistance(packet.renderDistance()) | ||
); | ||
ServerConfigurationNetworking.registerGlobalReceiver( | ||
ExtRenderDistance.TYPE, | ||
(packet, networkHandler, responseSender) -> | ||
((IRenderDistanceOverride) networkHandler).c2me_notickvd$setRenderDistance(packet.renderDistance()) | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.