在Minecraft 1.20.x版本的Forge中进行MOD开发,在服务器端执行命令
首先
我想在Minecraft里制作一个在夜晚有各种活动的事件,所以我开始涉足MOD制作。但是,这比我想象中的要困难得多,所以我决定写一篇文章来记录这个过程。
基本上,我只在服务器端进行实现,而玩家可以使用原始环境。因为我只是偶尔玩一下Minecraft,而对于Forge只是初级的了解,所以不会详细解释代码的细节(因为不会)。
这次想做的事
以下のコマンドをサーバー側で実行させたい。
/title @a title {"text":"Chapter I","bold":true}
为什么要在服务器端处理?
因为不想给以多重登录的玩家授权。
代码 (pinyin:
当玩家在服务器上登录时,执行上述命令的代码在服务器端执行。
@SubscribeEvent
public void OnPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
// Playerのインスタンス
Player player = event.getEntity();
// コマンド
String text = "title @a title {\"text\":\"Chapter I\",\"bold\":true}";
// 実行する際のパーミッション?
CommandSourceStack commandSourceStack = player.createCommandSourceStack().withSuppressedOutput().withPermission(4);
// コマンドの実行権生成
CommandDispatcher<CommandSourceStack> commanddispatcher = player.getServer().getCommands().getDispatcher();
// コマンド文字列の解析
ParseResults<CommandSourceStack> results = commanddispatcher.parse(text, commandSourceStack);
// 実行 成功なら1 失敗なら0を返す
if (serverPlayer.getServer().getCommands().performCommand(results, text) != 1){
LOGGER.debug("-------- Title Command Failure ---------");
}
}
评论是根据推测写的。
commanddispatcher.parse(text, commandSourceStack);
如果按照内部进行解析,似乎从每个命令的类中生成了对象?
如果在这个阶段发现命令有误,则会返回空对象,所以可能最好在这里进行判断。
我认为由于Minecraft运行在Java 8这一通常版本之上,最好不要使用类型推断之类的功能。
结果

请参考
下一次