Skip to content

Commit

Permalink
Holy moly, is this work?
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed May 10, 2024
1 parent ed04dd6 commit ee16638
Show file tree
Hide file tree
Showing 31 changed files with 196 additions and 309 deletions.
19 changes: 0 additions & 19 deletions common/src/main/java/nl/enjarai/doabarrelroll/DoABarrelRoll.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
import io.netty.buffer.Unpooled;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import nl.enjarai.doabarrelroll.api.event.ServerEvents;
import nl.enjarai.doabarrelroll.config.ModConfigServer;
import nl.enjarai.doabarrelroll.net.HandshakeServer;
import nl.enjarai.doabarrelroll.net.register.RollSyncServer;
import nl.enjarai.doabarrelroll.net.ServerConfigHolder;
import nl.enjarai.doabarrelroll.net.register.HandshakeServerRegister;
import nl.enjarai.doabarrelroll.net.register.ServerConfigUpdaterRegister;
import nl.enjarai.doabarrelroll.platform.Services;
import org.slf4j.Logger;

Expand All @@ -29,18 +22,6 @@ public static Identifier id(String path) {

public static void init() {
var configFile = Path.of("config");

Services.PLATFORM.registerNetworkChannels(HANDSHAKE_CHANNEL, SERVER_CONFIG_UPDATE_CHANNEL, ROLL_CHANNEL);

CONFIG_HOLDER = new ServerConfigHolder<>(configFile,
ModConfigServer.CODEC, ModConfigServer.DEFAULT, ServerEvents::updateServerConfig);
HANDSHAKE_SERVER = new HandshakeServer(CONFIG_HOLDER, player -> !ModConfigServer.canModify(player));

// Register server-side listeners for config syncing, this is done on
// both client and server to ensure everything works in LAN worlds as well.
RollSyncServer.init();
HandshakeServerRegister.init();
ServerConfigUpdaterRegister.init();
}

public static PacketByteBuf createBuf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import nl.enjarai.doabarrelroll.config.ActivationBehaviour;
import nl.enjarai.doabarrelroll.config.LimitedModConfigServer;
import nl.enjarai.doabarrelroll.config.ModConfig;
import nl.enjarai.doabarrelroll.config.ModConfigServer;
import nl.enjarai.doabarrelroll.net.register.HandshakeClientRegister;
import nl.enjarai.doabarrelroll.flight.RotationModifiers;
import nl.enjarai.doabarrelroll.net.HandshakeClient;
import nl.enjarai.doabarrelroll.platform.Services;
import nl.enjarai.doabarrelroll.util.MixinHooks;
import nl.enjarai.doabarrelroll.util.StarFoxUtil;

Expand Down Expand Up @@ -49,7 +47,6 @@ public static void init() {
ClientEvents.SERVER_CONFIG_UPDATE.register(ModConfig.INSTANCE::notifyPlayerOfServerConfig);

ModConfig.touch();
HandshakeClientRegister.init();

// Init barrel rollery.
StarFoxUtil.register();
Expand All @@ -63,7 +60,7 @@ public static void clearValues() {
}

public static boolean isFallFlying() {
if (!HANDSHAKE_CLIENT.getConfig().map(LimitedModConfigServer::forceEnabled).orElse(false)) {
if (!Services.CLIENT_NET.getHandshakeClient().getConfig().map(LimitedModConfigServer::forceEnabled).orElse(false)) {
var hybrid = ModConfig.INSTANCE.getActivationBehaviour() == ActivationBehaviour.HYBRID ||
ModConfig.INSTANCE.getActivationBehaviour() == ActivationBehaviour.HYBRID_TOGGLE;
if (hybrid && !MixinHooks.thirdJump) {
Expand Down
14 changes: 0 additions & 14 deletions common/src/main/java/nl/enjarai/doabarrelroll/EventCallbacks.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ public static void clientTick(MinecraftClient client) {
StarFoxUtil.clientTick(client);
}

public static void clientDisconnect() {
DoABarrelRollClient.HANDSHAKE_CLIENT.reset();
}

public static void onRenderCrosshair(DrawContext context, float tickDelta, int scaledWidth, int scaledHeight) {
if (!DoABarrelRollClient.isFallFlying()) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import nl.enjarai.doabarrelroll.config.LimitedModConfigServer;
import nl.enjarai.doabarrelroll.config.ModConfig;
import nl.enjarai.doabarrelroll.config.ModConfigScreen;
import nl.enjarai.doabarrelroll.platform.Services;
import org.lwjgl.glfw.GLFW;

import java.util.List;
Expand Down Expand Up @@ -114,7 +115,7 @@ public class ModKeybindings {

public static void clientTick(MinecraftClient client) {
while (TOGGLE_ENABLED.wasPressed()) {
if (!DoABarrelRollClient.HANDSHAKE_CLIENT.getConfig().map(LimitedModConfigServer::forceEnabled).orElse(false)) {
if (!Services.CLIENT_NET.getHandshakeClient().getConfig().map(LimitedModConfigServer::forceEnabled).orElse(false)) {
ModConfig.INSTANCE.setModEnabled(!ModConfig.INSTANCE.getModEnabled());
ModConfig.INSTANCE.save();

Expand All @@ -137,7 +138,7 @@ public static void clientTick(MinecraftClient client) {
}
}
while (TOGGLE_THRUST.wasPressed()) {
if (DoABarrelRollClient.HANDSHAKE_CLIENT.getConfig().map(LimitedModConfigServer::allowThrusting).orElse(false)) {
if (Services.CLIENT_NET.getHandshakeClient().getConfig().map(LimitedModConfigServer::allowThrusting).orElse(false)) {
ModConfig.INSTANCE.setEnableThrust(!ModConfig.INSTANCE.getEnableThrust());
ModConfig.INSTANCE.save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static Screen generateConfigScreen(Screen parent) {
ClientPlayerEntity player;
var onRealms = DoABarrelRollClient.isConnectedToRealms() &&
(player = MinecraftClient.getInstance().player) != null && player.hasPermissionLevel(2);
var serverConfig = DoABarrelRollClient.HANDSHAKE_CLIENT.getConfig();
var serverConfig = Services.CLIENT_NET.getHandshakeClient().getConfig();

var thrustingAllowed = new Dependable(serverConfig.map(LimitedModConfigServer::allowThrusting).orElse(!inWorld || onRealms));
var allowDisabled = new Dependable(!serverConfig.map(LimitedModConfigServer::forceEnabled).orElse(false));
Expand Down Expand Up @@ -234,8 +234,8 @@ public static Screen generateConfigScreen(Screen parent) {

// If we're in a world, use the synced config, otherwise, grab our local one.
var fullServerConfig = inWorld
? DoABarrelRollClient.HANDSHAKE_CLIENT.getFullConfig()
: Optional.of(DoABarrelRoll.CONFIG_HOLDER.instance);
? Services.CLIENT_NET.getHandshakeClient().getFullConfig()
: Optional.of(Services.SERVER_NET.getServerConfigHolder().instance);
MutableConfigServer mut;
if (fullServerConfig.isPresent()) {
mut = new MutableConfigServer(fullServerConfig.get());
Expand Down Expand Up @@ -285,13 +285,13 @@ public static Screen generateConfigScreen(Screen parent) {

if (mut != null) {
if (MinecraftClient.getInstance().world == null) {
DoABarrelRoll.CONFIG_HOLDER.instance = mut.toImmutable();
Services.SERVER_NET.getServerConfigHolder().instance = mut.toImmutable();
} else {
var original = DoABarrelRollClient.HANDSHAKE_CLIENT.getConfig();
var original = Services.CLIENT_NET.getHandshakeClient().getConfig();
if (original.isPresent()){
var imut = mut.toImmutable();
if (!imut.equals(original.get())) {
ServerConfigUpdateClient.prepUpdatePacket(imut);
Services.CLIENT_NET.sendConfigUpdatePacket(imut);
configListener.accept(imut);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import nl.enjarai.doabarrelroll.api.rotation.RotationInstant;
import nl.enjarai.doabarrelroll.config.serialization.ExpressionParserTypeAdapter;
import nl.enjarai.doabarrelroll.math.ExpressionParser;
import nl.enjarai.doabarrelroll.platform.Services;
import nl.enjarai.doabarrelroll.util.ToastUtil;

import java.io.*;
Expand Down Expand Up @@ -168,7 +169,7 @@ public boolean getEnableThrust() {
return true;
}

return DoABarrelRollClient.HANDSHAKE_CLIENT.getConfig()
return Services.CLIENT_NET.getHandshakeClient().getConfig()
.map(LimitedModConfigServer::allowThrusting)
.orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.entity.LivingEntity;
import nl.enjarai.doabarrelroll.DoABarrelRoll;
import nl.enjarai.doabarrelroll.platform.Services;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
Expand All @@ -23,7 +24,7 @@ public abstract class LivingEntityMixin {
require = 0 // We let this mixin fail if it needs to as a temporary workaround to be compatible with Connector.
)
private float doABarrelRoll$modifyKineticDamage(float original) {
var damageType = DoABarrelRoll.CONFIG_HOLDER.instance.kineticDamage();
var damageType = Services.SERVER_NET.getServerConfigHolder().instance.kineticDamage();

return switch (damageType) {
case VANILLA -> original;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import nl.enjarai.doabarrelroll.flight.RotationModifiers;
import nl.enjarai.doabarrelroll.math.MagicNumbers;
import nl.enjarai.doabarrelroll.mixin.roll.entity.PlayerEntityMixin;
import nl.enjarai.doabarrelroll.net.register.RollSyncClient;
import nl.enjarai.doabarrelroll.platform.Services;
import org.joml.Vector3d;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -35,7 +35,7 @@ public abstract class ClientPlayerEntityMixin extends PlayerEntityMixin {
var isRolling = doABarrelRoll$isRolling();
var rollDiff = doABarrelRoll$getRoll() - lastSentRoll;
if (isRolling != lastSentIsRolling || rollDiff != 0.0f) {
RollSyncClient.sendUpdate(this);
Services.CLIENT_NET.sendRollUpdate(this);

lastSentIsRolling = isRolling;
lastSentRoll = doABarrelRoll$getRoll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.server.network.EntityTrackerEntry;
import nl.enjarai.doabarrelroll.api.RollEntity;
import nl.enjarai.doabarrelroll.net.register.RollSyncServer;
import nl.enjarai.doabarrelroll.platform.Services;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand Down Expand Up @@ -31,7 +31,7 @@ public abstract class EntityTrackerEntryMixin {
var roll = rollEntity.doABarrelRoll$getRoll();

if (isRolling != lastIsRolling || roll != lastRoll) {
RollSyncServer.sendUpdates(entity);
Services.SERVER_NET.sendRollUpdates(entity);

lastIsRolling = isRolling;
lastRoll = roll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import java.util.function.BiFunction;
import java.util.function.Consumer;

public class HandshakeClient {
private final BiFunction<Integer, Boolean, ConfigResponseC2SPacket> responseConstructor;
public class HandshakeClient<P extends ConfigResponseC2SPacket> {
private final BiFunction<Integer, Boolean, P> responseConstructor;
private final Consumer<LimitedModConfigServer> updateCallback;
private LimitedModConfigServer serverConfig = null;
private ModConfigServer fullServerConfig = null;
private boolean hasConnected = false;

public HandshakeClient(BiFunction<Integer, Boolean, ConfigResponseC2SPacket> responseConstructor, Consumer<LimitedModConfigServer> updateCallback) {
public HandshakeClient(BiFunction<Integer, Boolean, P> responseConstructor, Consumer<LimitedModConfigServer> updateCallback) {
this.responseConstructor = responseConstructor;
this.updateCallback = updateCallback;
}
Expand All @@ -44,7 +44,7 @@ public boolean hasConnected() {
return hasConnected;
}

public ConfigResponseC2SPacket handleConfigSync(ConfigSyncS2CPacket packet) {
public P handleConfigSync(ConfigSyncS2CPacket packet) {
serverConfig = null;
fullServerConfig = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
import java.util.WeakHashMap;
import java.util.function.Function;

public class HandshakeServer {
public class HandshakeServer<P extends ConfigSyncS2CPacket> {
public static final int PROTOCOL_VERSION = 4;

private final PacketConstructor packetConstructor;
private final PacketConstructor<P> packetConstructor;
private final ServerConfigHolder 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(PacketConstructor packetConstructor, ServerConfigHolder configHolder, Function<ServerPlayNetworkHandler, Boolean> getsLimitedCheck) {
public HandshakeServer(PacketConstructor<P> packetConstructor, ServerConfigHolder configHolder, Function<ServerPlayNetworkHandler, Boolean> getsLimitedCheck) {
this.packetConstructor = packetConstructor;
this.configHolder = configHolder;
this.getsLimitedCheck = getsLimitedCheck;
Expand All @@ -54,7 +54,7 @@ public ClientInfo getHandshakeState(ServerPlayNetworkHandler handler) {
return syncStates.computeIfAbsent(handler, key -> new ClientInfo(HandshakeState.NOT_SENT, PROTOCOL_VERSION, true));
}

public ConfigSyncS2CPacket initiateConfigSync(ServerPlayNetworkHandler handler) {
public P initiateConfigSync(ServerPlayNetworkHandler handler) {
var isLimited = getsLimitedCheck.apply(handler);
getHandshakeState(handler).isLimited = isLimited;
var config = configHolder.instance;
Expand Down Expand Up @@ -128,8 +128,8 @@ public ClientInfo(HandshakeState state, int protocolVersion, boolean isLimited)
}
}

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

public enum HandshakeState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
import java.nio.file.StandardOpenOption;
import java.util.function.BiConsumer;

public class ServerConfigHolder {
public class ServerConfigHolder<P extends ConfigUpdateAckS2CPacket> {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

public final Path configFile;
public final Codec<ModConfigServer> codec;
private final PacketConstructor packetConstructor;
private BiConsumer<MinecraftServer, ModConfigServer> updateCallback;
private HandshakeServer handshakeServer;
private final PacketConstructor<P> packetConstructor;
private final BiConsumer<MinecraftServer, ModConfigServer> updateCallback;
private HandshakeServer<?> handshakeServer;
public ModConfigServer instance;

public ServerConfigHolder(Path configFile, Codec<ModConfigServer> codec, PacketConstructor packetConstructor, BiConsumer<MinecraftServer, ModConfigServer> updateCallback) {
public ServerConfigHolder(Path configFile, Codec<ModConfigServer> codec, PacketConstructor<P> packetConstructor, BiConsumer<MinecraftServer, ModConfigServer> updateCallback) {
this.configFile = configFile;
this.codec = codec;
this.packetConstructor = packetConstructor;
Expand Down Expand Up @@ -83,7 +83,7 @@ public void save() {
}
}

public ConfigUpdateAckS2CPacket clientSendsUpdate(ServerPlayerEntity player, ConfigUpdateC2SPacket packet) {
public P clientSendsUpdate(ServerPlayerEntity player, ConfigUpdateC2SPacket packet) {
var info = handshakeServer.getHandshakeState(player);
var accepted = info.state == HandshakeServer.HandshakeState.ACCEPTED;
var hasPermission = ModConfigServer.canModify(player.networkHandler);
Expand Down Expand Up @@ -133,11 +133,11 @@ public ConfigUpdateAckS2CPacket clientSendsUpdate(ServerPlayerEntity player, Con
}
}

public void setHandshakeServer(HandshakeServer handshakeServer) {
public void setHandshakeServer(HandshakeServer<?> handshakeServer) {
this.handshakeServer = handshakeServer;
}

public interface PacketConstructor {
ConfigUpdateAckS2CPacket construct(int protocolVersion, boolean success);
public interface PacketConstructor<P extends ConfigUpdateAckS2CPacket> {
P construct(int protocolVersion, boolean success);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package nl.enjarai.doabarrelroll.net;

import net.minecraft.network.PacketByteBuf;
import nl.enjarai.doabarrelroll.DoABarrelRoll;
import nl.enjarai.doabarrelroll.config.ModConfigServer;
import nl.enjarai.doabarrelroll.net.packet.ConfigUpdateAckS2CPacket;
import nl.enjarai.doabarrelroll.net.packet.ConfigUpdateC2SPacket;
import nl.enjarai.doabarrelroll.util.ToastUtil;

public class ServerConfigUpdateClient {
private final PacketConstructor packetConstructor;
public class ServerConfigUpdateClient<P extends ConfigUpdateC2SPacket> {
private final PacketConstructor<P> packetConstructor;
private boolean waitingForAck = false;

public ServerConfigUpdateClient(PacketConstructor packetConstructor) {
public ServerConfigUpdateClient(PacketConstructor<P> packetConstructor) {
this.packetConstructor = packetConstructor;
}

public ConfigUpdateC2SPacket prepUpdatePacket(ModConfigServer config) {
public P prepUpdatePacket(ModConfigServer config) {
waitingForAck = true;
return packetConstructor.construct(HandshakeServer.PROTOCOL_VERSION, config);
}
Expand Down Expand Up @@ -43,7 +42,7 @@ public void updateAcknowledged(ConfigUpdateAckS2CPacket packet) {
}
}

public interface PacketConstructor {
ConfigUpdateC2SPacket construct(int protocolVersion, ModConfigServer config);
public interface PacketConstructor<P extends ConfigUpdateC2SPacket> {
P construct(int protocolVersion, ModConfigServer config);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package nl.enjarai.doabarrelroll.platform.services;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

import java.util.function.Consumer;
import nl.enjarai.doabarrelroll.api.RollEntity;
import nl.enjarai.doabarrelroll.config.ModConfigServer;
import nl.enjarai.doabarrelroll.net.HandshakeClient;

public interface ClientNetworkUtils {
void sendPacket(Identifier channel, PacketByteBuf buf);
void sendRollUpdate(RollEntity entity);

void registerListener(Identifier channel, PacketListener listener);
void sendConfigUpdatePacket(ModConfigServer config);

interface PacketListener {
void accept(PacketByteBuf buf, Consumer<PacketByteBuf> responseSender);
}
HandshakeClient<?> getHandshakeClient();
}
Loading

0 comments on commit ee16638

Please sign in to comment.