Is it possible to have multiple Python programmers simultaneously running code on the same Bukkit server?

82 views
Skip to first unread message

LearningMC

unread,
Aug 13, 2019, 10:42:04 AM8/13/19
to Adventures in Minecraft Forum
Hi,
I read O'Hanlon's book and started up a Bukkit server.  I have multiple kids who are happily playing on the same Bukkit server, but we haven't figured out how to run Python code from multiple players' computers while playing on the same Bukkit server.  Currently, only the "host computer" can successfully run Python code.

When others (non-hosts) attempt to run Python code while playing on the same Bukkit server they get the error:

Traceback (most recent call last):
  File "C:\Users\MC\Desktop\Shared Folder\Minecraft\Installation Files\Python Programmer.py", line 2, in <module>
    mc = Minecraft.create()
  File "C:\Users\MC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mcpi\minecraft.py", line 171, in create
    return Minecraft(Connection(address, port))
  File "C:\Users\MC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mcpi\connection.py", line 17, in __init__
    self.socket.connect((address, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
>>> 

Does anyone know if it's possible to have multiple kids run their Python code on the same Bukkit server?  THANK YOU!

Dave Ames

unread,
Aug 13, 2019, 1:20:12 PM8/13/19
to Adventures in Minecraft Forum
If I remember correctly you do something like:

mc = minecraft.Minecraft.create(name="minecraft_user_name", address="ip_address_of_server")


You might need to supply the port number as well. I don't know how well multiple users running code simultaneously would work. I seem to remember Martin described trying it once and it being chaos.

You might be able to create multiple connections from the same code, one for each user as long as you know their usernames. But I haven't tried it. 

LearningMC

unread,
Aug 13, 2019, 3:38:13 PM8/13/19
to Adventures in Minecraft Forum
Thank you, your suggestion worked!!  Here's how I did it.  From non-host computers (computers connected to the server but not actually hosting the server), start up IDLE and type at the top of your .py file


mc = Minecraft.create('192.168.1.100', 4711)

Use your IP address but, for some reason I don't yet understand, the port number must be set to 4711.

Thank you again Dave!

LearningMC

unread,
Aug 13, 2019, 6:07:52 PM8/13/19
to Adventures in Minecraft Forum
I have a followup question.  I have multiple kids whose Python programs work in the same Bukkit server.

However, something very basic such as teleportation doesn't work for non-hosts players.  For example, if I am the host of the server the following code will work to teleport my character:

mc.player.setTilePos(100,79,100)

The above line will teleport me to 100,79,100.

But if the other players run the exact same code, they end up not teleporting at all.  Instead, it's MY characters that ends up getting teleported again.  They are teleporting my character, but not their own.  What are we doing wrong?

Dave Ames

unread,
Aug 13, 2019, 6:13:06 PM8/13/19
to Adventures in Minecraft Forum
The code will apply to the player specified when you call Minecraft.create() otherwise I think it defaults to the player from the local server. 

That's why you need to do something like: 

Minecraft.create(name="minecraft_user_name",
address="ip_address_of_server")

It may be username rather than name I can't quite remember and can't find the code I last used to do that.

When you pass the name of the player to create, it associates that connection with that player name, so then teleporting the player will move the one that connection refers to.

LearningMC

unread,
Aug 13, 2019, 8:43:39 PM8/13/19
to Adventures in Minecraft Forum
Minecraft.create() does not take a username parameter.  Only IP address and port number.  Also, I found a link that explains why the port must be set to 4711 and no other port number:


Anyways, if anyone out there knows how to get multiple players to teleport via Python using setPos, please let me know.  Currently, multiple players can use the setBlock command.

Dave Ames

unread,
Aug 14, 2019, 5:00:53 AM8/14/19
to Adventures in Minecraft Forum
Aha! 

Martin used to maintain the CanaryRaspberryJuice plugin here: https://github.com/martinohanlon/CanaryRaspberryJuice which does have that option. But it appears to not be an option on the current main RaspberryJuice version. Not sure how easy it would be to make this one work with CraftBukkit.

Dave

Martin O'Hanlon

unread,
Aug 15, 2019, 4:33:21 PM8/15/19
to Adventures in Minecraft Forum
As you have discovered the .player functions only update the "host" player (or more accurately the player who joins the server first).

Each player (and mob) is known as an entity and you can use the API to interact with all the entities on the server through their entity id.

A couple of examples:

Get a list of all the players entity ids, loop through them and transport them all to the same coordinate.
from mcpi.minecraft import Minecraft

mc = Minecraft.create()

# get a list of all the players entity ids
entity_ids = mc.getPlayerEntityIds()

# loop through all the players
for entity_id in entity_ids:
    
    mc.entity.setTilePos(entityId, 0, 0, 0)
Get the entity id of a specific player by their name.
from mcpi.minecraft import Minecraft

mc = Minecraft.create()

# get the entity id for my player
my_entity_id = mc.getPlayerEntityId("martinohanlon")

mc.entity.setTilePos(my_entity_id, 0, 0, 0)
Does this help?

Disclaimer - I havent tested this code!

You can find documentation for the api at https://www.stuffaboutcode.com/p/minecr ... rence.html

Dave Ames

unread,
Aug 15, 2019, 4:50:24 PM8/15/19
to Adventures in Minecraft Forum
Thanks Martin. I was having almost exactly this conversation with someone else today too. Have just sent her this post.

Cheers
Dave

LearningMC

unread,
Aug 15, 2019, 6:42:43 PM8/15/19
to Adventures in Minecraft Forum
Thank you both.  So it seems mc.getPlayerEntityIds (plural) works, but mc.getPlayerEntityId (singular) does not.  

Has anyone actually been able to use mc.getPlayerEntityID (singular)?

I keep getting the error "Minecraft object has no getPlayerEntityID attribute".  

solitone

unread,
Nov 16, 2019, 2:49:52 AM11/16/19
to Adventures in Minecraft Forum
Yes, I do use getPlayerEntityID() successfully. For example, the following code is an adaptation of the whereIAm2.py example from the book. It asks for a player name from the standard input (the command line), and periodically displays that player's position.

import mcpi.minecraft as minecraft
import time

# using default port 25565 (didn't manage to work with different port numbers)
mc = minecraft.Minecraft.create(address="YOUR_SERVER")

playerName = input("Player name? ")
entityId = mc.getPlayerEntityId(playerName)

while True:
    time.sleep(1)
    entityPos = mc.entity.getTilePos(entityId)   
    mc.postToChat(playerName + ": "
                  + "x=" + str(entityPos.x)
                  + " y=" + str(entityPos.y)
                  + " z=" + str(entityPos.z))
Reply all
Reply to author
Forward
0 new messages