Skip to content

Commit

Permalink
Update readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Jan 26, 2024
1 parent 5587810 commit 77dabc2
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 102 deletions.
80 changes: 16 additions & 64 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
[![License](https://img.shields.io/github/license/psu-de/MineSharp?style=for-the-badge)](https://github.com/psu-de/MineSharp/blob/main/LICENSE)
[![Nuget](https://img.shields.io/nuget/v/MineSharp.Bot?style=for-the-badge)](https://www.nuget.org/packages/MineSharp.Bot)

\
![banner](banner.png)

# MineSharp

**This Project is not finished and under development!**

Create Minecraft bots with C#! \
Inspired by [Mineflayer](https://github.com/PrismarineJS/mineflayer).
MineSharp is a framework to work with minecraft. \
My goal is to create a bot framework which is able to do anything you can do in the vanilla client,
but theoretically MineSharp could also be used to create a Server or custom client.

If you're interested in this project, feel free to contribute!
Currently MineSharp works with **Minecraft Java 1.18 - 1.20.4**.

Minecraft Java 1.18 - 1.20.4 supported
If you're interested in this project, feel free to contribute!

# Current features

- โœจ Supported Versions: 1.18.x - 1.20.4
- ๐Ÿ“ˆ Player Stats
- โšก Events
Expand All @@ -32,26 +32,24 @@ Minecraft Java 1.18 - 1.20.4 supported
- ๐Ÿ“ Chat (Reading and Writing)

# Roadmap
- ๐Ÿ”Ž Simple Pathfinder (see [feature/pathfinder](https://github.com/psu-de/MineSharp/pull/32))

- ~~โœจ Support 1.20.3 - 1.20.4~~
- ๐Ÿ”Ž Simple Pathfinder

# Example
# Example Snippet
See [MineSharp.Bot](../MineSharp.Bot/README.md) for more information about creating bots.
```csharp
using MineSharp.Bot;
using MineSharp.Bot.Plugins;

MineSharpBot bot = await MineSharpBot.CreateBot(
"MineSharpBot",
"127.0.0.1",
25565,
offline: true);
MineSharpBot bot = await new BotBuilder()
.Host("localhost")
.OfflineSession("MineSharpBot")
.CreateAsync();

var chat = bot.GetPlugin<ChatPlugin>();
ChatPlugin chat = bot.GetPlugin<ChatPlugin>();

if (!await bot.Connect())
{
Console.WriteLine("Could not connect to server!");
Console.WriteLine("Could not connect to server! Is the server running?");
Environment.Exit(1);
}

Expand All @@ -67,55 +65,9 @@ while (true)
if (input != null)
await chat.SendChat(input);
}

```

# Projects Overview

### [MineSharp.Core](../MineSharp.Core)

Contains core functionality and common Minecraft types like Entity, Block, etc...

### [MineSharp.Data](../MineSharp.Data)
MineSharp.Data is a wrapper for [minecraft-data](https://github.com/PrismarineJS/minecraft-data).
Currently the following data is supported:
- Biomes
- Blocks
- Collisions
- Effects
- Enchantments
- Entities
- Features
- Items
- Language
- Protocol (Packet id's and names)
- Recipes

### [MineSharp.Bot](../MineSharp.Bot)

API to directly interact with a minecraft server. \
See [MineSharp.Bot README](../MineSharp.Bot)

## Components

### [MineSharp.Auth](../Components/MineSharp.Auth)

Used to login to Microsoft, connect to Mojang Auth servers and create a Minecraft Session.

### [MineSharp.Physics](../Components/MineSharp.Physics)

Logic to simulate entity physics from minecraft\
Thanks to [ConcreteMC/Alex](https://github.com/ConcreteMC/Alex)

### [MineSharp.Protocol](../Components/MineSharp.Protocol)

Implements the Minecraft Protocol. Contains logic to connect to a Minecraft server and read/write packets from/to it.

### [MineSharp.World](../Components/MineSharp.World)

Basic functionality to represent a Minecraft World and interact with it.

### Links
### Credits
Without the following resources this project would not be possible. Thanks to all people involved in those projects!

- [wiki.vg](https://wiki.vg)
Expand Down
6 changes: 5 additions & 1 deletion MineSharp.Bot/MineSharpBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public class MineSharpBot
// This field is used for syncing block updates since 1.19.
internal int SequenceId = 0;

internal MineSharpBot(MinecraftClient client)
/// <summary>
/// Create a new MineSharpBot instance with a <see cref="MinecraftClient"/>
/// </summary>
/// <param name="client"></param>
public MineSharpBot(MinecraftClient client)
{
this.Client = client;
this.Data = this.Client.Data;
Expand Down
156 changes: 119 additions & 37 deletions MineSharp.Bot/README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,129 @@
## MineSharp.Bot

Connect and interact with Minecraft servers. \
A `MinecraftBot` is a composition of multiple `Plugin`s. \
Currently, these are the default plugins:
A `MineSharpBot` uses a `MinecraftClient` (see MineSharp.Protocol) to connect to a Minecraft Server. \
The bot can have multiple plugins. Each plugin can handle some packets sent by the server and/or provide methods to interact with the world.

## Creating Bots
A bot can be created using a `MinecraftClient`.
To help out, you can use the `BotBuilder` class to fluently create a bot.

### Bot Builder
- `.Host()` configure the hostname
- `.Port()` configure the port (default = 25565)
- `.Data()` configure the `MinecraftData` instance
- `.AutoDetectData()` (default) auto detect minecraft data version if none was configured
- `.Session()` configure the session object
- `.OfflineSession()` configure session to be an offline session
- `.OnlineSession()` configure session to be an online session (login happens when calling `Create()` or `CreateAsync()`)
- `.WithPlugin<T>()` add Plugin of type `T`
- `.ExcludeDefaultPlugins()` do not add default plugins (listed below)
- `.AutoConnect()` automatically connect to the server when creating the bot
- `.WithProxy()` configure a proxy
- `.CreateAsync()` create a new bot with the configuration
- `.Create()` equivalent of `CreateAsync().Result`

## Plugins

A plugin can handle packets sent by the server and/or provide methods to interact with the server.

Currently, these are the plugins enabled by default:
- Chat Plugin (Read and Write chat messages)
- Crafting Plugin (Craft items)
- Entity Plugin (Keeps track of entities)
- Physics Plugin (Simulate player physics)
- Player Plugin (Keeps track of the bot himself as well as other players and the weather)
- Window Plugin (Bot's inventory and open chests or other blocks)
- World Plugin (Keep track of the world)

### Example
This is an example for a simple chat bot.
```csharp
using MineSharp.Bot;
using MineSharp.Bot.Plugins;

MineSharpBot bot = await MineSharpBot.CreateBot(
"MineSharpBot",
"127.0.0.1",
25565,
offline: true);

var chat = bot.GetPlugin<ChatPlugin>();

if (!await bot.Connect())
{
Console.WriteLine("Could not connect to server!");
Environment.Exit(1);
}

while (true)
{
var input = Console.ReadLine();
if (input == "exit")
{
await bot.Disconnect();
break;
}

if (input != null)
await chat.SendChat(input);
}
```

TODO: Docs for every plugin
Other plugins not enabled by default:
- Auto Respawn (Automatically respawn when dead)

To add a plugin to the bot, `bot.LoadPlugin(plugin)` can be used. \
To access a plugin, use `bot.GetPlugin<>()`

#### Chat Plugin
- Handles all chat packets and provides abstraction for different minecraft versions
- Handle and parse the CommandTree
- โšก `OnChatMessageReceived` event. Fired when any chat message or game information message is received
- ๐Ÿ“จ `SendChat()` method. Send a chat message to the server.
- ๐Ÿ“ง `SendCommand()` method. Send a '/' command to the server. Only for mc >= 1.19

#### Crafting Plugin
- ๐Ÿ”Ž `FindRecipes()`. Find recipes for a given item that can be crafted with the items in the bots inventory
- ๐Ÿ”Ž `FindRecipe()`. Equivalent of `FindRecipes().FirstOrDefault()`
- ๐Ÿ”ข `CraftableAmount()`. Calculate how often a recipe can be crafting with the items in the bots inventory.
- ๐Ÿชš `Craft()`. Craft the given recipe `n` times.

#### Entity Plugin
- Handles all packets regarding entities (position, effects, etc..)
- โšก `OnEntitySpawn`. Fired when an entity spawned
- โšก `OnEntityDespawned`. Fired when an entity despawned
- โšก `OnEntityMoved`. Fired when an entity moved
- ๐Ÿท `Entities`. Dictionary mapping all entities from their numerical server id to the `Entity` object

#### Physics Plugin
- Update the bots position on the server
- โšก `BotMoved`. Fired when the bot moved
- โšก `PhysicsTick`. Fired after each tick of the physics simulation
- ๐ŸŽฎ `InputControls`. Input controls used to control movement
- ๐Ÿช‚ `Engine`. The underlying physics simulation / engine
- โณ `WaitForTicks()`. Wait until a number of physics ticks are completed
- โ›ฐ๏ธ `WaitForOnGround()`. Wait until the bot is on the ground
- ๐Ÿ”ƒ `ForceSetRotation()`. Set the bots rotation in a single tick
- ๐Ÿ‘“ `ForceLookAt()`. Look at the given position in a single tick
- ๐Ÿ‘€ `Look()`. Slowly transition to the given rotation
- ๐Ÿ‘€ `LookAt()`. Slowly look at the given position
- ๐Ÿ”ซ `Raycast()`. Returns the block the bot is currently looking at

#### Player Plugin
- Handles packets regarding the Bot entity and other players on the server
- โšก `OnHealthChanged`. Fired when the bots health, food or saturation was updated.
- โšก `OnRespawed`. Fired when the bot respawned or changed the dimension.
- โšก `OnDied`. Fired when the bot died.
- โšก `OnPlayerJoined`. Fired when another player joined the server
- โšก `OnPlayerLeft`. Fired when another player left the server
- โšก `OnPlayerLoaded`. Fired when another player came into the visible range of the bot and their entity was loaded.
- โšก `OnWeatherChanged`. Fired when the weather has changed. (TODO: Move to WorldPlugin)
- ๐Ÿค– `Self`. The `MinecraftPlayer` representing the bot itself
- ๐Ÿค– `Entity`. The `Entity` representing the bot itself (equivalent of `Self.Entity`)
- ๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ `Players`. A dictionary mapping all player's uuids to their `MinecraftPlayer` object
- ๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ `PlayerMap`. A dictionary mapping all player's numerical server id to their `MinecraftPlayer` object.
- ๐Ÿ’“ `Health`. Health of the Bot (value between 0.0 - 20.0)
- ๐Ÿ— `Saturation`. The Saturation level of the bot
- ๐Ÿ• `Food`. The food level of the bot
- ๐ŸŒ˜ `Dimension`. The name of the dimension the bot is currently in
- ๐Ÿƒ `IsAlive`. Boolean indicating whether the bot is alive
- ๐ŸŒง๏ธ `IsRaining`. Boolean indicating whether it is raining
- โ˜” `RainLevel`. Float indicating how much it is raining
- โ›ˆ๏ธ `ThunderLevel`. The thunder level
- โ˜€๏ธ `Respawn()`. Respawn the bot if it is dead.
- ๐Ÿ’ช `SwingArm()`. Plays the swing arm animation.
- ๐Ÿคบ `Attack()`. Attack the given entity

#### Window Plugin
- Handles packets regarding windows
- โšก `OnWindowOpened`. Fired when a window opened
- โšก `OnHeldItemChanged`. Fired when the held item changed
- ๐Ÿ“ฆ `Inventory`. The *Window* representing the bots inventory
- ๐ŸชŸ `CurrentlyOpenedWindow`. The window which is currently open.
- ๐ŸŽˆ `HeldItem`. The *Item* the bot is currently holding in the main hand
- ๐Ÿ‘‰ `SelectedHotbarIndex`. The index of the selected hotbar slot
- โŒ› `WaitForInventory()`. Wait until the inventory's item are loaded
- ๐Ÿงฐ `OpenContainer()`. Try to open the given block (eg. chest, crafting table, ...)
- โŒ `CloseWindow()`. Close the window
- ๐Ÿ‘‰ `SelectHotbarIndex()`. Set the selected hotbar index
- ๐Ÿ™‹ `UseItem()`. Use the item the bot is currently holding
- ๐Ÿ‘จโ€๐Ÿ”ง `EquipItem()`. Find and equip an item

#### World Plugin
- Handles all block and chunk packets
- ๐ŸŒ `World`. The world of the minecraft server
- โณ `WaitForChunks()`. Wait until all chunks in a radius around the bot are loaded
- โŒจ๏ธ `UpdateCommandBlock()`. Update a command block
- โ›๏ธ `MineBlock()`. Mine the given block
- ๐Ÿ‘ท `PlaceBlock()`. Place a block at the given position

#### Auto Respawn
- Automatically respawns the bot when it died
- โฐ๏ธ `RespawnDelay`. Delay before respawning

0 comments on commit 77dabc2

Please sign in to comment.