Skip to content

Commit

Permalink
Progress?
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed May 8, 2024
1 parent 23ca8d9 commit 6b1f72c
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 148 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'dev.architectury.loom' version '1.5-SNAPSHOT' apply(false)
id 'dev.architectury.loom' version '1.6-SNAPSHOT' apply(false)
id 'me.fallenbreath.yamlang' version '1.3.1' apply(false)
id "me.modmuss50.mod-publish-plugin" version "0.4.4" apply(false)
id 'maven-publish'
Expand Down Expand Up @@ -51,7 +51,7 @@ subprojects {
}

java {
toolchain.languageVersion = JavaLanguageVersion.of(17)
toolchain.languageVersion = JavaLanguageVersion.of(21)
withSourcesJar()
withJavadocJar()
}
Expand Down Expand Up @@ -84,7 +84,7 @@ subprojects {

tasks.withType(JavaCompile).configureEach {
it.options.encoding = 'UTF-8'
it.options.getRelease().set(17)
it.options.getRelease().set(21)
}

plugins.withId('dev.architectury.loom') {
Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {

modCompileOnly "net.fabricmc:fabric-loader:${fabric_loader_version}"

modImplementation("dev.isxander.yacl:yet-another-config-lib-common:${project.yacl_version}") {
modImplementation("dev.isxander:yet-another-config-lib:${project.yacl_version}-fabric") {
exclude group: "net.fabricmc"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;

public enum KineticDamage {
VANILLA,
Expand All @@ -16,4 +20,6 @@ public enum KineticDamage {
return DataResult.error(() -> "Unknown kinetic damage type: " + name);
}
}, KineticDamage::name);
public static final PacketCodec<ByteBuf, KineticDamage> PACKET_CODEC =
PacketCodecs.STRING.xmap(KineticDamage::valueOf, KineticDamage::name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;

public interface LimitedModConfigServer {
LimitedModConfigServer OPERATOR = new Impl(true, false);
Expand All @@ -13,6 +16,14 @@ static Codec<LimitedModConfigServer> getCodec() {
).apply(instance, Impl::new));
}

static PacketCodec<PacketByteBuf, LimitedModConfigServer> getPacketCodec() {
return PacketCodec.tuple(
PacketCodecs.BOOL, LimitedModConfigServer::allowThrusting,
PacketCodecs.BOOL, LimitedModConfigServer::forceEnabled,
Impl::new
);
}

boolean allowThrusting();

boolean forceEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.text.Text;
import nl.enjarai.doabarrelroll.DoABarrelRoll;
Expand All @@ -24,6 +28,14 @@ public record ModConfigServer(boolean allowThrusting,
Codec.INT.optionalFieldOf("installedTimeout", DEFAULT.installedTimeout()).forGetter(ModConfigServer::installedTimeout),
KineticDamage.CODEC.optionalFieldOf("kineticDamage", DEFAULT.kineticDamage()).forGetter(ModConfigServer::kineticDamage)
).apply(instance, ModConfigServer::new));
public static final PacketCodec<ByteBuf, ModConfigServer> PACKET_CODEC = PacketCodec.tuple(
PacketCodecs.BOOL, ModConfigServer::allowThrusting,
PacketCodecs.BOOL, ModConfigServer::forceEnabled,
PacketCodecs.BOOL, ModConfigServer::forceInstalled,
PacketCodecs.INTEGER, ModConfigServer::installedTimeout,
KineticDamage.PACKET_CODEC, ModConfigServer::kineticDamage,
ModConfigServer::new
);

@Override
public Integer getSyncTimeout() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
package nl.enjarai.doabarrelroll.net;

import com.google.gson.JsonParser;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import io.netty.buffer.Unpooled;
import net.minecraft.network.PacketByteBuf;
import nl.enjarai.doabarrelroll.DoABarrelRoll;
import nl.enjarai.doabarrelroll.config.LimitedModConfigServer;
import nl.enjarai.doabarrelroll.config.ModConfigServer;
import nl.enjarai.doabarrelroll.net.packet.ConfigResponseC2SPacket;
import nl.enjarai.doabarrelroll.net.packet.ConfigSyncS2CPacket;

import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;

public class HandshakeClient<L, F extends L> {
public static final int PROTOCOL_VERSION = 3;

private final Codec<F> transferCodec;
private final Codec<L> limitedTransferCodec;
private final Consumer<L> updateCallback;
private L serverConfig = null;
private F fullServerConfig = null;
public class HandshakeClient<R extends ConfigResponseC2SPacket> {
private final BiFunction<Integer, Boolean, R> responseConstructor;
private final Consumer<LimitedModConfigServer> updateCallback;
private LimitedModConfigServer serverConfig = null;
private ModConfigServer fullServerConfig = null;
private boolean hasConnected = false;

public HandshakeClient(Codec<F> transferCodec, Codec<L> limitedTransferCodec, Consumer<L> updateCallback) {
this.transferCodec = transferCodec;
this.limitedTransferCodec = limitedTransferCodec;
public HandshakeClient(BiFunction<Integer, Boolean, R> responseConstructor, Consumer<LimitedModConfigServer> updateCallback) {
this.responseConstructor = responseConstructor;
this.updateCallback = updateCallback;
}

/**
* Returns the server config if the client has received one for this server,
* returns an empty optional in any other case.
*/
public Optional<L> getConfig() {
public Optional<LimitedModConfigServer> getConfig() {
return Optional.ofNullable(serverConfig);
}

public Optional<F> getFullConfig() {
public Optional<ModConfigServer> getFullConfig() {
return Optional.ofNullable(fullServerConfig);
}

Expand All @@ -48,55 +44,28 @@ public boolean hasConnected() {
return hasConnected;
}

public PacketByteBuf handleConfigSync(PacketByteBuf buf) {
public R handleConfigSync(ConfigSyncS2CPacket packet) {
serverConfig = null;
fullServerConfig = null;

try {
var protocolVersion = buf.readInt();
if (protocolVersion < 1 || protocolVersion > PROTOCOL_VERSION) {
DoABarrelRoll.LOGGER.warn("Received config with unknown protocol version: {}, will attempt to load anyway", protocolVersion);
}

var data = buf.readString();
var isLimited = true;

if (protocolVersion >= 2) {
isLimited = buf.readBoolean();
}

if (protocolVersion == 2) {
var codec = isLimited ? limitedTransferCodec : transferCodec;
serverConfig = codec.parse(JsonOps.INSTANCE, JsonParser.parseString(data))
.getOrThrow(false, DoABarrelRoll.LOGGER::error);
if (!isLimited) {
//noinspection unchecked
fullServerConfig = (F) serverConfig;
}
} else {
serverConfig = limitedTransferCodec.parse(JsonOps.INSTANCE, JsonParser.parseString(data))
.getOrThrow(false, DoABarrelRoll.LOGGER::error);
if (!isLimited) {
var data2 = buf.readString();

fullServerConfig = transferCodec.parse(JsonOps.INSTANCE, JsonParser.parseString(data2))
.getOrThrow(false, DoABarrelRoll.LOGGER::error);
}
}
} catch (RuntimeException e) {
DoABarrelRoll.LOGGER.error("Failed to parse config from server", e);
var protocolVersion = packet.protocolVersion();
if (protocolVersion < 4) {
DoABarrelRoll.LOGGER.error("Received config with old protocol version: {}, this version is no longer supported!", protocolVersion);
return responseConstructor.apply(protocolVersion, false);
} else if (protocolVersion > HandshakeServer.PROTOCOL_VERSION) {
DoABarrelRoll.LOGGER.warn("Received config with unknown protocol version: {}, will attempt to load anyway", protocolVersion);
}

if (serverConfig != null) {
updateCallback.accept(serverConfig);
hasConnected = true;
DoABarrelRoll.LOGGER.info("Received config from server");
serverConfig = packet.applicableConfig();
if (!packet.isLimited()) {
fullServerConfig = packet.fullConfig();
}

var returnBuf = new PacketByteBuf(Unpooled.buffer());
returnBuf.writeInt(PROTOCOL_VERSION);
returnBuf.writeBoolean(serverConfig != null);
return returnBuf;
updateCallback.accept(serverConfig);
hasConnected = true;
DoABarrelRoll.LOGGER.info("Received config from server");

return responseConstructor.apply(protocolVersion, true);
}

public void reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,27 @@
import nl.enjarai.doabarrelroll.DoABarrelRoll;
import nl.enjarai.doabarrelroll.config.LimitedModConfigServer;
import nl.enjarai.doabarrelroll.config.ModConfigServer;
import nl.enjarai.doabarrelroll.net.packet.ConfigSyncS2CPacket;
import nl.enjarai.doabarrelroll.util.DelayedRunnable;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.Function;

public class HandshakeServer {
// Protocol version 1:
// | Protocol version (int) | Config data (string) |
// Protocol version 2:
// | Protocol version (int) | Limited/full config data (string) | Is limited (boolean) |
// Protocol version 3:
// | Protocol version (int) | Limited config data (string) | Is limited (boolean) | [Full config data (string)] (only if not limited) |
public static final int PROTOCOL_VERSION = 3;
public class HandshakeServer<P extends ConfigSyncS2CPacket> {
public static final int PROTOCOL_VERSION = 4;

private final PacketConstructor<P> packetConstructor;
private final ServerConfigHolder<ModConfigServer> configHolder;
private final Map<ServerPlayNetworkHandler, ClientInfo> syncStates = new WeakHashMap<>();
private final Map<ServerPlayNetworkHandler, DelayedRunnable> scheduledKicks = new WeakHashMap<>();
private final Function<ServerPlayNetworkHandler, Boolean> getsLimitedCheck;
private final Codec<ModConfigServer> transferCodec = ModConfigServer.CODEC;
private final Codec<LimitedModConfigServer> limitedTransferCodec = LimitedModConfigServer.getCodec();

public HandshakeServer(ServerConfigHolder<ModConfigServer> configHolder, Function<ServerPlayNetworkHandler, Boolean> getsLimitedCheck) {
public HandshakeServer(PacketConstructor<P> packetConstructor, ServerConfigHolder<ModConfigServer> configHolder, Function<ServerPlayNetworkHandler, Boolean> getsLimitedCheck) {
this.packetConstructor = packetConstructor;
this.configHolder = configHolder;
this.getsLimitedCheck = getsLimitedCheck;
}
Expand All @@ -59,53 +57,12 @@ public ClientInfo getHandshakeState(ServerPlayNetworkHandler handler) {
return syncStates.computeIfAbsent(handler, key -> new ClientInfo(HandshakeState.NOT_SENT, PROTOCOL_VERSION, true));
}

public PacketByteBuf getConfigSyncBuf(ServerPlayNetworkHandler handler) {
return getConfigSyncBuf(handler, getHandshakeState(handler).protocolVersion);
}

@SuppressWarnings("NonStrictComparisonCanBeEquality")
public PacketByteBuf getConfigSyncBuf(ServerPlayNetworkHandler handler, int protocolVersion) {
protocolVersion = Math.min(protocolVersion, PROTOCOL_VERSION);
var buf = new PacketByteBuf(Unpooled.buffer());

// Protocol version
buf.writeInt(protocolVersion);

// Config data
public P initiateConfigSync(ServerPlayNetworkHandler handler) {
var isLimited = getsLimitedCheck.apply(handler);
getHandshakeState(handler).isLimited = isLimited;
var config = configHolder.instance;
DataResult<JsonElement> data;
if (protocolVersion == 2) {
Codec<? super ModConfigServer> codec = isLimited ? limitedTransferCodec : transferCodec;
data = codec.encodeStart(JsonOps.INSTANCE, config);
} else {
data = limitedTransferCodec.encodeStart(JsonOps.INSTANCE, config.getLimited(handler));
}
try {
buf.writeString(data.getOrThrow(false, DoABarrelRoll.LOGGER::error).toString());
} catch (RuntimeException e) {
DoABarrelRoll.LOGGER.error("Failed to encode config", e);
buf.writeString("{}");
}

if (protocolVersion >= 2) {
// Limited status
buf.writeBoolean(isLimited);
}

if (protocolVersion >= 3 && !isLimited) {
// Operator modifiable config
var data2 = transferCodec.encodeStart(JsonOps.INSTANCE, config);
try {
buf.writeString(data2.getOrThrow(false, DoABarrelRoll.LOGGER::error).toString());
} catch (RuntimeException e) {
DoABarrelRoll.LOGGER.error("Failed to encode config", e);
buf.writeString("{}");
}
}

return buf;
return packetConstructor.construct(PROTOCOL_VERSION, config, isLimited ? null : config);
}

public void configSentToClient(ServerPlayNetworkHandler handler) {
Expand Down Expand Up @@ -183,6 +140,10 @@ public ClientInfo(HandshakeState state, int protocolVersion, boolean isLimited)
}
}

public interface PacketConstructor<P extends ConfigSyncS2CPacket> {
P construct(int protocolVersion, LimitedModConfigServer applicableConfig, @Nullable ModConfigServer fullConfig);
}

public enum HandshakeState {
NOT_SENT,
SENT,
Expand Down
Loading

0 comments on commit 6b1f72c

Please sign in to comment.