概述
注册命令与处理
- 内容简介
- 如何注册命令
- 权限设置
- plugin.yml权限
- luckpermission
内容简介
如何注册命令(理解)
如何重写JavaPlugin中的onCommand方法来做到命令处理(理解,重点)
如何在一个独立类中作为命令处理类(理解,重点)
如何注册命令
权限设置
看到这里你可能会问,若是没有权限组我该如何配置玩家的 权限 ?既然 Bukkit API 本身并没有提供用户组的预设,
你必须通过安装一款权限组插件,
像 permissionsBukkit 这样的插件来帮助你配置用户群. API 只是提供接口而不是提供已实现的类.
plugin.yml权限
luckpermission
插件使用方法
https://github.com/lucko/LuckPerms/wiki
如何进行命令的设计以实现玩家输入相应的指令完成相应的操作?
①编写yml文件,赋予玩家可用操作命令以及权限,包括打开图鉴及装备称号等相关需要输入命令以实现,例如/open 以打开各种界面,/vftxi 以装备特效及称号,/chengjiu 设置或查询玩家的成就点数;
在resources目录下新建一个名为plugin的文件,并将其后缀改为.yml
我所设置的操作命令及权限如下:
name: Myplugin
main: com.project.minecraft.main
version: 1.0.0
author: roger0123
depend:
- VexView
- PlaceholderAPI
- Vault
commands:
hgd:
description: "这个命令是好感度操作的命令"
usage: "使用方法: /hgd <Order>"
permission: "MyPlugin.hgd"
permission-message: "你没有操作好感度的权限"
open:
description: "这个命令是打开图鉴界面的命令"
usage: "使用方法: /open <GuiName>"
permission: "MyPlugin.open"
permission-message: "你没有打开图鉴界面的权限"
vftxi:
description: "这个命令是称号操作的命令"
usage: "使用方法: /vftxi <Order>"
permission: "MypPlugin.vftxi"
permission-message: "你没有装备称号的权限"
chengjiu:
description: "这个命令是成就操作的命令"
usage: "使用方法: /vftxi <Order>"
permission: "MypPlugin.chengjiu"
permission-message: "你没有操作成就的权限"
sayMyName:
description: this command says its sender's name
usage: "Usage: /sayMyName"
permission: "plugin.sayMyName"
permission-message: "you don't have access to this plugin"
handbook:
description: this command opens player's handbook
usage: "Usage: /handbook"
permission: "plugin.handbook"
permission-message: "you don't have access to this plugin"
DBCommand:
description: this command uses SQLite
usage: "Usage: /DBCommand"
permission: "plugin.DBCommand"
permission-message: "you don't have access to this plugin"
permissions:
hgd.*:
description: "操作好感度权限集合"
children:
hgd.set: op
hgd.set:
description: "管理员才有设置好感度的权限"
default: op
open.*:
description: "打开图鉴界面权限集合"
children:
open.tuijianGui: true
open.zhiwuGui: true
open.shengwuGui: true
open.zhuangbeiGui: true
open.taskGui: true
open.tujianGui:
description: "普通玩家就有打开图鉴界面的权限"
default: true
open.zhiwuGui:
description: "普通玩家就有打开植物图鉴界面的权限"
default: true
open.shengwuGui:
description: "普通玩家就有打开生物图鉴界面的权限"
default: true
open.zhuangbeiGui:
description: "普通玩家就有打开装备图鉴界面的权限"
default: true
open.chengjiuGui:
description: "普通玩家就有打开悬赏任务界面的权限"
default: true
vftxi.*:
description: "装备称号权限集合"
children:
vftxi.opench: true
vftxi.hidech: true
vftxi.showch: true
vftxi.chhelp: true
vftxi.opentx: true
vftxi.openhud: true
vftxi.closehud: true
vftxi.zbtx1: true
vftxi.zbtx2: true
vftxi.opench:
description: "普通玩家就有打开称号界面的权限"
default: true
vftxi.showch:
description: "普通玩家就有显示装备称号的权限"
default: true
vftxi.hidech:
description: "普通玩家就有取消装备称号的权限"
default: true
vftxi.chhelp:
description: "普通玩家就有打开称号帮助的权限"
default: true
vftxi.opentx:
description: "普通玩家就有打开特效界面的权限"
default: true
vftxi.openhud:
description: "普通玩家就有打开健康游戏公告的权限"
default: true
vftxi.closehud:
description: "普通玩家就有关闭健康游戏公告的权限"
default: true
vftxi.zbtx1:
description: "普通玩家就有装备特效1的权限"
default: true
vftxi.zbtx2:
description: "普通玩家就有装备特效2的权限"
default: true
chengjiu.*:
description: "成就操作权限集合"
children:
chengjiu.set: op
chengjiu.get: true
chengjiu.show: true
chengjiu.set:
description: "管理员才有设置成就点数的权限"
default: op
chengjiu.show:
description: "普通玩家就有查询成就列表的权限"
default: true
chengjiu.get:
description: "普通玩家就有查询成就点数的权限"
default: true
handbook.*:
description: "便捷命令权限集合"
children:
handbook.menu: true
handbook.creeper: true
handbook.menu:
description: "普通玩家就有打开菜单界面的权限"
default: true
handbook.creeper:
description: "普通玩家就有随地生成creeper的权限"
default: true
②编写Command类,
如对命令进行输入和玩家权限判断以及对玩家进行操作提示;
我所设计的操作命令包括(主菜单界面操作、图鉴界面操作和成就奖励界面操作)如下:
// 主菜单界面操作
package com.project.minecraft.commands;
import com.connorlinfoot.titleapi.TitleAPI;
import com.project.minecraft.vexview.MyGui_tujian;
import lk.vexview.api.VexViewAPI;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class MyCommandOpen implements CommandExecutor {
@Override
public boolean onCommand(CommandSender commandSender, Command command, String label, String[] strings) {
if (strings.length == 0){
commandSender.sendMessage("§5-----------图鉴界面模块-----------");
commandSender.sendMessage("§5 /open tujianGUi §d打开图鉴列表");
commandSender.sendMessage("§5 /open zhiwuGUi §d打开植物图鉴");
commandSender.sendMessage("§5 /open shengwuGui §d打开生物图鉴");
commandSender.sendMessage("§5 /open zhuangbeiGui §d打开装备图鉴");
commandSender.sendMessage("§5 /open taskGui §d打开悬赏任务");
return true;
}
if(commandSender instanceof Player){
Player player = (Player) commandSender;
if(!player.hasPermission("MyPlugin.open")){
return true;
}
if(label.equalsIgnoreCase("open")){
//open <GuiName>
if(strings[0].equalsIgnoreCase("tujianGUi")){
//打开图鉴界面
VexViewAPI.openGui(player, MyGui_tujian.tujianGui());
player.sendMessage("§d打开图鉴界面§e成功");
TitleAPI.sendTitle(player,5,4,5,"§1Minecraft图鉴","§2图鉴列表");
}else if(strings[0].equalsIgnoreCase("zhiwuGui")){
//打开植物图鉴界面
VexViewAPI.openGui(player, MyGui_tujian.zhiwuGui());
player.sendMessage("§d打开植物图鉴界面§e成功");
TitleAPI.sendTitle(player,5,4,5,"§1Minecraft图鉴","§2植物图鉴");
}else if(strings[0].equalsIgnoreCase("shengwuGui")){
//打开图鉴界面
VexViewAPI.openGui(player, MyGui_tujian.shengwuGui());
player.sendMessage("§d打开生物图鉴界面§e成功");
TitleAPI.sendTitle(player,5,4,5,"§1Minecraft图鉴","§2生物图鉴");
}else if(strings[0].equalsIgnoreCase("zhuangbeiGui")){
//打开装备图鉴界面
VexViewAPI.openGui(player, MyGui_tujian.zhuangbeiGui());
player.sendMessage("§d打开装备图鉴界面§e成功");
TitleAPI.sendTitle(player,5,4,5,"§1Minecraft图鉴","§2装备图鉴");
}else if(strings[0].equalsIgnoreCase("taskGui")){
//打开装备图鉴界面
VexViewAPI.openGui(player, MyGui_tujian.chengjiuGui());
player.sendMessage("§d打开悬赏任务界面§e成功");
TitleAPI.sendTitle(player,5,4,5,"§1Minecraft图鉴","§2悬赏任务");
}else{
player.sendMessage("§d打开图鉴界面§e失败,§f请检查输入的指令是否为open <GuiName>");
}
}
}else{
commandSender.sendMessage("§d打开图鉴界面§e失败,§f请检查输入的指令是否为open <GuiName>");
}
return true;
}
}
// 图鉴界面操作
package com.project.minecraft.commands;
import FzmXer.VexFxTitle.Gui.FxTitleGui;
import FzmXer.VexFxTitle.Gui.PlayerTags;
import FzmXer.VexFxTitle.SQL.DataManager;
import FzmXer.VexFxTitle.Utils.Utils;
import com.connorlinfoot.titleapi.TitleAPI;
import com.project.minecraft.dao.SQLManager;
import com.project.minecraft.vexview.Jiangli_Effect1;
import com.project.minecraft.vexview.Jiangli_Effect2;
import com.project.minecraft.vexview.MyGui_tujian;
import lk.vexview.api.VexViewAPI;
import lk.vexview.hud.VexImageShow;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class MyVexViewCmd implements CommandExecutor {
@Override
public boolean onCommand(CommandSender commandSender, Command command, String label, String[] strings) {
if (!(commandSender instanceof Player)) {
return true;
}
Player player = (Player) commandSender;
int cj = SQLManager.get().FindChengjiu(player.getName());
if (!player.hasPermission("MyPlugin.vftxi")) {
return true;
}
if (strings.length == 0) {
player.sendMessage("§5-----------奖励操作模块-----------");
player.sendMessage("§5 /vftxi openhud §d打开健康公告");
player.sendMessage("§5 /vftxi closehud §d关闭健康公告");
player.sendMessage("§5 /vftxi chhelp §d打开称号帮助");
player.sendMessage("§5 /vftxi opench §d打开称号界面");
player.sendMessage("§5 /vftxi showch §d显示装备称号");
player.sendMessage("§5 /vftxi hidech §d隐藏装备称号");
player.sendMessage("§5 /vftxi opentx §d打开特效界面");
player.sendMessage("§5 /vftxi zbtx1 §d 装备特效1 ");
player.sendMessage("§5 /vftxi zbtx2 §d 装备特效2 ");
}
if (label.equalsIgnoreCase("vftxi")) {
//vftxi <Order>
if (strings.length == 1) {
if (strings[0].equalsIgnoreCase("openhud")) {
VexImageShow vis = new VexImageShow("0", "[local]欢迎.jpg", -1, -1, 240, 160, 240, 160, 10);
VexViewAPI.sendHUD(player, vis);
player.sendMessage("§d欢迎" + player.getPlayerListName() + "§e进入游戏");
// } else if (strings[0].equalsIgnoreCase("1")){
// TagDirection td = new TagDirection(0,0,0,true,true);
// VexImageTag vit = new VexImageTag("chenghao1",0,0,0,"[local]羿若惊羽.gif",80,40,2,1,td);
// VexViewAPI.addPlayerTag(player,vit);
// VexViewAPI.addWorldTag(player.getWorld(),vit);
// player.sendMessage("§d装备称号1§e成功");
} else if (strings[0].equalsIgnoreCase("closehud")) {
VexViewAPI.removeHUD(player, "0");
player.sendMessage("§d关闭健康游戏公告§e成功");
// }else if(strings[0].equalsIgnoreCase("11")){
// VexViewAPI.removePlayerTag(player,"chenghao1");
// VexViewAPI.removeWorldTag(player.getWorld(),"tag1");
// player.sendMessage("§d取消称号1§e成功");
} else if (strings[0].equalsIgnoreCase("chhelp")) {
if (player.isOp()) {
player.sendMessage("§a--------------- §a[§eVexFxTitle§a] ---------------");
player.sendMessage("§a/vft open - 打开称号Gui.");
player.sendMessage("§a/vft show - 称号显示.");
player.sendMessage("§a/vft hide - 称号隐藏.");
player.sendMessage("§a/vft list [page] - 查看所有称号.");
player.sendMessage("§a/vft give [player] [id] - 给予玩家对应称号.");
player.sendMessage("§a/vft add [player] [id] - 添加玩家对应称号.");
player.sendMessage("§a/vft del [player] [id] - 删除玩家对应称号.");
player.sendMessage("§a/vft reload - 重载插件.");
} else {
player.sendMessage("§a--------------- §a[§eVexFxTitle§a] ---------------");
player.sendMessage("§a/vft open - 打开称号Gui.");
player.sendMessage("§a/vfy show - 称号对自己显示.");
player.sendMessage("§a/vft hide - 称号对自己隐藏.");
}
player.sendMessage("§a称号帮助§e已打开");
} else if (strings[0].equalsIgnoreCase("opench")) {
DataManager.SetTitleState(player.getUniqueId().toString(), 1);
VexViewAPI.openGui(player, new FxTitleGui(player, Utils.Scale(player)));
PlayerTags.ResPlayerTags(player);
player.sendMessage("§a称号显示界面§e已打开");
} else if (strings[0].equalsIgnoreCase("showch")) {
DataManager.SetTitleState(player.getUniqueId().toString(), 1);
PlayerTags.ResPlayerTags(player);
player.sendMessage("§a称号显示: 已开启");
} else if (strings[0].equalsIgnoreCase("hidech")) {
DataManager.SetTitleState(player.getUniqueId().toString(), 0);
PlayerTags.cencelRun(player);
player.sendMessage("§a称号显示: 已关闭");
} else if (strings[0].equalsIgnoreCase("opentx")) {
VexViewAPI.openGui(player, MyGui_tujian.jiangliGui(player));
player.sendMessage("§a称号显示界面§e已打开");
} else if (strings[0].equalsIgnoreCase("zbtx1")) {
if (cj > 0) {
Jiangli_Effect1 effect1 = new Jiangli_Effect1(player);
effect1.startEffect();
player.sendMessage("§d装备特效1“闪电风暴”§e成功");
TitleAPI.sendTitle(player, 5, 4, 5, "§2闪电风暴", "特效1");
} else {
player.sendMessage("§d成就点数不足!§e装备特效1“闪电风暴”失败");
}
} else if (strings[0].equalsIgnoreCase("zbtx2")) {
if (cj >= 1500) {
Jiangli_Effect2 effect2 = new Jiangli_Effect2(player);
effect2.startEffect();
player.sendMessage("§d装备特效2“龙之吐息”§e成功");
TitleAPI.sendTitle(player, 5, 4, 5, "§2龙之吐息", "特效2");
} else {
player.sendMessage("§d成就点数不足!§e装备特效2“龙之吐息”失败");
}
}
} else {
player.sendMessage("§a检查输入的指令是否为: /vftxi <order>");
}
} return false;
}
}
// 成就奖励界面操作
package com.project.minecraft.commands;
import com.project.minecraft.dao.SQLManager;
import com.project.minecraft.vexview.MyGui_tujian;
import lk.vexview.api.VexViewAPI;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class MyCommandCJ implements CommandExecutor {
@Override
public boolean onCommand(CommandSender commandSender, Command command, String label, String[] strings) {
int ap;
if (commandSender instanceof Player) {
Player player = (Player) commandSender;
if (strings.length == 0) {
if (player.isOp()) {
player.sendMessage("§5-----------成就操作模块-----------");
player.sendMessage("§5 /chengjiu show §d打开成就列表");
player.sendMessage("§5 /chengjiu get <playerName> §d查询成就点数");
player.sendMessage("§5 /chengjiu set <playerName> ap §d设置成就点数");
} else {
player.sendMessage("§5-----------成就操作模块-----------");
player.sendMessage("§5 /chengjiu show §d打开成就列表");
player.sendMessage("§5 /chengjiu get <playerName> §d查询成就点数");
}
return true;
}
if (!player.hasPermission("MyPlugin.chengjiu")) {
return true;
}
if (label.equalsIgnoreCase("chengjiu")) {
if (strings.length == 1) {
//chengjiu <order>
if (strings[0].equalsIgnoreCase("show")) {
//打开成就列表
VexViewAPI.openGui(player, MyGui_tujian.jiangliGui(player));
player.sendMessage("§d打开成就列表§e成功");
}else {
player.sendMessage("§d成就操作§e失败,§f请检查输入的指令是否为/chengjiu <order>");
}
}else if (strings.length == 2){
if(strings[0].equalsIgnoreCase("get")) {
//查询成就点数
player.sendMessage(player.getPlayer().getName()+"的成就点数为:"+SQLManager.get().FindChengjiu(player.getName()));
}
} else if(strings.length == 3 ){
//设置成就点数
//chengjiu set <playerName> ap
if (!commandSender.hasPermission("chengjiu.set")){
commandSender.sendMessage("§e§l你还没有权限节点chengjiu.set");
return true;
}
if (SQLManager.get().HasPlayerData(strings[1])){
SQLManager.get().UpDataChengjiu(strings[1],Integer.parseInt(strings[2]));
commandSender.sendMessage("§e§l成就点数设置成功");
}else {
commandSender.sendMessage("§e§l此玩家未登陆过服务器。");
}
}
}
} else{
if (strings.length==3){
if (strings[0].equalsIgnoreCase("set")){
if (SQLManager.get().HasPlayerData(strings[1])){
SQLManager.get().UpDataChengjiu(strings[1],Integer.parseInt(strings[2]));
commandSender.sendMessage("§e§l成就点数设置成功");
}else {
commandSender.sendMessage("§e§l此玩家未登陆过服务器。");
}
}
}else {
commandSender.sendMessage("控制台只能使用/chengjiu set <PlayerName> ap命令");
}
}
return true;
}
}
③在插件main方法中对命令进行注册,使命令可用可操作。
最后
以上就是含蓄朋友为你收集整理的基于Java的Minecraft游戏后端自定义插件 04注册命令与处理的全部内容,希望文章能够帮你解决基于Java的Minecraft游戏后端自定义插件 04注册命令与处理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复