1
1
package gs .mclo .fabric ;
2
2
3
- import com .mojang .brigadier .context . CommandContext ;
3
+ import com .mojang .brigadier .builder . LiteralArgumentBuilder ;
4
4
import gs .mclo .api .Log ;
5
5
import gs .mclo .api .MclogsClient ;
6
6
import gs .mclo .api .response .UploadLogResponse ;
7
+ import gs .mclo .fabric .commands .Command ;
8
+ import gs .mclo .fabric .commands .MclogsCommand ;
9
+ import gs .mclo .fabric .commands .MclogsListCommand ;
10
+ import gs .mclo .fabric .commands .MclogsShareCommand ;
11
+ import gs .mclo .fabric .commands .source .Source ;
12
+ import net .fabricmc .api .ClientModInitializer ;
7
13
import net .fabricmc .api .DedicatedServerModInitializer ;
14
+ import net .fabricmc .api .ModInitializer ;
15
+ import net .fabricmc .fabric .api .client .command .v1 .ClientCommandManager ;
16
+ import net .fabricmc .fabric .api .client .command .v1 .FabricClientCommandSource ;
8
17
import net .fabricmc .fabric .api .command .v1 .CommandRegistrationCallback ;
9
18
import net .fabricmc .loader .api .FabricLoader ;
10
19
import net .fabricmc .loader .api .ModContainer ;
20
+ import net .minecraft .server .command .CommandManager ;
11
21
import net .minecraft .server .command .ServerCommandSource ;
12
22
import net .minecraft .text .ClickEvent ;
13
23
import net .minecraft .text .LiteralText ;
24
34
import java .util .concurrent .CompletableFuture ;
25
35
import java .util .concurrent .ExecutionException ;
26
36
27
- public class MclogsFabricLoader implements DedicatedServerModInitializer {
37
+ public class MclogsFabric implements DedicatedServerModInitializer , ClientModInitializer , ModInitializer {
28
38
public static final Logger logger = LogManager .getLogger ();
29
39
private static final MclogsClient client = new MclogsClient ("Mclogs-fabric" );
40
+ private static final Command [] COMMANDS = new Command []{
41
+ new MclogsCommand (),
42
+ new MclogsListCommand (),
43
+ new MclogsShareCommand ()
44
+ };
30
45
31
46
/**
32
- * @param context command context
47
+ * @param source command source
33
48
* @return log files
34
49
* @throws IOException io exception
35
50
*/
36
- public static String [] getLogs (CommandContext < ServerCommandSource > context ) throws IOException {
37
- return client .listLogsInDirectory (context . getSource (). getMinecraftServer (). getRunDirectory ().getCanonicalPath ());
51
+ public static String [] getLogs (Source source ) throws IOException {
52
+ return client .listLogsInDirectory (source . getRunDirectory ().toString ());
38
53
}
39
54
40
55
/**
41
- * @param context command context
56
+ * @param source command source
42
57
* @return crash reports
43
58
* @throws IOException io exception
44
59
*/
45
- public static String [] getCrashReports (CommandContext < ServerCommandSource > context ) throws IOException {
46
- return client .listCrashReportsInDirectory (context . getSource (). getMinecraftServer (). getRunDirectory ().getCanonicalPath ());
60
+ public static String [] getCrashReports (Source source ) throws IOException {
61
+ return client .listCrashReportsInDirectory (source . getRunDirectory ().toString ());
47
62
}
48
63
49
- public static int share (ServerCommandSource source , String filename ) {
50
- client .setMinecraftVersion (source .getMinecraftServer (). getVersion ());
51
- logger .log (Level .INFO ,"Sharing " + filename );
64
+ public static int share (Source source , String filename ) {
65
+ client .setMinecraftVersion (source .getMinecraftVersion ());
66
+ logger .log (Level .INFO , "Sharing {}" , filename );
52
67
source .sendFeedback (new LiteralText ("Sharing " + filename ), false );
53
68
54
- Path directory = source .getMinecraftServer (). getRunDirectory (). toPath ();
69
+ Path directory = source .getRunDirectory ();
55
70
Path logs = directory .resolve ("logs" );
56
71
Path crashReports = directory .resolve ("crash-reports" );
57
72
Path log = directory .resolve ("logs" ).resolve (filename );
@@ -65,8 +80,8 @@ public static int share(ServerCommandSource source, String filename) {
65
80
Path logPath = log .toRealPath ();
66
81
isInAllowedDirectory = (logs .toFile ().exists () && logPath .startsWith (logs .toRealPath ()))
67
82
|| (crashReports .toFile ().exists () && logPath .startsWith (crashReports .toRealPath ()));
83
+ } catch (IOException ignored ) {
68
84
}
69
- catch (IOException ignored ) {}
70
85
71
86
if (!log .toFile ().exists () || !isInAllowedDirectory
72
87
|| !log .getFileName ().toString ().matches (Log .ALLOWED_FILE_NAME_PATTERN .pattern ())) {
@@ -81,30 +96,27 @@ public static int share(ServerCommandSource source, String filename) {
81
96
res .setClient (client );
82
97
if (res .isSuccess ()) {
83
98
LiteralText feedback = new LiteralText ("Your log has been uploaded: " );
84
- feedback .setStyle (Style .EMPTY .withColor (Formatting .GREEN ));
85
99
86
100
LiteralText link = new LiteralText (res .getUrl ());
87
- Style linkStyle = Style .EMPTY .withColor (Formatting .BLUE );
88
- linkStyle = linkStyle .withClickEvent (new ClickEvent (ClickEvent .Action .OPEN_URL ,res .getUrl ()));
101
+ Style linkStyle = Style .EMPTY
102
+ .withClickEvent (new ClickEvent (ClickEvent .Action .OPEN_URL , res .getUrl ()))
103
+ .withFormatting (Formatting .UNDERLINE );
89
104
link .setStyle (linkStyle );
90
105
91
- source .sendFeedback (feedback .append (link ),true );
106
+ source .sendFeedback (feedback .append (link ), true );
92
107
return 1 ;
93
- }
94
- else {
108
+ } else {
95
109
logger .error ("An error occurred when uploading your log: " );
96
110
logger .error (res .getError ());
97
111
LiteralText error = new LiteralText ("An error occurred. Check your log for more details" );
98
112
source .sendError (error );
99
113
return 0 ;
100
114
}
101
- }
102
- catch (FileNotFoundException |IllegalArgumentException e ) {
103
- LiteralText error = new LiteralText ("The log file " +filename +" doesn't exist. Use '/mclogs list' to list all logs." );
115
+ } catch (FileNotFoundException | IllegalArgumentException e ) {
116
+ LiteralText error = new LiteralText ("The log file " + filename + " doesn't exist. Use '/mclogs list' to list all logs." );
104
117
source .sendError (error );
105
118
return -1 ;
106
- }
107
- catch (IOException | InterruptedException | ExecutionException e ) {
119
+ } catch (IOException | InterruptedException | ExecutionException e ) {
108
120
source .sendError (new LiteralText ("An error occurred. Check your log for more details" ));
109
121
logger .error ("Could not get log file!" );
110
122
logger .error (e );
@@ -113,13 +125,33 @@ public static int share(ServerCommandSource source, String filename) {
113
125
}
114
126
115
127
@ Override
116
- public void onInitializeServer () {
128
+ public void onInitialize () {
117
129
Optional <ModContainer > mclogs = FabricLoader .getInstance ().getModContainer ("mclogs" );
118
130
client .setProjectVersion (mclogs .isPresent () ? mclogs .get ().getMetadata ().getVersion ().getFriendlyString () : "unknown" );
131
+ }
132
+
133
+ @ Override
134
+ public void onInitializeServer () {
119
135
CommandRegistrationCallback .EVENT .register ((dispatcher , dedicated ) -> {
120
- CommandMclogs .register (dispatcher );
121
- CommandMclogsList .register (dispatcher );
122
- CommandMclogsShare .register (dispatcher );
136
+ if (!dedicated ) {
137
+ return ;
138
+ }
139
+
140
+ logger .info ("Registering server commands" );
141
+ LiteralArgumentBuilder <ServerCommandSource > mclogs = CommandManager .literal ("mclogs" );
142
+ for (Command command : COMMANDS ) {
143
+ dispatcher .register (command .buildServer (mclogs ));
144
+ }
123
145
});
124
146
}
147
+
148
+ @ Override
149
+ public void onInitializeClient () {
150
+ logger .info ("Registering client commands" );
151
+ LiteralArgumentBuilder <FabricClientCommandSource > mclogsc = ClientCommandManager .literal ("mclogsc" );
152
+ for (Command command : COMMANDS ) {
153
+ ClientCommandManager .DISPATCHER .register (command .buildClient (mclogsc ));
154
+ }
155
+ }
156
+
125
157
}
0 commit comments