查看: 1596|回复: 1

[Python] Python控制我的世界,实现“伪指令”—— 传送位置 .tppos x y z

[复制链接]

141

主题

186

帖子

2095

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
2095

活跃会员最佳新人

QQ
发表于 2023-7-15 15:36:37 | 显示全部楼层 |阅读模式
首先,你需要在服务端安装: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

PyCharm DOS

玩家

玩家











上一篇:使用Python控制Minecraft(Java) - pycraft 和 minecraft-py 库
回复

使用道具 举报

141

主题

186

帖子

2095

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
2095

活跃会员最佳新人

QQ
 楼主| 发表于 2023-8-18 11:16:29 | 显示全部楼层
可以加个 try ... except ... 来判断玩家是否在线什么的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表