# @icetank/mcproxy

> a minecraft proxy library powered by mineflayer

Latest version **1.0.8-beta.3** (published 2024-05-06) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @icetank/mcproxy
pnpm add @icetank/mcproxy
yarn add @icetank/mcproxy
bun add @icetank/mcproxy
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8-beta.3 |
| Published | 2024-05-06 |
| First published | 2023-04-11 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 418.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | rob9315 |
| Maintainers | icetank |
| Keywords | minecraft, proxy, library, mineflayer |

## Links

- npm: https://www.npmjs.com/package/@icetank/mcproxy
- Repository: https://github.com/IceTank/mcproxy
- Homepage: https://github.com/icetank/mcproxy#readme
- Issues: https://github.com/icetank/mcproxy/issues
- npm.io page: https://npm.io/package/@icetank/mcproxy

## Dependencies (3)

- [deepcopy](https://npm.io/package/deepcopy.md) ^2.1.0
- [mineflayer](https://npm.io/package/mineflayer.md) ^4.11.0
- [smart-buffer](https://npm.io/package/smart-buffer.md) ^4.2.0

## Recent versions

- 1.0.8-beta.3 (latest) — 2024-05-06
- 1.0.8-beta.2 — 2024-04-29
- 1.0.8-beta.1 — 2024-04-27
- 1.0.7 — 2024-03-09
- 1.0.7-beta.1 — 2024-03-09
- 1.0.6 — 2024-02-04
- 1.0.5 — 2024-02-04
- 1.0.4 — 2023-08-15
- 1.0.3 — 2023-05-13
- 1.0.2 — 2023-04-20
- 1.0.1 — 2023-04-20
- 1.0.0 — 2023-04-11

## README

[![Release][release-shield]][release-link] [![Continuous Integration][ci-shield]][ci-link] [![Latest][semver-shield]][semver-link] [![Beta][beta-shield]][beta-link]

# mcproxy

a minecraft proxy library powered by mineflayer that replicates data as well as possible from available information of mineflayer. Forked version of [mcproxy](https://github.com/rob9315/mcproxy).

# Contribution

This project was inspired by [2bored2wait](https://github.com/themoonisacheese/2bored2wait) and now serves as a dependency of it. This project relies heavily on the great work that the awesome people of the [PrismarineJS project](https://prismarine.js.org/) have done.

# Installation

To add this to your project, just install it with your favourite package manager.

```sh
npm install @icetank/mcproxy
# or
yarn add @icetank/mcproxy
# or
pnpm add @icetank/mcproxy
```

It is recommended to use yarn for this project. Altho other package managers should work too.

# API

This project provides the `Conn` class, which enables you to create a connection to a server and connect clients to the Conn instance. The connection will stay after you disconnect from the Conn instance.

```ts
// How to instanciate Conn:
import { Conn } from '@icetank/mcproxy';
const conn = new Conn(botOptions: mineflayer.BotOptions, options: ConnOptions);
```

## Types and Classes

### Interface `BotOptions`

The botOptions which are needed in the constructor of Conn, are the same as the ones from mineflayer itself.

### Interface `ConnOptions`

ConnOptions regulate Conn-specific settings.

- Object. Optional
  - `optimizePacketWrite` - Boolean. Optional. Setting for writing the received packet buffer instead off re serializing the deserialized packet. Packets that had there data changed inside the middleware are effected by this. Defaults to `true`.
  - `toClientMiddleware` - [Middleware](#middleware). Optional. A default to Client middleware to be attached to every client.
  - `toServerMiddleware` - [Middleware](#middleware). Optional. A default to Server middleware to be attached to every client.

### `Client` | `pclient`

The Client class is the same as the minecraft-protocol client class with the one exception that it can also contain the following settings used in the Conn class to cause different behaviors.

- `toClientMiddlewares` - `Middleware[]`. To client [Middleware](#middleware) array
- `toServerMiddlewares` - `Middleware[]`. To server [Middleware](#middleware) array

### `Middleware`

A function to interact with send packets between a connected client and the server. The middleware function should return different values depending on what should be done with the packet:

- Changing packets: Return the new packet data
- Canceling the packet: Return `false`
- Un-Cancel a packet that was canceled by a previous packet: Return `true`
- Do nothing: Return `undefined`

The returned value can also be wrapped in a promise. The middleware will await the promise result before continuing to process the packet.

#### Middleware Arguments:

- `data` - Object that contains the packet in transit
  - `bound` - Either `server` or `client`. The direction the packet is traveling in.
  - `writeType` - At the moment only `packet`. The type off the packet in transit.
  - `meta` - Object of Packet meta
    - `name` - Packet name
    - `state` - Packet state
  - `pclient` - The client connected to this packet. Either the client sending the packet or undefined if the packet is send by the server
  - `data` - Parsed packet data
  - `isCanceled` - Boolean if the packet has already been canceled or not

```ts
const middlewareFunction: PacketMiddleware = ({ meta, isCanceled }) => {
  if (isCanceled) return; // Not necessary but may improve performance when using multiple middleware's after each other
  if (meta.name !== 'chat') return; // Returns undefined so the packet is not affected
  if (data.message.includes('censor')) return false; // Cancel all packets that have the word censor in the chat message string
};
```

### Class `StateData`

State Keeping class to extend prismarine-worlds missing state keeping information. Also holds the bot reference.

- `recipes` - `number[]` Used to keep track off recipes
- `flying` - `boolean` Used to keep track off if the proxy should be flying or not
- `bot` - Bot. A mineflayer bot instance attached to the connection.

### `generatePackets(stateData: StateData, pclient?: Client)`

- `stateData` - Instance off [`StateData`](#class-statedata)
- `pclient` - The pclient to generate data for

The internal method used to generate packets from a bot and an optional pclient. If a pclient is provided some aspects of the packets are changed such as the uuid and some version specific changes might be done for compatibility (though not all versions are supported \[yet])

```ts
import { generatePackets } from '@icetank/mcproxy';
let packets: Packet[] = generatePackets(bot, pclient?: Client);
packets.forEach((packet)=>pclient.write(...packet));
```

### `Conn.bot`

The [mineflayer Bot](https://github.com/PrismarineJS/mineflayer/blob/master/docs/api.md#bot) integrated into the library, **You cannot write with the bot's `bot._client.write()` method**, instead use the [`Conn.write()`](#connwriteif) method if you need to manually send packets.

### `Conn.pclient`

The proxyClient which is able to send packets to the server. Also receives them as a part of the `Conn.pclients` array. **Do not write to this manually**

### `Conn.pclients`

An array of all proxyClients which are attached to the Connection. Use [`Conn.attach()`](#connattach) to add a client to the array and `Conn.detach()`, they handle some more things which you'll probably want as well.

### `Conn.generatePackets(pclient?: Client)`

- `pclient` - Optional. Client to specify uuid and entity id when generating packets.

Returns the generated packets for the current gamestate

```ts
Conn.generatePackets(pclient?: Client): Packet[]
```

### `Conn.sendPackets(pclient: Client)`

- `pclient` - The client to send the packets to

Calls `Conn.generatePackets()` and sends the result to the proxyClient specified

```ts
Conn.sendPackets(pclient: Client);
```

### `Conn.attach(pclient: Client, options?)`

The pclient specified will be added to the `Conn.pclients` array and the `Conn.receivingPclients`, which means that it will receive all packets from the server. If you want the client to be able to send packets to the server as well, don't forget to call `Conn.link()`

- `pclient` - The client to be attached
- `options` - Optional. Object.
  - toClientMiddleware - Optional. A middleware function array to be used as this clients middle ware to the client. See middleware for a function definition.
  - toServerMiddleware - Optional. A middleware function array to be used as this clients middle ware to the server

```ts
Conn.attach(pclient: Client, options?: { toClientMiddleware?: PacketMiddleware[], toServerMiddleware?: PacketMiddleware[] })
```

### `Conn.detach(pclient: Client)`

The pclient specified will be removed from the `Conn.pclients` array, meaning that it will no longer receive packets from the server. If the client was linked before, `Conn.unlink()` will also be called.

- `pclient` - The client to detach.

```ts
Conn.detach(pclient: Client)
```

### `Conn.link()`

Stops the internal bot from sending any packets to the server and starts relaying all packets from the proxyClient to the server.

```ts
Conn.link(pclient: Client)
```

### `Conn.unlink()`

Reverses the `link` method. The bot becomes the one to send packets to the server again.
If the proxyClient should be detached as well

```ts
Conn.unlink();
```

### `Conn.writeIf()`

An internal method for filtering the bots Packets, can be used outside but as an API method basically useless.

```ts
Conn.writeIf(name, data);
```

### `Conn.disconnect()`

Disconnects from the server and detaches all pclients

<!-- markdown links -->

[npm-shield]: https://img.shields.io/github/package-json/v/rob9315/mcproxy?label=npm&style=for-the-badge
[npm-link]: https://www.npmjs.com/package/@icetank/mcproxy
[semver-link]: https://www.npmjs.com/package/@icetank/mcproxy

---
_Source: https://npm.io/package/@icetank/mcproxy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
