Skip to content

Commit 9735a92

Browse files
authored
Add simple scoreboard API (#2181)
1 parent 439a74a commit 9735a92

File tree

6 files changed

+525
-1
lines changed

6 files changed

+525
-1
lines changed

src/main/java/cn/nukkit/Nukkit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class Nukkit {
4242

4343
public final static Properties GIT_INFO = getGitInfo();
4444
public final static String VERSION = getVersion();
45-
public final static String API_VERSION = "1.0.14";
45+
public final static String API_VERSION = "1.0.15";
4646
public final static String CODENAME = "";
4747
@Deprecated
4848
public final static String MINECRAFT_VERSION = ProtocolInfo.MINECRAFT_VERSION;

src/main/java/cn/nukkit/network/protocol/ProtocolInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public interface ProtocolInfo {
126126
byte SERVER_SETTINGS_RESPONSE_PACKET = 0x67;
127127
byte SHOW_PROFILE_PACKET = 0x68;
128128
byte SET_DEFAULT_GAME_TYPE_PACKET = 0x69;
129+
byte REMOVE_OBJECTIVE_PACKET = 0x6a;
130+
byte SET_DISPLAY_OBJECTIVE_PACKET = 0x6b;
131+
byte SET_SCORE_PACKET = 0x6c;
132+
byte LAB_TABLE_PACKET = 0x6d;
133+
byte UPDATE_BLOCK_SYNCED_PACKET = 0x6e;
129134
byte MOVE_ENTITY_DELTA_PACKET = 0x6f;
130135
byte SET_SCOREBOARD_IDENTITY_PACKET = 0x70;
131136
byte SET_LOCAL_PLAYER_AS_INITIALIZED_PACKET = 0x71;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.nukkit.network.protocol;
2+
3+
import lombok.ToString;
4+
5+
@ToString
6+
public class RemoveObjectivePacket extends DataPacket {
7+
8+
public static final byte NETWORK_ID = ProtocolInfo.REMOVE_OBJECTIVE_PACKET;
9+
10+
public String objectiveId;
11+
12+
@Override
13+
public byte pid() {
14+
return NETWORK_ID;
15+
}
16+
17+
@Override
18+
public void decode() {
19+
}
20+
21+
@Override
22+
public void encode() {
23+
this.reset();
24+
this.putString(this.objectiveId);
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.nukkit.network.protocol;
2+
3+
import cn.nukkit.scoreboard.Scoreboard;
4+
import lombok.ToString;
5+
6+
@ToString
7+
public class SetDisplayObjectivePacket extends DataPacket {
8+
9+
public static final byte NETWORK_ID = ProtocolInfo.SET_DISPLAY_OBJECTIVE_PACKET;
10+
11+
public Scoreboard.DisplaySlot displaySlot;
12+
public String objectiveId;
13+
public String displayName;
14+
public String criteria;
15+
public Scoreboard.SortOrder sortOrder;
16+
17+
@Override
18+
public byte pid() {
19+
return NETWORK_ID;
20+
}
21+
22+
@Override
23+
public void decode() {
24+
}
25+
26+
@Override
27+
public void encode() {
28+
this.reset();
29+
this.putString(this.displaySlot.getType());
30+
this.putString(this.objectiveId);
31+
this.putString(this.displayName);
32+
this.putString(this.criteria);
33+
this.putVarInt(this.sortOrder.ordinal());
34+
}
35+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package cn.nukkit.network.protocol;
2+
3+
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.Getter;
6+
import lombok.ToString;
7+
8+
import java.util.List;
9+
10+
@ToString
11+
public class SetScorePacket extends DataPacket {
12+
13+
public static final byte NETWORK_ID = ProtocolInfo.SET_SCORE_PACKET;
14+
15+
public Action action;
16+
public final List<ScoreInfo> infos = new ObjectArrayList<>();
17+
18+
@Override
19+
public byte pid() {
20+
return NETWORK_ID;
21+
}
22+
23+
@Override
24+
public void decode() {
25+
}
26+
27+
@Override
28+
public void encode() {
29+
this.reset();
30+
this.putByte((byte) this.action.ordinal());
31+
this.putUnsignedVarInt(this.infos.size());
32+
33+
for (ScoreInfo info : this.infos) {
34+
this.putVarLong(info.scoreboardId);
35+
this.putString(info.objectiveId);
36+
this.putLInt(info.score);
37+
38+
if (this.action == Action.SET) {
39+
this.putByte((byte) info.type.ordinal());
40+
41+
switch (info.type) {
42+
case PLAYER:
43+
case ENTITY:
44+
this.putEntityUniqueId(info.entityId);
45+
break;
46+
case FAKE:
47+
this.putString(info.name);
48+
break;
49+
default:
50+
throw new IllegalArgumentException("Invalid score info type");
51+
}
52+
}
53+
}
54+
}
55+
56+
public enum Action {
57+
SET,
58+
REMOVE
59+
}
60+
61+
@Getter
62+
@EqualsAndHashCode
63+
@ToString
64+
public static class ScoreInfo {
65+
66+
private final long scoreboardId;
67+
private final String objectiveId;
68+
private final int score;
69+
private final ScorerType type;
70+
private final String name;
71+
private final long entityId;
72+
73+
/**
74+
* Score info for fake player
75+
* @param scoreboardId
76+
* @param objectiveId
77+
* @param score score
78+
* @param name line text
79+
*/
80+
public ScoreInfo(long scoreboardId, String objectiveId, int score, String name) {
81+
this.scoreboardId = scoreboardId;
82+
this.objectiveId = objectiveId;
83+
this.score = score;
84+
this.type = ScorerType.FAKE;
85+
this.name = name;
86+
this.entityId = -1;
87+
}
88+
89+
/**
90+
* Score info for player/entity
91+
* @param scoreboardId
92+
* @param objectiveId
93+
* @param type entity type; PLAYER or ENTITY
94+
* @param score score
95+
* @param entityId entity id
96+
*/
97+
public ScoreInfo(long scoreboardId, String objectiveId, int score, ScorerType type, long entityId) {
98+
if (type != ScorerType.PLAYER && type != ScorerType.ENTITY) {
99+
throw new IllegalArgumentException("Scorer type must be either PLAYER or ENTITY");
100+
}
101+
102+
this.scoreboardId = scoreboardId;
103+
this.objectiveId = objectiveId;
104+
this.score = score;
105+
this.type = type;
106+
this.name = null;
107+
this.entityId = entityId;
108+
}
109+
110+
public enum ScorerType {
111+
INVALID,
112+
PLAYER,
113+
ENTITY,
114+
FAKE
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)