Skip to main content

A library for creating minecraft datapacks.

Project description

Introduction to pynecraft

pynecraft is a Python package that lets you create a data pack and its functions using python, so you can take advantage of python’s tools. It generates the files required for a data pack from your python code.

Writing data packs requires handling a lot of details, from putting things in the right folders in the correct format, to how those things are expressed in the first place. All errors are discovered during reload, or possibly at run time, rather than when you’re editing the files in the first place.

In contrast, Python (and other actual programming languages) have syntax checkers, errors for misspelled function and field names, IDEs that do auto-completion and checks for improper data types. And of course you have basic programming language tools, like loops and functions with parameters that generate similar output for varying input.

pynecraft lets you use all that in creating your data pack. It represents the data pack as an object that understands much of the layout of the data pack, and provides ways to generate commands inside functions from within Python, as well as JSON files all around the data pack. This means you have all the power of a real programming language to generate your commands, including syntax and other error checks, python functions that generate minecraft commands that can be used in minecraft functions. And pynecraft also knows where to store what files in a data pack, and how to generate some boilerplate, like the pack.mcmeta.

As a simple example, the following is a “hello_world” data pack:

pack = DataPack('hello_world')
pack.function_set.add(Function('hello').add(say('Hello, World!')))
pack.save(minecraft_world)

We create a DataPack object, and add a Function to it. That function has, as its sole command, the result of calling say, which is a pynecraft function that returns a minecraft say command with the provided string. We add it to the function hello_world:hello inside the hello_world data pack. Any number of commands can be added at once, or any other time before the save(), which writes out the data pack files to a specified directory.

If ‘minecraft_world’ is a minecraft world save, the save() method will create:

minecraft_world
|-- datapacks
    |-- hello_world
        |-- README                 # A generated warning not to edit this by hand
        |-- pack.mcmeta
        |-- data
            |-- functions
                |-- hello.mcfunction
                    "say Hello, World!"

Pynecraft also has simple calls for obscure or complex minecraft mechanisms. For example, there is a Score class that holds the definition of a score (name and objective). You can use it as a parameter to commands like scoreboard, but more importantly, it has methods to generate commands simply, such as score.add(15) to generate scoreboard players add <i>player objective</i> 15.

Other examples of simplifications include:

  • Item.of(<i>block</i>), which returns the NBT required to store an item including its state and data as (say) an item you would summon into the world to be picked up by a player

  • The Sign class that generates all the messy NBT to set sign texts and commands, so:

    Sign((None, 'hi', 'there'), (tell(p(), 'Hello!'),)).place((0, 100, 0), WEST)

    …which gives a list of three lines of text for the sign and one tell command, generates the lovely:

    setblock 0 100 0 oak_sign[rotation=4]{front_text: {messages: ['{"text": "",
      "clickEvent": {"action": "run_command", "value": "/tell @p Hello!"}}',
      '{"text": "hi"}', '{"text": "there"}', '{"text": ""}']}}

    …a command that places an oak sign facing west at 0 100 0 with three lines of text which, when touched by the player, tells them “Hello!” (in case you couldn’t decode all that NBT). (Actually it generates two commands, the first of which sets that block to air, to work around the fact that setting a sign at a place sometimes will not overwrite an existing sign, which is another thing you’d probably prefer not to worry about, and pynecraft takes care of for you.)

Pynecraft is distributed under the standard MIT license.

Why?

Python has tools: editors, debugger, formatters, etc. It also checks syntax at compile time and has run-time type checking. These are really useful for eliminating errors from your commands quickly and easily.

It is a full language, so you can use it to generate your commands. You can have a check for every mob that starts with ‘a’, or every value of a score from one to ten. You can write a python function that generates complicated commands based on a few parameters.

There are macro languages that could help with this, but the quoting issues alone can be daunting, and they won’t do the syntax and run-time validation of commands.

I wrote pynecraft because I was working on my RestWorld resource pack test world. It makes extensive use of functions. After using a macro system to generate things for a while, it was clear that this was ugly and difficult. Pynecraft made a big difference. The “saying what I mean to say” stage of development nearly entirely vanished. I still had bugs and made mistakes, out they were almost never typos or command syntax problems. And with some of the simplification tools, I also stopped making mistakes about how relatively complicated things were done.

I hope it will help you, too!

(Currently pynecraft only supports Java Edition commands. A Bedrock version is certainly possible as a companion project; if someone wants to undertake it, please let me know.)

Resources

Here are some useful resources:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynecraft-0.7.1.tar.gz (129.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynecraft-0.7.1-py3-none-any.whl (103.5 kB view details)

Uploaded Python 3

File details

Details for the file pynecraft-0.7.1.tar.gz.

File metadata

  • Download URL: pynecraft-0.7.1.tar.gz
  • Upload date:
  • Size: 129.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for pynecraft-0.7.1.tar.gz
Algorithm Hash digest
SHA256 adc60327c8d27807e92c78b45021df9b82716a6f42e48917326fda79e0221a7e
MD5 e2a3da87a29f1c4912270c47a2310257
BLAKE2b-256 3d83930ba72a09c766c859ab743bf0beba46563c0575ba74fd9dcd54d63403d3

See more details on using hashes here.

File details

Details for the file pynecraft-0.7.1-py3-none-any.whl.

File metadata

  • Download URL: pynecraft-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 103.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.6

File hashes

Hashes for pynecraft-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe2d6c7ad244457cd338327800f4380f46eb369794d2508515961b6d16025d15
MD5 cd3d2806fbde86ad8b7f73df8de4ab85
BLAKE2b-256 078e5e7cdf0f9e3aededeee86677d97b491bf6a7c34f848c9d8a2819a20b912f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page