Skip to content

Commit

Permalink
Added ViaVersion hook to fix protocol issues
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed Dec 28, 2020
1 parent 259f58b commit c58637a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/java/cc/ghast/packet/PacketManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cc.ghast.packet;

import cc.ghast.packet.buffer.ProtocolByteBuf;
import cc.ghast.packet.chain.ChainManager;
import cc.ghast.packet.compat.HookManager;
import cc.ghast.packet.listener.ChannelListener;
import lombok.Getter;
import org.bukkit.plugin.Plugin;
Expand All @@ -18,11 +20,13 @@ public enum PacketManager {
private Plugin plugin;
private ChainManager manager;
private ChannelListener listener;
private HookManager hookManager;

public void init(Plugin plugin) {
this.plugin = plugin;
this.manager = new ChainManager();
this.listener = new ChannelListener(this);
this.hookManager = new HookManager();
System.out.println("[Artemis Test] Enabled Test Decoder");
;
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/cc/ghast/packet/codec/ArtemisDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,26 @@ protected boolean decode(MutableByteBuf in) throws Exception {
if (in.readableBytes() != 0) {

// Get the var_int packet id of the packet. This is quite important as it's what determines it's type
int id = Converters.VAR_INT.read(in);
final int id = Converters.VAR_INT.read(in);

if (debug) {
System.out.println("Reader index=" + in.readerIndex());
System.out.println("Id of " + id);
}

// Initialize the protocol byte buf
final ProtocolByteBuf protocolByteBuf = new ProtocolByteBuf(in);

// Modify with hooks
PacketManager.INSTANCE.getHookManager().modifyAll(profile, direction, protocolByteBuf);

// Collect the packet from the enum map. This needs to be rewritten for better accuracy tho
Packet<?> packet = profile.getProtocol().getPacket(direction, id, profile.getUuid(), profile.getVersion());

if (packet != null) {
if (packet instanceof ReadableBuffer) {
ReadableBuffer buffer = (ReadableBuffer) packet;
buffer.read(new ProtocolByteBuf(in));
buffer.read(protocolByteBuf);
}

//System.out.println("pakcet=" + packet.getRealName());
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cc/ghast/packet/codec/ArtemisEncoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cc.ghast.packet.codec;

import cc.ghast.packet.PacketManager;
import cc.ghast.packet.buffer.ProtocolByteBuf;
import cc.ghast.packet.exceptions.InvalidPacketException;
import cc.ghast.packet.profile.Profile;
Expand Down Expand Up @@ -43,7 +44,10 @@ private void encode(Packet<?> packet, ByteBuf buf){
throw new InvalidPacketException(packet.getClass());
}

ProtocolByteBuf buffer = new ProtocolByteBuf(MutableByteBuf.translate((ByteBuf) buf));
final ProtocolByteBuf buffer = new ProtocolByteBuf(MutableByteBuf.translate((ByteBuf) buf));

// Modify with hooks
PacketManager.INSTANCE.getHookManager().modifyAll(profile, ProtocolDirection.OUT, buffer);

buffer.writeVarInt(packetId);

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/cc/ghast/packet/compat/HookManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cc.ghast.packet.compat;

import cc.ghast.packet.buffer.ProtocolByteBuf;
import cc.ghast.packet.profile.Profile;
import cc.ghast.packet.protocol.ProtocolDirection;
import cc.ghast.packet.wrapper.netty.MutableByteBuf;
import org.bukkit.Bukkit;

import java.util.ArrayList;
import java.util.List;

/**
* @author Ghast
* @since 28/12/2020
* ArtemisPacket © 2020
*/
public class HookManager {
private final List<PacketModifier> modifiers = new ArrayList<>();

public HookManager() {
this.init();
}

public void init() {
final boolean viaversion = Bukkit.getPluginManager().getPlugin("ViaVersion") != null;

if (viaversion) modifiers.add(new ViaVersionHook());
}

public void modifyAll(Profile profile, ProtocolDirection direction, ProtocolByteBuf byteBuf) {
modifiers.forEach(e -> e.modify(profile, direction, byteBuf));
}
}
2 changes: 2 additions & 0 deletions src/main/java/cc/ghast/packet/compat/ViaVersionHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public ProtocolByteBuf modify(Profile profile, ProtocolDirection direction, Prot
e.printStackTrace();
}

byteBuf.resetReaderIndex();

return byteBuf;
}

Expand Down

0 comments on commit c58637a

Please sign in to comment.