1
- use std:: fs:: { self , File , OpenOptions } ;
1
+ use std:: fs:: { self , remove_dir_all , File , OpenOptions } ;
2
2
use std:: io:: { self , BufRead , Seek , SeekFrom } ;
3
3
use std:: path:: Path ;
4
4
mod utils;
5
- use crate :: { consts, gracefully_exit} ;
5
+ use crate :: config:: { Difficulty , Gamemode } ;
6
+ use crate :: { config, consts, gracefully_exit} ;
7
+ use cactus_world:: level:: { create_nbt, LevelDat , VersionInfo } ;
6
8
use colored:: Colorize ;
7
9
use log:: { error, info, warn} ;
8
10
use serde:: { Deserialize , Serialize } ;
@@ -15,7 +17,7 @@ pub fn init() -> std::io::Result<()> {
15
17
create_server_properties ( )
16
18
}
17
19
18
- /// Checks if the eula is agreed, if not creates it .
20
+ /// Checks if the eula is agreed, if not shutdown the server with failure code .
19
21
fn eula ( ) -> io:: Result < ( ) > {
20
22
let path = Path :: new ( consts:: file_paths:: EULA ) ;
21
23
if !path. exists ( ) {
@@ -39,15 +41,15 @@ fn create_server_properties() -> io::Result<()> {
39
41
let path = Path :: new ( consts:: file_paths:: PROPERTIES ) ;
40
42
let content = consts:: file_contents:: server_properties ( ) ;
41
43
42
- utils:: create_file ( path, & content)
44
+ utils:: create_file ( path, Some ( & content) )
43
45
}
44
46
45
47
/// Creates the 'eula.txt' file if it does not already exist.
46
48
fn create_eula ( ) -> io:: Result < ( ) > {
47
49
let path = Path :: new ( consts:: file_paths:: EULA ) ;
48
50
let content = consts:: file_contents:: eula ( ) ;
49
51
50
- utils:: create_file ( path, & content)
52
+ utils:: create_file ( path, Some ( & content) )
51
53
}
52
54
53
55
/// Check if the 'eula.txt' has been agreed to.
@@ -67,58 +69,66 @@ fn check_eula() -> io::Result<bool> {
67
69
}
68
70
69
71
pub fn create_other_files ( ) {
70
- match utils:: create_file_nn ( Path :: new ( consts:: file_paths:: BANNED_IP ) ) {
71
- Ok ( _) => info ! ( "Created file {}" , consts :: file_paths :: BANNED_IP ) ,
72
- Err ( e) => info ! (
72
+ match utils:: create_file ( Path :: new ( consts:: file_paths:: BANNED_IP ) , None ) {
73
+ Ok ( _) => ( ) ,
74
+ Err ( e) => error ! (
73
75
"Failed to create the file {} as error:{}" ,
74
76
consts:: file_paths:: BANNED_IP ,
75
77
e
76
78
) ,
77
79
}
78
- match utils:: create_file_nn ( Path :: new ( consts:: file_paths:: BANNED_PLAYERS ) ) {
79
- Ok ( _) => info ! ( "Created file {}" , consts :: file_paths :: BANNED_PLAYERS ) ,
80
- Err ( e) => info ! (
80
+ match utils:: create_file ( Path :: new ( consts:: file_paths:: BANNED_PLAYERS ) , None ) {
81
+ Ok ( _) => ( ) ,
82
+ Err ( e) => error ! (
81
83
"Failed to create the file {} as error:{}" ,
82
84
consts:: file_paths:: BANNED_PLAYERS ,
83
85
e
84
86
) ,
85
87
}
86
- match utils:: create_file_nn ( Path :: new ( consts:: file_paths:: OPERATORS ) ) {
87
- Ok ( _) => info ! ( "Created file {}" , consts :: file_paths :: OPERATORS ) ,
88
- Err ( e) => info ! (
88
+ match utils:: create_file ( Path :: new ( consts:: file_paths:: OPERATORS ) , None ) {
89
+ Ok ( _) => ( ) ,
90
+ Err ( e) => error ! (
89
91
"Failed to create the file {} as error:{}" ,
90
92
consts:: file_paths:: OPERATORS ,
91
93
e
92
94
) ,
93
95
}
94
- match utils:: create_file_nn ( Path :: new ( consts:: file_paths:: SESSION ) ) {
95
- Ok ( _) => info ! ( "Created file {}" , consts :: file_paths :: SESSION ) ,
96
- Err ( e) => info ! (
96
+ match utils:: create_file ( Path :: new ( consts:: file_paths:: SESSION ) , None ) {
97
+ Ok ( _) => ( ) ,
98
+ Err ( e) => error ! (
97
99
"Failed to create the file {} as error:{}" ,
98
100
consts:: file_paths:: SESSION ,
99
101
e
100
102
) ,
101
103
}
102
- match utils:: create_file_nn ( Path :: new ( consts:: file_paths:: USERCACHE ) ) {
103
- Ok ( _) => info ! ( "Created file {}" , consts :: file_paths :: USERCACHE ) ,
104
- Err ( e) => info ! (
104
+ match utils:: create_file ( Path :: new ( consts:: file_paths:: USERCACHE ) , None ) {
105
+ Ok ( _) => ( ) ,
106
+ Err ( e) => error ! (
105
107
"Failed to create the file {} as error:{}" ,
106
108
consts:: file_paths:: USERCACHE ,
107
109
e
108
110
) ,
109
111
}
110
- match utils:: create_file_nn ( Path :: new ( consts:: file_paths:: WHITELIST ) ) {
111
- Ok ( _) => info ! ( "Created file {}" , consts :: file_paths :: WHITELIST ) ,
112
- Err ( e) => info ! (
112
+ match utils:: create_file ( Path :: new ( consts:: file_paths:: WHITELIST ) , None ) {
113
+ Ok ( _) => ( ) ,
114
+ Err ( e) => error ! (
113
115
"Failed to create the file {} as error:{}" ,
114
116
consts:: file_paths:: WHITELIST ,
115
117
e
116
118
) ,
117
119
}
120
+ match create_level_file ( ) {
121
+ Ok ( _) => ( ) ,
122
+ Err ( e) => error ! (
123
+ "Failed to create the file at {} as error {}" ,
124
+ consts:: file_paths:: LEVEL ,
125
+ e
126
+ ) ,
127
+ }
118
128
}
119
129
pub fn create_dirs ( ) {
120
130
match utils:: create_dir ( Path :: new ( consts:: directory_paths:: LOGS ) ) {
121
- Ok ( _) => info ! ( "Created dir{}" , consts :: directory_paths :: LOGS ) ,
131
+ Ok ( _) => ( ) ,
122
132
Err ( e) => info ! (
123
133
"Failed to create dir{} as error: {}" ,
124
134
consts:: directory_paths:: LOGS ,
@@ -127,7 +137,7 @@ pub fn create_dirs() {
127
137
}
128
138
129
139
match utils:: create_dir ( Path :: new ( consts:: directory_paths:: WORLDS_DIRECTORY ) ) {
130
- Ok ( _) => info ! ( "No existing world data, creating new world" ) ,
140
+ Ok ( _) => ( ) ,
131
141
Err ( e) => info ! (
132
142
"Failed to create dir{} as error: {}" ,
133
143
consts:: directory_paths:: WORLDS_DIRECTORY ,
@@ -136,7 +146,7 @@ pub fn create_dirs() {
136
146
}
137
147
138
148
match utils:: create_dir ( Path :: new ( consts:: directory_paths:: OVERWORLD ) ) {
139
- Ok ( _) => info ! ( "Created dir{}" , consts :: directory_paths :: OVERWORLD ) ,
149
+ Ok ( _) => ( ) ,
140
150
Err ( e) => info ! (
141
151
"Failed to create dir{} as error: {}" ,
142
152
consts:: directory_paths:: OVERWORLD ,
@@ -145,7 +155,7 @@ pub fn create_dirs() {
145
155
}
146
156
147
157
match utils:: create_dir ( Path :: new ( consts:: directory_paths:: THE_END ) ) {
148
- Ok ( _) => info ! ( "Created dir{}" , consts :: directory_paths :: THE_END ) ,
158
+ Ok ( _) => ( ) ,
149
159
Err ( e) => info ! (
150
160
"Failed to create dir{} as error: {}" ,
151
161
consts:: directory_paths:: THE_END ,
@@ -154,7 +164,7 @@ pub fn create_dirs() {
154
164
}
155
165
156
166
match utils:: create_dir ( Path :: new ( consts:: directory_paths:: NETHER ) ) {
157
- Ok ( _) => info ! ( "Created dir{}" , consts :: directory_paths :: NETHER ) ,
167
+ Ok ( _) => ( ) ,
158
168
Err ( e) => info ! (
159
169
"Failed to create dir{} as error: {}" ,
160
170
consts:: directory_paths:: NETHER ,
@@ -258,6 +268,7 @@ pub fn clean_files() -> Result<(), std::io::Error> {
258
268
consts:: file_paths:: SESSION ,
259
269
consts:: file_paths:: USERCACHE ,
260
270
consts:: file_paths:: WHITELIST ,
271
+ consts:: file_paths:: LEVEL ,
261
272
] ;
262
273
263
274
// Delete files using the `remove_file` helper function
@@ -276,9 +287,52 @@ pub fn clean_files() -> Result<(), std::io::Error> {
276
287
277
288
// Delete directories using the `remove_dir` helper function
278
289
for dir in & directories {
279
- remove_dir ( dir) ?;
290
+ remove_dir_all ( dir) ?;
280
291
}
281
-
282
- info ! ( "Files cleaned successfully before starting the server." ) ;
292
+ let info = "[Info]" . green ( ) ;
293
+ println ! (
294
+ "{} Files cleaned successfully before starting the server." ,
295
+ info
296
+ ) ;
283
297
gracefully_exit ( crate :: ExitCode :: Success ) ;
284
298
}
299
+ fn create_level_file ( ) -> Result < ( ) , std:: io:: Error > {
300
+ if Path :: new ( consts:: file_paths:: LEVEL ) . exists ( ) {
301
+ info ! ( "level.dat file already exist, not altering it" ) ;
302
+ return Ok ( ( ) ) ;
303
+ }
304
+ let server_version = VersionInfo {
305
+ id : 4440 ,
306
+ snapshot : false ,
307
+ series : "main" . into ( ) ,
308
+ name : consts:: minecraft:: VERSION . into ( ) ,
309
+ } ;
310
+ let data = LevelDat {
311
+ version : server_version,
312
+ difficulty : match config:: Settings :: new ( ) . difficulty {
313
+ Difficulty :: Easy => 0 ,
314
+ Difficulty :: Normal => 1 ,
315
+ Difficulty :: Hard => 2 ,
316
+ } ,
317
+ game_type : match config:: Settings :: new ( ) . gamemode {
318
+ Gamemode :: Survival => 0 ,
319
+ Gamemode :: Creative => 1 ,
320
+ Gamemode :: Spectator => 3 ,
321
+ Gamemode :: Adventure => 2 ,
322
+ } ,
323
+
324
+ hardcore : config:: Settings :: new ( ) . hardcore ,
325
+ level_name : match config:: Settings :: new ( ) . level_name {
326
+ Some ( words) => words,
327
+ _ => "Overworld" . into ( ) ,
328
+ } ,
329
+
330
+ ..Default :: default ( )
331
+ } ;
332
+
333
+ let result = create_nbt ( & data, consts:: file_paths:: LEVEL ) ;
334
+ if result. is_ok ( ) {
335
+ info ! ( "File created at {}" , consts:: file_paths:: LEVEL )
336
+ }
337
+ result
338
+ }
0 commit comments