Arcturus-Community/src/main/java/com/eu/habbo/messages/ClientMessage.java

85 lines
1.8 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.messages;
import com.eu.habbo.util.PacketUtils;
2018-07-06 15:30:00 +02:00
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
2019-05-26 20:14:53 +02:00
public class ClientMessage {
2018-07-06 15:30:00 +02:00
private final int header;
private final ByteBuf buffer;
2019-05-26 20:14:53 +02:00
public ClientMessage(int messageId, ByteBuf buffer) {
this.header = messageId;
this.buffer = ((buffer == null) || (buffer.readableBytes() == 0) ? Unpooled.EMPTY_BUFFER : buffer);
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public ByteBuf getBuffer() {
return this.buffer;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public int getMessageId() {
return this.header;
2018-07-06 15:30:00 +02:00
}
2020-05-30 04:25:23 +02:00
/**
*
* @return
* @throws CloneNotSupportedException
*/
@Override
2019-05-26 20:14:53 +02:00
public ClientMessage clone() throws CloneNotSupportedException {
2018-07-06 15:30:00 +02:00
return new ClientMessage(this.header, this.buffer.duplicate());
}
2019-05-26 20:14:53 +02:00
public int readShort() {
try {
2018-07-06 15:30:00 +02:00
return this.buffer.readShort();
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
}
return 0;
}
2019-05-26 20:14:53 +02:00
public Integer readInt() {
try {
2018-07-06 15:30:00 +02:00
return this.buffer.readInt();
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
}
return 0;
}
2019-05-26 20:14:53 +02:00
public boolean readBoolean() {
try {
2018-07-06 15:30:00 +02:00
return this.buffer.readByte() == 1;
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
}
return false;
}
2019-05-26 20:14:53 +02:00
public String readString() {
try {
2019-03-18 02:22:00 +01:00
int length = this.readShort();
2018-07-06 15:30:00 +02:00
byte[] data = new byte[length];
this.buffer.readBytes(data);
2019-03-18 02:22:00 +01:00
return new String(data);
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
return "";
}
}
2019-05-26 20:14:53 +02:00
public String getMessageBody() {
return PacketUtils.formatPacket(this.buffer);
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public int bytesAvailable() {
2018-07-06 15:30:00 +02:00
return this.buffer.readableBytes();
}
public boolean release() {
return this.buffer.release();
}
2018-07-06 15:30:00 +02:00
}