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

Add simple scoreboard API #2181

Merged
merged 3 commits into from
Jun 11, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/Nukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Nukkit {

public final static Properties GIT_INFO = getGitInfo();
public final static String VERSION = getVersion();
public final static String API_VERSION = "1.0.14";
public final static String API_VERSION = "1.0.15";
public final static String CODENAME = "";
@Deprecated
public final static String MINECRAFT_VERSION = ProtocolInfo.MINECRAFT_VERSION;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/cn/nukkit/network/protocol/ProtocolInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public interface ProtocolInfo {
byte SERVER_SETTINGS_RESPONSE_PACKET = 0x67;
byte SHOW_PROFILE_PACKET = 0x68;
byte SET_DEFAULT_GAME_TYPE_PACKET = 0x69;
byte REMOVE_OBJECTIVE_PACKET = 0x6a;
byte SET_DISPLAY_OBJECTIVE_PACKET = 0x6b;
byte SET_SCORE_PACKET = 0x6c;
byte LAB_TABLE_PACKET = 0x6d;
byte UPDATE_BLOCK_SYNCED_PACKET = 0x6e;
byte MOVE_ENTITY_DELTA_PACKET = 0x6f;
byte SET_SCOREBOARD_IDENTITY_PACKET = 0x70;
byte SET_LOCAL_PLAYER_AS_INITIALIZED_PACKET = 0x71;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cn.nukkit.network.protocol;

import lombok.ToString;

@ToString
public class RemoveObjectivePacket extends DataPacket {

public static final byte NETWORK_ID = ProtocolInfo.REMOVE_OBJECTIVE_PACKET;

public String objectiveId;

@Override
public byte pid() {
return NETWORK_ID;
}

@Override
public void decode() {
}

@Override
public void encode() {
this.reset();
this.putString(this.objectiveId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.nukkit.network.protocol;

import cn.nukkit.scoreboard.Scoreboard;
import lombok.ToString;

@ToString
public class SetDisplayObjectivePacket extends DataPacket {

public static final byte NETWORK_ID = ProtocolInfo.SET_DISPLAY_OBJECTIVE_PACKET;

public Scoreboard.DisplaySlot displaySlot;
public String objectiveId;
public String displayName;
public String criteria;
public Scoreboard.SortOrder sortOrder;

@Override
public byte pid() {
return NETWORK_ID;
}

@Override
public void decode() {
}

@Override
public void encode() {
this.reset();
this.putString(this.displaySlot.getType());
this.putString(this.objectiveId);
this.putString(this.displayName);
this.putString(this.criteria);
this.putVarInt(this.sortOrder.ordinal());
}
}
117 changes: 117 additions & 0 deletions src/main/java/cn/nukkit/network/protocol/SetScorePacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package cn.nukkit.network.protocol;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;

import java.util.List;

@ToString
public class SetScorePacket extends DataPacket {

public static final byte NETWORK_ID = ProtocolInfo.SET_SCORE_PACKET;

public Action action;
public final List<ScoreInfo> infos = new ObjectArrayList<>();

@Override
public byte pid() {
return NETWORK_ID;
}

@Override
public void decode() {
}

@Override
public void encode() {
this.reset();
this.putByte((byte) this.action.ordinal());
this.putUnsignedVarInt(this.infos.size());

for (ScoreInfo info : this.infos) {
this.putVarLong(info.scoreboardId);
this.putString(info.objectiveId);
this.putLInt(info.score);

if (this.action == Action.SET) {
this.putByte((byte) info.type.ordinal());

switch (info.type) {
case PLAYER:
case ENTITY:
this.putEntityUniqueId(info.entityId);
break;
case FAKE:
this.putString(info.name);
break;
default:
throw new IllegalArgumentException("Invalid score info type");
}
}
}
}

public enum Action {
SET,
REMOVE
}

@Getter
@EqualsAndHashCode
@ToString
public static class ScoreInfo {

private final long scoreboardId;
private final String objectiveId;
private final int score;
private final ScorerType type;
private final String name;
private final long entityId;

/**
* Score info for fake player
* @param scoreboardId
* @param objectiveId
* @param score score
* @param name line text
*/
public ScoreInfo(long scoreboardId, String objectiveId, int score, String name) {
this.scoreboardId = scoreboardId;
this.objectiveId = objectiveId;
this.score = score;
this.type = ScorerType.FAKE;
this.name = name;
this.entityId = -1;
}

/**
* Score info for player/entity
* @param scoreboardId
* @param objectiveId
* @param type entity type; PLAYER or ENTITY
* @param score score
* @param entityId entity id
*/
public ScoreInfo(long scoreboardId, String objectiveId, int score, ScorerType type, long entityId) {
if (type != ScorerType.PLAYER && type != ScorerType.ENTITY) {
throw new IllegalArgumentException("Scorer type must be either PLAYER or ENTITY");
}

this.scoreboardId = scoreboardId;
this.objectiveId = objectiveId;
this.score = score;
this.type = type;
this.name = null;
this.entityId = entityId;
}

public enum ScorerType {
INVALID,
PLAYER,
ENTITY,
FAKE
}
}
}
Loading
Loading