我要加入

夢想家公會

會長:chenjinsheng / 此間半開一盞茶開設日:2015-05-17 23:58:58

  • EXP

  • 資金13569  
  • 關連作品我的世界、我的世界 Classic、我的世界... 看更多
  • 招募制度:自由加入制
  • 成員:23 人
  • 昨日人氣:0

海綿寶寶日記(用指令打招呼吧~)

推上精選編輯

近期編輯:chenjinsheng ...看更多

海綿寶寶日記(用指令打招呼吧~)

利用事件在服務器內顯示文字後,接下來要在遊戲中或伺服器上用指令來完成文字顯示,指令的建立與註冊也是基礎中的基礎。

Command.java
public class Command implements CommandExecutor{

   @Override
   public CommandResult execute(CommandSource commandSource, CommandContext
           commandContext) throws CommandException {
      // 執行內容
      return CommandResult.success(); // 回傳成功,empty則是失敗
}

上面應該看到了錯誤處理這部份,而採用「throws」就代表著只要有錯誤在CommandException內會造成服務器當機,而並非單純顯示錯誤而已。

執行內容
if (commandSource instanceof Player) {
   // 檢查是玩家,進一步轉型並取得玩家名稱
   Player player = (Player) commandSource;
   player.sendMessage(Texts.of("嗨! " + player.getName() + " !!"));
}
else if(commandSource instanceof ConsoleSource) {
   // 檢查是控制台,直接回傳給控制台
   commandSource.sendMessage(Texts.of("嗨!管理員!!"));
}
else if(commandSource instanceof CommandBlockSource) {
   // 回傳給指令方塊,運作方塊後再打開會顯在先前輸出內
   commandSource.sendMessage(Texts.of("嗨!指令方塊!!"));
}

在註冊之前還需要有指令的身份證,由於身份證是固定的,我們採用靜態的方法直接編在這裡就可以了。

身份證
public static final CommandSpec spec = CommandSpec.builder()
       .description(Texts.of("對自己打招呼的指令")) // 建立說明
       .permission("anikaba.hello.hi") // 設定權限
       .executor(new Command()) // 指令類
       .build();

最後就是在主類註冊剛編輯的指令。

HelloSponge.java
// 註冊插件ID,名稱,版本號
@Plugin(id = "anikaba-HelloSponge", name = "Hello Sponge", version = "v0.1")
public class HelloSponge {
  @Inject
  public static Game game; // 使用靜態寫法方便其他類取得

  @Listener
  public void onEnable(GamePreInitializationEvent event) {
    game = event.getGame();
    // 註冊指令”hi”
    game.getCommandDispatcher().register(this, Command.spec, "hi");
  }
}

公會首頁

主選單
關聯資料

目前沒有資料連到「海綿寶寶日記(用指令打招呼吧~)」。


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】