Skip to content

Commit

Permalink
fix: Working Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Oct 28, 2023
1 parent 98c1a34 commit 04de198
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 159 deletions.
6 changes: 3 additions & 3 deletions backend/fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ dependencies {
mappings(loom.officialMojangMappings())
modImplementation(libs.fabric.loader)
modImplementation(libs.fabric.api)
include(libs.mixinsextras)
implementation(libs.mixinsextras)
annotationProcessor(libs.mixinsextras)
// include(libs.mixinsextras)
// implementation(libs.mixinsextras)
// annotationProcessor(libs.mixinsextras)

shadeModule(projects.signedvelocityBackendCommon)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import io.github._4drian3d.signedvelocity.common.SignedQueue;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.minecraft.resources.ResourceLocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class SignedVelocity implements DedicatedServerModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("SignedVelocity");
public static final ResourceLocation CHANNEL = new ResourceLocation("signedvelocity", "main");
public static final SignedQueue CHAT_QUEUE = new SignedQueue();
public static final SignedQueue COMMAND_QUEUE = new SignedQueue();

@Override
public void onInitializeServer() {
LOGGER.info("Started SignedVelocity");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github._4drian3d.signedvelocity.fabric.mixins;

import io.github._4drian3d.signedvelocity.fabric.model.SignedChatCommandPacket;
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

@Mixin(ServerboundChatCommandPacket.class)
public class ChatCommandPacketMixin implements SignedChatCommandPacket {
@Unique
private boolean signedVelocity$handled = false;

@Override
public boolean signedVelocity$handled() {
return this.signedVelocity$handled;
}

@Override
public void signedVelocity$handled(boolean handled) {
this.signedVelocity$handled = handled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github._4drian3d.signedvelocity.fabric.mixins;

import io.github._4drian3d.signedvelocity.fabric.model.SignedPlayerChatMessage;
import net.minecraft.network.chat.PlayerChatMessage;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

@Mixin(PlayerChatMessage.class)
public class PlayerChatMixin implements SignedPlayerChatMessage {
@Unique
public boolean signedVelocity$handled;

@Override
public boolean signedVelocity$handled() {
return signedVelocity$handled;
}

@Override
public void signedVelocity$handled(boolean handled) {
signedVelocity$handled = handled;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github._4drian3d.signedvelocity.fabric.mixins;

import io.github._4drian3d.signedvelocity.common.SignedResult;
import io.github._4drian3d.signedvelocity.fabric.SignedVelocity;
import io.github._4drian3d.signedvelocity.fabric.model.SignedPlayerChatMessage;
import net.minecraft.network.chat.ChatType;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static java.util.Objects.requireNonNull;

@Mixin(value = PlayerList.class, priority = 1)
public abstract class PlayerListMixin {
@Shadow
public abstract void broadcastChatMessage(PlayerChatMessage playerChatMessage, ServerPlayer serverPlayer, ChatType.Bound bound);

@Inject(
method = "broadcastChatMessage(Lnet/minecraft/network/chat/PlayerChatMessage;Lnet/minecraft/server/level/ServerPlayer;Lnet/minecraft/network/chat/ChatType$Bound;)V",
at = @At("HEAD"), cancellable = true)
public void signedVelocity$handleChat(
PlayerChatMessage playerChatMessage,
ServerPlayer serverPlayer,
ChatType.Bound bound,
CallbackInfo ci
) {
requireNonNull(serverPlayer);
if (!((SignedPlayerChatMessage)(Object)playerChatMessage).signedVelocity$handled()) {
((SignedPlayerChatMessage)(Object)playerChatMessage).signedVelocity$handled(true);
final SignedResult result = SignedVelocity.CHAT_QUEUE.dataFrom(serverPlayer.getUUID())
.nextResult().join();
// Cancelled Result
if (result.cancelled()) {
ci.cancel();
return;
}
final String modified = result.message();
// Modified Result
if (modified != null) {
this.broadcastChatMessage(playerChatMessage.withUnsignedContent(Component.literal(modified)), serverPlayer, bound);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.github._4drian3d.signedvelocity.fabric.mixins;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import io.github._4drian3d.signedvelocity.common.SignedQueue;
import io.github._4drian3d.signedvelocity.common.SignedResult;
import io.github._4drian3d.signedvelocity.fabric.SignedVelocity;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import org.spongepowered.asm.mixin.Mixin;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
Expand All @@ -19,10 +20,10 @@ public abstract class ServerCommonPacketListenerMixin {
@Inject(at = @At("HEAD"), method = "handleCustomPayload", cancellable = true)
private void signedVelocity$onPluginMessage(ServerboundCustomPayloadPacket packet, CallbackInfo ci) {
if (packet.payload() instanceof PacketByteBufPayload payload && payload.id().equals(SignedVelocity.CHANNEL)) {
final FriendlyByteBuf input = payload.data();
final UUID playerId = UUID.fromString(input.readUtf());
final String source = input.readUtf();
final String result = input.readUtf();
final ByteArrayDataInput input = ByteStreams.newDataInput(payload.data().unwrap().array());
final UUID playerId = UUID.fromString(input.readUTF());
final String source = input.readUTF();
final String result = input.readUTF();

final SignedQueue queue = switch (source) {
case "COMMAND_RESULT" -> SignedVelocity.COMMAND_QUEUE;
Expand All @@ -31,7 +32,7 @@ public abstract class ServerCommonPacketListenerMixin {
};
final SignedResult resulted = switch (result) {
case "CANCEL" -> SignedResult.cancel();
case "MODIFY" -> SignedResult.modify(input.readUtf());
case "MODIFY" -> SignedResult.modify(input.readUTF());
case "ALLOWED" -> SignedResult.allowed();
default -> throw new IllegalArgumentException("Invalid result " + result);
};
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github._4drian3d.signedvelocity.fabric.mixins;

import io.github._4drian3d.signedvelocity.common.SignedResult;
import io.github._4drian3d.signedvelocity.fabric.SignedVelocity;
import io.github._4drian3d.signedvelocity.fabric.model.SignedChatCommandPacket;
import net.minecraft.network.chat.LastSeenMessages;
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value = ServerGamePacketListenerImpl.class, priority = 1)
public abstract class ServerGamePacketListenerMixin {
@Shadow
public ServerPlayer player;

@Shadow
protected abstract void performChatCommand(ServerboundChatCommandPacket serverboundChatCommandPacket, LastSeenMessages lastSeenMessages);

@Inject(
method = "performChatCommand",
at = @At("HEAD"),
cancellable = true)
public void signedVelocity$handleChatCommand(
ServerboundChatCommandPacket packet,
LastSeenMessages lastSeenMessages,
CallbackInfo ci
) {
if (!((SignedChatCommandPacket)(Object) packet).signedVelocity$handled()) {
return;
}
((SignedChatCommandPacket)(Object) packet).signedVelocity$handled(true);
final SignedResult result = SignedVelocity.COMMAND_QUEUE.dataFrom(player.getUUID())
.nextResult().join();
if (result.cancelled()) {
ci.cancel();
return;
}
final String modified = result.toModify();
if (modified != null) {
// TODO: verify
this.performChatCommand(
new ServerboundChatCommandPacket(
modified,
packet.timeStamp(),
packet.salt(),
packet.argumentSignatures(),
packet.lastSeenMessages()
),
lastSeenMessages
);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github._4drian3d.signedvelocity.fabric.model;

public interface SignedChatCommandPacket {
boolean signedVelocity$handled();

void signedVelocity$handled(boolean handled);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github._4drian3d.signedvelocity.fabric.model;

public interface SignedPlayerChatMessage {
boolean signedVelocity$handled();

void signedVelocity$handled(boolean handled);
}
Loading

0 comments on commit 04de198

Please sign in to comment.