Skip to content

Commit

Permalink
Change config
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Sep 29, 2023
1 parent 6380088 commit 39619ca
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 69 deletions.
75 changes: 65 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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.",
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -58,5 +58,5 @@ func main() {
log.Warn("Remove the -nogui argument to load the web panel")
}
}
start(cfg)
start(server.ServerConfig(cfg))
}
7 changes: 6 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,7 +22,7 @@ import (
)

type Server struct {
Config *ServerConfig
Config *config.ServerConfig
Logger logger.Logger
CommandGraph *commands.Graph

Expand Down Expand Up @@ -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)
}
Expand Down
60 changes: 4 additions & 56 deletions server/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,15 @@ 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"
"github.com/dynamitemc/dynamite/server/world"
"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{
Expand All @@ -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,
Expand Down

0 comments on commit 39619ca

Please sign in to comment.