- Saved searches
- Use saved searches to filter your results more quickly
- License
- Macuyiko/minecraft-python
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Installing Minecraft¶
- Minecraft¶
- Python 3 Distribution¶
- Java¶
- Minecraft Python API and Minecraft Spigot server¶
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A Jython driven plugin and interpreter system for Minecraft (on top of Spigot)
License
Macuyiko/minecraft-python
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Minecraft Server Python Interpreter
minecraft-python is a Spigot plugin providing the ability to control Minecraft using Python. Contrary to other approaches, this project aims to expose the whole Bukkit API to Python, instead of only providing a few commands by hardcoding or wrapping these in a Spigot plugin.
More background information on how this project came to be can be found on this blog post (a bit outdated at the moment).
You can watch a Youtube video showing off some of the possibilities (also a bit outdated by now but gets the idea across).
The implementation is based on Jython. This has the benefit that the complete Python interpreter system runs inside of the JVM, but comes with the drawback that it only supports Python 2.
With the Jython based system, you have the ability to interact with a Jython interpreter through a telnet server, websocket server, and through chat commands ( /py , /pyload and /pyrestart ). /py runs a line of Python code on a Jython interpreter (each player gets their own interpreter instance). A . (dot) at the beginning of the line can be used in case indentation with whitespace needs to be provided (the Minecraft server removes whitespace so this is provided as a workaround). /pyrestart restarts the Jython interpreter. /pyload takes a local Python file (in the running directory or on the Desktop of the server) and executes it in the Jython interpreter.
Alternatively, remote-client.py can be used to set up a Python REPL that will send commands to the remote Jython interpreter over a websocket connection.
A Telnet client can be used to connect to a telnet-based interface to the remote interpreter.
A built-in Python module, mcapi.py , provides some predefined handy commands which can be imported in the remote interpreter. Putting .py files in a python-plugins directory runs these as «plugins» when starting up the plugin. This interpreter keeps running and can be used to set up global hooks. Other interpreters will be cleaned out after some period of inactivity.
Jython only supports Python 2 for now, and it seems it’ll remain that way for a long while longer. There are various Python 3 JVM interop projects available, though none of which seem to offer the ease-of-use of a full Python on JVM implementation as Jython does.
Py4j comes close, and an earlier commit did provide a way to interact with Minecraft using this library. However, the Py4J implementation relies heavily on callbacks between Python and a JVM, which are sent over the network. Combining this with lots of thread-juggling and Spigot’s internal thread model is daunting. The implementation worked, but was very unstable when trying to perform lots of actions on the Spigot server, so I ultimately removed it from the code base for now. See this commit to get an idea where things ended up — I might add this back in in a separate branch later on.
The explicit goal of this project is to allow programming Minecraft using Python and to provide the full Bukkit API in this environment without resorting to manually wrapping these through a Spigot plugin. Other interesting projects in this space are:
- https://github.com/ammaraskar/pyCraft: modern, Python3-compatible, well-documented library for communication with a MineCraft server. This is on the networking level, however, and rather low-level.
- https://github.com/r1chardj0n3s/pycode-minecraft: similar to command blocks, this plugin allows to code scripts on «Python Blocks». Also uses Jython internally.
- http://www.computercraft.info/: an amazing project adding computers and more to Minecraft, provind a coding interface using Lua. This is all in-game, however, comparable to command blocks or pycode-minecraft . A fine way to work with computers in Minecraft, though less so to work with Minecraft in computers.
- https://github.com/martinohanlon/mcpi: combines https://github.com/py3minepi/py3minepi and https://github.com/martinohanlon/minecraft-stuff. Exposes only some basic commands by sending them over the wire to a Minecraft: Pi Edition server.
- https://github.com/zhuowei/RaspberryJuice: a plugin that implements the Minecraft Pi Edition API, so that mcpi above can be used together with a normal Minecraft server. https://github.com/wensheng/JuicyRaspberryPie extends this a little bit. https://www.nostarch.com/programwithminecraft uses RaspberryJuice + mcpi to write its examples. A nice approach, with the downside that many «cool» Spigot commands are not available (fireworks, spawning, explosions, . ).
- http://scriptcraftjs.org/: similar approach, but uses JavaScript and adds more boilerplate code between the JS engine Java interaction. A bit out of date, sadly.
As of its latest version, the plugin is installed just like any other Spigot plugin. You’ll need Java 8 at least.
On boot, lib-common and python directories will be created automatically. If you want to access other Minecraft plugins in your Python scripts, their JAR files can be copied over to a lib-custom directory.
Below is a short example of what you can do with the interpreter:
# Import some modules from mcapi import * from time import sleep from random import randint MY_NAME = "Macuyiko" # Note: all code runs asynchronously by default. If you want to make world edits, Spigot # forces you to execute these on a synchronised task. Most of the methods included in mcapi # will take care of this automatically # Set the time to sundawn time(0) # Zap the point where I'm looking at bolt(lookingat(player(MY_NAME))) # A small explosion instead explosion(lookingat(player(MY_NAME)), power=2) # Generate a tree (only works if there is room) tree(lookingat(player(MY_NAME))) # Spawn some particles particle(lookingat(player(MY_NAME))) # Spawn an entity (chicken by default) spawn(lookingat(player(MY_NAME))) # Fireworks fireworks(lookingat(player(MY_NAME))) # Let's create an exploding chicken spell def exploding_chicken(player_name): yell("Creating an exploding chicken") chicken = spawn(lookingat(player(player_name))) for i in range(5,0,-1): yell("%s second(s) left. " % i) sleep(1) explosion(location(chicken), power=2) # Try it! exploding_chicken(MY_NAME) # Now let's define a command for this spell # Command functions take a special form func(caller, params) @asynchronous() def cmd_explode_spell(caller, params): exploding_chicken(caller.getName()) # Commands are executed synchronously by Spigot # This means that all actions in exploding_chicken would use the state of the # world as it was at the current server tick when executing the command, # hence, we use the @asynchronous() decorator to force this function to be # ran asynchronously, only synchronising every time a synchronous command is called add_command('chickenspell', cmd_explode_spell) # Try typing `/chickenspell` in Minecraft chat window # Commands can be unregistered using remove_command('chickenspell') # Let's register another command to show of the asynchronous workings @asynchronous() def cmd_growme(caller, params): beginning = lookingat(player(caller.getName())) position = [beginning.x, beginning.y, beginning.z] for i in range(100): setblock(position) #
Plugins works similarly (place this as a .py file in a python-plugins directory):
from mcapi import * from time import sleep from random import randint from org.bukkit.event.player import PlayerJoinEvent @asynchronous() def join_event(e): player = e.getPlayer() player_location = location(player) player_location.y += 20 yell("A chickeny hello to player %s" % (player.getName(),)) for i in range(10): spawn(player_location) listener = add_event_listener(PlayerJoinEvent, join_event)
This project is distributed as BSD 3-Clause License software. See LICENSE.txt for details.
Installing Minecraft¶
The software used for this course is provided as companion to the book Learn to Program with Minecraft. It is recommended that the students purchase the book.
To get things running, you will need:
- Minecraft
- Python 3
- Java
- Minecraft Python API
- The Minecraft server
Minecraft¶
Visit the Minecraft homepage to download. If you do not have an account, please email me and I will make sure you are provided with one.
Python 3 Distribution¶
Python 3 is the distribution we will be using. If you have Python 2, it is recommended that you uninstall Python 2 and install Python 3. If you don’t, there will be some inconsitencies that could be devestatingly confusing. Also, Python 3 has a lot of really cool, new features that aren’t in Python 2.
There are several ways to get Python. I personally recommend the Anaconda distribution. It has a bunch of things packaged with it above and beyond Python that make it useful. Anaconda comes with the Spyder editor. It is a decent editor, but I would recommend:
- PyCharm.
- If you download PyCharm, make sure you download the Community Edition.
- This is my personal favorite. It is lightweight and has many extensions.
- However, it does not run or debug Python files as easily as PyCharm.
- Very similar to Sublime
Java¶
You should have both Minecraft and Python installed at this point. You need to set Java up in order to run the server.
- Click the Start Menu (or press the Windows key)
- Type “cmd” to find the program called cmd. Open this.
- Type java -version at the prompt
- If you see an output describing the version of Java, you already have it and can continue to the next section.
- If you don’t, or it can’t find java, then go to here: http://www.java.com/en/download/
- Click Free Java Download. Then click Agree and Start
- When it is finished downloading, install this.
- IMPORTANT: If it asks you to install extra things or set Yahoo! as your homepage, click no.
Minecraft Python API and Minecraft Spigot server¶
An API is an interface. We will use it as a library that lets us communicate with the Minecraft server. We will not be able to edit the server in any way, but instead, just tell it instructions.
We will be using the Spigot server because it allows for the API to talk to it. Standard Minecraft does not.
- Go to https://www.nostarch.com/pythonwithminecraft/
- Download the MinecraftTools.zip for your operating system.
- When it has finished downloading, you can open it.
- a zip file is known as a compressed file
- it allows you to compress a set of files to make them smaller for downloading
- all operating systems let you open these files
- Important Although it looks like you have a folder, the contents of the Zip file are not a folder
- Create a folder somewhere convenient and name it MinecraftTools.
- Inside the Zip file, you can click “Extract all” or similar button.
- Extract it to your MinecraftTools folder.
- Go to the folder and double click the Install_API file.
- Now, you can run the server.
- There is a file called Start_Server. Running this will start the server.
- If you have any trouble, email me.
© Copyright 2016, Brian McMahan. Revision 94fef083 .
Versions latest Downloads pdf htmlzip epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.