Skip to content

Commit

Permalink
Fixed annoyance
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed Nov 29, 2020
1 parent 73ec461 commit 84c97f0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/main/java/cc/ghast/packet/codec/ArtemisEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public ArtemisEncoder(Profile profile) {
protected void encode(ChannelHandlerContext channelHandlerContext, Packet<?> obj, ByteBuf byteBuf) {
try {
encode(obj, byteBuf);
System.out.println("Wrote packet ");
} catch (Exception e){
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static Injector build() {

Map<UUID, Profile> profiles = new WeakHashMap<>();

void inject(Object channel, UUID uuid, InetAddress inetAddress);
void inject(Object channel, UUID uuid, String inetAddress);

default Profile getProfile(UUID uuid) {
return profiles.get(uuid);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package cc.ghast.packet.listener.injector;

import cc.ghast.packet.codec.ArtemisDecoder;
import cc.ghast.packet.codec.ArtemisDecoderLegacy;
import cc.ghast.packet.codec.ArtemisEncoder;
import cc.ghast.packet.codec.ArtemisEncoderLegacy;
import cc.ghast.packet.nms.ProtocolVersion;
import cc.ghast.packet.profile.Profile;
import cc.ghast.packet.protocol.EnumProtocolCurrent;
import cc.ghast.packet.protocol.ProtocolDirection;
import cc.ghast.packet.reflections.ReflectUtil;
import cc.ghast.packet.utils.HookUtil;
import cc.ghast.packet.wrapper.packet.Packet;
import lombok.SneakyThrows;
import net.minecraft.util.io.netty.channel.Channel;
import net.minecraft.util.io.netty.channel.ChannelFuture;
import net.minecraft.util.io.netty.channel.ChannelFutureListener;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.net.InetAddress;
import java.util.UUID;

/**
Expand All @@ -30,7 +35,7 @@ public InjectorLegacy() {
@Override
public void inject(AsyncPlayerPreLoginEvent event) {
Channel channel = (Channel) ReflectUtil.getChannel(event.getUniqueId(), event.getAddress().getHostAddress());
inject(channel, event.getUniqueId(), event.getAddress());
inject(channel, event.getUniqueId(), event.getAddress().getHostAddress());
}

@Override
Expand All @@ -42,17 +47,34 @@ public void uninject(PlayerQuitEvent event) {
}

@Override
public void inject(Object channel, UUID uuid, InetAddress inetAddress) {
public void inject(Object channel, UUID uuid, String inetAddress) {
Profile profile = new Profile(uuid, inetAddress, ProtocolVersion.getGameVersion(), channel);
((Channel)channel).pipeline().addBefore(HookUtil.getHookBehind(), clientBound, new ArtemisDecoderLegacy(profile, ProtocolDirection.IN));
((Channel)channel).pipeline().addAfter(HookUtil.getHookForward(), serverBound, new ArtemisDecoderLegacy(profile, ProtocolDirection.OUT));
profile.setProtocol(EnumProtocolCurrent.PLAY);
((Channel)channel).pipeline().addBefore(HookUtil.getHookBehind(), clientBound,
new ArtemisDecoderLegacy(profile, ProtocolDirection.IN));
((Channel)channel).pipeline().addAfter(HookUtil.getHookOutbound(), serverBound,
new ArtemisDecoderLegacy(profile, ProtocolDirection.OUT));
((Channel)channel).pipeline().addLast(encoder, new ArtemisEncoderLegacy(profile));
profiles.put(uuid, profile);
}

@Override
public Profile getProfile(UUID uuid) {
return profiles.computeIfAbsent(uuid, e -> {
String addy = parseAddress(Bukkit.getPlayer(e).getAddress());
Channel pipeline = (Channel) ReflectUtil.getChannel(e, addy);
inject(pipeline, e, addy);
return profiles.get(e);
});
}

@Override
@SneakyThrows
public void writePacket(Player player, Packet<?> packet) {
Channel channel = (Channel) profiles.get(player.getUniqueId()).getChannel();
Channel channel = (Channel) this.getProfile(player.getUniqueId()).getChannel();

ChannelFuture channelfuture = channel.writeAndFlush(packet);
channelfuture.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
Expand All @@ -34,7 +35,7 @@ public InjectorModern() {
@Override
public void inject(AsyncPlayerPreLoginEvent event) {
Channel channel = (Channel) ReflectUtil.getChannel(event.getUniqueId(), event.getAddress().getHostAddress());
inject(channel, event.getUniqueId(), event.getAddress());
inject(channel, event.getUniqueId(), event.getAddress().getHostAddress());
}

@Override
Expand All @@ -50,7 +51,7 @@ public void uninject(PlayerQuitEvent event) {
}

@Override
public void inject(Object channel, UUID uuid, InetAddress inetAddress) {
public void inject(Object channel, UUID uuid, String inetAddress) {
Profile profile = new Profile(uuid, inetAddress, ProtocolVersion.getGameVersion(), channel);
profile.setProtocol(EnumProtocolCurrent.PLAY);
((Channel)channel).pipeline().addBefore(HookUtil.getHookBehind(), clientBound, new ArtemisDecoder(profile, ProtocolDirection.IN));
Expand All @@ -59,10 +60,20 @@ public void inject(Object channel, UUID uuid, InetAddress inetAddress) {
profiles.put(uuid, profile);
}

@Override
public Profile getProfile(UUID uuid) {
return profiles.computeIfAbsent(uuid, e -> {
String addy = parseAddress(Bukkit.getPlayer(e).getAddress());
Channel pipeline = (Channel) ReflectUtil.getChannel(e, addy);
inject(pipeline, e, addy);
return profiles.get(e);
});
}

@Override
@SneakyThrows
public void writePacket(Player player, Packet<?> packet) {
Channel channel = (Channel) profiles.get(player.getUniqueId()).getChannel();
Channel channel = (Channel) this.getProfile(player.getUniqueId()).getChannel();

ChannelFuture channelfuture = channel.writeAndFlush(packet);
channelfuture.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cc/ghast/packet/profile/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
@Data
public class Profile {
private final UUID uuid;
private final InetAddress address;
private final String address;
private ProtocolVersion version;
private Object channel;
private EnumProtocol protocol;

public Profile(UUID uuid, InetAddress address, ProtocolVersion version, Object channel) {
public Profile(UUID uuid, String address, ProtocolVersion version, Object channel) {
this.uuid = uuid;
this.address = address;
this.version = version;
Expand Down

0 comments on commit 84c97f0

Please sign in to comment.