1+ package com .hitech0926 .hubcommand ;
2+
3+ import com .google .inject .Inject ;
4+ import com .velocitypowered .api .command .CommandSource ;
5+ import com .velocitypowered .api .command .SimpleCommand ;
6+ import com .velocitypowered .api .event .Subscribe ;
7+ import com .velocitypowered .api .event .proxy .ProxyInitializeEvent ;
8+ import com .velocitypowered .api .event .proxy .ProxyShutdownEvent ;
9+ import com .velocitypowered .api .plugin .Plugin ;
10+ import com .velocitypowered .api .plugin .annotation .DataDirectory ;
11+ import com .velocitypowered .api .proxy .Player ;
12+ import com .velocitypowered .api .proxy .ProxyServer ;
13+ import com .velocitypowered .api .proxy .server .RegisteredServer ;
14+ import net .kyori .adventure .text .Component ;
15+ import net .kyori .adventure .text .minimessage .MiniMessage ;
16+ import org .slf4j .Logger ;
17+ import org .yaml .snakeyaml .Yaml ;
18+
19+ import java .io .File ;
20+ import java .io .IOException ;
21+ import java .io .InputStream ;
22+ import java .nio .file .Files ;
23+ import java .nio .file .Path ;
24+ import java .util .Map ;
25+ import java .util .Optional ;
26+
27+ @ Plugin (
28+ id = "hubcommand" ,
29+ name = "HubCommand" ,
30+ version = "1.0.0" ,
31+ authors = {"Hitech0926" }
32+ )
33+ public class HubCommand {
34+ private final ProxyServer server ;
35+ private final Logger logger ;
36+ private final Path dataDirectory ;
37+ private boolean enabled ;
38+ private String lobbyServer ;
39+ private String sendSuccessful ;
40+ private String noPermission ;
41+ private String noConsole ;
42+
43+ @ Inject
44+ public HubCommand (ProxyServer server , Logger logger , @ DataDirectory Path dataDirectory ) {
45+ this .server = server ;
46+ this .logger = logger ;
47+ this .dataDirectory = dataDirectory ;
48+ }
49+
50+ @ Subscribe
51+ public void onProxyInitialization (ProxyInitializeEvent event ) {
52+ logger .info ("HubCommand插件已加载!" );
53+
54+ // 加载配置
55+ loadConfig ();
56+
57+ // 注册命令
58+ server .getCommandManager ().register (
59+ server .getCommandManager ().metaBuilder ("hub" ).build (),
60+ new HubCommandExecutor (this )
61+ );
62+ server .getCommandManager ().register (
63+ server .getCommandManager ().metaBuilder ("lobby" ).build (),
64+ new HubCommandExecutor (this )
65+ );
66+ }
67+
68+ @ Subscribe
69+ public void onProxyShutdown (ProxyShutdownEvent event ) {
70+ logger .info ("HubCommand插件已卸载!" );
71+ }
72+
73+ public ProxyServer getServer () {
74+ return server ;
75+ }
76+
77+ private void loadConfig () {
78+ File file = dataDirectory .resolve ("config.yml" ).toFile ();
79+ Yaml yaml = new Yaml ();
80+ if (!file .exists ()) {
81+ try (InputStream inputStream = getClass ().getClassLoader ().getResourceAsStream ("config.yml" )) {
82+ Files .copy (inputStream , file .toPath ());
83+ } catch (IOException e ) {
84+ logger .error ("无法创建配置文件" , e );
85+ return ;
86+ }
87+ }
88+ try (InputStream inputStream = Files .newInputStream (file .toPath ())) {
89+ Map <String , Object > config = yaml .load (inputStream );
90+ enabled = (boolean ) config .getOrDefault ("enabled" , true );
91+ lobbyServer = (String ) config .getOrDefault ("lobby-server" , "lobby" );
92+ sendSuccessful = (String ) config .getOrDefault ("send-successful" , "<green>你已被传送到大厅!" );
93+ noPermission = (String ) config .getOrDefault ("no-permission" , "<red>你没有权限执行此命令!" );
94+ noConsole = (String ) config .getOrDefault ("no-console" , "<red>只有玩家可以执行此命令!" );
95+ } catch (IOException e ) {
96+ logger .error ("无法加载配置文件" , e );
97+ }
98+ }
99+
100+ private static class HubCommandExecutor implements SimpleCommand {
101+ private final HubCommand plugin ;
102+
103+ public HubCommandExecutor (HubCommand plugin ) {
104+ this .plugin = plugin ;
105+ }
106+
107+ @ Override
108+ public void execute (Invocation invocation ) {
109+ CommandSource source = invocation .source ();
110+ String [] args = invocation .arguments ();
111+
112+ if (!plugin .enabled ) {
113+ return ;
114+ }
115+
116+ // 处理命令逻辑
117+ if (args .length == 0 ) {
118+ // 玩家传送自己
119+ if (!(source instanceof Player )) {
120+ source .sendMessage (MiniMessage .miniMessage ().deserialize (plugin .noConsole ));
121+ return ;
122+ }
123+
124+ Player player = (Player ) source ;
125+ if (!player .hasPermission ("hubcommand.player" )) {
126+ player .sendMessage (MiniMessage .miniMessage ().deserialize (plugin .noPermission ));
127+ return ;
128+ }
129+
130+ teleportPlayer (player );
131+ } else if (args .length == 1 ) {
132+ // 管理员传送其他玩家
133+ if (!source .hasPermission ("hubcommand.admin" )) {
134+ source .sendMessage (MiniMessage .miniMessage ().deserialize (plugin .noPermission ));
135+ return ;
136+ }
137+
138+ Optional <Player > targetPlayer = plugin .getServer ().getPlayer (args [0 ]);
139+ if (targetPlayer .isPresent ()) {
140+ teleportPlayer (targetPlayer .get ());
141+ }
142+ }
143+ }
144+
145+ private void teleportPlayer (Player player ) {
146+ Optional <RegisteredServer > lobbyServer = plugin .getServer ().getServer (plugin .lobbyServer );
147+ if (lobbyServer .isPresent ()) {
148+ player .createConnectionRequest (lobbyServer .get ()).fireAndForget ();
149+ player .sendMessage (MiniMessage .miniMessage ().deserialize (plugin .sendSuccessful ));
150+ }
151+ }
152+ }
153+ }
0 commit comments