-
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
Showing
17 changed files
with
374 additions
and
467 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
73 changes: 48 additions & 25 deletions
73
src/main/java/com/github/zly2006/enclosure/network/ConfirmRequestS2CPacket.kt
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 |
---|---|---|
@@ -1,31 +1,54 @@ | ||
package com.github.zly2006.enclosure.network; | ||
package com.github.zly2006.enclosure.network | ||
|
||
import com.github.zly2006.enclosure.gui.EnclosureGui; | ||
import com.github.zly2006.enclosure.gui.*; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | ||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; | ||
import net.fabricmc.fabric.api.networking.v1.PacketSender; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.network.ClientPlayNetworkHandler; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.text.Text; | ||
import com.github.zly2006.enclosure.gui.ConfirmScreen | ||
import com.github.zly2006.enclosure.gui.EnclosureGui | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking | ||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry | ||
import net.minecraft.client.MinecraftClient | ||
import net.minecraft.network.PacketByteBuf | ||
import net.minecraft.network.codec.PacketCodec | ||
import net.minecraft.network.packet.CustomPayload | ||
import net.minecraft.text.Text | ||
import net.minecraft.util.Identifier | ||
|
||
public class ConfirmRequestS2CPacket implements ClientPlayNetworking.PlayChannelHandler { | ||
@Override | ||
public void receive(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { | ||
if (client.currentScreen instanceof EnclosureGui) { | ||
Text message = buf.readText(); | ||
client.execute(() -> { | ||
responseSender.sendPacket(NetworkChannels.CONFIRM, PacketByteBufs.empty()); | ||
client.setScreen(new ConfirmScreen(client.currentScreen, message, () -> { | ||
assert client.player != null; | ||
client.player.networkHandler.sendCommand("enclosure confirm"); | ||
})); | ||
}); | ||
} | ||
class ConfirmRequestS2CPacket( | ||
val message: Text | ||
) : CustomPayload { | ||
var client = MinecraftClient.getInstance() | ||
constructor(buf: PacketByteBuf): this( | ||
Text.Serialization.fromJson(buf.readString(), MinecraftClient.getInstance().world!!.registryManager)!! | ||
) | ||
|
||
|
||
private fun write(buf: PacketByteBuf) { | ||
buf.writeString(Text.Serialization.toJsonString(message, client.world!!.registryManager)) | ||
} | ||
|
||
public static void register() { | ||
ClientPlayNetworking.registerGlobalReceiver(NetworkChannels.CONFIRM, new ConfirmRequestS2CPacket()); | ||
override fun getId(): CustomPayload.Id<out CustomPayload?> { | ||
return ID | ||
} | ||
|
||
companion object { | ||
val ID: CustomPayload.Id<ConfirmRequestS2CPacket?> = CustomPayload.Id(Identifier("enclosure:confirm")) | ||
val CODEC: PacketCodec<PacketByteBuf, ConfirmRequestS2CPacket?> = CustomPayload.codecOf( | ||
{ obj, buf -> obj!!.write(buf) }, | ||
{ buf -> ConfirmRequestS2CPacket(buf) }) | ||
|
||
@JvmStatic | ||
fun register() { | ||
PayloadTypeRegistry.configurationS2C().register(ID, CODEC) | ||
ClientPlayNetworking.registerGlobalReceiver(ID) { payload: ConfirmRequestS2CPacket?, context: ClientPlayNetworking.Context? -> | ||
val client = MinecraftClient.getInstance() | ||
if (client.currentScreen is EnclosureGui) { | ||
client.execute { | ||
ClientPlayNetworking.send(payload) | ||
client.setScreen(ConfirmScreen(client.currentScreen, payload!!.message) { | ||
assert(client.player != null) | ||
client.player!!.networkHandler.sendCommand("enclosure confirm") | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
119 changes: 53 additions & 66 deletions
119
src/main/java/com/github/zly2006/enclosure/network/EnclosureInstalledC2SPacket.kt
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 |
---|---|---|
@@ -1,76 +1,63 @@ | ||
package com.github.zly2006.enclosure.network; | ||
|
||
import com.github.zly2006.enclosure.ServerMainKt; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | ||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; | ||
import net.fabricmc.fabric.api.networking.v1.PacketSender; | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; | ||
import net.fabricmc.loader.api.SemanticVersion; | ||
import net.fabricmc.loader.api.Version; | ||
import net.fabricmc.loader.api.VersionParsingException; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.network.ServerPlayNetworkHandler; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.text.Text; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.github.zly2006.enclosure.ServerMainKt.MOD_VERSION; | ||
|
||
public class EnclosureInstalledC2SPacket implements ServerPlayNetworking.PlayChannelHandler { | ||
public static final Map<ServerPlayerEntity, Version> installedClientMod = new HashMap<>(); | ||
|
||
private EnclosureInstalledC2SPacket() { | ||
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> | ||
installedClientMod.remove(handler.player)); | ||
package com.github.zly2006.enclosure.network | ||
|
||
import com.github.zly2006.enclosure.MOD_VERSION | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking | ||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking | ||
import net.fabricmc.loader.api.Version | ||
import net.minecraft.network.PacketByteBuf | ||
import net.minecraft.network.codec.PacketCodec | ||
import net.minecraft.network.packet.CustomPayload | ||
import net.minecraft.server.MinecraftServer | ||
import net.minecraft.server.network.ServerPlayNetworkHandler | ||
import net.minecraft.server.network.ServerPlayerEntity | ||
import net.minecraft.util.Identifier | ||
|
||
class EnclosureInstalledC2SPacket( | ||
val version: Version | ||
) : CustomPayload { | ||
|
||
constructor(buf: PacketByteBuf?) : this(Version.parse(buf!!.readString())) | ||
|
||
private fun write(buf: PacketByteBuf) { | ||
buf.writeString(version.friendlyString) | ||
} | ||
|
||
public static boolean isInstalled(@Nullable ServerPlayerEntity player) { | ||
if (player == null) return false; | ||
return installedClientMod.containsKey(player); | ||
} | ||
|
||
public static Version clientVersion(ServerPlayerEntity connection) { | ||
return installedClientMod.get(connection); | ||
} | ||
override fun getId() = ID | ||
|
||
public static void register() { | ||
EnclosureInstalledC2SPacket listener = new EnclosureInstalledC2SPacket(); | ||
ServerPlayNetworking.registerGlobalReceiver(NetworkChannels.ENCLOSURE_INSTALLED, listener); | ||
} | ||
companion object { | ||
val installedClientMod: MutableMap<ServerPlayerEntity, Version> = HashMap() | ||
val ID: CustomPayload.Id<EnclosureInstalledC2SPacket?> = CustomPayload.Id(Identifier("enclosure:installed")) | ||
val CODEC: PacketCodec<PacketByteBuf, EnclosureInstalledC2SPacket?> = CustomPayload.codecOf( | ||
{ obj, buf -> obj!!.write(buf) }, | ||
{ buf -> EnclosureInstalledC2SPacket(buf) }) | ||
|
||
public static void send() { | ||
PacketByteBuf buf = PacketByteBufs.create(); | ||
buf.writeString(MOD_VERSION.getFriendlyString()); | ||
ClientPlayNetworking.send(NetworkChannels.ENCLOSURE_INSTALLED, buf); | ||
} | ||
@JvmStatic | ||
fun isInstalled(player: ServerPlayerEntity?): Boolean { | ||
if (player == null) return false | ||
return installedClientMod.containsKey(player) | ||
} | ||
|
||
@Override | ||
public void receive(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, @NotNull PacketByteBuf buf, PacketSender responseSender) { | ||
Version version; | ||
try { | ||
version = Version.parse(buf.readString()); | ||
if (version instanceof SemanticVersion clientVersion && MOD_VERSION instanceof SemanticVersion serverVersion && | ||
clientVersion.getVersionComponent(0) == serverVersion.getVersionComponent(0) && | ||
clientVersion.getVersionComponent(1) >= serverVersion.getVersionComponent(1)) { | ||
ServerMainKt.LOGGER.info(player.getName().getString() + " joined with a matching enclosure client."); | ||
installedClientMod.put(player, version); | ||
fun clientVersion(connection: ServerPlayerEntity): Version? { | ||
return installedClientMod[connection] | ||
} | ||
|
||
// send uuid data | ||
PacketByteBuf buf2 = PacketByteBufs.create(); | ||
NbtCompound compound = new NbtCompound(); | ||
ServerMainKt.minecraftServer.getUserCache().byName.forEach((name, entry) -> compound.putUuid(name, entry.profile.getId())); | ||
buf2.writeNbt(compound); | ||
ServerPlayNetworking.send(player, NetworkChannels.SYNC_UUID, buf2); | ||
} else { | ||
player.sendMessage(Text.translatable("enclosure.message.outdated", MOD_VERSION.getFriendlyString(), version.getFriendlyString()), false); | ||
fun register() { | ||
PayloadTypeRegistry.configurationC2S().register(ID, CODEC) | ||
ServerPlayNetworking.registerGlobalReceiver<EnclosureInstalledC2SPacket?>(ID) { payload: EnclosureInstalledC2SPacket?, context: ServerPlayNetworking.Context -> | ||
val player = context.player() as ServerPlayerEntity | ||
installedClientMod[player] = payload!!.version | ||
} | ||
} catch (VersionParsingException ignored) { } | ||
ServerPlayConnectionEvents.DISCONNECT.register(ServerPlayConnectionEvents.Disconnect { handler: ServerPlayNetworkHandler, server: MinecraftServer? -> | ||
installedClientMod.remove(handler.player) | ||
}) | ||
} | ||
|
||
@JvmStatic | ||
fun send() { | ||
ClientPlayNetworking.send(EnclosureInstalledC2SPacket(MOD_VERSION)) | ||
} | ||
} | ||
} |
Oops, something went wrong.