Skip to content

Commit 7c7afb1

Browse files
committed
Merge remote-tracking branch 'origin/dev'
2 parents 9a5883d + febdf22 commit 7c7afb1

File tree

24 files changed

+140
-178
lines changed

24 files changed

+140
-178
lines changed

Cargo.toml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,28 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
dot-properties = "0.2.0"
8-
colored = "2.1.0"
9-
integer-encoding = "4.0.2"
7+
colored = "3.0.0"
108
tokio = { version = "1.39.3", features = ["full"] }
119
thiserror = "2.0.2"
12-
rand = "0.8.5"
1310
log = "0.4.22"
1411
env_logger = "0.11.5"
1512
chrono = "0.4.38"
1613
ctrlc = "3.4.5"
1714
once_cell = "1.19.0"
18-
tempfile = "3.12.0"
1915
serde = { version = "1.0", features = ["derive"] }
2016
serde_json = "1.0.127"
2117
reqwest = { version = "0.12.7", features = ["json", "rustls-tls"] }
2218
clap = { version = "4.5.17", features = ["derive"] }
23-
sha2 = "0.10.8"
24-
byteorder = "1.5.0"
2519
bytes = "1.9.0"
2620
image = "0.25.5"
2721
base64 = "0.22.1"
2822
dashmap = "6.1.0"
29-
30-
23+
cactus_world = { git = "https://github.com/Cactus-minecraft-server/World.git", package = "world", branch = "main" }
3124
[profile.release]
32-
opt-level = 3 # optimiosation level 3 is the best
25+
opt-level = 3
3326
debug = false
34-
split-debuginfo = 'unified'
3527
lto = true
3628

3729
[dev-dependencies]
38-
criterion = "0.4"
39-
40-
41-
30+
rand = "0.9.2"
31+
tempfile = "3.21.0"

src/args/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::shutdown::ExitCode;
12
use crate::{fs_manager, gracefully_exit};
23
use clap::Parser;
34
use log::{error, info};
4-
use crate::shutdown::ExitCode;
55

66
#[derive(Parser)]
77
#[command(name = "CactusMC")]

src/commands/command_line.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,3 @@ pub async fn handle_input() -> ! {
6262
}
6363
}
6464
}
65-

src/consts/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub mod file_paths {
6767
pub const USERCACHE: &str = "usercache.json";
6868
pub const SESSION: &str = "session.lock";
6969
pub const SERVER_ICON: &str = "server-icon.png";
70+
pub const LEVEL: &str = "world/level.dat";
7071
}
7172

7273
pub mod directory_paths {

src/file_folder_parser/mod.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/fs_manager/mod.rs

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use std::fs::{self, File, OpenOptions};
1+
use std::fs::{self, remove_dir_all, File, OpenOptions};
22
use std::io::{self, BufRead, Seek, SeekFrom};
33
use std::path::Path;
44
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};
68
use colored::Colorize;
79
use log::{error, info, warn};
810
use serde::{Deserialize, Serialize};
@@ -15,7 +17,7 @@ pub fn init() -> std::io::Result<()> {
1517
create_server_properties()
1618
}
1719

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.
1921
fn eula() -> io::Result<()> {
2022
let path = Path::new(consts::file_paths::EULA);
2123
if !path.exists() {
@@ -39,15 +41,15 @@ fn create_server_properties() -> io::Result<()> {
3941
let path = Path::new(consts::file_paths::PROPERTIES);
4042
let content = consts::file_contents::server_properties();
4143

42-
utils::create_file(path, &content)
44+
utils::create_file(path, Some(&content))
4345
}
4446

4547
/// Creates the 'eula.txt' file if it does not already exist.
4648
fn create_eula() -> io::Result<()> {
4749
let path = Path::new(consts::file_paths::EULA);
4850
let content = consts::file_contents::eula();
4951

50-
utils::create_file(path, &content)
52+
utils::create_file(path, Some(&content))
5153
}
5254

5355
/// Check if the 'eula.txt' has been agreed to.
@@ -67,58 +69,66 @@ fn check_eula() -> io::Result<bool> {
6769
}
6870

6971
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!(
7375
"Failed to create the file {} as error:{}",
7476
consts::file_paths::BANNED_IP,
7577
e
7678
),
7779
}
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!(
8183
"Failed to create the file {} as error:{}",
8284
consts::file_paths::BANNED_PLAYERS,
8385
e
8486
),
8587
}
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!(
8991
"Failed to create the file {} as error:{}",
9092
consts::file_paths::OPERATORS,
9193
e
9294
),
9395
}
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!(
9799
"Failed to create the file {} as error:{}",
98100
consts::file_paths::SESSION,
99101
e
100102
),
101103
}
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!(
105107
"Failed to create the file {} as error:{}",
106108
consts::file_paths::USERCACHE,
107109
e
108110
),
109111
}
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!(
113115
"Failed to create the file {} as error:{}",
114116
consts::file_paths::WHITELIST,
115117
e
116118
),
117119
}
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+
}
118128
}
119129
pub fn create_dirs() {
120130
match utils::create_dir(Path::new(consts::directory_paths::LOGS)) {
121-
Ok(_) => info!("Created dir{}", consts::directory_paths::LOGS),
131+
Ok(_) => (),
122132
Err(e) => info!(
123133
"Failed to create dir{} as error: {}",
124134
consts::directory_paths::LOGS,
@@ -127,7 +137,7 @@ pub fn create_dirs() {
127137
}
128138

129139
match utils::create_dir(Path::new(consts::directory_paths::WORLDS_DIRECTORY)) {
130-
Ok(_) => info!("No existing world data, creating new world"),
140+
Ok(_) => (),
131141
Err(e) => info!(
132142
"Failed to create dir{} as error: {}",
133143
consts::directory_paths::WORLDS_DIRECTORY,
@@ -136,7 +146,7 @@ pub fn create_dirs() {
136146
}
137147

138148
match utils::create_dir(Path::new(consts::directory_paths::OVERWORLD)) {
139-
Ok(_) => info!("Created dir{}", consts::directory_paths::OVERWORLD),
149+
Ok(_) => (),
140150
Err(e) => info!(
141151
"Failed to create dir{} as error: {}",
142152
consts::directory_paths::OVERWORLD,
@@ -145,7 +155,7 @@ pub fn create_dirs() {
145155
}
146156

147157
match utils::create_dir(Path::new(consts::directory_paths::THE_END)) {
148-
Ok(_) => info!("Created dir{}", consts::directory_paths::THE_END),
158+
Ok(_) => (),
149159
Err(e) => info!(
150160
"Failed to create dir{} as error: {}",
151161
consts::directory_paths::THE_END,
@@ -154,7 +164,7 @@ pub fn create_dirs() {
154164
}
155165

156166
match utils::create_dir(Path::new(consts::directory_paths::NETHER)) {
157-
Ok(_) => info!("Created dir{}", consts::directory_paths::NETHER),
167+
Ok(_) => (),
158168
Err(e) => info!(
159169
"Failed to create dir{} as error: {}",
160170
consts::directory_paths::NETHER,
@@ -258,6 +268,7 @@ pub fn clean_files() -> Result<(), std::io::Error> {
258268
consts::file_paths::SESSION,
259269
consts::file_paths::USERCACHE,
260270
consts::file_paths::WHITELIST,
271+
consts::file_paths::LEVEL,
261272
];
262273

263274
// Delete files using the `remove_file` helper function
@@ -276,9 +287,52 @@ pub fn clean_files() -> Result<(), std::io::Error> {
276287

277288
// Delete directories using the `remove_dir` helper function
278289
for dir in &directories {
279-
remove_dir(dir)?;
290+
remove_dir_all(dir)?;
280291
}
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+
);
283297
gracefully_exit(crate::ExitCode::Success);
284298
}
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

Comments
 (0)