cn.nukkit.network.protocol.TextPacket Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powernukkit Show documentation
Show all versions of powernukkit Show documentation
A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.
package cn.nukkit.network.protocol;
import cn.nukkit.api.DeprecationDetails;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import cn.nukkit.utils.BinaryStream;
import io.netty.util.internal.EmptyArrays;
import lombok.ToString;
/**
* @since 15-10-13
*/
@ToString
public class TextPacket extends DataPacket {
public static final byte NETWORK_ID = ProtocolInfo.TEXT_PACKET;
@Override
public byte pid() {
return NETWORK_ID;
}
public static final byte TYPE_RAW = 0;
public static final byte TYPE_CHAT = 1;
public static final byte TYPE_TRANSLATION = 2;
public static final byte TYPE_POPUP = 3;
public static final byte TYPE_JUKEBOX_POPUP = 4;
public static final byte TYPE_TIP = 5;
public static final byte TYPE_SYSTEM = 6;
public static final byte TYPE_WHISPER = 7;
public static final byte TYPE_ANNOUNCEMENT = 8;
@PowerNukkitOnly("Backward compatibility")
@Deprecated @DeprecationDetails(
since = "1.3.0.0-PN", toBeRemovedAt = "1.4.0.0-PN",
reason = "Renamed to TYPE_JSON by NukkitX", replaceWith = "TYPE_OBJECT")
public static final byte TYPE_JSON = 9;
@Since("1.3.0.0-PN") public static final byte TYPE_OBJECT = 9;
@Since("1.3.0.0-PN") public static final byte TYPE_OBJECT_WHISPER = 10;
public byte type;
public String source = "";
public String message = "";
public String[] parameters = EmptyArrays.EMPTY_STRINGS;
public boolean isLocalized = false;
public String xboxUserId = "";
public String platformChatId = "";
@Override
public void decode() {
this.type = (byte) getByte();
this.isLocalized = this.getBoolean() || type == TYPE_TRANSLATION;
switch (type) {
case TYPE_CHAT:
case TYPE_WHISPER:
case TYPE_ANNOUNCEMENT:
this.source = this.getString();
case TYPE_RAW:
case TYPE_TIP:
case TYPE_SYSTEM:
case TYPE_OBJECT:
case TYPE_OBJECT_WHISPER:
this.message = this.getString();
break;
case TYPE_TRANSLATION:
case TYPE_POPUP:
case TYPE_JUKEBOX_POPUP:
this.message = this.getString();
this.parameters = this.getArray(String.class, BinaryStream::getString);
}
this.xboxUserId = this.getString();
this.platformChatId = this.getString();
}
@Override
public void encode() {
this.reset();
this.putByte(this.type);
this.putBoolean(this.isLocalized || type == TYPE_TRANSLATION);
switch (this.type) {
case TYPE_CHAT:
case TYPE_WHISPER:
case TYPE_ANNOUNCEMENT:
this.putString(this.source);
case TYPE_RAW:
case TYPE_TIP:
case TYPE_SYSTEM:
case TYPE_OBJECT:
case TYPE_OBJECT_WHISPER:
this.putString(this.message);
break;
case TYPE_TRANSLATION:
case TYPE_POPUP:
case TYPE_JUKEBOX_POPUP:
this.putString(this.message);
this.putUnsignedVarInt(this.parameters.length);
for (String parameter : this.parameters) {
this.putString(parameter);
}
}
this.putString(this.xboxUserId);
this.putString(this.platformChatId);
}
}