# Minecraft RPC

A [gRPC](https://grpc.io) interface for Minecraft

## Project info

Project is based on [minecraf gRPC project](https://github.com/real-itu/minecraft-rpc) created for EvoCraft.
It is uses [SpongeVanila](https://spongepowered.org/) version `11.0.0=SNAPSHOT` and it can broke with new updates.

Citation

    @inproceedings{
        grbic2021evocraft,
        title={EvoCraft: A new challenge for open-endedness},
        author={Grbic, Djordje and Palm, Rasmus Berg and Najarro, Elias and Glanois, Claire and Risi, Sebastian},
        booktitle={International Conference on the Applications of Evolutionary Computation (Part of EvoStar)},
        pages={325--340},
        year={2021},
        organization={Springer}
    }

## How to Run project

Generate gRPC files

### Preparation

Install following required resources
* __java__ 17.0.10 
  * You can download java for example from [here](https://www.openlogic.com/openjdk-downloads). 
  * You can check your version with `java -version`
* __maven__ 3.9.6 
  * You can download java from [here](https://maven.apache.org/download.cgi).
  * You can check your version with `mvn -version`
* __python__ 3.11 
  * You can download java from [here](https://www.python.org/downloads/).
  * You can check your version with `python --version`
* __grpc_tools__ 1.60.1 
  * You can install if via `pip install grpcio-tools` (or `pip install  grpcio-tools==1.60.1`).
  * You can check your version with `pip show grpcio-tools`

(The project was run and tested on: __OS__ - Win11)

### Java Server & Sponge Mod (Plugin) .jar

    mvn clean package 

resulting fat `.jar` file will be generated to `targer/minecraft-rpc-x.x.x.jar`

### Python client

general

    {/path/to/python311/}python -m grpc_tools.protoc -I=src/main/proto --python_out=clients/{output_folder} --pyi_out=clients/{output_folder} --grpc_python_out=clients/{output_folder} src/main/proto/minecraft.proto

example

    C:\Users\Martin\AppData\Local\Programs\Python\Python311\python -m grpc_tools.protoc -I=src/main/proto --python_out=clients/python --pyi_out=clients/python --grpc_python_out=clients/python src/main/proto/minecraft.proto

resulting files will be in `clients/{output_folder}`.
- minecraft_pb2.py
- minecraft_pb2.pyi
- minecraft_pb2_grpc.py

## Migration from Sponge API7 to API8+

Highlight of the most important changes in migrating.

### Important pom.xml change

This part of `pom.xml` prevents error caused by the method` toImmutableMap(Function, Function)` is not found in the `ImmutableMap` class at runtime.
(version `32.1.3-android` for sponge `11.0.0=SNAPSHOT`)

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>{version}-android</version>
                <scope>provided</scope>
                <exclusions>
                    <exclusion>
                        <groupId>*</groupId>
                        <artifactId>*</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

Updating all dependencies and other packages versions to correct one for the version

| Artifact Id           | Old version   | New version       |
|-----------------------|---------------|-------------------| 
| java                  | 8             | 17                |
| protobuf-java         | 3.7.1         | 3.19.0            | 
| grpc-all              | 1.21.0        | 1.60.1            |
| spongeapi             | 7.2.0         | 11.0.0-SNAPSHOT   |
| protobuf-maven-plugin | 0.5.1         | 0.6.1             |
| maven-site-plugin     | 3.7.1         | 3.9.1             |
| maven-shade-plugin    | 2.3           | 3.2.4             |
| maven-compiler-plugin | 3.8.0         | 3.12.1            | 

### Add Resources

Sponge API8+ requires `sponge_plugins.json`. The file needs to be placed in `src/main/resources/META-INF/`.
Make sure that entry point is correct

    "entrypoint": "dk.itu.real.ooe.MinecraftRPC",

### Code changes

New Sponge API8+ is vastly different and the majority of the code was rewritten.

### New block types in proto file

New versions of __Minecraft__ can introduce new blocks. 
To correctly adjust the proto file generate the description of adjusted `enum BlockType` via code in `blocks_enum_generator\` folder.
Place desired version of __SpongeVanilla__ server to the folder under name `sp.jar` and run `generate_blocks_enum.bat` from command line while in the folder.
The result will be in `blocks_enum.txt`

## Set up the modded Minecraft server

### Starting the modded Minecraft server

* Create a folder `minecraft-rpc` (or with your desired name `{name}`).
* Download the latest stable [spongevanilla 1.20.2](https://spongepowered.org/downloads/spongevanilla?minecraft=1.20.2&offset=0) into `minecraft-rpc`
* Generate the plugin-server `minecraft-rpc-xxx.jar` and place it to `minecraft-rpc/mods`.
* Your `minecraft-rpc` should look like (where `xxx` are version numbers)
```
minecraft-rpc/
    spongevanilla-1.20.2-xxx.jar
    mods/
        minecraft-rpc-xxx.jar      
```
* Start the server with `java -jar spongevanilla-1.20.2-xxx.jar`
  * (or `java -Xmx{n}G -Xms{n}G -jar spongevanilla-1.20.2-xxx.jar` where `{n}` is amount memory to dedicate to process)
* The first time you start the server you must accept the Minecraft EULA by modifying `eula.txt`
* You should see a bunch of output including `[... INFO] [minecraft_rpc]: Listening on 5001`.
  This means it's working and the server is ready for commands on **port 5001**.

### Calling the server

The interface is defined using [gRPC](https://grpc.io). Read the definition at [src/main/proto/minecraft.proto](src/main/proto/minecraft.proto)

See [clients](clients) for generated clients and examples

Using the interface definition file you can generate clients for (almost) any programming language you like. See [https://grpc.io/docs/languages/](https://grpc.io/docs/languages/)