Skip to content

Commit

Permalink
Some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Oct 6, 2023
1 parent 35b6663 commit 7f0a1ed
Show file tree
Hide file tree
Showing 15 changed files with 3,967 additions and 20 deletions.
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type ServerConfig struct {
ViewDistance int `toml:"view_distance"`
SimulationDistance int `toml:"simulation_distance"`
Superflat bool `toml:"superflat"`
CacheSpawnChunks bool `toml:"cache_spawn_chunks"`
MOTD string `toml:"motd"`
Whitelist Whitelist `toml:"whitelist"`
Web Web `toml:"web"`
Expand Down Expand Up @@ -101,9 +102,10 @@ func defaultConfig() ServerConfig {
Enforce: false,
Enable: false,
},
Gamemode: "survival",
Hardcore: false,
MaxPlayers: 20,
Gamemode: "survival",
CacheSpawnChunks: true,
Hardcore: false,
MaxPlayers: 20,
Messages: Messages{
NotInWhitelist: "You are not whitelisted.",
Banned: "You are banned from this server.",
Expand Down
1 change: 1 addition & 0 deletions core_commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ var Commands = &commands.Graph{
reload_cmd,
gamemode_cmd,
restart_cmd,
ram_cmd,
},
}
22 changes: 22 additions & 0 deletions core_commands/ram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core_commands

import (
"fmt"
"runtime"

"github.com/dynamitemc/dynamite/server/commands"
)

var ram_cmd = &commands.Command{
Name: "ram",
Aliases: []string{"mem"},
Execute: func(ctx commands.CommandContext) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
ctx.Reply(fmt.Sprintf("Allocated: %v MiB, Total Allocated: %v MiB, Heap in Use: %v MiB", bToMb(m.Alloc), bToMb(m.TotalAlloc), bToMb(m.HeapInuse)))
},
}

func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
3 changes: 2 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"fmt"
"os"
"time"

"github.com/dynamitemc/dynamite/util"
Expand Down Expand Up @@ -53,7 +54,7 @@ func (logger *Logger) Error(format string, a ...interface{}) {
time := getDateString()
str := fmt.Sprintf(format, a...)
logger.append(fmt.Sprintf("[%s ERROR]: %s", time, str))
fmt.Printf("[%s %s]: %s\n", time, red("ERROR"), str)
fmt.Fprintf(os.Stderr, "[%s %s]: %s\n", time, red("ERROR"), str)
}

func (logger *Logger) Warn(format string, a ...interface{}) {
Expand Down
7 changes: 4 additions & 3 deletions server/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (srv *Server) ValidateConn(conn *minecraft.Conn) bool {
}

if srv.Config.Whitelist.Enable {
if !srv.IsWhitelisted(conn.Info.Name) {
if !srv.IsWhitelisted(conn.Info.UUID) {
reason = srv.Config.Messages.NotInWhitelist
}
}
Expand Down Expand Up @@ -98,9 +98,10 @@ func (srv *Server) IsIPBanned(ip string) bool {
return false
}

func (srv *Server) IsWhitelisted(name string) bool {
func (srv *Server) IsWhitelisted(uuid [16]byte) bool {
suuid := hex.EncodeToString(uuid[:])
for _, u := range srv.WhitelistedPlayers {
if u.Name == name {
if u.UUID == suuid {
return true
}
}
Expand Down
35 changes: 35 additions & 0 deletions server/item/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package item

import (
_ "embed"
"encoding/json"
)

//go:embed items.json
var it []byte

type Item struct {
ProtocolID int32 `json:"protocol_id"`
}

var items map[string]Item

func loadItems() {
var itemData struct {
Item struct {
Default string `json:"default"`
Entries map[string]Item `json:"entries"`
ProtocolID int32 `json:"protocol_id"`
} `json:"minecraft:item"`
}
json.Unmarshal(it, &itemData)
items = itemData.Item.Entries
}

func GetItem(name string) (item Item, ok bool) {
if items == nil {
loadItems()
}
it, ok := items[name]
return it, ok
}
Loading

0 comments on commit 7f0a1ed

Please sign in to comment.