您尚未登录,请登录后浏览更多内容! 登录 | 加入最MC

QQ登录

只需一步,快速开始

 找回密码
 加入最MC

QQ登录

只需一步,快速开始

查看: 6824|回复: 4

[其他] [码农世界专题2]Bukkit非新手向插件编写教程1——HelloWorld!

[复制链接]
  • TA的每日心情
    开心
    2016-7-1 20:14
  • 签到天数: 282 天

    [LV.8]以坛为家I

    发表于 2015-5-31 10:26:00 | 显示全部楼层 |0人打赏|阅读模式
    本帖最后由 笔记本 于 2015-5-31 17:38 编辑

    Hello大家好!
    我是笔记本,一个资深(zhongdeng)插件码农,今天给大家带来的是非新手向
    插件编写教程,这次是第一期.
    首先我们来写一个helloworld.
    学生:不是非新手向吗!
    来来来我们别急,这次的helloworld可是:
    让Boss血条显示HelloWorld!
    这次看上去是不是高大上了呢?

    OK现在我们开始上课啦~

    首先我们要准备一个API.
    什么你说API是什么东西?
    请滚去百度
    我们要使用的api为:BarApi(不是bra啦)
    BukkitDev地址:http://dev.bukkit.org/bukkit-plugins/bar-api/
    下载下来,然后按照原本BukkitAPI的添加方法添加到你的项目.(都说了这个是非新手向,新手部分不教)

    然后自己写上onEnable和onDisable还有onCommand.
    现在你的代码大概是这样:

    1. package <你的包名>;

    2. import org.bukkit.plugin.java.JavaPlugin;
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandSender;

    5. public class <你的类名> extends JavaPlugin
    6. public void onEnable(){
    7.                 getLogger().info("Has Enabled");
    8.         }
    9.         public void onDisable(){
    10.                 getLogger().info("Has Disabled");
    11.         }
    12. public boolean onCommand(CommandSender sender, Command command, String label, String[] args){


    13. reture true;
    14. }
    复制代码
    有onCommand,有onEnable,有onDisable.

    现在呢,我们来添加命令.

    加上if(cmd.getName().equalsIgnoreCase("helloworld)){

    }



    现在你就成功的创建了一个命令.
    现在要让它说话了,还记得那个Bukkit的BarAPI网页么?
    里面有下面这几句话
    BarAPI.setMessage(Player player, String message)

    Set a message for the player. It will remain there until the player logs off or another plugin overrides it.

    BarAPI.setMessage(Player player, String message, float percent)

    Same as above except you can set the % of the health bar. 100 shows the entire health bar, 50 shows half the health bar and so on.

    BarAPI.setMessage(final Player player, String message, int seconds)

    Sets a timed message for the player. It will remain until the timer runs out. The health automatically reduces based on how long the timer is.

    BarAPI.hasBar(Player player)

    Pretty self explanatory, returns a boolean.

    BarAPI.removeBar(Player player)

    Also pretty self explanatory.

    BarAPI.setHealth(Player player, float percent)

    Allows you to modify the health of an existing bar. If the player has no bar, this does nothing.

    是程序员应该都看得懂吧?

    现在我们在刚才上面添加命令的{}里面写上
    BarAPI.setMessage("HelloWorld!",  10);
    然后import需要的文件(如果你添加了api的话是可以看到BarAPI的文件的)
    想给它加上颜色代码?
    例如我要给它改成红色,那么就把代码改成.
    BarAPI.setMessage(ChatColor.RED + "HelloWorld!",  10);
    注意ChatColor不能importBukkit的要importBarAPI的.
    然后你就可以在plugin.yml里面添加你的command了.
    放到服务器测试运行的时候也要把BarAPI一起放到plugins里面,
    现在BarAPI就是你的helloworld的前置插件了.




    那么现在给大家来一个作业吧~

    写一个用/sayhello来启动的Boss血条显示文字的插件,显示文字为:hellozuimc!,并且颜色为黄色,显示时间为30秒
    把写好的代码发在下面就行.


    下节课预告:自己写一个Boss血条公告以及Permissions(权限).

    评分

    参与人数 1人气 +1 收起 理由
    Bug↘静£かに + 1 最MC有你更精彩!

    查看全部评分

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
  • TA的每日心情
    开心
    2016-6-20 23:23
  • 签到天数: 77 天

    [LV.6]常住居民II

    发表于 2015-5-31 19:50:07 | 显示全部楼层
    看起来好难-  -···
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
  • TA的每日心情
    郁闷
    2016-2-25 19:33
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    发表于 2016-2-17 18:56:28 | 显示全部楼层
    woc居然是笔记本。。。_(:зゝ∠)_那个。。以前在起源见过的说233333
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

    该用户从未签到

    发表于 2016-5-26 18:17:57 | 显示全部楼层
    谁有中文的bukkit api翻译1.7.10的

    点评

    在目前为止BukkitAPI并没有翻译的 而且如果要做这方面的开发 能看懂英文不是理所应当的事情吗?  详情 回复 发表于 2016-5-26 19:50
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
  • TA的每日心情
    擦汗
    2016-5-27 18:53
  • 签到天数: 9 天

    [LV.3]偶尔看看II

    发表于 2016-5-26 19:50:50 | 显示全部楼层
    wulianjun1 发表于 2016-5-26 18:17
    谁有中文的bukkit api翻译1.7.10的

    在目前为止BukkitAPI并没有翻译的 而且如果要做这方面的开发
    能看懂英文不是理所应当的事情吗?
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    您需要登录后才可以回帖 登录 | 加入最MC

    本版积分规则