block

package
Version: v0.0.0-...-2a33acd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BoundingBox_NONE = BoundingBox{
	-1, -1, -1,
	-1, -1, -1,
}

This is a shorthand for BoundBox{-1, -1, -1, -1, -1, -1} (an empty bounding box).

Functions

func LoadBlocks

func LoadBlocks()

This loads all blocks in the game, using the registry from CreateRegistry(). Once called, you cannot change the blocks anymore.

func Validate

func Validate(v Version)

This reads from data/{v}.json, and then validates that against the registered blocks. Useful for implementing new versions. This migh panic, if the json file does not exist. If it can load the file, then it will print a lot of stuff to stdout.

Types

type BoundingBox

type BoundingBox struct {
	X1, Y1, Z1 float64
	X2, Y2, Z2 float64
}

This is the bounding box of the block. If all the values are 0, then this is a full block (same as 0, 0, 0, 1, 1, 1). If all values are -1, then this is an empty box. If any values are > 1 or < 0, and it's not an empty box, this is an invalid box.

type BurgerData

type BurgerData struct {
	// List of all blocks in the game
	Blocks struct {
		// Alphebetical list of blocks. Most useful option, as it gives all blockstate ids.
		BlockList map[string]struct {
			// Display name
			DisplayName string `json:"display_name"`
			// Block name
			Name string `json:"text_id"`
			// Hardness
			Hardness float32 `json:"hardness"`
			// Resistance
			Resistance float32 `json:"resistance"`
			// Min blockstate id
			MinState int32 `json:"min_state_id"`
			// Max blockstate id
			MaxState int32 `json:"max_state_id"`
			// max - min + 1
			NumStates int32 `json:"num_states"`
			// List of properties
			Properties []struct {
				// Property name
				Name string `json:"name"`
				// Property type. If it is int, then the
				// possible values are just 0-num_values.
				// If it is bool, then the possible values
				// are true, false. If it is anything else,
				// then the possible values are listed under
				// Values.
				Type string `json:"type"`
				// Number of possible values this property can have.
				NumValues int32 `json:"num_values"`
				// List of possible values. May be empty,
				// depending on Type.
				Values []string `json:"values"`
			} `json:"states"`
		} `json:"block"`
	} `json:"blocks"`
}

type ChunkPos

type ChunkPos struct {
	X, Z int32
}

This is a chunk position. It is used to refer to chunk columns within a world. It is also used as a key for chunks within a world

func (ChunkPos) ForEachColumn

func (c ChunkPos) ForEachColumn(f func(pos Pos) bool)

This calls f for every column within the chunk. The position passed to f has X and Z set, but Y is 0. If f returns false, then the loop stops.

type Color

type Color int
const (
	Color_AIR Color = iota
	Color_GRASS
	Color_SAND
	Color_WOOL
	Color_TNT
	Color_ICE
	Color_IRON
	Color_FOLIAGE
	Color_SNOW
	Color_CLAY
	Color_DIRT
	Color_STONE
	Color_WATER
	Color_WOOD
	Color_QUARTZ
	Color_ORANGE
	Color_MAGENTA
	Color_LIGHT_BLUE
	Color_YELLOW
	Color_LIME
	Color_PINK
	Color_GRAY
	Color_LIGHT_GRAY
	Color_CYAN
	Color_PURPLE
	Color_BLUE
	Color_BROWN
	Color_GREEN
	Color_RED
	Color_BLACK
	Color_GOLD
	Color_DIAMOND
	Color_LAPIS
	Color_EMERALD
	Color_OBSIDIAN
	Color_NETHERRACK
	Color_WHITE_TERRACOTTA
	Color_ORANGE_TERRACOTTA
	Color_MAGENTA_TERRACOTTA
	Color_LIGHT_BLUE_TERRACOTTA
	Color_YELLOW_TERRACOTTA
	Color_LIME_TERRACOTTA
	Color_PINK_TERRACOTTA
	Color_GRAY_TERRACOTTA
	Color_LIGHT_GRAY_TERRACOTTA
	Color_CYAN_TERRACOTTA
	Color_PURPLE_TERRACOTTA
	Color_BLUE_TERRACOTTA
	Color_BROWN_TERRACOTTA
	Color_GREEN_TERRACOTTA
	Color_RED_TERRACOTTA
	Color_BLACK_TERRACOTTA
	Color_CRIMSON_NYLIUM
	Color_CRIMSON_STEM
	Color_CRIMSON_HYPHAE
	Color_WARPED_NYLIUM
	Color_WARPED_STEM
	Color_WARPED_HYPHAE
	Color_WARPED_WART
)

type Face

type Face int
const (
	FACE_BOTTOM Face = iota
	FACE_TOP
	FACE_NORTH
	FACE_SOUTH
	FACE_WEST
	FACE_EAST
)

func FaceFromString

func FaceFromString(s string) Face

This gets the block face according to a string. Will return FACE_BOTTOM if the string is invalid.

func (Face) Invert

func (f Face) Invert() Face

The returns the opposite of the face. For example, bottom returns top. This returns the bottom face if the face is invalid.

func (Face) Left

func (f Face) Left() Face

This gets the face that is one to the left of the given face. For example, north returns west. Bottom and top return themselves.

func (Face) Offset

func (f Face) Offset() (int32, int32, int32)

This returns the block offset of the face. For example, the bottom face returns 0, -1, 0. This returns 0, 0, 0 if the face is invalid.

func (Face) Right

func (f Face) Right() Face

This gets the face that is one to the right of the given face. For example, north returns east. Bottom and top return themselves.

func (Face) String

func (f Face) String() string

This returns the string name of the face. This will return an empty string if invalid.

func (Face) Valid

func (f Face) Valid() bool

type Kind

type Kind struct {
	// contains filtered or unexported fields
}

singleton struct for each block type (not each block state)

func GetKind

func GetKind(name string) *Kind

Returns a kind from a name. Will return nil if the block does not exist. Will panic if LoadBlocks() has not been called yet. If you need the info about another block during load, call registry.Get().

func (*Kind) AllProperties

func (k *Kind) AllProperties() map[string]map[string]struct{}

This returns all of the possible properties for the type. This does not return a copy, so editing this map will break things in terrible and unexpected ways.

func (*Kind) DefaultProperties

func (k *Kind) DefaultProperties() map[string]string

This returns the default properties for the kind. This will copy all the properties, but because properties is usually a pretty short list, this is still fast.

func (*Kind) DefaultType

func (k *Kind) DefaultType() *Type

This returns the default type of the kind. If the kind is nil, this will return air.

func (*Kind) Name

func (k *Kind) Name() string

Returns the name of the kind. This will call the registry's Name() function, which should always return the same value.

func (*Kind) Type

func (k *Kind) Type(properties map[string]string) *Type

This returns the block type based on a list of properties If the passed map is incomplete, it will use default values for the remaining fields. If there is only one state, it will return that. This will never return nil.

func (*Kind) Types

func (k *Kind) Types() map[uint32]*Type

This returns a map of all state ids to types. This will copy the internal states map, so it is safe to edit. However, this function is decently slow, especially for blocks like redstone, which can have thousands of states.

type Pos

type Pos struct {
	X, Y, Z int32
}

Block position. This should be used any time a block position in a world is needed. This only stores X, Y, Z, so you will need something else if you want to store the world along with a position.

func PosFromLong

func PosFromLong(value uint64) Pos

Converts a long into 3 ints, according to minecraft's position type. This is in the order X, Z, then Y. For versions 1.14+.

func PosFromOldLong

func PosFromOldLong(value uint64) Pos

Converts a long into 3 ints, according to minecraft's old position type. This is in the order X, Y, then Z. For versions 1.8 - 1.13. NOTE: During server packet handling, all longs will be in the new format. The proxy translates them correctly. So, this function should only be used on the proxy.

func (Pos) Add

func (p Pos) Add(x, y, z int32) Pos

This adds the coorsponding x, y and z values to pos.X, pos.Y, and pos.Z. This new value is returned, and the original is not changed.

func (Pos) AddX

func (p Pos) AddX(amount int32) Pos

Adds returns the current position with pos.X increased by x. Does not modify the original pos.

func (Pos) AddY

func (p Pos) AddY(amount int32) Pos

Adds returns the current position with pos.Y increased by y. Does not modify the original pos.

func (Pos) AddZ

func (p Pos) AddZ(amount int32) Pos

Adds returns the current position with pos.Z increased by z. Does not modify the original pos.

func (Pos) AsLong

func (p Pos) AsLong() uint64

This returns a long which is used in packets, for versions 1.14+

func (Pos) AsOldLong

func (p Pos) AsOldLong() uint64

This returns a long which is used in packets, for versions 1.8-1.13

func (Pos) ChunkPos

func (p Pos) ChunkPos() ChunkPos

This returns the chunk postition that pos is in.

func (Pos) ChunkRelPos

func (p Pos) ChunkRelPos() Pos

This returns a new position, where the X and Z are relative to the chunk (between 0 and 15). Y is unchanged.

func (Pos) ChunkRelX

func (p Pos) ChunkRelX() int32

This returns the block position relative to the chunk it's in. This will always be between 0 and 15.

func (Pos) ChunkRelY

func (p Pos) ChunkRelY() int32

This returns the block position relative to the chunk it's in. This will always be between 0 and 15. NOTE: This function probably isn't very useful. It is only used internally when accessing individual chunk sections, which is all wrapped within world/chunk.Chunk. This function is not used in ChunkRelPos(), as typically this is not what you want.

func (Pos) ChunkRelZ

func (p Pos) ChunkRelZ() int32

This returns the block position relative to the chunk it's in. This will always be between 0 and 15.

func (Pos) ChunkX

func (p Pos) ChunkX() int32

This returns the chunk that this block position is in. It just divides X by 16, but rounds down.

func (Pos) ChunkY

func (p Pos) ChunkY() int32

This returns the chunk that this block position is in. It just divides Y by 16, but rounds down. Since chunks are usually accessed in columns, this function probably won't be very useful. ChunkPos() does not call this function, and you probably shouldn't either. It is only used internally within world/chunk.Chunk, when accessing chunk sections.

func (Pos) ChunkZ

func (p Pos) ChunkZ() int32

This returns the chunk that this block position is in. It just divides Z by 16, but rounds down.

func (Pos) Distance

func (p Pos) Distance(o Pos) float32

Returns the distance between the two block positions.

func (Pos) InChunk

func (p Pos) InChunk(c ChunkPos) bool

This returns true if the position is within the chunk column.

func (Pos) OffsetFace

func (p Pos) OffsetFace(face Face) Pos

This returns a new block position, which is offset one block in the direction of face.

func (Pos) WithX

func (p Pos) WithX(x int32) Pos

This copies p, sets X to x, then returns the copy. If you want to change only the X of a position, you can do this in the arguments of a function.

func (Pos) WithY

func (p Pos) WithY(y int32) Pos

This copies p, sets Y to y, then returns the copy. If you want to change only the Y of a position, you can do this in the arguments of a function.

func (Pos) WithZ

func (p Pos) WithZ(z int32) Pos

This copies p, sets Z to z, then returns the copy. If you want to change only the Z of a position, you can do this in the arguments of a function.

type Property

type Property struct {
	// contains filtered or unexported fields
}

This is a property. Each blockstate is a possible combination of properties. So when all of the blocks are compiled into a blockstate map, all possible combinations of properties are given a unique id.

Each property can have one of three types: enum, int or bool. Read more about this in the NewProperty function.

func NewProperty

func NewProperty(name string, values ...interface{}) *Property

This creates a new property. The first paramater must be a unique name for this property. For the next paramaters, the type infers which kind of property you would like.

In order to make this an enum type, just pass in two or more strings. The default value will be the first string passed.

In order to make this an int, just pass in an int type, which is the number of ints you want. For example, the redstone block uses an int for it's 'power' property. To create that, you would pass in 16. The default will be 0.

In order to create a bool type, pass in true or false. The value you pass in will be the default.

func (*Property) Default

func (p *Property) Default() string

func (*Property) Name

func (p *Property) Name() string

func (*Property) Size

func (p *Property) Size() int

func (*Property) Values

func (p *Property) Values() []string

type Register

type Register interface {
	Name() string
	SetName(name string)
	Properties() []*Property
	DefaultProperties() map[string]string

	Settings(set Settings)
	GetSettings() Settings
	Color(bs *State) Color
	Sound(bs *State) string
	BoundingBox(t *Type) BoundingBox
	Transparent(t *Type) bool

	Versions() map[Version]map[string]uint32

	Drops(w World, pos Pos) (string, int)
}

type RegisterBase

type RegisterBase struct {
	V1_8_ID int
	// contains filtered or unexported fields
}

Users create these, and pass them into Register() Once all blocks have been registered, these are all converted into *Kinds

func NewRegister

func NewRegister() *RegisterBase

func (*RegisterBase) AddProperty

func (r *RegisterBase) AddProperty(p *Property)

func (*RegisterBase) BoundingBox

func (r *RegisterBase) BoundingBox(t *Type) BoundingBox

func (*RegisterBase) Color

func (r *RegisterBase) Color(bs *State) Color

func (*RegisterBase) DefaultProperties

func (r *RegisterBase) DefaultProperties() map[string]string

func (*RegisterBase) Drops

func (r *RegisterBase) Drops(w World, pos Pos) (string, int)

func (*RegisterBase) GetSettings

func (r *RegisterBase) GetSettings() Settings

DO NOT USE THIS. This should only when you need to do something like copy all of the settings for a block (example: stairs copying all settings from planks). Otherwise, this should not be used, as the other specific methods can be overriten by other blocks. For example, a log changes color when it is place at a different angle. The settings is never updated, but the result from Color() is updated.

func (*RegisterBase) Name

func (r *RegisterBase) Name() string

func (*RegisterBase) Properties

func (r *RegisterBase) Properties() []*Property

func (*RegisterBase) SetDefaultProperty

func (r *RegisterBase) SetDefaultProperty(key, val string)

func (*RegisterBase) SetName

func (r *RegisterBase) SetName(name string)

func (*RegisterBase) Settings

func (r *RegisterBase) Settings(set Settings)

Sets the settings for a block. Will panic if called twice.

func (*RegisterBase) Sound

func (r *RegisterBase) Sound(bs *State) string

func (*RegisterBase) Transparent

func (r *RegisterBase) Transparent(t *Type) bool

func (*RegisterBase) Versions

func (r *RegisterBase) Versions() map[Version]map[string]uint32

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

This is a global block registry. It is a small wrapper over VersionRegistry, which allows a registry to be copied to be used for a certain version.

func CreateRegistry

func CreateRegistry() *Registry

This sets the main block registry to a new registry. If there is already a registry, it will be overriden.

func NewRegistry

func NewRegistry() *Registry

This creates a new block registry.

func (*Registry) Add

func (r *Registry) Add(version Version, name string, item Register)

This adds a new block to the registry. The version should be a version >= 1.13. The order of Add calls only matters for the order of the block data. If you add a block to 1.14, then another to 1.13, the one from 1.13 will carry over to 1.14.

func (*Registry) Generate

func (r *Registry) Generate() (map[string]*Kind, []*Type, map[Version][]*Type)

Generates a kinds and types map. This is what is used to convert all registered blocks into a usable blockstate map.

func (*Registry) Get

func (r *Registry) Get(version Version, name string) Register

This gets an item from the registry. The version can be any valid version, and this will act the same for all versions, with one caveat: if you call Update(), then this will return different items for different versions. Other than that, the version does not matter for this call.

func (*Registry) Update

func (r *Registry) Update(version Version, name string, item Register)

This updates a block. For example, noteblocks got some more notes in 1.14. So, you might call Add(1.13, "noteblock", old_data), and then Update(1.14, "noteblock", new_data).

type Settings

type Settings struct {
	Tool                 Tool
	Hardness, Resistance float64
	Color                Color
	Sound                string
	Versions             VersionsMap

	BoundingBox BoundingBox
	Transparent bool
}

This is basically a large struct which is used to declare what settings a block has. These settings cover all attributes that all blocks have: Hitbox, Properties, Sound, Color, Tool, etc.

func (Settings) Copy

func (s Settings) Copy() Settings

This is a deep copy. It is mostly used to copy the versions map

type State

type State struct {
	// contains filtered or unexported fields
}

one for each block in the world

func NewState

func NewState(set_type set_type_func, pos Pos, block_type *Type) *State

func NewStateFromID

func NewStateFromID(set_type set_type_func, pos Pos, id uint32) *State

func (*State) AllProperties

func (s *State) AllProperties() map[string]map[string]struct{}

func (*State) Pos

func (s *State) Pos() Pos

func (*State) Properties

func (s *State) Properties() map[string]string

func (*State) SetFromCommandString

func (s *State) SetFromCommandString(name string)

the format minecraft:chest[facing=west]

func (*State) SetProperties

func (s *State) SetProperties(new_props map[string]string) error

func (*State) SetProperty

func (s *State) SetProperty(key string, val string) error

func (*State) SetType

func (s *State) SetType(new_type *Type) error

func (*State) State

func (s *State) State() uint32

func (*State) StateVersion

func (s *State) StateVersion(version Version) uint32

func (*State) Type

func (s *State) Type() *Type

func (*State) X

func (s *State) X() int32

func (*State) Y

func (s *State) Y() int32

func (*State) Z

func (s *State) Z() int32

type Tool

type Tool int
const (
	Tool_PICKAXE Tool = iota
	Tool_AXE
	Tool_SHOVEL
	Tool_HOE
	Tool_SHEARS
	Tool_SWORD
)

Resets iota

type Type

type Type struct {
	// contains filtered or unexported fields
}

One for each possible block state

func Air

func Air() *Type

The default block in the game. The type returned can be compared against any block type to check if it's air.

func TypeFromID

func TypeFromID(id uint32) *Type

func TypeFromIDVersion

func TypeFromIDVersion(id uint32, version Version) *Type

func (*Type) BoundingBox

func (t *Type) BoundingBox() BoundingBox

Returns the bounding box of the block. If this block does not have a bounding box, this will return BoundingBox_NONE.

func (*Type) CommandString

func (t *Type) CommandString() string

func (*Type) HasProperty

func (t *Type) HasProperty(name string) bool

This returns true if the property exists, and false if otherwise.

func (*Type) Kind

func (t *Type) Kind() *Kind

func (*Type) Name

func (t *Type) Name() string

func (*Type) PossiblePropertyValues

func (t *Type) PossiblePropertyValues(name string) []string

This returns a list of all possible values for one property key.

func (*Type) PropString

func (t *Type) PropString() string

This generates a string in the format "minecraft:log axis=x". Properties will always be in alphabetical order. Used internally for property to type lookups.

func (*Type) Properties

func (t *Type) Properties() map[string]string

This copies all properties from type into a new map. This is slow, and probably shouldn't be used very much.

func (*Type) Property

func (t *Type) Property(name string) string

This gets the property for the given name. If the property does not exist, it will return an empty string.

func (*Type) State

func (t *Type) State() uint32

func (*Type) StateVersion

func (t *Type) StateVersion(version Version) uint32

func (*Type) String

func (t *Type) String() string

func (*Type) Transparent

func (t *Type) Transparent() bool

Returns true if this block is transparent to block light.

type Version

type Version int32

This is a block versions. This is probably just an iota for every major version in the game. This allows older versions of blocks to be packed into an array, instead of a map to protocol versions.

const (
	V1_8 Version = iota
	// Will probably never be implemented.
	// V1_9
	// V1_10
	// V1_11
	V1_12
	V1_13
	V1_14
	V1_15
	V1_16

	VINVALID Version = -1

	// Latest block version to use. Should always map to a valid version.
	VLATEST Version = V1_16
)

func (Version) String

func (v Version) String() string

type VersionRegistry

type VersionRegistry struct {
	// contains filtered or unexported fields
}

func NewVersionRegistryFrom

func NewVersionRegistryFrom(other *VersionRegistry) *VersionRegistry

This creates a registry for a single version. If other is non-nil, then any actions made to other will also be applied to this version. So, if you have a 1.13 registry, then you can create a NewVersionRegistryFrom(v1.13) to create a 1.14 registry. Now, you can register things to the 1.13 registry, and they will be added to the 1.14 registry as well. Also, multiple registries may be based off of one version.

func (*VersionRegistry) Add

func (r *VersionRegistry) Add(name string, item Register)

This registers a new item. id should be a constant, defined in user-space code (probably with iota). id is not used for sorting, the order of items depends on the order that Add is called. So, for packets, ID would be the grpc packet id, which doesn't need to be the same as any minecraft packet id. If id is -1, then this is treated as a placeholder, and nil will be inserted into the internal array.

func (*VersionRegistry) Generate

func (r *VersionRegistry) Generate() (map[string]*Kind, []*Type)

func (*VersionRegistry) Get

func (r *VersionRegistry) Get(name string) (item Register)

This gets the item at the id, and panics if the id is not within the registry.

func (*VersionRegistry) GetOk

func (r *VersionRegistry) GetOk(name string) (item Register, ok bool)

This gets the item at the id, and returns false if the id is not within the registry. This does not return the internal index, as that value has no meaning.

func (*VersionRegistry) InsertAfter

func (r *VersionRegistry) InsertAfter(name, new_name string, item Register)

This inserts item after name in the list. The item's name will be new_name.

func (*VersionRegistry) List

func (r *VersionRegistry) List() []Register

This lists all elements added to the registry, in the order that they were addded.

func (*VersionRegistry) MoveAfter

func (r *VersionRegistry) MoveAfter(name, new_name string)

This moves the item at name after new_name in the list.

func (*VersionRegistry) Remove

func (r *VersionRegistry) Remove(name string)

Will remove an item from the registry. This will decrease all items that have a greater index down by one.

func (*VersionRegistry) Set

func (r *VersionRegistry) Set(name string, item Register)

This overrides an item. Useful for if a packet slightly changes between versions.

The old id is infered, since this should only be used to override packets. If you call this function, and id is not in the registry, it will panic. To add a new item, use Add or Insert instead.

func (*VersionRegistry) String

func (r *VersionRegistry) String() string

func (*VersionRegistry) Validate

func (r *VersionRegistry) Validate() error

type VersionsMap

type VersionsMap map[Version]map[string]uint32

type World

type World interface {
	Block(pos Pos) *State
	BlockType(pos Pos) *Type

	SetBlock(pos Pos, t *Type)
	SetBlockProperty(pos Pos, key, val string)
	SetBlockProperties(pos Pos, props map[string]string)

	FixDesync(pos Pos)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL