Skip to content

Commit

Permalink
Merge branch 'master' into leveldb
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriM1 committed Jun 11, 2024
2 parents 0038ffb + 9735a92 commit 9ee7817
Show file tree
Hide file tree
Showing 4 changed files with 522 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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() {
this.decodeUnsupported();
}

@Override
public void encode() {
this.reset();
this.putString(this.objectiveId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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() {
this.decodeUnsupported();
}

@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());
}
}
118 changes: 118 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,118 @@
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() {
this.decodeUnsupported();
}

@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

0 comments on commit 9ee7817

Please sign in to comment.