From 39619cab2f0de1343cbcbdc335abb36e4a61d20d Mon Sep 17 00:00:00 2001 From: oqammx86 <84847714+oq-x@users.noreply.github.com> Date: Fri, 29 Sep 2023 03:17:25 +0300 Subject: [PATCH] Change config --- config/config.go | 75 +++++++++++++++++++++++++++++++++++------ main.go | 4 +-- server/server.go | 7 +++- server/server_config.go | 60 +++------------------------------ 4 files changed, 77 insertions(+), 69 deletions(-) diff --git a/config/config.go b/config/config.go index cbac5bb..62c1ecd 100644 --- a/config/config.go +++ b/config/config.go @@ -5,11 +5,66 @@ import ( "io/fs" "os" - "github.com/dynamitemc/dynamite/server" "github.com/pelletier/go-toml/v2" ) -func LoadConfig(path string, data *server.ServerConfig) { +type Tablist struct { + Header []string `toml:"header"` + Footer []string `toml:"footer"` +} + +type Web struct { + ServerIP string `toml:"server_ip"` + ServerPort int `toml:"server_port"` + Password string `toml:"password"` + Enable bool `toml:"enable"` +} + +type Messages struct { + NotInWhitelist string `toml:"not_in_whitelist"` + Banned string `toml:"banned"` + ServerFull string `toml:"server_full"` + AlreadyPlaying string `toml:"already_playing"` + PlayerJoin string `toml:"player_join"` + PlayerLeave string `toml:"player_leave"` + UnknownCommand string `toml:"unknown_command"` + ProtocolNew string `toml:"protocol_new"` + ProtocolOld string `toml:"protocol_old"` + InsufficientPermissions string `toml:"insufficient_permissions"` + ReloadComplete string `toml:"reload_complete"` + ServerClosed string `toml:"server_closed"` + OnlineMode string `toml:"online_mode"` +} + +type Chat struct { + Format string `toml:"format"` + Colors bool `toml:"colors"` + Enable bool `toml:"enable"` +} + +type Whitelist struct { + Enforce bool `toml:"enforce"` + Enable bool `toml:"enable"` +} + +type ServerConfig struct { + ServerIP string `toml:"server_ip"` + ServerPort int `toml:"server_port"` + ViewDistance int `toml:"view_distance"` + SimulationDistance int `toml:"simulation_distance"` + MOTD string `toml:"motd"` + Whitelist Whitelist `toml:"whitelist"` + Web Web `toml:"web"` + Gamemode string `toml:"gamemode"` + Hardcore bool `toml:"hardcore"` + MaxPlayers int `toml:"max_players"` + Online bool `toml:"online_mode"` + Tablist Tablist `toml:"tablist"` + Chat Chat `toml:"chat"` + Messages Messages `toml:"messages"` +} + +func LoadConfig(path string, data *ServerConfig) { file, err := os.Open(path) if errors.Is(err, fs.ErrNotExist) { config := defaultConfig() @@ -21,7 +76,7 @@ func LoadConfig(path string, data *server.ServerConfig) { } } -func CreateConfig(path string, config server.ServerConfig) error { +func CreateConfig(path string, config ServerConfig) error { file, err := os.Create(path) if err != nil { return err @@ -31,22 +86,22 @@ func CreateConfig(path string, config server.ServerConfig) error { return encoder.Encode(config) } -func defaultConfig() server.ServerConfig { - return server.ServerConfig{ +func defaultConfig() ServerConfig { + return ServerConfig{ ServerIP: "0.0.0.0", ServerPort: 25565, ViewDistance: 10, SimulationDistance: 10, MOTD: "A DynamiteMC Minecraft server!", Online: true, - Whitelist: server.Whitelist{ + Whitelist: Whitelist{ Enforce: false, Enable: false, }, Gamemode: "survival", Hardcore: false, MaxPlayers: 20, - Messages: server.Messages{ + Messages: Messages{ NotInWhitelist: "You are not whitelisted.", Banned: "You are banned from this server.", ServerFull: "The server is full.", @@ -61,17 +116,17 @@ func defaultConfig() server.ServerConfig { ServerClosed: "Server closed.", OnlineMode: "The server is in online mode.", }, - Web: server.Web{ + Web: Web{ ServerIP: "0.0.0.0", ServerPort: 8080, Password: "ChangeMe", Enable: false, }, - Tablist: server.Tablist{ + Tablist: Tablist{ Header: []string{}, Footer: []string{}, }, - Chat: server.Chat{ + Chat: Chat{ Colors: false, Format: "<%player_prefix%%player%> %message%", Enable: true, diff --git a/main.go b/main.go index 24f2f38..528fd66 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func start(cfg server.ServerConfig) { func main() { log.Info("Starting Dynamite 1.20.1 Server") - var cfg server.ServerConfig + var cfg config.ServerConfig config.LoadConfig("config.toml", &cfg) log.Debug("Loaded config") @@ -58,5 +58,5 @@ func main() { log.Warn("Remove the -nogui argument to load the web panel") } } - start(cfg) + start(server.ServerConfig(cfg)) } diff --git a/server/server.go b/server/server.go index 30c1659..fa45681 100644 --- a/server/server.go +++ b/server/server.go @@ -13,6 +13,7 @@ import ( "github.com/aimjel/minecraft" //"github.com/dynamitemc/dynamite/web" + "github.com/dynamitemc/dynamite/config" "github.com/dynamitemc/dynamite/logger" "github.com/dynamitemc/dynamite/server/commands" "github.com/dynamitemc/dynamite/server/player" @@ -21,7 +22,7 @@ import ( ) type Server struct { - Config *ServerConfig + Config *config.ServerConfig Logger logger.Logger CommandGraph *commands.Graph @@ -145,6 +146,10 @@ func (srv *Server) Reload() error { *addresses[i] = u } + + // load config + config.LoadConfig("config.toml", srv.Config) + for _, p := range srv.Players { p.SendCommands(srv.CommandGraph) } diff --git a/server/server_config.go b/server/server_config.go index 655b2a2..2113b08 100644 --- a/server/server_config.go +++ b/server/server_config.go @@ -6,6 +6,7 @@ import ( "sync" "github.com/aimjel/minecraft" + "github.com/dynamitemc/dynamite/config" "github.com/dynamitemc/dynamite/logger" "github.com/dynamitemc/dynamite/server/commands" "github.com/dynamitemc/dynamite/server/plugins" @@ -13,61 +14,7 @@ import ( "github.com/dynamitemc/dynamite/util" ) -type Tablist struct { - Header []string `toml:"header"` - Footer []string `toml:"footer"` -} - -type Web struct { - ServerIP string `toml:"server_ip"` - ServerPort int `toml:"server_port"` - Password string `toml:"password"` - Enable bool `toml:"enable"` -} - -type Messages struct { - NotInWhitelist string `toml:"not_in_whitelist"` - Banned string `toml:"banned"` - ServerFull string `toml:"server_full"` - AlreadyPlaying string `toml:"already_playing"` - PlayerJoin string `toml:"player_join"` - PlayerLeave string `toml:"player_leave"` - UnknownCommand string `toml:"unknown_command"` - ProtocolNew string `toml:"protocol_new"` - ProtocolOld string `toml:"protocol_old"` - InsufficientPermissions string `toml:"insufficient_permissions"` - ReloadComplete string `toml:"reload_complete"` - ServerClosed string `toml:"server_closed"` - OnlineMode string `toml:"online_mode"` -} - -type Chat struct { - Format string `toml:"format"` - Colors bool `toml:"colors"` - Enable bool `toml:"enable"` -} - -type Whitelist struct { - Enforce bool `toml:"enforce"` - Enable bool `toml:"enable"` -} - -type ServerConfig struct { - ServerIP string `toml:"server_ip"` - ServerPort int `toml:"server_port"` - ViewDistance int `toml:"view_distance"` - SimulationDistance int `toml:"simulation_distance"` - MOTD string `toml:"motd"` - Whitelist Whitelist `toml:"whitelist"` - Web Web `toml:"web"` - Gamemode string `toml:"gamemode"` - Hardcore bool `toml:"hardcore"` - MaxPlayers int `toml:"max_players"` - Online bool `toml:"online_mode"` - Tablist Tablist `toml:"tablist"` - Chat Chat `toml:"chat"` - Messages Messages `toml:"messages"` -} +type ServerConfig config.ServerConfig func (cfg *ServerConfig) Listen(address string, logger logger.Logger, commandGraph *commands.Graph) (*Server, error) { lnCfg := minecraft.ListenConfig{ @@ -94,8 +41,9 @@ func (cfg *ServerConfig) Listen(address string, logger logger.Logger, commandGra logger.Error("Failed to load world: %s", err) os.Exit(1) } + c := config.ServerConfig(*cfg) srv := &Server{ - Config: cfg, + Config: &c, listener: ln, Logger: logger, world: w,