Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement player network join/leave events #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lilypad.client.connect.api.event;

import java.util.UUID;

/**
* Called when a player has been joined the network
* when the session is authenticated.
*/
public class PlayerJoinEvent extends Event {

private String playerName;
private UUID playerUUID;

/**
*
* @param playerName the name of the player that has joined
* @param playerUUID the uuid of the player that has joined
*/
public PlayerJoinEvent(String playerName, UUID playerUUID) {
this.playerName = playerName;
this.playerUUID = playerUUID;
}

/**
*
* @return the name of the player that has joined
*/
public String getName() {
return this.playerName;
}

/**
*
* @return the uuid of the player that has joined
*/
public UUID getUUID() {
return this.playerUUID;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lilypad.client.connect.api.event;

import java.util.UUID;

/**
* Called when a player has been left the network
* when the session is authenticated.
*/
public class PlayerLeaveEvent extends Event {

private String playerName;
private UUID playerUUID;

/**
*
* @param playerName the name of the player that has left
* @param playerUUID the uuid of the player that has left
*/
public PlayerLeaveEvent(String playerName, UUID playerUUID) {
this.playerName = playerName;
this.playerUUID = playerUUID;
}

/**
*
* @return the name of the player that has left
*/
public String getName() {
return this.playerName;
}

/**
*
* @return the uuid of the player that has left
*/
public UUID getUUID() {
return this.playerUUID;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
import java.io.IOException;
import java.net.InetSocketAddress;

import lilypad.client.connect.api.event.MessageEvent;
import lilypad.client.connect.api.event.RedirectEvent;
import lilypad.client.connect.api.event.ServerAddEvent;
import lilypad.client.connect.api.event.ServerRemoveEvent;
import lilypad.client.connect.api.event.*;
import lilypad.client.connect.api.result.StatusCode;
import lilypad.packet.common.Packet;
import lilypad.packet.connect.ConnectPacketConstants;
import lilypad.packet.connect.impl.MessagePacket;
import lilypad.packet.connect.impl.RedirectPacket;
import lilypad.packet.connect.impl.ResultPacket;
import lilypad.packet.connect.impl.ServerAddPacket;
import lilypad.packet.connect.impl.ServerPacket;
import lilypad.packet.connect.impl.*;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
Expand Down Expand Up @@ -78,6 +71,16 @@ protected void channelRead0(ChannelHandlerContext context, Packet packet) throws
this.connect.dispatchEvent(serverRemoveEvent);
}
break;
case 0x06:
PlayerPacket playerPacket = (PlayerPacket) packet;
if(playerPacket.isJoining()) {
PlayerJoinEvent playerJoinEvent = new PlayerJoinEvent(playerPacket.getPlayerName(), playerPacket.getPlayerUUID());
this.connect.dispatchEvent(playerJoinEvent);
} else {
PlayerLeaveEvent playerLeaveEvent = new PlayerLeaveEvent(playerPacket.getPlayerName(), playerPacket.getPlayerUUID());
this.connect.dispatchEvent(playerLeaveEvent);
}
break;
default:
context.close(); // invalid packet
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package lilypad.packet.connect;

import lilypad.packet.common.PacketCodecRegistry;
import lilypad.packet.connect.impl.KeepalivePacketCodec;
import lilypad.packet.connect.impl.MessagePacketCodec;
import lilypad.packet.connect.impl.RedirectPacketCodec;
import lilypad.packet.connect.impl.RequestPacketCodec;
import lilypad.packet.connect.impl.ResultPacketCodec;
import lilypad.packet.connect.impl.ServerPacketCodec;
import lilypad.packet.connect.impl.*;

public class ConnectPacketCodecRegistry extends PacketCodecRegistry {

Expand All @@ -19,6 +14,7 @@ public ConnectPacketCodecRegistry() {
this.register(new MessagePacketCodec());
this.register(new RedirectPacketCodec());
this.register(new ServerPacketCodec());
this.register(new PlayerPacketCodec());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lilypad.packet.connect.impl;

import lilypad.packet.common.Packet;

import java.util.UUID;

public class PlayerPacket extends Packet {

public static final int opcode = 0x06;

private boolean joining;
private String playerName;
private UUID playerUUID;

public PlayerPacket(boolean joining, String playerName, UUID playerUUID) {
super(opcode);
this.joining = joining;
this.playerName = playerName;
this.playerUUID = playerUUID;
}

public boolean isJoining() {
return this.joining;
}

public String getPlayerName() {
return playerName;
}

public UUID getPlayerUUID() {
return playerUUID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lilypad.packet.connect.impl;

import io.netty.buffer.ByteBuf;
import lilypad.packet.common.PacketCodec;
import lilypad.packet.common.util.BufferUtils;

import java.util.UUID;

public class PlayerPacketCodec extends PacketCodec<PlayerPacket> {

public PlayerPacketCodec() {
super(PlayerPacket.opcode);
}

public PlayerPacket decode(ByteBuf buffer) throws Exception {
boolean addOrRemove = buffer.readBoolean();
String name = BufferUtils.readString(buffer);
long high = buffer.readLong();
long low = buffer.readLong();
UUID uuid = new UUID(high, low);
return new PlayerPacket(addOrRemove, name, uuid);
}

public void encode(PlayerPacket packet, ByteBuf buffer) {
buffer.writeBoolean(packet.isJoining());
BufferUtils.writeString(buffer, packet.getPlayerName());
buffer.writeLong(packet.getPlayerUUID().getMostSignificantBits());
buffer.writeLong(packet.getPlayerUUID().getLeastSignificantBits());
}

}