Skip to content

Commit

Permalink
1.6.0 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
lt-name authored May 18, 2021
1 parent bbf61aa commit d5e5347
Show file tree
Hide file tree
Showing 34 changed files with 487 additions and 78 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.smallaswater.npc</groupId>
<artifactId>RsNPCX</artifactId>
<version>1.5.1</version>
<version>1.6.0<!-- -SNAPSHOT--></version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/smallaswater/npc/OnListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/**
* @author lt_name
*/
@SuppressWarnings("unused")
public class OnListener implements Listener {

private final RsNpcX rsNpcX;
Expand All @@ -39,7 +40,7 @@ public void onDamage(EntityDamageEvent event) {
}
this.executeCommand(player, config);
for (String message : config.getMessages()) {
player.sendMessage("[" + config.getName() + "] " + VariableManage.stringReplace(player, message));
player.sendMessage(VariableManage.stringReplace(player, message, config));
}
}
}
Expand All @@ -54,7 +55,7 @@ private void executeCommand(Player player, RsNpcConfig rsNpcConfig) {
if ("con".equals(c[1])) {
try {
Server.getInstance().dispatchCommand(Server.getInstance().getConsoleSender(),
VariableManage.stringReplace(player, cm));
VariableManage.stringReplace(player, cm, rsNpcConfig));
} catch (Exception e) {
this.rsNpcX.getLogger().error(
"控制台权限执行命令时出现错误!NPC:" + rsNpcConfig.getName() +
Expand All @@ -73,7 +74,7 @@ private void executeCommand(Player player, RsNpcConfig rsNpcConfig) {
}
try {
Server.getInstance().dispatchCommand(Server.getInstance().getConsoleSender(),
VariableManage.stringReplace(player, cm));
VariableManage.stringReplace(player, cm, rsNpcConfig));
} catch (Exception e) {
this.rsNpcX.getLogger().error(
"OP权限执行命令时出现错误!NPC:" + rsNpcConfig.getName() +
Expand All @@ -93,7 +94,7 @@ private void executeCommand(Player player, RsNpcConfig rsNpcConfig) {
}
}
try {
Server.getInstance().dispatchCommand(player, VariableManage.stringReplace(player, cm));
Server.getInstance().dispatchCommand(player, VariableManage.stringReplace(player, cm, rsNpcConfig));
} catch (Exception e) {
this.rsNpcX.getLogger().error(
"玩家权限执行命令时出现错误!NPC:" + rsNpcConfig.getName() +
Expand Down
84 changes: 52 additions & 32 deletions src/main/java/com/smallaswater/npc/RsNpcX.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
import com.smallaswater.npc.entitys.EntityRsNpc;
import com.smallaswater.npc.tasks.CheckNpcEntityTask;
import com.smallaswater.npc.utils.RsNpcLoadException;
import com.smallaswater.npc.utils.Util;
import com.smallaswater.npc.utils.Utils;
import com.smallaswater.npc.variable.DefaultVariable;
import com.smallaswater.npc.variable.VariableManage;
import lombok.Getter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -43,7 +41,9 @@ public class RsNpcX extends PluginBase {

private static RsNpcX rsNpcX;

@Getter
private final HashMap<String, Skin> skins = new HashMap<>();
@Getter
private final HashMap<String, RsNpcConfig> npcs = new HashMap<>();
private final String[] defaultSkins = new String[]{"小丸子", "小埋", "小黑苦力怕", "尸鬼", "拉姆", "熊孩子", "狂三", "米奇", "考拉", "黑岩射手"};

Expand All @@ -55,19 +55,29 @@ public static RsNpcX getInstance() {
public void onLoad() {
rsNpcX = this;
VariableManage.addVariable("default", DefaultVariable.class);

File file = new File(getDataFolder() + "/Npcs");
if (!file.exists() && !file.mkdirs()) {
this.getLogger().error("Npcs文件夹创建失败");
}
}

@Override
public void onEnable() {
this.getLogger().info("RsNpcX开始加载");
Entity.registerEntity("EntityRsNpc", EntityRsNpc.class);

this.getLogger().info("开始加载皮肤");
this.saveDefaultSkin();
this.loadSkins();

this.getLogger().info("开始加载NPC");
this.loadNpcs();

this.getServer().getPluginManager().registerEvents(new OnListener(this), this);

this.getServer().getScheduler().scheduleRepeatingTask(this, new CheckNpcEntityTask(this), 60);

this.getLogger().info("RsNpcX加载完成");
}

Expand Down Expand Up @@ -104,28 +114,29 @@ private void loadNpcs() {
}

private void saveDefaultSkin() {
if (!(new File(getDataFolder() + "/Skins")).exists()) {
getLogger().info("未检测到Skins文件夹,正在创建");
if (!(new File(getDataFolder() + "/Skins")).mkdirs()) {
getLogger().info("Skins文件夹创建失败");
File file = new File(this.getDataFolder() + "/Skins");
if (!file.exists()) {
this.getLogger().info("未检测到Skins文件夹,正在创建");
if (!file.mkdirs()) {
this.getLogger().info("Skins文件夹创建失败");
} else {
getLogger().info("Skins 文件夹创建完成,正在保存预设皮肤");
this.getLogger().info("Skins 文件夹创建完成,正在保存预设皮肤");
for (String s : this.defaultSkins) {
if (!(new File(getDataFolder() + "/Skins/" + s)).exists() &&
!(new File(getDataFolder() + "/Skins/" + s)).mkdirs()) {
getLogger().info("载入 " + s + "失败");
File f = new File(this.getDataFolder() + "/Skins/" + s);
if (!f.exists() && !f.mkdirs()) {
this.getLogger().info("载入 " + s + "失败");
} else {
saveResource("skin/" + s + "/skin.json", "/Skins/" + s + "/skin.json", false);
saveResource("skin/" + s + "/skin.png", "/Skins/" + s + "/skin.png", false);
getLogger().info("成功保存 " + s + " 皮肤");
this.saveResource("Skins/" + s + "/skin.json");
this.saveResource("Skins/" + s + "/skin.png");
this.getLogger().info("成功保存 " + s + " 皮肤");
}
}
}
}
}

private void loadSkins() {
File[] files = (new File(getDataFolder() + "/Skins")).listFiles();
File[] files = (new File(this.getDataFolder() + "/Skins")).listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
String skinName = file.getName();
Expand Down Expand Up @@ -155,7 +166,7 @@ private void loadSkins() {
geometryName = entry1.getKey();
}
skin.setGeometryName(geometryName);
skin.setGeometryData(Util.readFile(skinJsonFile));
skin.setGeometryData(Utils.readFile(skinJsonFile));
}
this.skins.put(skinName, skin);
this.getLogger().info("皮肤 " + skinName + " 读取完成");
Expand Down Expand Up @@ -190,7 +201,7 @@ public boolean onCommand(final CommandSender sender, Command command, String lab
map.put("x", player.getX());
map.put("y", player.getY());
map.put("z", player.getZ());
map.put("yaw", Util.getYaw(player));
map.put("yaw", Utils.getYaw(player));
map.put("level", player.getLevel().getName());
config.set("坐标", map);
config.save();
Expand Down Expand Up @@ -226,10 +237,26 @@ public boolean onCommand(final CommandSender sender, Command command, String lab
sender.sendMessage("§c§l请输入要删除的NPC的名字!");
}
return true;
case "reload":
if (!(new File(getDataFolder() + "/Npcs")).exists() && !(new File(getDataFolder() + "/Npcs")).mkdirs()) {
this.getLogger().error("Npcs文件夹创建失败");
case "addroute":
Player player = (Player) sender;
if (args.length > 1) {
String name = args[1];
if (!this.npcs.containsKey(name)) {
sender.sendMessage("§c§lNPC " + name + " 不存在!");
return true;
}
RsNpcConfig rsNpcConfig = this.npcs.get(name);
rsNpcConfig.getRoute().add(player.clone());
List<String> list = rsNpcConfig.getConfig().getStringList("route");
list.add(player.getX() + ":" + player.getY() + ":" + player.getZ());
rsNpcConfig.getConfig().set("route", list);
rsNpcConfig.getConfig().save();
sender.sendMessage("§a§l已添加到路径");
}else {
sender.sendMessage("§c§l请输入要设置的NPC的名字!");
}
return true;
case "reload":
for (Level level : Server.getInstance().getLevels().values()) {
for (Entity entity : level.getEntities()) {
if (entity instanceof EntityRsNpc) {
Expand Down Expand Up @@ -257,22 +284,15 @@ public boolean onCommand(final CommandSender sender, Command command, String lab

public void sendCommandHelp(CommandSender sender) {
sender.sendMessage("§a§l >> §eHelp for RsNPCX §a<<");
sender.sendMessage("§a§l/rsnpcx create <名称> §7创建NPC");
sender.sendMessage("§a§l/rsnpcx delete <名称> §7移除NPC");
sender.sendMessage("§a§l/rsnpcx create <NPC名称> §7在当前位置创建NPC");
sender.sendMessage("§a§l/rsnpcx delete <NPC名称> §7移除NPC");
sender.sendMessage("§a§l/rsnpcx addroute <NPC名称> §7将当前位置添加到NPC路径");
sender.sendMessage("§a§l/rsnpcx reload §7重载NPC");
sender.sendMessage("§a§l >> §eHelp for RsNPC §a<<");
}

public HashMap<String, Skin> getSkins() {
return this.skins;
}

public Skin getSkinByName(String name) {
return this.getSkins().getOrDefault(name, RsNpcX.getInstance().getSkins().get("尸鬼"));
}

public HashMap<String, RsNpcConfig> getNpcs() {
return this.npcs;
}

}
79 changes: 59 additions & 20 deletions src/main/java/com/smallaswater/npc/data/RsNpcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.nukkit.item.Item;
import cn.nukkit.level.Level;
import cn.nukkit.level.Location;
import cn.nukkit.math.Vector3;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.utils.Config;
import com.smallaswater.npc.RsNpcX;
Expand Down Expand Up @@ -46,6 +47,9 @@ public class RsNpcConfig {

private final ArrayList<String> cmds = new ArrayList<>();
private final ArrayList<String> messages = new ArrayList<>();

@Getter
private final ArrayList<Vector3> route = new ArrayList<>();

private EntityRsNpc entityRsNpc;

Expand All @@ -64,39 +68,72 @@ public RsNpcConfig(@NonNull String name, @NonNull Config config) throws RsNpcLoa
(double) map.getOrDefault("yaw", 0D), 0, level);

this.hand = Item.fromString("".equals(config.getString("手持", "")) ? "0:0" : config.getString("手持", ""));
config.set("手持", this.hand.getId() + ":" + this.hand.getDamage());
this.armor[0] = Item.fromString("".equals(config.getString("头部")) ? "0:0" : config.getString("头部"));
config.set("头部", this.armor[0].getId() + ":" + this.armor[0].getDamage());
this.armor[1] = Item.fromString("".equals(config.getString("胸部")) ? "0:0" : config.getString("胸部"));
config.set("胸部", this.armor[1].getId() + ":" + this.armor[1].getDamage());
this.armor[2] = Item.fromString("".equals(config.getString("腿部")) ? "0:0" : config.getString("腿部"));
config.set("腿部", this.armor[2].getId() + ":" + this.armor[2].getDamage());
this.armor[3] = Item.fromString("".equals(config.getString("脚部")) ? "0:0" : config.getString("脚部"));
config.set("脚部", this.armor[3].getId() + ":" + this.armor[3].getDamage());

String skinName = config.getString("皮肤", "尸鬼");
config.set("皮肤", skinName);
this.skin = RsNpcX.getInstance().getSkinByName(skinName);

this.lookAtThePlayer = config.getBoolean("看向玩家", true);
config.set("看向玩家", this.lookAtThePlayer);


this.enableEmote = config.getBoolean("表情动作.启用");
config.set("表情动作.启用", this.enableEmote);
this.emoteIDs.addAll(config.getStringList("表情动作.表情ID"));
config.set("表情动作.表情ID", this.emoteIDs);
this.showEmoteInterval = config.getInt("表情动作.间隔(秒)", 10);
config.set("表情动作.间隔(秒)", this.showEmoteInterval);


this.canProjectilesTrigger = config.getBoolean("允许抛射物触发", true);
config.set("允许抛射物触发", this.canProjectilesTrigger);

this.cmds.addAll(config.getStringList("点击执行指令"));
config.set("点击执行指令", this.cmds);

this.messages.addAll(config.getStringList("发送消息"));
config.set("发送消息", this.messages);

config.save();

for (String string : config.getStringList("route")) {
String[] s = string.split(":");
this.route.add(new Vector3(Double.parseDouble(s[0]),
Double.parseDouble(s[1]),
Double.parseDouble(s[2])));
}

//更新配置文件
this.save();
}

public void save() {
this.config.set("name", this.showName);

HashMap<String, Object> map = this.config.get("坐标", new HashMap<>());
map.put("level", this.levelName);
map.put("x", this.location.getX());
map.put("y", this.location.getY());
map.put("z", this.location.getZ());
map.put("yaw", this.location.getYaw());
this.config.set("坐标", map);

this.config.set("手持", this.hand.getId() + ":" + this.hand.getDamage());
this.config.set("头部", this.armor[0].getId() + ":" + this.armor[0].getDamage());
this.config.set("胸部", this.armor[1].getId() + ":" + this.armor[1].getDamage());
this.config.set("腿部", this.armor[2].getId() + ":" + this.armor[2].getDamage());
this.config.set("脚部", this.armor[3].getId() + ":" + this.armor[3].getDamage());

this.config.set("看向玩家", this.lookAtThePlayer);

this.config.set("表情动作.启用", this.enableEmote);
this.config.set("表情动作.表情ID", this.emoteIDs);
this.config.set("表情动作.间隔(秒)", this.showEmoteInterval);

this.config.set("允许抛射物触发", this.canProjectilesTrigger);

this.config.set("点击执行指令", this.cmds);
this.config.set("发送消息", this.messages);

ArrayList<String> list = new ArrayList<>();
for (Vector3 vector3 : this.route) {
list.add(vector3.getX() + ":" + vector3.getY() + ":" + vector3.getZ());
}
this.config.set("route", list);

this.config.save();
}

public void checkEntity() {
Expand All @@ -114,11 +151,13 @@ public void checkEntity() {
this.entityRsNpc.setScale(1F);
this.entityRsNpc.spawnToAll();
}
this.entityRsNpc.setPosition(this.location);
if (this.getRoute().isEmpty()) {
this.entityRsNpc.setPosition(this.location);
}
if (!this.lookAtThePlayer) {
this.entityRsNpc.setRotation(this.location.yaw, this.location.pitch);
}
this.entityRsNpc.setNameTag(VariableManage.stringReplace(null, this.showName));
this.entityRsNpc.setNameTag(VariableManage.stringReplace(null, this.showName, this));
}
}

Expand Down
Loading

0 comments on commit d5e5347

Please sign in to comment.