Skip to content

Commit

Permalink
Merge branch 'master' into PM1E
Browse files Browse the repository at this point in the history
# Conflicts:
#	pom.xml
#	src/main/java/com/smallaswater/npc/entitys/EntityRsNPCCustomEntity.java
#	src/main/resources/plugin.yml
  • Loading branch information
lt-name committed Jan 6, 2024
2 parents b1727f9 + e0bd32f commit a40976f
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 41 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.smallaswater</groupId>
<artifactId>RsNPC</artifactId>
<version>2.4.2-PM1E<!-- -SNAPSHOT--></version>
<version>2.4.3-PM1E<!-- -SNAPSHOT--></version>

<name>RsNPC</name>
<description>RsNPC -- NPC plugin for Nukkit</description>
Expand Down Expand Up @@ -88,7 +88,7 @@
<dependency>
<groupId>cn.nukkit</groupId>
<artifactId>Nukkit</artifactId>
<version>PM1E_1.20.50.3397</version>
<version>MOT-java8-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/smallaswater/npc/RsNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ public void onLoad() {

@Override
public void onEnable() {
this.loadLanguage();

switch (GameCoreDownload.checkAndDownload()) {
case 1:
Server.getInstance().getPluginManager().disablePlugin(this);
Expand All @@ -114,6 +112,8 @@ public void onEnable() {
break;
}

this.loadLanguage();

try {
if (Server.getInstance().getPluginManager().getPlugin("AutoUpData") != null) {
if (AutoData.defaultUpDataByMaven(this, this.getFile(), "com.smallaswater", "RsNPC", "PM1E")) {
Expand Down Expand Up @@ -224,6 +224,7 @@ private void loadNpcs() {
* 加载内置皮肤
*/
private void loadPrivateSkins() {
this.skins.put("private_steve", DEFAULT_SKIN);
String[] skins = { "阳", "糖菲_slim", "玉茗_slim" };
for (String skinName : skins) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AddRouteSubCommand(String name) {

@Override
public boolean canUser(CommandSender sender) {
return sender.isPlayer() && sender.isOp();
return sender.isPlayer() && sender.hasPermission("RsNPC.admin.addroute");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CreateSubCommand(String name) {

@Override
public boolean canUser(CommandSender sender) {
return sender.isPlayer() && sender.isOp();
return sender.isPlayer() && sender.hasPermission("RsNPC.admin.create");
}

@Override
Expand Down Expand Up @@ -65,11 +65,9 @@ public boolean execute(CommandSender sender, String label, String[] args) {
}
this.rsNPC.getNpcs().put(name, rsNpcConfig);
rsNpcConfig.checkEntity();
//玄学解决首次生成不显示的问题
Server.getInstance().getScheduler().scheduleDelayedTask(this.rsNPC, () -> {
rsNpcConfig.getEntityRsNpc().close();
rsNpcConfig.checkEntity();
}, 20);
//修复首次生成不显示的问题 通过重复生成实体解决nk未能及时发送PlayerListPacket的问题
Server.getInstance().getScheduler().scheduleDelayedTask(this.rsNPC, () -> rsNpcConfig.getEntityRsNpc().close(), 20);
Server.getInstance().getScheduler().scheduleDelayedTask(this.rsNPC, rsNpcConfig::checkEntity, 40);
sender.sendMessage(this.rsNPC.getLanguage().translateString("tips.npcCreateSuccess", name));
} else {
sender.sendMessage(this.rsNPC.getLanguage().translateString("tips.nameRequired"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public DeleteSubCommand(String name) {

@Override
public boolean canUser(CommandSender sender) {
return sender.isOp();
return sender.hasPermission("RsNPC.admin.delete");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ReloadSubCommand(String name) {

@Override
public boolean canUser(CommandSender sender) {
return sender.isOp();
return sender.hasPermission("RsNPC.admin.reload");
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/smallaswater/npc/data/RsNpcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.smallaswater.npc.utils.Utils;
import com.smallaswater.npc.utils.exception.RsNpcConfigLoadException;
import com.smallaswater.npc.utils.exception.RsNpcLoadException;
import com.smallaswater.npc.variable.VariableManage;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
Expand Down Expand Up @@ -165,7 +164,7 @@ public RsNpcConfig(@NonNull String name, @NonNull Config config) throws RsNpcCon
}

try {
this.skinName = config.getString("皮肤", "默认");
this.skinName = config.getString("皮肤", "private_steve");
if (!RsNPC.getInstance().getSkins().containsKey(this.skinName)) {
RsNPC.getInstance().getLogger().warning("NPC: " + this.name + " 皮肤: " + this.skinName + " 不存在!已切换为默认皮肤!");
}
Expand Down Expand Up @@ -394,7 +393,7 @@ public void checkEntity() {
if (!this.lookAtThePlayer) {
this.entityRsNpc.setRotation(this.location.yaw, this.location.pitch);
}
this.entityRsNpc.setNameTag(VariableManage.stringReplace(null, this.showName, this));
this.entityRsNpc.setNameTag(this.showName /*VariableManage.stringReplace(null, this.showName, this)*/);
}
}

Expand Down
64 changes: 55 additions & 9 deletions src/main/java/com/smallaswater/npc/entitys/EntityRsNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
import cn.nukkit.Server;
import cn.nukkit.block.BlockLiquid;
import cn.nukkit.entity.EntityHuman;
import cn.nukkit.entity.data.EntityMetadata;
import cn.nukkit.entity.data.Skin;
import cn.nukkit.level.Level;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.math.Vector3;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.network.protocol.EmotePacket;
import cn.nukkit.network.protocol.ProtocolInfo;
import cn.nukkit.network.protocol.RemoveEntityPacket;
import cn.nukkit.network.protocol.SetEntityLinkPacket;
import cn.nukkit.network.protocol.*;
import com.smallaswater.npc.RsNPC;
import com.smallaswater.npc.data.RsNpcConfig;
import com.smallaswater.npc.route.Node;
import com.smallaswater.npc.route.RouteFinder;
import com.smallaswater.npc.variable.VariableManage;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

import java.util.HashSet;
import java.util.LinkedList;

public class EntityRsNPC extends EntityHuman {

@Getter
private final RsNpcConfig config;
private int emoteSecond = 0;
private int nextRouteIndex = 0;
Expand Down Expand Up @@ -70,14 +72,9 @@ public EntityRsNPC(@NonNull FullChunk chunk, @NonNull CompoundTag nbt, RsNpcConf
this.dataProperties.putFloat(EntityUtils.getEntityField("DATA_BOUNDING_BOX_HEIGHT", DATA_BOUNDING_BOX_HEIGHT), this.getHeight());
this.dataProperties.putFloat(EntityUtils.getEntityField("DATA_BOUNDING_BOX_WIDTH", DATA_BOUNDING_BOX_WIDTH), this.getWidth());
this.dataProperties.putInt(EntityUtils.getEntityField("DATA_HEALTH", DATA_HEALTH), (int) this.getHealth());
this.sendData(this.getViewers().values().toArray(new Player[0]));
}
}

public RsNpcConfig getConfig() {
return this.config;
}

@Override
public float getWidth() {
if (this.config != null && this.config.isEnableCustomCollisionSize()) {
Expand Down Expand Up @@ -273,11 +270,13 @@ public void addMovement(double x, double y, double z, double yaw, double pitch,
public void spawnTo(Player player) {
if (this.getNetworkId() == -1) {
super.spawnTo(player);
this.sendData(player);
}

if (!this.hasSpawned.containsKey(player.getLoaderId()) && this.chunk != null && player.usedChunks.containsKey(Level.chunkHash(this.chunk.getX(), this.chunk.getZ()))) {
this.hasSpawned.put(player.getLoaderId(), player);
player.dataPacket(this.createAddEntityPacket());
this.sendData(player);
}
if (this.riding != null) {
this.riding.spawnTo(player);
Expand All @@ -303,4 +302,51 @@ public void despawnFrom(Player player) {
this.hasSpawned.remove(player.getLoaderId());
}
}

@Override
public void setSkin(Skin skin) {
Skin oldSkin = this.getSkin();
super.setSkin(skin);
this.sendSkin(oldSkin);
}

protected void sendSkin(Skin oldSkin) {
PlayerSkinPacket packet = new PlayerSkinPacket();
packet.skin = this.getSkin();
packet.newSkinName = this.getSkin().getSkinId();
packet.oldSkinName = oldSkin != null ? oldSkin.getSkinId() : "old";
packet.uuid = this.getUniqueId();
HashSet<Player> players = new HashSet<>(this.getViewers().values());
if (!players.isEmpty()) {
Server.broadcastPacket(players, packet);
}
}

@Override
public void sendData(Player player, EntityMetadata data) {
SetEntityDataPacket pk = new SetEntityDataPacket();
pk.eid = this.getId();
pk.metadata = data == null ? this.dataProperties : data;
pk.metadata.putString(
EntityUtils.getEntityField("DATA_NAMETAG", DATA_NAMETAG),
VariableManage.stringReplace(player, this.getNameTag(), this.getConfig())
);
player.dataPacket(pk);
}

@Override
public void sendData(Player[] players, EntityMetadata data) {
SetEntityDataPacket pk = new SetEntityDataPacket();
pk.eid = this.getId();
pk.metadata = data == null ? this.dataProperties : data;

for(Player player : players) {
SetEntityDataPacket clone = (SetEntityDataPacket) pk.clone();
clone.metadata.putString(
EntityUtils.getEntityField("DATA_NAMETAG", DATA_NAMETAG),
VariableManage.stringReplace(player, this.getNameTag(), this.getConfig())
);
player.dataPacket(clone);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
import cn.nukkit.entity.custom.CustomEntity;
import cn.nukkit.entity.custom.EntityDefinition;
import cn.nukkit.entity.data.IntEntityData;
import cn.nukkit.entity.data.Skin;
import cn.nukkit.level.Level;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.network.protocol.AddEntityPacket;
import cn.nukkit.network.protocol.DataPacket;
import cn.nukkit.network.protocol.SetEntityLinkPacket;
import cn.nukkit.network.protocol.types.EntityLink;
import com.smallaswater.npc.data.RsNpcConfig;
import com.smallaswater.npc.variable.VariableManage;
import lombok.NonNull;

import static cn.nukkit.network.protocol.SetEntityLinkPacket.TYPE_PASSENGER;

/**
* 基于自定义实体实现的RsNPC实体
* 基于自定义实体功能实现的RsNPC实体
*
* @author LT_Name
*/
Expand Down Expand Up @@ -110,7 +117,7 @@ public void addMovement(double x, double y, double z, double yaw, double pitch,
public void spawnTo(Player player) {
if (!this.hasSpawned.containsKey(player.getLoaderId()) && this.chunk != null && player.usedChunks.containsKey(Level.chunkHash(this.chunk.getX(), this.chunk.getZ()))) {
this.hasSpawned.put(player.getLoaderId(), player);
player.dataPacket(createAddEntityPacket());
player.dataPacket(createAddEntityPacket(player));
}

if (this.riding != null) {
Expand All @@ -126,4 +133,18 @@ public void spawnTo(Player player) {
}
}

public DataPacket createAddEntityPacket(Player player) {
AddEntityPacket pk = (AddEntityPacket) this.createAddEntityPacket();
pk.metadata.putString(
EntityUtils.getEntityField("DATA_NAMETAG", DATA_NAMETAG),
VariableManage.stringReplace(player, this.getNameTag(), this.getConfig())
);
return pk;
}

@Override
public void setSkin(Skin skin) {
this.skin = skin;
}

}
26 changes: 16 additions & 10 deletions src/main/java/com/smallaswater/npc/utils/FormHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ public static void sendMain(@NotNull Player player) {
simple.setContent(getRandomMessage() + "\n\n");
}

simple.addButton(new ResponseElementButton(language.translateString("gui.main.button.createNPCText")).onClicked(FormHelper::sendCreateNpc));
simple.addButton(new ResponseElementButton(language.translateString("gui.main.button.adminNPCText")).onClicked(FormHelper::sendAdminNpcSelect));
simple.addButton(new ResponseElementButton(language.translateString("gui.main.button.reloadText"))
.onClicked(cp -> Server.getInstance().dispatchCommand(cp, "rsnpc reload"))
);
if (player.hasPermission("RsNPC.admin.create")) {
simple.addButton(new ResponseElementButton(language.translateString("gui.main.button.createNPCText")).onClicked(FormHelper::sendCreateNpc));
if (player.hasPermission("RsNPC.admin.delete")) {
simple.addButton(new ResponseElementButton(language.translateString("gui.main.button.adminNPCText")).onClicked(FormHelper::sendAdminNpcSelect));
}
}
if (player.hasPermission("RsNPC.admin.reload")) {
simple.addButton(new ResponseElementButton(language.translateString("gui.main.button.reloadText"))
.onClicked(cp -> Server.getInstance().dispatchCommand(cp, "rsnpc reload"))
);
}

player.showFormWindow(simple);
}
Expand Down Expand Up @@ -134,7 +140,7 @@ public static void sendAdminNpc(@NotNull Player player, @NotNull RsNpcConfig rsN
"\n显示名称一直可见: " + toAdminNpcBooleanShowText(rsNpcConfig.isNameTagAlwaysVisible()) +
"\n坐标:\n x: " + NukkitMath.round(rsNpcConfig.getLocation().getX(), 2) +
"\n y: " + NukkitMath.round(rsNpcConfig.getLocation().getY(), 2) +
"\n z: " + NukkitMath.round(rsNpcConfig.getLocation().getZ(), 2) + "" +
"\n z: " + NukkitMath.round(rsNpcConfig.getLocation().getZ(), 2) +
"\n yaw: " + NukkitMath.round(rsNpcConfig.getLocation().getYaw(), 3) +
"\n 世界: " + rsNpcConfig.getLocation().getLevel().getName() +
"\n物品:\n 手持: " + hand.getId() + ":" + hand.getDamage() +
Expand Down Expand Up @@ -233,7 +239,7 @@ public static void sendAdminNpcConfig(@NotNull Player player, @NotNull RsNpcConf
custom.onResponded((formResponseCustom, cp) -> {
try {
String showName = formResponseCustom.getInputResponse(0);
if ("".equals(showName.trim())) {
if (showName.trim().isEmpty()) {
cp.sendMessage(language.translateString("gui.adminNPCConfig.responded.showNameNull"));
return;
}
Expand Down Expand Up @@ -324,7 +330,7 @@ public static void sendAdminNpcConfigEmote(@NotNull Player player, @NotNull RsNp
rsNpcConfig.getEmoteIDs().clear();
String[] emoteIDs = formResponseCustom.getInputResponse(2).split(";");
for (String id : emoteIDs) {
if (!"".equals(id.trim())) {
if (!id.trim().isEmpty()) {
rsNpcConfig.getEmoteIDs().add(id);
}
}
Expand Down Expand Up @@ -393,7 +399,7 @@ public static void sendAdminNpcConfigCommandAdd(@NotNull Player player, @NotNull

custom.onResponded((formResponseCustom, cp) -> {
String cmd = formResponseCustom.getInputResponse(1).replace("&", "");
if ("".equals(cmd.trim())) {
if (cmd.trim().isEmpty()) {
cp.sendMessage("命令不能为空!");
return;
}
Expand Down Expand Up @@ -483,7 +489,7 @@ public static void sendAdminNpcConfigMessageAdd(@NotNull Player player, @NotNull

custom.onResponded((formResponseCustom, cp) -> {
String message = formResponseCustom.getInputResponse(1);
if ("".equals(message.trim())) {
if (message.trim().isEmpty()) {
cp.sendMessage(language.translateString("gui.adminNPCConfigMessageAdd.responded.messageNull"));
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/Npc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ nameTagAlwaysVisible: true
胸部: ""
腿部: ""
脚部: ""
皮肤: "默认"
皮肤: "private_steve"
实体NetworkId: -1
实体大小: 1.0
看向玩家: true
Expand Down
20 changes: 17 additions & 3 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: RsNPC
main: com.smallaswater.npc.RsNPC
version: "2.4.2-PM1E"
api: ["Nukkit-PM1E"]
version: "2.4.3-PM1E"
api:
- "Nukkit-MOT"
- "Nukkit-PM1E"
authors: ["若水", "LT_Name"]
load: POSTWORLD
softdepend:
Expand All @@ -10,4 +12,16 @@ softdepend:
permissions:
RsNPC.admin:
default: op
description: "RsNPC插件主命令"
description: "RsNPC 插件主命令"
RsNPC.admin.create:
default: op
description: "RsNPC 创建NPC命令"
RsNPC.admin.delete:
default: op
description: "RsNPC 删除NPC命令"
RsNPC.admin.reload:
default: op
description: "RsNPC 重载配置文件命令"
RsNPC.admin.addroute:
default: op
description: "RsNPC 添加NPC移动路径命令"

0 comments on commit a40976f

Please sign in to comment.