Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started chat protocol implementation.
- Loading branch information
Sam Jakob Harker
committed
Jan 14, 2018
1 parent
1bb0617
commit cc49954
Showing
8 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/main/java/com/cloutteam/jarcraftinator/protocol/packet/PacketIn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package com.cloutteam.jarcraftinator.protocol.packet; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
|
||
public abstract class PacketIn extends Packet { | ||
|
||
public abstract void onReceive(int length, DataInputStream in) throws Exception; | ||
public abstract void onReceive(int length, DataInputStream in) throws IOException; | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/cloutteam/jarcraftinator/protocol/packet/PacketPlayInChat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.cloutteam.jarcraftinator.protocol.packet; | ||
|
||
import com.cloutteam.jarcraftinator.utils.VarData; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
|
||
public class PacketPlayInChat extends PacketIn { | ||
|
||
private boolean isValid; | ||
private String message; | ||
|
||
@Override | ||
public void onReceive(int length, DataInputStream in) throws IOException { | ||
int stringSize = VarData.readVarInt(in); | ||
|
||
if(stringSize > 256){ | ||
isValid = false; | ||
message = null; | ||
}else { | ||
isValid = true; | ||
message = VarData.readVarString(in, stringSize); | ||
} | ||
} | ||
|
||
public boolean isValid() { | ||
return isValid; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/cloutteam/jarcraftinator/protocol/packet/PacketPlayInKeepAlive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.cloutteam.jarcraftinator.protocol.packet; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
|
||
public class PacketPlayInKeepAlive extends PacketIn { | ||
|
||
private long keepAliveId; | ||
|
||
@Override | ||
public void onReceive(int length, DataInputStream in) throws IOException { | ||
keepAliveId = in.readLong(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/cloutteam/jarcraftinator/protocol/packet/PacketPlayOutChat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.cloutteam.jarcraftinator.protocol.packet; | ||
|
||
import com.cloutteam.jarcraftinator.utils.VarData; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
|
||
public class PacketPlayOutChat extends PacketOut { | ||
|
||
private String chatComponent; | ||
|
||
public PacketPlayOutChat(String chatComponent){ | ||
this.chatComponent = chatComponent; | ||
} | ||
|
||
@Override | ||
public void send(DataOutputStream out) throws IOException { | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); | ||
|
||
byte[] packetId = VarData.getVarInt(0x0F); | ||
|
||
// Begin packet data | ||
VarData.writeVarString(dataOutputStream, chatComponent); | ||
dataOutputStream.writeByte(0x00); // 0 = chat box | ||
// End packet data | ||
|
||
// Flush packet data | ||
dataOutputStream.flush(); | ||
dataOutputStream.close(); | ||
byteArrayOutputStream.flush(); | ||
|
||
// Send the entire packet to the client | ||
VarData.writeVarInt(out, packetId.length + dataOutputStream.size()); | ||
out.write(packetId); | ||
out.write(byteArrayOutputStream.toByteArray()); | ||
out.flush(); | ||
|
||
// Close our temporary streams | ||
byteArrayOutputStream.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters