Tnze / go-mc

Collection of Go libraries for Minecraft
https://go-mc.github.io/tutorial/
MIT License
857 stars 116 forks source link

A way to access chests/crafting tables? #52

Closed JosonChong closed 4 years ago

JosonChong commented 4 years ago

Hello there,

Thank you so much for the awesome library. I am trying to use this library to create a bot that can grab stuffs in the chest and then craft them to items I want. Is that possible to be done?

Tnze commented 4 years ago

Yes! You can do it. What you need is make sure the target chest is in ranged. Then UseBlock it. The server will send you a Open Window packet, which you must handle by setting c.Events a event handler to receive.

Do the same thing with the crafting table if you want to craft them.

JosonChong commented 4 years ago

Your advice is very useful, thank you very much!

I am also trying to add an auto-attack feature for AFK farms to my bot. I know that I can use the entity id from mob spawn event. However, I am so lost on how to keep track of the mobs and know when they are in my attack range.

Do I use the entity relative move package? But this package is giving me weird results, even if I used the formula provided here Entity Position For example, 4 packages were sent to me when my character jumped once Delta X, Delta Y, Delta Z: 0 1365 0 Delta X, Delta Y, Delta Z: 0 2031 0 Delta X, Delta Y, Delta Z: 0 -296 0 Delta X, Delta Y, Delta Z: 0 -2792 0 And that seems like it doesn't add up, did I do anything wrong?

It would be best if you can give me some hints, thank you!

Tnze commented 4 years ago

Do you handle both two packets related to entity movement? Including Entity Position and Entity Position and Rotation? Also you need receive Entity Rotation if you wanna trace the rotation of course.

JosonChong commented 4 years ago

Yes, I think so.

Here is my code if you don't mind looking at it. Sorry if I made stupid mistakes cuz it's my first time writing Go code.

var testX, testY, testZ = float64(0), float64(0), float64(0)  //init when SpawnPlayer package is received
var testPlayerID = 0 //init when SpawnPlayer package is received

func handleEntityLookAndRelativeMove(c *Client, p pk.Packet) error {
    var (
        EntityID               pk.VarInt
        DeltaX, DeltaY, DeltaZ pk.Short
        Yaw, Pitch             pk.Byte
        OnGround               pk.Boolean
    )
    err := p.Scan(&EntityID, &DeltaX, &DeltaY, &DeltaZ, &Yaw, &Pitch, &OnGround)
    if err != nil {
        return err
    }

    if testPlayerID != 0 && int(EntityID) == testPlayerID {
        testX = getCurrentPos(testX, int(DeltaX))
        testY = getCurrentPos(testY, int(DeltaY))
        testZ = getCurrentPos(testZ, int(DeltaZ))
        fmt.Println("player looked and relative moved! ", DeltaX, DeltaY, DeltaZ)
        fmt.Println("new location:", testX, testY, testZ)
    }

    return nil
}

func handleEntityRelativeMove(c *Client, p pk.Packet) error {
    var (
        EntityID               pk.VarInt
        DeltaX, DeltaY, DeltaZ pk.Short
        OnGround               pk.Boolean
    )
    err := p.Scan(&EntityID, &DeltaX, &DeltaY, &DeltaZ, &OnGround)
    if err != nil {
        return err
    }

    if testPlayerID != 0 && int(EntityID) == testPlayerID {
        testX = getCurrentPos(testX, int(DeltaX))
        testY = getCurrentPos(testY, int(DeltaY))
        testZ = getCurrentPos(testZ, int(DeltaZ))
        fmt.Println("player relative moved! ", DeltaX, DeltaY, DeltaZ, OnGround)
        fmt.Println("new location:", testX, ", ", testY, ", ", testZ)
    }

    return nil
}

func getCurrentPos(prevPos float64, delta int) float64 {
    distanceTravelled := float64(delta) / 4096
    return prevPos + distanceTravelled
}