自我隔离这段时间学了一点Java 想实战一下,碰到这些个问题。新人代码写的烂,大佬轻喷。
这是写的一个方法,想实现创建一个背包,读取yml文件添加物品的效果。
出现了第二个循环那里只有第二次发送物品才能实现ItemStack中的setname方法,以及lore
getConfig那个循环倒是照设想的运行。
- public static void openGui(Player p) {
-
- File dates2 = new File(ArtMail.plugin.getDataFolder() + "\\Mailbook.yml"); //读取邮件列表文件
- YamlConfiguration ed2 = YamlConfiguration.loadConfiguration(dates2);
- File dates = new File(ArtMail.plugin.getDataFolder() + "\\playerdata\" + p.getName() + ".yml"); //读取玩家数据
- YamlConfiguration ed = YamlConfiguration.loadConfiguration(dates);
-
- String title = ArtMail.plugin.getConfig().getString("info.title").replace("%p%", p.getName());
-
-
- Inventory inv = Bukkit.createInventory(null, 54,title.replace("&","§")); //创建GUI界面 (邮箱)
-
- for(int i =0;i<54;i++) {
- ItemStack item = new ItemStack(Material.WHITE_STAINED_GLASS_PANE);
- ItemMeta im = item.getItemMeta();
- im.setDisplayName(ArtMail.plugin.getConfig().getString("item.fenge.name"));
- ArrayList<String> lore = new ArrayList<>();
- for(String line : ArtMail.plugin.getConfig().getStringList("item.fenge.lore")) {
- lore.add(line);
- }
- im.setLore(lore);
- item.setItemMeta(im);
- inv.setItem(i, item);
- }
-
-
- if(ed.getInt("mailamount")>0) {
- for(int i=0;i<ed.getInt("mailamount");i++) {
- String name = ed.getString("Mailbook"+i+".name");
- String Dname = ed2.getString("Maillist."+name+".name");
- ArrayList<String> lore = new ArrayList<>();
- for(String line : ed.getStringList("Mailbook"+name+".lore")) {
- lore.add(line);
- }
- ItemStack item = new ItemStack(Material.BOOK);
- ItemMeta im = item.getItemMeta();
-
- // String Dname = ed2.getString("Maillist."+name+".name");
- im.setDisplayName(Dname);
- im.setLore(lore);
- im.setUnbreakable(true);
- item.setItemMeta(im);
- inv.setItem(i, item);
- }
- }
-
- p.closeInventory();
- p.openInventory(inv);
- }
复制代码 |
这是重写的onCommand方法部分的代码
(实在抱歉,代码太辣眼睛了呜呜呜呜)
- public class Command implements CommandExecutor{
- @Override
- public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String arg, String[] args) {
- Player p = (Player) sender;
- if(args.length==0) {
- if(sender.isOp()) {
- for(String help : ArtMail.plugin.getConfig().getStringList("message.adminhelp")) {
- help = help.replace("&","§");
- sender.sendMessage(help);
- }
- return true;
- }else {
- for(String help : ArtMail.plugin.getConfig().getStringList("message.playerhelp")) {
- help = help.replace("&","§");
- sender.sendMessage(help);
- }
- return true;
- }
-
- }
- if(args[0].equalsIgnoreCase("open")) {
- Gui.openGui(p);
- return true;
- }
- if(args[0].equalsIgnoreCase("sendmail")) {
- if(args.length<3) {
- p.sendMessage(ArtMail.plugin.getConfig().getString("message.noargs"));
- return true;
- }
- Player p2 = Bukkit.getPlayer(args[1]);
- String pn = p2.getName();
- File file2 = new File(ArtMail.plugin.getDataFolder()+ "\\playerdata\"+pn+".yml");
- YamlConfiguration ed = YamlConfiguration.loadConfiguration(file2);
- int i = ed.getInt("mailamount");
- i = i+1;
- ed.set("mailamount", i);
- ed.set("Mailbook"+i+".name", args[2]);
- try {
- ed.save(file2);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return true;
- }
-
-
-
- return true;
- }
-
- }
复制代码 |
附上图
https://sm.ms/image/27yCMI4xdGO3tEB
0: 你以为你用的数据库吗, 不要每次都从磁盘读, 解析yaml数据大了CPU和内存占用都很大的
1:
int i = ed.getInt("mailamount"); // i = 0
i = i+1; // i = 1
ed.set("mailamount", i); // i = 1
ed.set("Mailbook"+i+".name", args[2]); // i = 1
String name = ed.getString("Mailbook"+i+".name"); // i = 0
String Dname = ed2.getString("Maillist."+name+".name"); // name = null, Dname= null
ArrayList<String> lore = new ArrayList<>();
for(String line : ed.getStringList("Mailbook"+name+".lore")) { // ed.getStringList() = new ArrayList<>();
lore.add(line);
}