Skip to content

Commit

Permalink
新增玩家皮肤发送事件:SendPlayerListDataEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
boybook committed Sep 24, 2023
1 parent 6f6a4e5 commit f14d20c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cn.nukkit.event.HandlerList;
import cn.nukkit.event.level.LevelInitEvent;
import cn.nukkit.event.level.LevelLoadEvent;
import cn.nukkit.event.player.SendPlayerListDataEvent;
import cn.nukkit.event.server.BatchPacketsEvent;
import cn.nukkit.event.server.QueryRegenerateEvent;
import cn.nukkit.inventory.CraftingManager;
Expand Down Expand Up @@ -1019,10 +1020,39 @@ public void updatePlayerListData(UUID uuid, long entityId, String name, Skin ski
}

public void updatePlayerListData(UUID uuid, long entityId, String name, Skin skin, String xboxUserId, Player[] players) {
PlayerListPacket pk = new PlayerListPacket();
pk.type = PlayerListPacket.TYPE_ADD;
pk.entries = new PlayerListPacket.Entry[]{new PlayerListPacket.Entry(uuid, entityId, name, skin, xboxUserId)};
Server.broadcastPacket(players, pk);
Set<Player> playersSet = null;
for (Player player : players) {
SendPlayerListDataEvent event = new SendPlayerListDataEvent(player, uuid, entityId, name, skin, xboxUserId);
event.call();
if (event.isCancelled()) {
if (playersSet == null) {
playersSet = new HashSet<>(Arrays.asList(players));
}
playersSet.remove(player);
} else if (event.isDirty()) {
if (playersSet == null) {
playersSet = new HashSet<>(Arrays.asList(players));
}
playersSet.remove(player);
PlayerListPacket pk = new PlayerListPacket();
pk.type = PlayerListPacket.TYPE_ADD;
pk.entries = new PlayerListPacket.Entry[]{new PlayerListPacket.Entry(event.getUuid(), event.getEntityId(), event.getName(), event.getSkin(), event.getXboxUserId())};
player.dataPacket(pk);
}
}
if (playersSet != null) {
if (!playersSet.isEmpty()) {
PlayerListPacket pk = new PlayerListPacket();
pk.type = PlayerListPacket.TYPE_ADD;
pk.entries = new PlayerListPacket.Entry[]{new PlayerListPacket.Entry(uuid, entityId, name, skin, xboxUserId)};
Server.broadcastPacket(playersSet, pk);
}
} else {
PlayerListPacket pk = new PlayerListPacket();
pk.type = PlayerListPacket.TYPE_ADD;
pk.entries = new PlayerListPacket.Entry[]{new PlayerListPacket.Entry(uuid, entityId, name, skin, xboxUserId)};
Server.broadcastPacket(players, pk);
}
}

public void updatePlayerListData(UUID uuid, long entityId, String name, Skin skin, String xboxUserId, Collection<Player> players) {
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/cn/nukkit/event/player/SendPlayerListDataEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cn.nukkit.event.player;

import cn.nukkit.Player;
import cn.nukkit.entity.data.Skin;
import cn.nukkit.event.Cancellable;
import cn.nukkit.event.HandlerList;
import lombok.Getter;

import java.util.UUID;

/**
* 发送玩家列表数据事件
* 但是需要注意,这里面的player是指接收到的玩家,而不是皮肤和uuid对应的玩家!
*/
public class SendPlayerListDataEvent extends PlayerEvent implements Cancellable {

private static final HandlerList handlers = new HandlerList();

public static HandlerList getHandlers() {
return handlers;
}

@Getter
private final UUID uuid;

@Getter
private final long entityId;

@Getter
private String name;

@Getter
private Skin skin;

@Getter
private String xboxUserId;

@Getter
private boolean dirty = false;

/**
* 发送玩家列表数据事件
* @param player 需要注意,这里面的player是指接收到的玩家,而不是皮肤和uuid对应的玩家!
* @param uuid UUID
* @param entityId 实体ID
* @param name 名字
* @param skin 皮肤
* @param xboxUserId Xbox用户ID
*/
public SendPlayerListDataEvent(Player player, UUID uuid, long entityId, String name, Skin skin, String xboxUserId) {
this.uuid = uuid;
this.entityId = entityId;
this.name = name;
this.xboxUserId = xboxUserId;
this.player = player;
this.skin = skin;
}

public void setSkin(Skin skin) {
this.skin = skin;
this.dirty = true;
}

public void setName(String name) {
this.name = name;
this.dirty = true;
}

public void setXboxUserId(String xboxUserId) {
this.xboxUserId = xboxUserId;
this.dirty = true;
}

}

0 comments on commit f14d20c

Please sign in to comment.