Skip to content

Instantly share code, notes, and snippets.

@noahcoad
Last active March 2, 2024 22:02
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save noahcoad/fc9d3984a5d4d61648269c0a9477c622 to your computer and use it in GitHub Desktop.
Save noahcoad/fc9d3984a5d4d61648269c0a9477c622 to your computer and use it in GitHub Desktop.
Code Minecraft with Python on Mac OSX

Code Minecraft with Python on Mac OSX

Here's a step-by-step to get started scripting Minecraft with Python on Mac OSX

Overview

There was a special build of Minecraft for the Raspberry Pi called Minecraft Pi that had a Python interface and library. You could code in Python to manipulate your Minecraft world! But you'd have to code on a Raspberry Pi computer. However thanks to using the RaspberryJuice plugin with a Spigot server, you can do this locally on a Mac. Probably in Linux or Windows too, but I'll leave that up to you.

Here we have a rainbow created by the rainbox script. rainbow

Step-by-step

# guide for Minecraft v1.13.1 server
# update minecraft server and spigot links for latest versions
 
# get started
mkdir minecraft_py; cd minecraft_py; mkdir spigot; cd spigot
 
# download minecraft server from https://minecraft.net/en-us/download/server/
wget https://launcher.mojang.com/v1/objects/fe123682e9cb30031eae351764f653500b7396c9/server.jar
 
# download spigot server from https://getbukkit.org/download/spigot
wget -O spigot.jar https://cdn.getbukkit.org/spigot/spigot-1.13.1.jar
 
# agree to eula
echo eula=true > eula.txt
 
# get RaspberryJuice from https://dev.bukkit.org/projects/raspberryjuice
mkdir plugins; cd plugins
wget -O raspberryjuice-1.11.jar https://dev.bukkit.org/projects/raspberryjuice/files/2496319/download
 
# get the mcpi libraries from https://github.com/zhuowei/RaspberryJuice
cd ../..
git clone https://github.com/zhuowei/RaspberryJuice.git
mkdir py
cp -r RaspberryJuice/src/main/resources/mcpi/api/python/modded/mcpi py/
 
# download an example python script from https://www.dropbox.com/s/k29ms42nzvgehjk/rainbow.py?dl=0
# alternatively see example here https://dev.bukkit.org/projects/raspberryjuice
cd py
wget -O rainbow.py https://www.dropbox.com/s/k29ms42nzvgehjk/rainbow.py\?dl\=1
 
# run spigot
cd ../spigot; java -jar spigot.jar
 
# in a new terminal under the minecraft_py/py folder
python2 rainbow.py

You're Running!

Start minecraft making sure the version matches (1.13.1 in this case), go into multiplayer mode, add a server with address: localhost

Connect, look around you, and you'll see the rainbow!

Want a first challenge? try making the rainbow thicker =)

Also

@thundernixon
Copy link

Hi Noah, thanks for posting these instructions!

I'm getting through most of it (it was a bit tricky to set up the right version of Java – I had to download v11 from Oracle, then activate it with these instructions).

However, I'm getting stuck at the very last step:

code/minecraft_py/py ▶ python2 rainbow.py
Traceback (most recent call last):
  File "rainbow.py", line 1, in <module>
    import mcpi.minecraft as minecraft
  File "/Users/stephennixon/code/minecraft_py/py/mcpi/minecraft.py", line 133
    results.append(ProjectileEvent.Hit(*map(int,info[0:4]),*info[4:]))
                                                           ^
SyntaxError: invalid syntax

mcpi/minecraft.py L126-L134

I don't see anything obviously wrong with the syntax at this portion ... any idea what might be wrong?

@thundernixon
Copy link

Update: The syntax error was fixed, and I figured out how to get this running! Documented here: zhuowei/RaspberryJuice#90

Thanks for the great instructions!

@thundernixon
Copy link

I’ve had a bunch of fun with this, and put together some slightly more interesting starter code (posted in my other GitHub account):

https://gist.github.com/arrowtype/5ab1acc544671a9408fe285dfdc5fc1b

@MarcinAtNovela
Copy link

https://dev.bukkit.org/projects/raspberryjuice/files/2496319/download
Resolving dev.bukkit.org (dev.bukkit.org)... 104.19.146.132, 104.19.147.132
Connecting to dev.bukkit.org (dev.bukkit.org)|104.19.146.132|:443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2020-01-14 15:11:39 ERROR 403: Forbidden.

Please help!

@voronkovventures
Copy link

Hello! I have the same issue as Marcin:
wget -O raspberryjuice-1.11.jar https://dev.bukkit.org/projects/raspberryjuice/files/2496319/download
"Andreys-MacBook-Pro-2:plugins andreyvoronkov$ wget -O raspberryjuice-1.11.jar https://dev.bukkit.org/projects/raspberryjuice/files/2496319/download
--2020-02-09 12:10:42-- https://dev.bukkit.org/projects/raspberryjuice/files/2496319/download
Распознаётся dev.bukkit.org (dev.bukkit.org)… 2606:4700::6813:9384, 2606:4700::6813:9284, 104.19.147.132, ...
Подключение к dev.bukkit.org (dev.bukkit.org)|2606:4700::6813:9384|:443... соединение установлено.
HTTP-запрос отправлен. Ожидание ответа… 403 Forbidden
2020-02-09 12:10:42 ОШИБКА 403: Forbidden."

How to fix it?

@voronkovventures
Copy link

Ok, on one Mac it works now. But on another two there are errors.
Error number 1, when I run rainbow.py:
MacBook-Pro-user:py user$ python rainbow.py
Traceback (most recent call last):
File "rainbow.py", line 7, in
mc = minecraft.Minecraft.create()
File "/Users/user/Documents/minecraft_py/py/mcpi/minecraft.py", line 376, in create
return Minecraft(Connection(address, port))
File "/Users/user/Documents/minecraft_py/py/mcpi/connection.py", line 17, in init
self.socket.connect((address, port))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused
MacBook-Pro-user:py user$

@pbower
Copy link

pbower commented Aug 7, 2020

Ok, on one Mac it works now. But on another two there are errors.
Error number 1, when I run rainbow.py:
MacBook-Pro-user:py user$ python rainbow.py
Traceback (most recent call last):
File "rainbow.py", line 7, in
mc = minecraft.Minecraft.create()
File "/Users/user/Documents/minecraft_py/py/mcpi/minecraft.py", line 376, in create
return Minecraft(Connection(address, port))
File "/Users/user/Documents/minecraft_py/py/mcpi/connection.py", line 17, in init
self.socket.connect((address, port))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused
MacBook-Pro-user:py user$

Hi, I also receive this error. How do we fix ?

(base) Peters-MBP:py pbower$ python2 rainbow.py

Traceback (most recent call last):
File "rainbow.py", line 7, in
mc = minecraft.Minecraft.create()
File "/Users/pbower/Documents/Projects/minecraft_py/py/mcpi/minecraft.py", line 376, in create
return Minecraft(Connection(address, port))
File "/Users/pbower/Documents/Projects/minecraft_py/py/mcpi/connection.py", line 17, in init
self.socket.connect((address, port))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused

@webknjaz
Copy link

webknjaz commented Mar 5, 2021

socket.error: [Errno 61] Connection refused

Looks like you don't have Minecraft running. Start it first before attempting to connect to it...

@Janik-ux
Copy link

@voronkovventures @MarcinAtNovela

https://dev.bukkit.org/projects/raspberryjuice/files/2496319/download
Resolving dev.bukkit.org (dev.bukkit.org)... 104.19.146.132, 104.19.147.132
Connecting to dev.bukkit.org (dev.bukkit.org)|104.19.146.132|:443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2020-01-14 15:11:39 ERROR 403: Forbidden.

Please help!

I had the same problem and a test with curl discovered, that cloudflare asks to solve a captcha because it thinks you are a bot. Not sure if this was also your error. It works to download the file manually from https://dev.bukkit.org/projects/raspberryjuice/files/2496319 and place it into the plugins folder.
Unfortunately I don't know a method to bypass this barrier, but I hope it helps someone who also gets stuck at this point!

Thank you very much for this tutorial @noahcoad it helped me as a minecraft plugin beginner!

@redsnapper73
Copy link

redsnapper73 commented Jan 7, 2022

I could find a solution by installing JDK over homebrew (other JDK should work fine as well probably):

brew install java
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdkMBP 2015, MacOS 11.6.2

Hi, thank you for this good walkthrough, though, I have troubles starting the server:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/bukkit/craftbukkit/bootstrap/Main has been compiled by a more recent version of the Java Runtime (class file version 60.0), this version of the Java Runtime only recognizes class file versions up to 52.0

I have the most recent version of java installed:
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)

Can somebody help me please?
Thank you, Andreas

@redsnapper73
Copy link

When I tried to do the first teleportations I stumbled about a mismatch in the coordinates between the ones shown in the Minecraft window and the ones entered in Python script (mc.setPos(x,y,z)). Just enter 'setworldspawn 0 0 0' in the server console, then everything works fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment