Coder Social home page Coder Social logo

psu-de / minesharp Goto Github PK

View Code? Open in Web Editor NEW
76.0 7.0 10.0 30.85 MB

High-Level async API to build minecraft bots.

License: MIT License

C# 100.00%
bot csharp minecraft minecraft-api minecraft-bot pathfinder bot-api bot-framework minecraft-bots minecraft-protocol

minesharp's Introduction

Hi there 👋

I am Paul Sütterlin. Currently I am studying Computer Science at Karlsruhe Institute of Technology - KIT. I love learning new technologies and connect with other developers.

My most used languages

I'm also very interested in AI and automation

minesharp's People

Contributors

psu-de avatar qwqtoday avatar stone3311 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

minesharp's Issues

Pathfinder status

The Pathfinder has the following abilities:

  • XZ movement
  • Jumping up
  • Jumping over 1-3 air blocks
  • Dropping down one block

Currently the pathfinder only uses WASD movement, not rotation with the mouse. However sprinting only works for 'W' (forward) movement, so I will try to update this at least for jumping over multiple blocks.

Moves I want to add:

  • Climbing / Ladder moves
  • Falling down multiple blocks without fall damage
  • Falling down many blocks if the bot can do a water MLG
  • More advanced parkour jumping
  • Destroy and place blocks

Also the ability to dynamically update a Goal and a Path is quite useful and will be added at some point.

GameMode enum may be incorrect

Is the game mode enum correct? I have a simple bot that automatically switches to spectator mode at night time, something like:

    if (currentTime is >= 12_542 and <= 23_459)
    {
        if (player.GameMode is not GameMode.Spectator)
        {
            await chat.SendCommand("gamemode spectator");
        }
    }

This correctly sets the game mode, but the issue is that it runs the command every time through the loop, because player.GameMode reports as GameMode.Adventure not GameMode.Spectator.

Are these final two values correct, or should they be 2 and 3?

/// <summary>
/// Adventure game mode
/// </summary>
Adventure = 3,
/// <summary>
/// Spectator game mode
/// </summary>
Spectator = 4

Update Game Mode doesn't have any notes here: https://wiki.vg/Protocol#Player_Info_Update but other sections have it defined, like here https://wiki.vg/Protocol#Game_Event

3 	Change game mode 	0: Survival, 1: Creative, 2: Adventure, 3: Spectator. 

Creative items

How can I make the bot get an item from the creative inventory?
I tried these:

ItemInfo diamond = bot.Data.Items.ByName("diamond")!;
bot.GetPlugin<WindowPlugin>().Inventory!.SetSlot(new Slot(new Item(diamond, 1, null, null), 1));

and also this

ItemInfo diamond = bot.Data.Items.ByName("diamond")!;
        
Window mainInventory = new Window(255, "", 4 * 9, null, null);
Window inventory     = new Window(0, "Inventory", 9, mainInventory, null);

inventory.SetSlot(new Slot(new Item(diamond, 1, null, null), 1));
        

but both don't work

also how does mining work?

when I do

Block b = worldPl.World.GetBlockAt(new Position(x, y, z));

await worldPl.MineBlock(b, BlockFace.Top);

nothing happens

Immutable datatypes

Make all Vector3, Position and AABB instances immutable by default, and add a class for mutable instances.

Make the API's safer

Unexpected and undefined behavior can be introduced quite easily currently, and its hard to spot the problem. Often it is also unclear why you shouldn't use a method.
List of problems:

  • Mutable Vector3, and AABB's
  • SetSlot methods for Windows. (Unsafe to use for MineSharp.Bot windows)
  • Item and Slot are mutable
    ... and probably more

NullReferenceException in PacketFactory after a while

Sometimes when a client is running for some time, the PacketFactory throws an Exception like this:

System.NullReferenceException HResult=0x80004003 Message= Object reference not set to an instance of an object. Source= MineSharp.Protocol Stack Trace: at MineSharp.Protocol.PacketFactory.WritePacket(IPacketPayload packet) in ...\MineSharp\Components\MineSharp.Protocol\PacketFactory.cs: Zeile103

Apparently after a while null is sent as a packet, but I have no idea why this happens...

for now if the packet is null it will just be ignored

Cannot read Packet DeclareCommands

When I updated minecraft-data to the latest version, and regenerated the source files, the following error occures when starting the bot:

[ERROR][16:43:44][Protocol  ] > Error reading packet!
[ERROR][16:43:44][Protocol  ] > System.Exception: Invalid value: 'minecraft:nbt_tag'
   at MineSharp.Data.Protocol.CommandNode.ExtraNodeDataSwitch.ExtraNodeDataSwitchState2Container.PropertiesSwitch.Read(PacketBuffer buffer, String state) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 1738
   at MineSharp.Data.Protocol.CommandNode.ExtraNodeDataSwitch.ExtraNodeDataSwitchState2Container.<>c__DisplayClass20_0.<Read>b__2(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 1803
   at MineSharp.Data.Protocol.CommandNode.ExtraNodeDataSwitch.ExtraNodeDataSwitchState2Container.Read(PacketBuffer buffer, FlagsBitfield flags) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 1803
   at MineSharp.Data.Protocol.CommandNode.ExtraNodeDataSwitch.<>c__DisplayClass8_0.<Read>b__2(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 1824
   at MineSharp.Data.Protocol.CommandNode.ExtraNodeDataSwitch.Read(PacketBuffer buffer, Byte state, FlagsBitfield flags) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 1824
   at MineSharp.Data.Protocol.CommandNode.<>c__DisplayClass21_0.<Read>b__3(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 1854
   at MineSharp.Data.Protocol.CommandNode.Read(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 1854
   at MineSharp.Data.Protocol.Play.Clientbound.PacketDeclareCommands.<>c.<Read>b__10_3(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 4091
   at MineSharp.Data.Protocol.PacketBuffer.ReadArray[T](Int32 length, Func`2 reader) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 255
   at MineSharp.Data.Protocol.Play.Clientbound.PacketDeclareCommands.<>c.<Read>b__10_0(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 4091
   at MineSharp.Data.Protocol.Play.Clientbound.PacketDeclareCommands.Read(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 4091
   at MineSharp.Data.Protocol.Play.Clientbound.Packet.ParamsSwitch.<>c.<Read>b__6_15(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 7301
   at MineSharp.Data.Protocol.Play.Clientbound.Packet.ParamsSwitch.Read(PacketBuffer buffer, String state) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 7301
   at MineSharp.Data.Protocol.Play.Clientbound.Packet.<>c__DisplayClass11_0.<Read>b__1(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 7615
   at MineSharp.Data.Protocol.Play.Clientbound.Packet.Read(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 7615
   at MineSharp.Data.Protocol.Play.Clientbound.PlayPacketFactory.ReadPacket(PacketBuffer buffer) in D:\MineSharp\Data\MineSharp.Data\Protocol.cs:line 7621
   at MineSharp.Protocol.PacketFactory.BuildPacket(Byte[] data, Int32 uncompressedLength) in D:\MineSharp\Components\MineSharp.Protocol\PacketFactory.cs:line 74

FormatException when obtaining a session for the first time

When trying to obtain a new online session for a new email for the first time, this exception is thrown.
The session however is saved and it works the next time.

Unhandled exception. System.FormatException: String '2024-02-22T15:56:54.142272440Z' was not recognized as a valid DateTime.
   at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)
   at MineSharp.Auth.Responses.PlayerCertificate.FromBlob(CertificateBlob blob) in /_/Components/MineSharp.Auth/Responses/PlayerCertificate.cs:line 83
   at MineSharp.Auth.MinecraftApi.FetchCertificates(String token) in /_/Components/MineSharp.Auth/MinecraftApi.cs:line 98
   at MineSharp.Auth.MicrosoftAuth.Login(String username, DeviceCodeHandler handler, MinecraftApi api) in /_/Components/MineSharp.Auth/MicrosoftAuth.cs:line 92
   at MineSharp.Bot.BotBuilder.CreateAsync() in /_/MineSharp.Bot/BotBuilder.cs:line 228
   at Program.<Main>$(String[] args) in /home/psu/projects/Testing/MineSharpNugetTests/MineSharpNugetTests/Program.cs:line 6
   at Program.<Main>(String[] args)

Pathfinder enhancements

The pathfinder module is currently very simple.

Currently, it only supports walking in each direction (not diagonal). Also it can only find a path to a goal that is on the same Y-Level.
Some ToDo's to make it more usable:

  • Add a timeout
  • Jump and fall down blocks
  • Climbing ladders and vines
  • Check if block is walkable based on bounding boxes
  • Special movements like tripple block jumps
  • Maybe destroy and place blocks
  • Swimming (must be implemented in physics first)
  • Find a good way to move from one node to another (currently very unreliable)
  • Optimizing the A*-Algorithm
  • Support block changes while finding path

Fix the chat system

The chat is currently a little bit weird and not properly implemented.

  • Provide a nice chat component api
  • normalize chat messages for all versions (before 1.20.3 chats were sent as a translatable chat component, after 1.20.3 the client has to apply a translation itself using the registry)
  • union between json and nbt chat components
  • delete message packet

IndexOutOfRangeException when handling MultiBlockUpdate Packets

Not everytime, but often when handling MultiBlockUpdate Packets, an exception like this is thrown:

There occurred an error while handling the packet: System.IndexOutOfRangeException: Index was outside the bounds of the array. at MineSharp.World.World.MultiblockUpdate(Int64[] blocks, Int32 cX, Int32 cY, Int32 cZ) in ...\MineSharp\Components\MineSharp.World\World.cs:line 60 at MineSharp.Bot.Modules.WorldModule.handleMultiBlockChange(PacketMultiBlockChange packet) in ...\MineSharp\MineSharp.Bot\Modules\WorldModule.cs:line 61 at MineSharp.Bot.MinecraftBot.<>c__DisplayClass126_01.<On>b__1(IPacketPayload p) in ...\MineSharp\MineSharp.Bot\MinecraftBot.cs:line 167 at MineSharp.Bot.MinecraftBot.Events_PacketReceived(MinecraftClient client, IPacketPayload packet) in ...\MineSharp\MineSharp.Bot\MinecraftBot.cs:line 185 at MineSharp.Protocol.MinecraftClient.<>c__DisplayClass53_0.<StreamLoop>b__1(Object _) in ...\MineSharp\Components\MineSharp.Protocol\MinecraftClient.cs:line 188

Stuck in Connect() method

In 1.20.3 and 1.20.4, the Connect() method sometimes never resolves.

I think this is because of the PhysicsPlugin, which calls await WorldPlugin.WaitForChunks(), which never resolves because the client does not received any chunks.

My guess is that the bot messes up the ChunkBatchReceivedPacket packet with a ChunksPerTick of 0, so the server never sends any chunks again.

Edit: The bug also occurs in 1.20.1. The PlayerPlugin never finishes initialization.

Cannot use different Microsoft accounts to log in

Once logged in to a Microsoft account, MineSharp is always using this account (at least on Linux), no matter what account identifier provided.
I'm pretty sure it worked on Windows when I first implemented it, but now on Linux it doesn't work.

Why make it so difficult to read and write packets?

I studied roughly how your program works and was very surprised. It seems to me, but maybe packet read and write methods would create a lot of overhead. Why didn't you just create an IPacket interface with Read and Write methods?

See how it can be made simpler in my library.

1.19 and bedrock support

Hi.
So im actually making my own minecraft client based on MineSharp and i would love to ask.
Can you guys please implement 1.19 and bedrock suport? Bedrock is easy cause ConcreteMC/Alex already has it.
But about 1.19. Why did no one implement it yet? Is it because of missing docs or what?
Also I would love to collab to add some more features.

Please continue this conversation using e-mail or discord:
- Email: [email protected]
- Discord: ItsRedly#1174

Thank you.

Refresh json files once in a while

MineSharp.Data downloads json files for a specific version when it's first loaded. But them as well as the dataPaths.json file are never refreshed.

Compile time version selection by using preprocessor directives

Currently minesharp compiles every version data to MimeSharp.Data, which makes the compiled output bigger than it's needs. If we use preprocessor directives to declare version at compile time, the compiler won't include all version data to compiled output, which makes the output smaller.

We can also take advantage of this to create version specific features so we can have the unnecessary features of a specific version not to compile. This makes the code will potentially run faster because the if checks at runtime is already gone on versions thst if check is unnecessary on.

Move command doesn't work

Hello,

Really like the project and the command line interface with autocomplete is very cool. Just having an issue with move Forward for example.

An error occurred while performing command!
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
  at bool System.Collections.Generic.List`1.Enumerator.MoveNext()
  at void MineSharp.ConsoleClient.Console.Commands.Misc.BotLogCommand.DoAction(string[] argv, CancellationToken

Window items dont load

Since the new protocol data, when an WindowItems Packet is received, the following exception is thrown:

[INFO ][20:41:13][Bot ] > Initialized Player entity: Location=(-129,5 / 68,12482421918145 / -57,5) [ERROR][20:41:13][Protocol ] > There occured an error while handling the packet: System.IndexOutOfRangeException: Index was outside the bounds of the array. at MineSharp.Windows.Window.SetSlot(Slot slot) in E:\Programming\MineSharp\Components\MineSharp.Windows\Window.cs:line 106 at MineSharp.Windows.Window.UpdateSlots(Slot[] slotData) in E:\Programming\MineSharp\Components\MineSharp.Windows\Window.cs:line 127 at MineSharp.Bot.Modules.WindowsModule.handleWindowItems(PacketWindowItems packet) in E:\Programming\MineSharp\MineSharp.Bot\Modules\WindowsModule.cs:line 166 at MineSharp.Bot.MinecraftBot.<>c__DisplayClass126_01.<On>b__1(IPacketPayload p) in E:\Programming\MineSharp\MineSharp.Bot\MinecraftBot.cs:line 167 at MineSharp.Bot.MinecraftBot.Events_PacketReceived(MinecraftClient client, IPacketPayload packet) in E:\Programming\MineSharp\MineSharp.Bot\MinecraftBot.cs:line 185 at MineSharp.Protocol.MinecraftClient.<>c__DisplayClass54_0.<<streamLoop>b__1>d.MoveNext() in E:\Programming\MineSharp\Components\MineSharp.Protocol\MinecraftClient.cs:line 201

Probably because MineSharp.Data.Protocol.Slot.ToSlot() just sets SlotNumber to -1

Support 1.20.2-1.20.4

A lot of errors thrown when connecting to 1.20.2 server.
Not yet tested with 1.20.3+

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.