Skip to content

Commit

Permalink
fix: CE kt
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Apr 2, 2024
1 parent 6e996ad commit 3c42b61
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 467 deletions.
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ org.gradle.jvmargs=-Xmx5G
org.gradle.parallel=true
# check these on https://fabricmc.net/develop
# Mod Properties
mod_version=0.3.6
archices_preview_version=
mod_version=0.3.7
archices_preview_version=potato

maven_group=com.github.zly2006
archives_base_name=enclosure-fabric
# Dependencies
rei_version = 9.1.550

minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.7
minecraft_version=24w14potato
yarn_mappings=24w14potato+build.3
loader_version=0.15.9

#Fabric api
fabric_version=0.96.11+1.20.4
fabric_version=0.96.13+1.20.5
12 changes: 4 additions & 8 deletions src/main/java/com/github/zly2006/enclosure/EnclosureList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import com.github.zly2006.enclosure.ServerMain.getAllEnclosures
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtList
import net.minecraft.nbt.NbtString
import net.minecraft.registry.RegistryWrapper
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.world.PersistentState
import java.io.File

class EnclosureList(world: ServerWorld, isRoot: Boolean) : PersistentState() {
private val areaMap: MutableMap<String, EnclosureArea> = HashMap()
Expand Down Expand Up @@ -36,12 +36,12 @@ class EnclosureList(world: ServerWorld, isRoot: Boolean) : PersistentState() {
}
}

override fun writeNbt(nbt: NbtCompound): NbtCompound {
override fun writeNbt(nbt: NbtCompound, registryLookup: RegistryWrapper.WrapperLookup?): NbtCompound {
val list = NbtList()
for (area in areaMap.values) {
list.add(NbtString.of(area.name))
val compound = NbtCompound()
area.writeNbt(compound)
area.writeNbt(compound, null)
nbt.put(area.name, compound)
}
nbt.put(ENCLOSURE_LIST_KEY, list)
Expand Down Expand Up @@ -85,13 +85,9 @@ class EnclosureList(world: ServerWorld, isRoot: Boolean) : PersistentState() {
areaMap[area.name] = area
markDirty()
}

override fun save(file: File) {
super.save(file)
}
}

const val DATA_VERSION_KEY = "data_version"
const val SUB_ENCLOSURES_KEY = "sub_lands"
const val ENCLOSURE_LIST_KEY = "enclosure.land_list"
const val ENCLOSURE_PREFIX = "enclosure:"
const val ENCLOSURE_PREFIX = "enclosure:"
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")
})
}
}
}
}
}
}
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))
}
}
}
Loading

0 comments on commit 3c42b61

Please sign in to comment.