Skip to content

Commit

Permalink
Protocol break - Identify packets by their ID instead of name
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Oct 12, 2024
1 parent c63f087 commit f1228a5
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.neznamy.tab.bridge.shared;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import me.neznamy.tab.bridge.shared.message.outgoing.*;
Expand Down Expand Up @@ -54,7 +56,10 @@ public void setGameModeRaw(int gameMode) {
}

public void sendPluginMessage(OutgoingMessage message) {
sendPluginMessage(message.write().toByteArray());
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeByte(OutgoingMessage.PACKET_IDS.get(message.getClass()));
message.write(out);
sendPluginMessage(out.toByteArray());
}

public abstract void sendPluginMessage(byte[] message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@RequiredArgsConstructor
public class TABBridge {

public static final String CHANNEL_NAME = "tab:bridge-5";
public static final String CHANNEL_NAME = "tab:bridge-6";
public static final String PLUGIN_VERSION = "6.0.0-SNAPSHOT";
@Getter @Setter private static TABBridge instance;

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

import com.google.common.io.ByteArrayDataInput;
import me.neznamy.tab.bridge.shared.BridgePlayer;
import me.neznamy.tab.bridge.shared.message.outgoing.PermissionResult;
import me.neznamy.tab.bridge.shared.message.outgoing.HasPermission;
import org.jetbrains.annotations.NotNull;

public class PermissionCheck implements IncomingMessage {
Expand All @@ -16,6 +16,6 @@ public void read(@NotNull ByteArrayDataInput in) {

@Override
public void process(@NotNull BridgePlayer player) {
player.sendPluginMessage(new PermissionResult(permission, player.hasPermission(permission)));
player.sendPluginMessage(new HasPermission(permission, player.hasPermission(permission)));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,11 +10,7 @@ public class GroupChange implements OutgoingMessage {
private String group;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Group");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeUTF(group);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

@AllArgsConstructor
public class PermissionResult implements OutgoingMessage {
public class HasPermission implements OutgoingMessage {

private String permission;
private boolean result;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Permission");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeUTF(permission);
out.writeBoolean(result);
return out;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,33 @@
import com.google.common.io.ByteArrayDataOutput;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public interface OutgoingMessage {

@NotNull
ByteArrayDataOutput write();
/** Map of packet IDs of custom plugin messages */
Map<Class<? extends OutgoingMessage>, Byte> PACKET_IDS = new HashMap<Class<? extends OutgoingMessage>, Byte>() {{
byte i = 0;
put(PlaceholderError.class, i++);
put(UpdateGameMode.class, i++);
put(HasPermission.class, i++);
put(SetInvisible.class, i++);
put(SetDisguised.class, i++);
put(WorldChange.class, i++);
put(GroupChange.class, i++);
put(SetVanished.class, i++);
put(UpdatePlaceholder.class, i); // Same ID as relational
put(UpdateRelationalPlaceholder.class, i++);
put(PlayerJoinResponse.class, i++);
put(RegisterPlaceholder.class, i);
}};

/**
* Writes the content into provided byte output.
*
* @param out
* Output to write into
*/
void write(@NotNull ByteArrayDataOutput out);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -12,16 +11,12 @@ public class PlaceholderError implements OutgoingMessage {
private Throwable exception;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("PlaceholderError");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeUTF(placeholderMessage);
out.writeInt(exception.getStackTrace().length+1);
out.writeUTF(exception.getClass().getName() + ": " + exception.getMessage());
for (StackTraceElement e : exception.getStackTrace()) {
out.writeUTF("\tat " + e.toString());
}
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -14,13 +13,10 @@ public class PlayerJoinResponse implements OutgoingMessage {
private String group;
private Map<String, Object> placeholders;
private int gameMode;

@Override
@NotNull

@SuppressWarnings("unchecked")
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("PlayerJoinResponse");
@Override
public void write(@NotNull ByteArrayDataOutput out) {
out.writeUTF(world);
if (group != null) out.writeUTF(group);
out.writeInt(placeholders.size());
Expand All @@ -38,6 +34,5 @@ public ByteArrayDataOutput write() {
}
}
out.writeInt(gameMode);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,11 +10,7 @@ public class RegisterPlaceholder implements OutgoingMessage {
private String identifier;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("RegisterPlaceholder");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeUTF(identifier);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,11 +10,7 @@ public class SetDisguised implements OutgoingMessage {
private boolean disguised;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Disguised");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeBoolean(disguised);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,11 +10,7 @@ public class SetInvisible implements OutgoingMessage {
private boolean invisible;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Invisible");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeBoolean(invisible);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,11 +10,7 @@ public class SetVanished implements OutgoingMessage {
private boolean vanished;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Vanished");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeBoolean(vanished);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,11 +10,7 @@ public class UpdateGameMode implements OutgoingMessage {
private int gameMode;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("UpdateGameMode");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeInt(gameMode);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -12,12 +11,8 @@ public class UpdatePlaceholder implements OutgoingMessage {
private String value;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Placeholder");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeUTF(identifier);
out.writeUTF(value);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -13,13 +12,9 @@ public class UpdateRelationalPlaceholder implements OutgoingMessage {
private String value;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Placeholder");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeUTF(identifier);
out.writeUTF(otherPlayer);
out.writeUTF(value);
return out;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.neznamy.tab.bridge.shared.message.outgoing;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,11 +10,7 @@ public class WorldChange implements OutgoingMessage {
private String world;

@Override
@NotNull
public ByteArrayDataOutput write() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("World");
public void write(@NotNull ByteArrayDataOutput out) {
out.writeUTF(world);
return out;
}
}

0 comments on commit f1228a5

Please sign in to comment.