首先,你需要在服务端安装:GitHub - zhuowei/RaspberryJuice: A plugin for Bukkit implementing the Minecraft Pi API(RaspberryJuice插件),然后在电脑安装mcpi库 —— pip install mcpi。需要注意的是,要将插件设置里的location选项改为“ABSOLUTE”,否则所用坐标是相对于玩家的。
main.py[Python] 纯文本查看 复制代码 from mcpi.minecraft import Minecraft
import time
ip_address = "127.0.0.1" # 服务器ip
mc = Minecraft.create(ip_address)
while True:
time.sleep(0.01)
tp_pos()
tp_pos函数[Python] 纯文本查看 复制代码 def tp_pos():
"""玩家发送 .tppos x y z 将其传送到指定坐标"""
try:
chat_events = mc.events.pollChatPosts()
for chatEvent in chat_events:
print(chatEvent)
# 格式如同 ChatEvent(ChatEvent.POST, 252, .tppos 100 64 100)
command1 = str(chatEvent).split(" ", -1)[2].split(",", -1)[0]
print("Player used command: " + command1)
if command1 == ".tppos":
player = str(chatEvent).split(" ", -1)[1].split(",", -1)[0]
print("Player's entity id: " + player)
pos_list = str(chatEvent).split(" ", -1)
x = pos_list[3]
y = str(int(pos_list[4])+1) # 这里+1防止半身入地
z = pos_list[5].split(")", -1)[0]
pos = x, y, z
print("To position: " + str(pos) + "\n\n")
mc.entity.setPos(player, pos)
mc.postToChat("Test result: success!")
else:
continue
finally:
return False
在tp_pos()函数中,有些令人发麻的写法:
player = str(chatEvent).split(" ", -1)[1].split(",", -1)[0]
因为chatEvent返回的是
ChatEvent(ChatEvent.POST, 251, .tppos -833 63 343)
然后我们要在其中“切片”,选出我们需要的数据,比如中间的 251 是我当前的实体id
上面的写法可以干净的返回 251
效果:
服务端
PyCharm DOS
玩家
|