util

package
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: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PERLIN_SIZE        = 0x10
	PERLIN_REPEAT_SIZE = PERLIN_SIZE * PERLIN_SIZE
)

Variables

This section is empty.

Functions

func ParseVarInt

func ParseVarInt(data []byte) (int32, int32)

Will parse a varint. Returns the number, and the number of bytes that it read.

func ReadVarInt

func ReadVarInt(r io.Reader) (int32, int32)

Will parse a varint from the given reader. If the reader returns an error, this will return -1, -1. Otherwise, this will return the number, and the number of bytes read.

func ToVarInt

func ToVarInt(value int32) []byte

Types

type Buffer

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

This is a custom buffer, optimized for minecraft packets. This is used whenever the proxy sends a packet.

func NewBuffer

func NewBuffer() *Buffer

This creates a new buffer, containing no data.

func NewBufferFromSlice

func NewBufferFromSlice(data []byte, start_pos int) (*Buffer, error)

This creates a new buffer, which points to the given slice. If the slice is big enough, the buffer will not allocate any more memory. It will just start writing into the slice at the given index.

func (*Buffer) GetData

func (b *Buffer) GetData() []byte

This returns a slice which goes from the very start of the buffer passed in, to the most recently written byte. So if you started writing at index 10, and wrote 5 bytes, this would return a slice that is 15 bytes long.

func (*Buffer) GetRawData

func (b *Buffer) GetRawData() []byte

This just gives you the buffer that was originally passed into NewBufferFromSlice, or the buffer that it created in NewBuffer().

func (*Buffer) Length

func (b *Buffer) Length() int32

Returns the length of the buffer. If the buffer started being written at index 10, and you wrote 5 bytes, this would return 15.

func (*Buffer) WriteBool

func (b *Buffer) WriteBool(value bool)

Writes a bool into the buffer. Will write 0x01 if the bool is true, and 0x00 if it was false.

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(value byte)

This writes a single byte into the buffer. All other functions that write a single byte call this one. This will either overwrite a single byte in the internal slice, or append a single byte.

func (*Buffer) WriteByteArray

func (b *Buffer) WriteByteArray(value []byte)

This writes a byte array into the buffer. All other functions that write multiple bytes call this one. If there is enough space in the internal buffer, then it will just copy the values passed in. If there is some space, but not enough for the whole array, it will copy the first section, then append the second section. Otherwise, it will just append the slice passed in. You can see examples of this in buffer_test.go.

func (*Buffer) WriteDouble

func (b *Buffer) WriteDouble(value float64)

This writes a double, encoded in IEEE 754 binary format.

func (*Buffer) WriteFloat

func (b *Buffer) WriteFloat(value float32)

This writes a float, encoded in IEEE 754 binary format.

func (*Buffer) WriteInt

func (b *Buffer) WriteInt(value int32)

This writes an int, encoded in big endian.

func (*Buffer) WriteIntArray

func (b *Buffer) WriteIntArray(value []int32)

This writes an array of ints.

func (*Buffer) WriteLittleEndianShort

func (b *Buffer) WriteLittleEndianShort(value uint16)

This writes a short, encoded in little endian.

func (*Buffer) WriteLong

func (b *Buffer) WriteLong(value uint64)

This writes a long, encoded in big endian.

func (*Buffer) WriteLongArray

func (b *Buffer) WriteLongArray(value []uint64)

This writes an array of longs.

func (*Buffer) WriteNullString

func (b *Buffer) WriteNullString(value string)

This writes a null terminated string.

func (*Buffer) WriteOldPosition

func (b *Buffer) WriteOldPosition(pos block.Pos)

This writes a minecraft block position, for versions before 1.14. This is a long, where 3 values are encoded. It starts with the x value, encoded as a 26 bit integer, followed by the y value, which is a 12 bit integer, followed by the z value, which is a 26 bit integer.

func (*Buffer) WritePosition

func (b *Buffer) WritePosition(pos block.Pos)

This writes a minecraft block position. This is a long, where 3 values are encoded. It starts with the x value, encoded as a 26 bit integer, followed by the z value, which is a 26 bit integer. followed by the y value, which is a 12 bit integer,

func (*Buffer) WriteShort

func (b *Buffer) WriteShort(value uint16)

This writes a short, encoded in big endian.

func (*Buffer) WriteString

func (b *Buffer) WriteString(value string)

This writes a length prefixed string.

func (*Buffer) WriteUUID

func (b *Buffer) WriteUUID(value UUID)

This writes a minecraft UUID, as 16 bytes.

func (*Buffer) WriteVarInt

func (b *Buffer) WriteVarInt(value int32)

This writes a varint, according to minecraft's format. This means that the number of bytes it writes can change, and it can be anywhere between 1 and 5 bytes. All negative numbers are encoded in 5 bytes, so it is still useful to be able to write 4 byte ints.

type Chat

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

func NewChat

func NewChat() *Chat

func NewChatFromString

func NewChatFromString(message string) *Chat

func NewChatFromStringColor

func NewChatFromStringColor(message string, color string) *Chat
func (c *Chat) AddLink(text, address string)

func (*Chat) AddLinkColor

func (c *Chat) AddLinkColor(text, color, address string)

func (*Chat) AddSection

func (c *Chat) AddSection(text string)

func (*Chat) AddSectionColor

func (c *Chat) AddSectionColor(text string, color string)

func (*Chat) AddSectionFormatted

func (c *Chat) AddSectionFormatted(text, color string, bold, italic, underlined bool)

func (*Chat) JSONObject

func (c *Chat) JSONObject() interface{}

This returns an object that can be passed into json.Marshal, to produce a chat message.

func (*Chat) String

func (c *Chat) String() string

Converts to chat message to a non colored string

func (*Chat) ToFormatCodes

func (c *Chat) ToFormatCodes() string

This returns the chat messages, formatted using the legacy color codes. This format is more compact, but is deprecated (even though it still works after years).

func (*Chat) ToJSON

func (c *Chat) ToJSON() string

This returns the chat message, formatted in json.

type FastRand

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

This is an implementation of wyhash64. It uses a uint64 as it's state, and will use math.bits to perform 128 bit integer multiplication. After benchmarking this, it appears to be ~4 times faster than math/rand.

func NewFastRand

func NewFastRand(seed int64) *FastRand

Creates a new FastRand random number generators, and seeds it with the given seed.

func (*FastRand) Int63

func (f *FastRand) Int63() int64

Returns a pseudo-random positive 64 bit integer.

func (*FastRand) Seed

func (f *FastRand) Seed(seed int64)

func (*FastRand) Uint64

func (f *FastRand) Uint64() uint64

Returns a pseudo-random 64 bit integer.

type LayeredNoise

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

This is a multi octave noise generator.

func NewLayeredNoise

func NewLayeredNoise(source rand.Source, octaves int) *LayeredNoise

func (*LayeredNoise) Sample

func (l *LayeredNoise) Sample(x, y float64) float64

Will always return a value between -1 and 1

type Noise

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

This is a perlin noise generator

func NewNoise

func NewNoise(source rand.Source) *Noise

func (*Noise) Sample

func (n *Noise) Sample(x, y float64) float64

type NoiseSampler

type NoiseSampler interface {
	// This should sample a noise or layered noise map.
	Sample(x, y float64) float64
}

Noise sampler interface.

type RandomGrid

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

func NewRandomGrid

func NewRandomGrid(source rand.Source, dist int, min_dist float32) *RandomGrid

This creates a new random point grid. A random point grid is a grid where every cell (usually) has a point in it. Dist is the size of each grid cell, and min_dist is the minimum required distance between points. If min_dist is too large, then more cells will no longer have points. Each time a cell needs a point, it will try to generate one 10 times, before giving up and leaving the point empty.

func (*RandomGrid) ClosestPoint

func (r *RandomGrid) ClosestPoint(x, y int) (ret RandomGridPoint)

func (*RandomGrid) ContainsPoint

func (r *RandomGrid) ContainsPoint(x, y int) bool

x and y are absolute coordinates, which corrospond to a point in the grid. If the grid dist is 4, then x and y should be divided by 4 to find the actual square that they are searching. This returns true if the point in that square lies within the smaller square coordinates that were passed in.

This is used in terrain generation, by passing in absolute block coordinates. If the function returns true, a tree is placed at that position.

func (*RandomGrid) PointsInArea

func (r *RandomGrid) PointsInArea(x1, y1, x2, y2 int) []RandomGridPoint

This returns a list of absolute coordinates, which are all points that lie within the given rectangle. The points are in the same coordinate space as the coordinates passed into ContainsPoint

type RandomGridPoint

type RandomGridPoint struct {
	X int
	Y int
}

This is a point in absolute coordinates

type UUID

type UUID [16]byte

This is a UUID, used for player account ids. This is passed around by value in most situations. Internally, this is a big endian array of 16 bytes.

func NewUUID

func NewUUID() UUID

Creates a UUID with the value 0.

func NewUUIDFromBytes

func NewUUIDFromBytes(value [16]byte) UUID

Creates a new UUID from a byte array.

func NewUUIDFromProto

func NewUUIDFromProto(value *pb.UUID) UUID

Creates a new UUID from a UUID protobuf. Used in the server when decoding the UUID sent from the proxy.

func NewUUIDFromRand

func NewUUIDFromRand() UUID

Creates a UUID by reading 16 bytes from math/rand. Use NewUUIDFromReader if you need crypto/rand.

func NewUUIDFromReader

func NewUUIDFromReader(reader io.Reader) UUID

Creates a UUID by reading 16 bytes from the given reader.

func NewUUIDFromString

func NewUUIDFromString(value string) UUID

Creates a UUID from a hex string. Will panic if the string is not 32 characters. (Does not work with dashed UUIDS)

func (UUID) GetBytes

func (u UUID) GetBytes() []byte

Gets the big endian bytes of the UUID.

func (UUID) String

func (u UUID) String() string

Used for printing. Simply calls ToDashedString

func (UUID) ToDashedString

func (u UUID) ToDashedString() string

Returns the hex string of the UUID, with dashes inserted.

func (UUID) ToProto

func (u UUID) ToProto() *pb.UUID

func (UUID) ToString

func (u UUID) ToString() string

Returns the hex string of the UUID, without dashes.

type Voronoi

type Voronoi struct {
	// Point grid for the voronoi map.
	*RandomGrid
}

This is a small layer ontop of a RandomGrid.

func NewVoronoi

func NewVoronoi(source rand.Source, scale int) *Voronoi

Creates an empty voronoi map.

func (*Voronoi) Get

func (v *Voronoi) Get(x, y int) int

This returns a unique id for each voronoi section.

type WarpedVoronoi

type WarpedVoronoi struct {
	// Underlying voronoi map
	*Voronoi
	// contains filtered or unexported fields
}

This adds two noise maps to a voronoi map, which distort it in the x and y axis.

func NewWarpedVoronoi

func NewWarpedVoronoi(source rand.Source, scale int, noise_strength float64) *WarpedVoronoi

Creates an empty voronoi map.

func (*WarpedVoronoi) Get

func (v *WarpedVoronoi) Get(x, y int) int

This returns a unique id for each voronoi section.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL