basic

package
v0.0.0-...-c10fe95 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package basic provides some basic packet handler which client needs.

[Player]

The Player is attached to a [Client] by calling NewPlayer before the client joins a server.

There is 4 kinds of clientbound packet is handled by this package.

  • LoginPacket, for cache player info. The player info will be stored in [Player.PlayerInfo].
  • KeepAlivePacket, for avoid the client to be kicked by the server.
  • PlayerPosition, is only received when server teleporting the player.
  • Respawn, for updating player info, which may change when player respawned.

[EventsListener]

Handles some basic event you probably need.

  • GameStart
  • Disconnect
  • HealthChange
  • Death

Index

Constants

View Source
const (
	Jacket
	LeftSleeve
	RightSleeve
	LeftPantsLeg
	RightPantsLeg
	Hat
)

Used by Settings.DisplayedSkinParts. For each bit set if shows match part.

Variables

View Source
var DefaultSettings = Settings{
	Locale:             "zh_CN",
	ViewDistance:       15,
	ChatMode:           0,
	DisplayedSkinParts: Jacket | LeftSleeve | RightSleeve | LeftPantsLeg | RightPantsLeg | Hat,
	MainHand:           1,

	EnableTextFiltering: false,
	AllowListing:        true,

	Brand: "vanilla",
}

DefaultSettings are the default settings of client

Functions

This section is empty.

Types

type Error

type Error struct {
	Err error
}

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

type EventsListener

type EventsListener struct {
	// GameStart event is called when the login process is completed and the player is ready to play.
	//
	// If you want to do some action when the bot joined the server like sending a chat message,
	// this event is the right place to do it.
	GameStart func() error

	// Disconnect event is called before the server disconnects your client.
	// When the server willfully disconnects the client, it will send a ClientboundDisconnect packet and tell you why.
	// On vanilla client, the reason is displayed in the disconnect screen.
	//
	// This information may be very useful for debugging, and generally you should record it into the log.
	//
	// If the connection is disconnected due to network reasons or the client's initiative,
	// this event will not be triggered.
	Disconnect func(reason chat.Message) error

	// HealthChange event is called when the player's health or food changed.
	HealthChange func(health float32, foodLevel int32, foodSaturation float32) error

	// Death event is a special case of HealthChange.
	// It will be called after HealthChange handler called (if it isn't nil)
	// when the player's health is less than or equal to 0.
	//
	// Typically, you should call [Player.Respawn] in this handler.
	Death func() error

	// Teleported event is called when the server think the player position in the client side is wrong,
	// and send a ClientboundPlayerPosition packet to correct the client.
	//
	// Typically, you need to do two things in this handler:
	// - Update the player's position and rotation you tracked to the correct position.
	// - Call [Player.AcceptTeleportation] to send a teleport confirmation packet to the server.
	//
	// Before you confirm the teleportation, the server will not accept any player motion packets.
	//
	// The position coordinates and rotation are absolute or relative depends on the flags.
	// The flag byte is a bitfield, specifies whether each coordinate value is absolute or relative.
	// For more information, see https://wiki.vg/Protocol#Synchronize_Player_Position
	Teleported func(x, y, z float64, yaw, pitch float32, flags byte, teleportID int32, dismountVehicle bool) error
}

EventsListener is a collection of event handlers. Fill the fields with your handler functions and pass it to NewPlayer to create the Player manager. For the event you don't want to handle, just leave it nil.

type Player

type Player struct {
	Settings Settings

	PlayerInfo
	WorldInfo
	// contains filtered or unexported fields
}

func NewPlayer

func NewPlayer(c *bot.Client, settings Settings, events EventsListener) *Player

NewPlayer create a new Player manager.

func (*Player) AcceptTeleportation

func (p *Player) AcceptTeleportation(teleportID pk.VarInt) error

AcceptTeleportation is used to send a teleport confirmation packet to the server. Typically, you should call this method when received a ClientboundPlayerPosition packet (in the [Teleported] event handler).

func (*Player) Respawn

func (p *Player) Respawn() error

Respawn is used to send a respawn packet to the server. Typically, you should call this method when the player is dead (in the [Death] event handler).

type PlayerInfo

type PlayerInfo struct {
	EID          int32 // The player's Entity ID (EID).
	Hardcore     bool  // Is hardcore
	Gamemode     byte  // Gamemode. 0: Survival, 1: Creative, 2: Adventure, 3: Spectator.
	PrevGamemode int8  // Previous Gamemode
}

type Settings

type Settings struct {
	Locale             string // 地区
	ViewDistance       int    // 视距
	ChatMode           int    // 聊天模式
	ChatColors         bool   // 聊天颜色
	DisplayedSkinParts uint8  // 皮肤显示
	MainHand           int    // 主手

	// Enables filtering of text on signs and written book titles.
	// Currently, always false (i.e. the filtering is disabled)
	EnableTextFiltering bool
	AllowListing        bool

	// The brand string presented to the server.
	Brand string
}

Settings of client

type WorldInfo

type WorldInfo struct {
	RegistryCodec       registry.NetworkCodec
	DimensionType       string
	DimensionNames      []string // Identifiers for all worlds on the server.
	DimensionName       string   // Name of the world being spawned into.
	HashedSeed          int64    // First 8 bytes of the SHA-256 hash of the world's seed. Used client side for biome noise
	MaxPlayers          int32    // Was once used by the client to draw the player list, but now is ignored.
	ViewDistance        int32    // Render distance (2-32).
	SimulationDistance  int32    // The distance that the client will process specific things, such as entities.
	ReducedDebugInfo    bool     // If true, a vanilla client shows reduced information on the debug screen. For servers in development, this should almost always be false.
	EnableRespawnScreen bool     // Set to false when the doImmediateRespawn gamerule is true.
	IsDebug             bool     // True if the world is a debug mode world; debug mode worlds cannot be modified and have predefined blocks.
	IsFlat              bool     // True if the world is a superflat world; flat worlds have different void fog and a horizon at y=0 instead of y=63.
}

WorldInfo content player info in server.

Jump to

Keyboard shortcuts

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