multiworld

package module
v0.0.0-...-ebd4229 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 8 Imported by: 0

README

df-multiworld

A simple implementation of world transfer in dragonfly-mc.

This package provides:

  • A WorldProvider to manage multiple worlds.
  • Helper functions to transfer entities (especially players) across worlds.
  • A built-in /multiworld (alias /mw) command with subcommands for teleporting.

Installation

import (
  "fmt"
  "log/slog"

  // Automatically register the command and load all worlds
  // just by importing the package
  "github.com/redstonecraftgg/df-multiworld"

  "github.com/df-mc/dragonfly/server"
)

func main() {
  conf, err := server.DefaultConfig().Config(slog.Default())
  if err != nil {
    panic(err)
  }
  srv := conf.New()
  srv.CloseOnProgramEnd()

  // Manually register the original world created by the server
  // using the folder name inside the root path
  multiworld.Worlds.Worlds["world"] = srv.World()

  srv.Listen()
  for p := range srv.Accept() {
    _ = p
  }
}

Usage

With function

If you want to move a player programmatically:

multiworld.TransferPlayer(playerObj, Worlds, "world2", mgl64.Vec3{0, 64, 0})

This will:

  • Save the player's entity handle.

  • Remove it from the current world.

  • Re-add it to the target world.

  • Teleport it to the given position.


With commands

The package also registers a command automatically:

  • /multiworld teleport <world>

  • Alias: /mw tp <world>

This will transfer the player to the given world.

Important: make sure the target position is not obstructed (e.g. no solid block), otherwise the player may suffocate.

The default teleport position is: 0, 64, 0

Features

  • Simple MapWorldProvider using Go map[string]*world.World.

  • TransferPlayer helper with proper entity handling.

  • Autocomplete support for world names in chat.

Notes

  • Only loaded worlds can be teleported to.

  • If you want custom spawn locations, you can extend TransferPlayer with your own logic.

  • Currently designed for player transfers, but can be adapted for other entities if possible.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Worlds = &MapWorldProvider{
	Worlds: make(map[string]*world.World),
}

Worlds is a global alias for the MapWorldProvider instance.

Functions

func GetWorldNames

func GetWorldNames() []string

GetWorldNames returns a slice of all loaded world names. Example usage: names := GetWorldNames()

func LoadWorlds

func LoadWorlds()

LoadWorlds reads and loads all worlds from the "worlds" folder into memory. It creates the folder if it does not exist. Example usage: LoadWorlds()

func TransferEntity

func TransferEntity(eH *world.EntityHandle, provider WorldProvider, worldName string, pos mgl64.Vec3) error

TransferEntity moves an entity (player or other) from its current world to another world at a given position. Example usage: TransferEntity(entityHandle, Worlds, "world2", mgl64.Vec3{0, 64, 0})

func TransferPlayer

func TransferPlayer(p *player.Player, provider WorldProvider, worldName string, pos mgl64.Vec3) error

TransferPlayer is a helper function to move a player to another world at a given position. Example usage: TransferPlayer(playerObj, Worlds, "world2", mgl64.Vec3{0, 64, 0})

Types

type MWTp

type MWTp struct {
	Tp        cmd.SubCommand          `cmd:"tp"`
	WorldName cmd.Optional[WorldEnum] `cmd:"world,enum"`
}

MWTp is an alias command structure for teleporting (short form). Usage example: /world tp <world_name>

func (MWTp) Run

func (c MWTp) Run(src cmd.Source, o *cmd.Output, tx *world.Tx)

Run executes the MWTp command, which is an alias for MultiWorldTp. Example: /world tp world2

type MapWorldProvider

type MapWorldProvider struct {
	Worlds map[string]*world.World
}

MapWorldProvider is a simple implementation of WorldProvider using a map.

func (*MapWorldProvider) GetWorld

func (m *MapWorldProvider) GetWorld(name string) (*world.World, bool)

GetWorld retrieves a world by name from the map. Returns the world and true if found, otherwise nil and false.

type MultiWorldList

type MultiWorldList struct {
	List cmd.SubCommand `cmd:"list"`
}

MultiWorldList is a command structure for listing all loaded worlds. Usage example: /world list

func (MultiWorldList) Run

func (MultiWorldList) Run(src cmd.Source, o *cmd.Output, tx *world.Tx)

Run executes the MultiWorldList command, sending the list of available worlds to the player. Example: /world list

type MultiWorldTp

type MultiWorldTp struct {
	Teleport  cmd.SubCommand          `cmd:"teleport"`
	WorldName cmd.Optional[WorldEnum] `cmd:"world,enum"`
}

MultiWorldTp is a command structure for teleporting to another world. Usage example: /world teleport <world_name>

func (MultiWorldTp) Run

func (c MultiWorldTp) Run(src cmd.Source, o *cmd.Output, tx *world.Tx)

Run executes the MultiWorldTp command, teleporting the player to the specified world. Example: /world teleport world2

type WorldEnum

type WorldEnum string

WorldEnum is a string type used for world name enumeration in commands.

func (WorldEnum) Options

func (WorldEnum) Options(src cmd.Source) []string

func (WorldEnum) Type

func (WorldEnum) Type() string

type WorldProvider

type WorldProvider interface {
	GetWorld(name string) (*world.World, bool)
}

WorldProvider is an interface for world providers, allowing retrieval of worlds by name.

Jump to

Keyboard shortcuts

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