bukket插件例子(4)代码分析

来源:互联网 发布:约瑟夫环 java 编辑:程序博客网 时间:2024/06/06 02:44


试着分析一下/get命令,命令类似/give,/get x y代表给用户y个x物件

原代码如下

初始化函数

super(plugin, "get", 1, 2);
其中最少包含1个参数,最多2个


doPlayerCommand来自BaseCommand的实现

逻辑如下,如果参数长度是2,将倍数times存起来

如果x输入的是名字,那么就把有这个名字的全都加给用户

if (c.toString().toLowerCase().contains(cur.toLowerCase()))
例如/get diamond 20就会把所有是砖石的都给用户,包括diamond_boots, diamond_pickaxe

如果x输入的是数字,那么就把符合这个id要求的给用户

例如/get 262 2就会把所有id是262的物件给用户。


代码如下,


public class CmdGetblock extends BaseCommand {Map<String, ConfigurationSection> allKits;public CmdGetblock(MainPlugin plugin) {super(plugin, "get", 1, 2);}@Override@SuppressWarnings("deprecation")protected boolean doPlayerCommand(Player player, Command cmd, String commandLabel, String[] split) {              Integer times = Integer.valueOf(1);      if (split.length == 2)      {        try        {          times = Integer.valueOf(Integer.parseInt(split[1]));        }        catch (Exception localException) {}        if (times.intValue() < 0) {          times = Integer.valueOf(1);        }      }      if ((split.length == 1) || (split.length == 2))      {        String cur = split[0];        boolean found = false;        Material[] arrayOfMaterial;        int j = (arrayOfMaterial = Material.values()).length;        for (int ai = 0; ai < j; ai++)        {          Material c = arrayOfMaterial[ai];          if (c.toString().toLowerCase().contains(cur.toLowerCase()))          {            found = true;            for (int ki = 0; ki < times.intValue(); ki++) {              player.getInventory().addItem(new ItemStack[] { new ItemStack(c.getId(), 1) });            }            player.sendMessage("Material added maybe: " + c.toString() + " with id " + String.valueOf(c.getId()));          }        }        if (!found) {          try          {            Integer x = Integer.valueOf(Integer.parseInt(split[0]));            Material[] all = Material.values();            int k = all.length;            for (j = 0; j < k; j++)            {              Material c = all[j];              if (c.getId() == x.intValue())              {                found = true;                for (int i = 0; i < times.intValue(); i++) {                  player.getInventory().addItem(new ItemStack[] { new ItemStack(c.getId(), 1) });                }                player.sendMessage("Material added maybe: " + c.toString() + " with id " + String.valueOf(c.getId()));              }            }          }          catch (Exception localException1) {}        }        return true;      }      return false;}}


0 0
原创粉丝点击