Skip to content

Commit

Permalink
more blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Nov 14, 2023
1 parent 9064cba commit 74733e6
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 20 deletions.
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ var cfg config.Config
var prof bool

func main() {
if util.HasArg("-gencfg") {
log.Info("Generating config file")
f, _ := os.OpenFile("config.toml", os.O_RDWR|os.O_CREATE, 0666)
toml.NewEncoder(f).Encode(config.DefaultConfig)
f.Close()
return
}
server.OldState, _ = term.MakeRaw(int(os.Stdin.Fd()))

log.Info("Starting Dynamite 1.20.1 server")
Expand Down
34 changes: 34 additions & 0 deletions server/block/acacia_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package block

import "github.com/dynamitemc/dynamite/server/world/chunk"

type AcaciaLog struct {
Stripped bool
Axis Axis
}

func (d AcaciaLog) EncodedName() string {
if d.Stripped {
return "minecraft:stripped_acacia_log"
}
return "minecraft:acacia_log"
}

func (g AcaciaLog) Properties() map[string]string {
return map[string]string{
"axis": g.Axis,
}
}

func (AcaciaLog) New(n string, p map[string]string) chunk.Block {
return AcaciaLog{
Stripped: n == "minecraft:stripped_acacia_log",
Axis: p["axis"],
}
}

func (AcaciaLog) BreakInfo() BreakInfo {
return BreakInfo{
Hardness: 2,
}
}
2 changes: 1 addition & 1 deletion server/block/air.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func (Air) Properties() map[string]string {
return nil
}

func (a Air) New(map[string]string) chunk.Block {
func (a Air) New(string, map[string]string) chunk.Block {
return a
}
2 changes: 1 addition & 1 deletion server/block/bedrock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func (g Bedrock) Properties() map[string]string {
return nil
}

func (d Bedrock) New(p map[string]string) chunk.Block {
func (d Bedrock) New(string, map[string]string) chunk.Block {
return d
}
34 changes: 34 additions & 0 deletions server/block/birch_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package block

import "github.com/dynamitemc/dynamite/server/world/chunk"

type BirchLog struct {
Stripped bool
Axis Axis
}

func (d BirchLog) EncodedName() string {
if d.Stripped {
return "minecraft:stripped_birch_log"
}
return "minecraft:birch_log"
}

func (g BirchLog) Properties() map[string]string {
return map[string]string{
"axis": g.Axis,
}
}

func (BirchLog) New(n string, p map[string]string) chunk.Block {
return BirchLog{
Stripped: n == "minecraft:stripped_birch_log",
Axis: p["axis"],
}
}

func (BirchLog) BreakInfo() BreakInfo {
return BreakInfo{
Hardness: 2,
}
}
18 changes: 18 additions & 0 deletions server/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,27 @@ func init() {
chunk.RegisterBlock(GrassBlock{})
chunk.RegisterBlock(Snow{})
chunk.RegisterBlock(Bedrock{})
chunk.RegisterBlock(AcaciaLog{})
chunk.RegisterBlock(AcaciaLog{Stripped: true})

chunk.RegisterBlock(BirchLog{})
chunk.RegisterBlock(BirchLog{Stripped: true})

chunk.RegisterBlock(CherryLog{})
chunk.RegisterBlock(CherryLog{Stripped: true})

chunk.RegisterBlock(DarkOakLog{})
chunk.RegisterBlock(DarkOakLog{Stripped: true})

chunk.RegisterBlock(OakLog{})
chunk.RegisterBlock(OakLog{Stripped: true})

chunk.RegisterBlock(SpruceLog{})
chunk.RegisterBlock(SpruceLog{Stripped: true})
}

type BreakInfo struct {
Hardness float64
Unbreakable bool
}

Expand Down
34 changes: 34 additions & 0 deletions server/block/cherry_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package block

import "github.com/dynamitemc/dynamite/server/world/chunk"

type CherryLog struct {
Stripped bool
Axis Axis
}

func (d CherryLog) EncodedName() string {
if d.Stripped {
return "minecraft:stripped_cherry_log"
}
return "minecraft:cherry_log"
}

func (g CherryLog) Properties() map[string]string {
return map[string]string{
"axis": g.Axis,
}
}

func (CherryLog) New(n string, p map[string]string) chunk.Block {
return CherryLog{
Stripped: n == "minecraft:stripped_cherry_log",
Axis: p["axis"],
}
}

func (CherryLog) BreakInfo() BreakInfo {
return BreakInfo{
Hardness: 2,
}
}
44 changes: 44 additions & 0 deletions server/block/dark_oak_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package block

import (
"github.com/dynamitemc/dynamite/server/world/chunk"
)

type Axis = string

const (
AxisX Axis = "x"
AxisY Axis = "y"
AxisZ Axis = "z"
)

type DarkOakLog struct {
Stripped bool
Axis Axis
}

func (d DarkOakLog) EncodedName() string {
if d.Stripped {
return "minecraft:stripped_dark_oak_log"
}
return "minecraft:dark_oak_log"
}

func (g DarkOakLog) Properties() map[string]string {
return map[string]string{
"axis": g.Axis,
}
}

func (DarkOakLog) New(n string, p map[string]string) chunk.Block {
return DarkOakLog{
Stripped: n == "minecraft:stripped_dark_oak_log",
Axis: p["axis"],
}
}

func (DarkOakLog) BreakInfo() BreakInfo {
return BreakInfo{
Hardness: 2,
}
}
2 changes: 1 addition & 1 deletion server/block/dirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func (g Dirt) Properties() map[string]string {
return nil
}

func (d Dirt) New(p map[string]string) chunk.Block {
func (d Dirt) New(string, map[string]string) chunk.Block {
return d
}
5 changes: 1 addition & 4 deletions server/block/grass_block.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package block

import (
"fmt"

"github.com/dynamitemc/dynamite/server/block/pos"
"github.com/dynamitemc/dynamite/server/world"
"github.com/dynamitemc/dynamite/server/world/chunk"
Expand All @@ -22,7 +20,7 @@ func (g GrassBlock) Properties() map[string]string {
}
}

func (GrassBlock) New(p map[string]string) chunk.Block {
func (GrassBlock) New(_ string, p map[string]string) chunk.Block {
var a GrassBlock
if p["snowy"] == "true" {
a.Snowy = true
Expand All @@ -33,7 +31,6 @@ func (GrassBlock) New(p map[string]string) chunk.Block {
func (g GrassBlock) Tick(p pos.BlockPosition, d *world.Dimension, _ uint) chunk.Block {
if g.Snowy {
b := d.Block(p.X(), p.Y()+1, p.Z())
fmt.Printf("%s\n\r", b.EncodedName())
if _, ok := b.(Snow); !ok {
g.Snowy = false
}
Expand Down
34 changes: 34 additions & 0 deletions server/block/oak_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package block

import "github.com/dynamitemc/dynamite/server/world/chunk"

type OakLog struct {
Stripped bool
Axis Axis
}

func (d OakLog) EncodedName() string {
if d.Stripped {
return "minecraft:stripped_oak_log"
}
return "minecraft:oak_log"
}

func (g OakLog) Properties() map[string]string {
return map[string]string{
"axis": g.Axis,
}
}

func (OakLog) New(n string, p map[string]string) chunk.Block {
return OakLog{
Stripped: n == "minecraft:stripped_oak_log",
Axis: p["axis"],
}
}

func (OakLog) BreakInfo() BreakInfo {
return BreakInfo{
Hardness: 2,
}
}
2 changes: 1 addition & 1 deletion server/block/snow.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s Snow) Properties() map[string]string {
}
}

func (Snow) New(m map[string]string) chunk.Block {
func (Snow) New(_ string, m map[string]string) chunk.Block {
l, _ := strconv.Atoi(m["layers"])
if l == 0 {
l = 1
Expand Down
34 changes: 34 additions & 0 deletions server/block/spruce_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package block

import "github.com/dynamitemc/dynamite/server/world/chunk"

type SpruceLog struct {
Stripped bool
Axis Axis
}

func (d SpruceLog) EncodedName() string {
if d.Stripped {
return "minecraft:stripped_spruce_log"
}
return "minecraft:spruce_log"
}

func (g SpruceLog) Properties() map[string]string {
return map[string]string{
"axis": g.Axis,
}
}

func (SpruceLog) New(n string, p map[string]string) chunk.Block {
return SpruceLog{
Stripped: n == "minecraft:stripped_spruce_log",
Axis: p["axis"],
}
}

func (SpruceLog) BreakInfo() BreakInfo {
return BreakInfo{
Hardness: 2,
}
}
6 changes: 3 additions & 3 deletions server/world/chunk/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package chunk
type Block interface {
EncodedName() string

New(map[string]string) Block
New(string, map[string]string) Block

Properties() map[string]string
}
Expand All @@ -17,8 +17,8 @@ func (u UnknownBlock) EncodedName() string {
return u.encodedName
}

func (u UnknownBlock) New(m map[string]string) Block {
return UnknownBlock{encodedName: u.encodedName, properties: m}
func (u UnknownBlock) New(n string, m map[string]string) Block {
return UnknownBlock{encodedName: n, properties: m}
}

func (u UnknownBlock) Properties() map[string]string {
Expand Down
24 changes: 18 additions & 6 deletions server/world/chunk/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ var nbtBytes []byte
type blockInfo struct {
Properties map[string][]string `json:"properties"`

States []struct {
Id int `json:"id"`
Default bool `json:"default"`
Properties map[string]string
}
States []blockState
}

type blockState struct {
Id int `json:"id"`
Default bool `json:"default"`
Properties map[string]string
}

var blocks map[string]blockInfo
Expand Down Expand Up @@ -55,7 +57,17 @@ func DefaultBlock(name string) Block {
return nil
}
block := blocks[b.EncodedName()]
return b.New(block.States[0].Properties)
d, _ := defaultState(block.States)
return b.New(name, d.Properties)
}

func defaultState(s []blockState) (_ blockState, ok bool) {
for _, i := range s {
if i.Default {
return i, ok
}
}
return
}

func GetBlockId(b Block) (int, bool) {
Expand Down
2 changes: 1 addition & 1 deletion server/world/chunk/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newSection(data []int64, blocks []blockEntry, bLight, sLight []int8) (s *se
for _, entry := range blocks {
b := GetBlock(entry.Name)
if entry.Properties != nil {
b = b.New(entry.Properties)
b = b.New(entry.Name, entry.Properties)
}

id, ok := GetBlockId(b)
Expand Down
Loading

0 comments on commit 74733e6

Please sign in to comment.