Skip to content

Commit

Permalink
依赖改为nkmot,添加打开对话框播放声音功能
Browse files Browse the repository at this point in the history
  • Loading branch information
lt-name committed Dec 26, 2024
1 parent c53761a commit 9c24eb9
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 69 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lib.GameCore.version>1.6.11</lib.GameCore.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<lib.GameCore.version>1.6.11-PM1E</lib.GameCore.version>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -87,8 +87,8 @@
<dependencies>
<dependency>
<groupId>cn.nukkit</groupId>
<artifactId>nukkit</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>Nukkit</artifactId>
<version>MOT-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/smallaswater/npc/OnListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public OnListener(RsNPC rsNPC) {

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityVehicleEnter(EntityVehicleEnterEvent event) {
if (event.getEntity() instanceof EntityRsNPC ||
event.getVehicle() instanceof EntityRsNPC) {
if (event.getEntity() instanceof EntityRsNPC) {
event.setCancelled(true);
}
}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/smallaswater/npc/RsNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void onEnable() {

try {
if (Server.getInstance().getPluginManager().getPlugin("AutoUpData") != null) {
if (AutoData.defaultUpDataByMaven(this, this.getFile(), "com.smallaswater", "RsNPC", null)) {
if (AutoData.defaultUpDataByMaven(this, this.getFile(), "com.smallaswater", "RsNPC", "")) {
return;
}
}
Expand All @@ -130,9 +130,9 @@ public void onEnable() {

//检查插件分支是否和核心匹配
NukkitTypeUtils.NukkitType nukkitType = NukkitTypeUtils.getNukkitType();
if (nukkitType != NukkitTypeUtils.NukkitType.NUKKITX && nukkitType != NukkitTypeUtils.NukkitType.POWER_NUKKIT) {
if (nukkitType != NukkitTypeUtils.NukkitType.PM1E && nukkitType != NukkitTypeUtils.NukkitType.MOT) {
this.getLogger().error(this.getLanguage().translateString("plugin.load.pluginBranchError", nukkitType.getShowName(), this.getVersion()));
this.getServer().getPluginManager().disablePlugin(this);
//this.getServer().getPluginManager().disablePlugin(this);
return;
}

Expand Down Expand Up @@ -347,7 +347,18 @@ private void loadSkins() {

skin.setTrusted(true);

if (skin.isValid()) {
boolean skinIsValid = false;
try {
skinIsValid = (boolean) Skin.class.getMethod("isValid", boolean.class).invoke(skin, this.getServer().doNotLimitSkinGeometry);
} catch (Exception exception) {
try {
skinIsValid = (boolean) Skin.class.getMethod("isValid").invoke(skin);
} catch (Exception e) {
this.getLogger().error("Skin validation failed!", e);
}
}

if (skinIsValid) {
this.skins.put(skinName, skin);
this.getLogger().info(this.getLanguage().translateString("plugin.load.skin.loadSucceed", skinName));
} else {
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/com/smallaswater/npc/data/RsNpcConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.smallaswater.npc.data;

import cn.lanink.gamecore.utils.ConfigUtils;
import cn.lanink.gamecore.utils.CustomEntityUtils;
import cn.nukkit.Server;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.custom.EntityDefinition;
import cn.nukkit.entity.custom.EntityManager;
import cn.nukkit.entity.data.Skin;
import cn.nukkit.item.Item;
import cn.nukkit.level.Level;
Expand Down Expand Up @@ -97,7 +98,7 @@ public class RsNpcConfig {

// 自定义实体
private boolean enableCustomEntity;
private String customEntityIdentifier;
private EntityDefinition customEntityDefinition;
private int customEntitySkinId;

//自定义碰撞大小
Expand Down Expand Up @@ -269,10 +270,17 @@ public RsNpcConfig(@NonNull String name, @NonNull Config config) throws RsNpcCon

try {
this.enableCustomEntity = this.config.getBoolean("CustomEntity.enable", false);
this.customEntityIdentifier = this.config.getString("CustomEntity.identifier", "RsNPC:Demo");
String identifier = this.config.getString("CustomEntity.identifier", "RsNPC:Demo");
this.customEntitySkinId = this.config.getInt("CustomEntity.skinId", 0);
if (this.enableCustomEntity && CustomEntityUtils.getRuntimeId(this.customEntityIdentifier) == -1) {
CustomEntityUtils.registerCustomEntity(this.customEntityIdentifier);
if (this.enableCustomEntity) {
this.customEntityDefinition = EntityManager.get().getDefinition(identifier);
if (this.customEntityDefinition == null) { //不存在时注册新的
this.customEntityDefinition = EntityDefinition.builder()
.identifier(identifier)
.spawnEgg(false)
.implementation(EntityRsNPCCustomEntity.class).build();
EntityManager.get().registerDefinition(this.customEntityDefinition);
}
}
}catch (Exception e) {
throw new RsNpcConfigLoadException("NPC配置 自定义实体配置加载失败!请检查配置文件!", e);
Expand Down Expand Up @@ -344,7 +352,7 @@ public void save() {
this.config.set("对话框.页面", this.dialogPagesName);

this.config.set("CustomEntity.enable", this.enableCustomEntity);
this.config.set("CustomEntity.identifier", this.customEntityIdentifier);
this.config.set("CustomEntity.identifier", this.customEntityDefinition == null ? "RsNPC:Demo" : this.customEntityDefinition.getIdentifier());
this.config.set("CustomEntity.skinId", this.customEntitySkinId);

this.config.set("CustomCollisionSize.enable", this.enableCustomCollisionSize);
Expand All @@ -370,11 +378,11 @@ public void checkEntity() {
.putCompound("Skin", (new CompoundTag())
.putByteArray("Data", this.skin.getSkinData().data)
.putString("ModelId", this.skin.getSkinId()));
if (this.enableCustomEntity && this.customEntityIdentifier != null) {
if (this.enableCustomEntity && this.customEntityDefinition != null) {
nbt.putInt("skinId", this.customEntitySkinId);
this.entityRsNpc = new EntityRsNPCCustomEntity(this.location.getChunk(), nbt, this);
EntityRsNPCCustomEntity entityRsNPC = (EntityRsNPCCustomEntity) this.entityRsNpc;
entityRsNPC.setIdentifier(this.customEntityIdentifier);
entityRsNPC.setDefinition(this.customEntityDefinition);
}else {
this.entityRsNpc = new EntityRsNPC(this.location.getChunk(), nbt, this);
this.entityRsNpc.setSkin(this.getSkin());
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/com/smallaswater/npc/dialog/DialogPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.lanink.gamecore.utils.packet.ProtocolVersion;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.network.protocol.PlaySoundPacket;
import cn.nukkit.network.protocol.ProtocolInfo;
import cn.nukkit.utils.Config;
import com.smallaswater.npc.RsNPC;
Expand Down Expand Up @@ -61,6 +62,7 @@ public static class DialogPage {
private final String key;
private final String title;
private final String content;
private final Sound sound;
private final ArrayList<Button> buttons = new ArrayList<>();

private String closeGo;
Expand All @@ -70,6 +72,7 @@ public DialogPage (@NotNull DialogPages dialogPages, @NotNull Map<String, Object
this.key = (String) map.get("key");
this.title = (String) map.get("title");
this.content = (String) map.get("content");
this.sound = new Sound((Map<String, Object>) map.getOrDefault("sound", new HashMap<>()));
((List<Map<String, Object>>) map.get("buttons")).forEach(button -> this.buttons.add(new Button(button)));
if (map.containsKey("close")) {
Map<String, Object> closeMap = (Map<String, Object>) map.get("close");
Expand Down Expand Up @@ -102,6 +105,17 @@ public void send(@NotNull EntityRsNPC entityRsNpc, @NotNull Player player) {
}
}, 5);

if (this.sound.isEnable() && !"".equals(this.sound.getIdentifier())) {
PlaySoundPacket packet = new PlaySoundPacket();
packet.name = this.sound.getIdentifier();
packet.volume = 1;
packet.pitch = 1;
packet.x = player.getFloorX();
packet.y = player.getFloorY();
packet.z = player.getFloorZ();
player.dataPacket(packet);
}

AdvancedFormWindowDialog windowDialog = new AdvancedFormWindowDialog(
VariableManage.stringReplace(player, this.title, entityRsNpc.getConfig()),
VariableManage.stringReplace(player, this.content, entityRsNpc.getConfig()),
Expand Down Expand Up @@ -137,12 +151,23 @@ public void send(@NotNull EntityRsNPC entityRsNpc, @NotNull Player player) {
windowDialog.send(player);
}

@Getter
public static class Sound {

private final boolean enable;
private final String identifier;

public Sound(@NotNull Map<String, Object> map) {
this.enable = (boolean) map.getOrDefault("enable", false);
this.identifier = (String) map.getOrDefault("identifier", "");
}
}

@Getter
public static class Button {

@Getter
private final String text;

@Getter
private final List<ButtonAction> buttonActions = new ArrayList<>();

public Button(@NotNull Map<String, Object> map) {
Expand Down Expand Up @@ -170,18 +195,14 @@ public Button(@NotNull Map<String, Object> map) {
}
}

@Setter
@Getter
public static class ButtonAction {

@Getter
@Setter
private ButtonActionType type;

@Getter
@Setter
private String data;

@Getter
@Setter
private List<String> listData = new ArrayList<>();

public ButtonAction(@NotNull ButtonActionType type) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/smallaswater/npc/entitys/EntityRsNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,10 @@ public void sendData(Player[] players, EntityMetadata data) {
player.dataPacket(clone);
}
}

//为了兼容PM1E,我们不使用setCanBeSavedWithChunk()方法
@Override
public boolean canBeSavedWithChunk() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.smallaswater.npc.entitys;

import cn.lanink.gamecore.utils.CustomEntityUtils;
import cn.lanink.gamecore.utils.EntityUtils;
import cn.nukkit.Player;
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;
Expand All @@ -11,21 +12,24 @@
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实体
*
* @author LT_Name
*/
public class EntityRsNPCCustomEntity extends EntityRsNPC {
public class EntityRsNPCCustomEntity extends EntityRsNPC implements CustomEntity {

private static final EntityDefinition DEFAULT_DEFINITION = EntityDefinition.builder()
.identifier("RsNPC")
.spawnEgg(false)
.implementation(EntityRsNPCCustomEntity.class)
.build();

private String identifier;
private EntityDefinition definition;

@Deprecated
public EntityRsNPCCustomEntity(FullChunk chunk, CompoundTag nbt) {
Expand All @@ -36,17 +40,45 @@ public EntityRsNPCCustomEntity(@NonNull FullChunk chunk, @NonNull CompoundTag nb
super(chunk, nbt, config);
}

public void setDefinition(EntityDefinition definition) {
this.definition = definition;
}

@Override
public int getNetworkId() {
return CustomEntityUtils.getRuntimeId(this.identifier);
return this.getEntityDefinition().getRuntimeId();
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
this.definition = EntityDefinition.builder()
.identifier(identifier)
.spawnEgg(false)
.implementation(EntityRsNPCCustomEntity.class)
.build();
}

public String getIdentifier() {
return this.identifier;
/**
* 获取实体定义
* (PNX和PM1E分支独有方法)
*
* @return 实体定义
*/
public EntityDefinition getDefinition() {
return this.getEntityDefinition();
}

/**
* 获取实体定义
* (PM1E分支独有方法)
*
* @return 实体定义
*/
@Override
public EntityDefinition getEntityDefinition() {
if (this.definition == null) {
return DEFAULT_DEFINITION;
}
return this.definition;
}

public void setSkinId(int skinId) {
Expand Down Expand Up @@ -97,32 +129,6 @@ public void spawnTo(Player player) {
}
}

@Override
public DataPacket createAddEntityPacket() {
AddEntityPacket addEntity = new AddEntityPacket();
addEntity.type = this.getNetworkId();
addEntity.entityUniqueId = this.getId();
addEntity.id = this.getIdentifier();
addEntity.entityRuntimeId = this.getId();
addEntity.yaw = (float) this.yaw;
addEntity.headYaw = (float) this.yaw;
addEntity.pitch = (float) this.pitch;
addEntity.x = (float) this.x;
addEntity.y = (float) this.y + this.getBaseOffset();
addEntity.z = (float) this.z;
addEntity.speedX = (float) this.motionX;
addEntity.speedY = (float) this.motionY;
addEntity.speedZ = (float) this.motionZ;
addEntity.metadata = this.dataProperties;

addEntity.links = new EntityLink[this.passengers.size()];
for (int i = 0; i < addEntity.links.length; i++) {
addEntity.links[i] = new EntityLink(this.getId(), this.passengers.get(i).getId(), i == 0 ? EntityLink.TYPE_RIDER : TYPE_PASSENGER, false, false);
}

return addEntity;
}

public DataPacket createAddEntityPacket(Player player) {
AddEntityPacket pk = (AddEntityPacket) this.createAddEntityPacket();
pk.metadata.putString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class GameCoreDownload {
// 每个任务下载 128 kb数据
private static final int THRESHOLD = 128 * 1024;

public static final String MINIMUM_GAME_CORE_VERSION = "1.6.11";
public static final String MINIMUM_GAME_CORE_VERSION = "1.6.11-PM1E";
private static String ACTUAL_MINIMUM_GAME_CORE_VERSION;

private static final String MAVEN_URL_CENTRAL = "https://repo1.maven.org/maven2/";
Expand Down
Loading

0 comments on commit 9c24eb9

Please sign in to comment.