本文整理汇总了Python中connection.Connection.send_flat方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.send_flat方法的具体用法?Python Connection.send_flat怎么用?Python Connection.send_flat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类connection.Connection
的用法示例。
在下文中一共展示了Connection.send_flat方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import send_flat [as 别名]
class Minecraft:
"""The main class to interact with a running instance of Minecraft Pi."""
def __init__(self, connection=None, autoId=True):
if connection:
self.conn = connection
else:
self.conn = Connection()
self.camera = CmdCamera(self.conn)
self.entity = CmdEntity(self.conn)
if autoId:
try:
playerId = int(environ['MINECRAFT_PLAYER_ID'])
self.player = CmdPlayer(self.conn,playerId=playerId)
except:
self.player = CmdPlayer(self.conn)
else:
self.player = CmdPlayer(self.conn)
self.events = CmdEvents(self.conn)
self.enabledNBT = False
def spawnEntity(self, *args):
"""Spawn entity (type,x,y,z,tags) and get its id => id:int"""
return int(self.conn.sendReceive("world.spawnEntity", args))
def removeEntity(self, *args):
"""Remove entity (id)"""
self.conn.send("world.removeEntity", args)
def getBlock(self, *args):
"""Get block (x,y,z) => id:int"""
return int(self.conn.sendReceive_flat("world.getBlock", floorFlatten(args)))
def getBlockWithData(self, *args):
"""Get block with data (x,y,z) => Block"""
ans = self.conn.sendReceive_flat("world.getBlockWithData", floorFlatten(args))
return Block(*map(int, ans.split(",")[:2]))
def getBlockWithNBT(self, *args):
"""
Get block with data and nbt (x,y,z) => Block (if no NBT) or (Block,nbt)
For this to work, you first need to do setting("include_nbt_with_data",1)
"""
if not self.enabledNBT:
self.setting("include_nbt_with_data",1)
self.enabledNBT = True
try:
ans = self.conn.sendReceive_flat("world.getBlockWithData", floorFlatten(args))
except RequestError:
# retry in case we had a Fail from the setting
ans = self.conn.receive()
else:
ans = self.conn.sendReceive_flat("world.getBlockWithData", floorFlatten(args))
id,data = (map(int, ans.split(",")[:2]))
commas = 0
for i in range(0,len(ans)):
if ans[i] == ',':
commas += 1
if commas == 2:
if '{' in ans[i+1:]:
return Block(id,data,ans[i+1:])
else:
break
return Block(id,data)
"""
@TODO
"""
# must have no NBT tags in any Block instances
def getBlocks(self, *args):
"""Get a cuboid of blocks (x0,y0,z0,x1,y1,z1) => [id:int]"""
return int(self.conn.sendReceive_flat("world.getBlocks", floorFlatten(args)))
# must have no NBT tags in Block instance
def setBlock(self, *args):
"""Set block (x,y,z,id,[data])"""
self.conn.send_flat("world.setBlock", floorFlatten(args))
def setBlockWithNBT(self, *args):
"""Set block (x,y,z,id,data,nbt)"""
data = list(flatten(args))
self.conn.send_flat("world.setBlock", list(floorFlatten(data[:5]))+data[5:])
# must have no NBT tags in Block instance
def setBlocks(self, *args):
"""Set a cuboid of blocks (x0,y0,z0,x1,y1,z1,id,[data])"""
self.conn.send_flat("world.setBlocks", floorFlatten(args))
def setBlocksWithNBT(self, *args):
"""Set a cuboid of blocks (x0,y0,z0,x1,y1,z1,id,data,nbt)"""
data = list(flatten(args))
self.conn.send_flat("world.setBlocks", list(floorFlatten(data[:8]))+data[8:])
def getHeight(self, *args):
"""Get the height of the world (x,z) => int"""
return int(self.conn.sendReceive_flat("world.getHeight", floorFlatten(args)))
def getPlayerId(self, *args):
"""Get the id of the current player"""
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import send_flat [as 别名]
#.........这里部分代码省略.........
def fallbackGetBlocksWithNBT(self, *args):
return self.fallbackGetCuboid(self.getBlockWithNBT, args)
def getBlocks(self, *args):
"""
Get a cuboid of blocks (x0,y0,z0,x1,y1,z1) => [id:int]
Packed with a y-loop, x-loop, z-loop, in this order.
"""
try:
ans = self.conn.sendReceive_flat("world.getBlocks", floorFlatten(args))
return map(int, ans.split(","))
except:
self.getBlocks = self.fallbackGetBlocks
return self.fallbackGetBlocks(*args)
def getBlocksWithData(self, *args):
"""Get a cuboid of blocks (x0,y0,z0,x1,y1,z1) => [Block(id:int, meta:int)]"""
try:
ans = self.conn.sendReceive_flat("world.getBlocksWithData", floorFlatten(args))
return [Block(*map(int, x.split(",")[:2])) for x in ans.split("|")]
except:
self.getBlocksWithData = self.fallbackGetBlocksWithData
return self.fallbackGetBlocksWithData(*args)
def getBlocksWithNBT(self, *args):
"""Get a cuboid of blocks (x0,y0,z0,x1,y1,z1) => [Block(id, meta, nbt)]"""
try:
if not self.enabledNBT:
self.setting("include_nbt_with_data",1)
self.enabledNBT = True
try:
ans = self.conn.sendReceive_flat("world.getBlocksWithData", floorFlatten(args))
except RequestError:
# retry in case we had a Fail from the setting
ans = self.conn.receive()
else:
ans = self.conn.sendReceive_flat("world.getBlocksWithData", floorFlatten(args))
ans = self.conn.sendReceive_flat("world.getBlocksWithData", floorFlatten(args))
return [stringToBlockWithNBT(x, pipeFix = True) for x in ans.split("|")]
except:
self.getBlocksWithNBT = self.fallbackGetBlocksWithNBT
return self.fallbackGetBlocksWithNBT(*args)
# must have no NBT tags in Block instance
def setBlock(self, *args):
"""Set block (x,y,z,id,[data])"""
self.conn.send_flat("world.setBlock", floorFlatten(args))
def setBlockWithNBT(self, *args):
"""Set block (x,y,z,id,data,nbt)"""
data = list(flatten(args))
self.conn.send_flat("world.setBlock", list(floorFlatten(data[:5]))+data[5:])
# must have no NBT tags in Block instance
def setBlocks(self, *args):
"""Set a cuboid of blocks (x0,y0,z0,x1,y1,z1,id,[data])"""
self.conn.send_flat("world.setBlocks", floorFlatten(args))
def setBlocksWithNBT(self, *args):
"""Set a cuboid of blocks (x0,y0,z0,x1,y1,z1,id,data,nbt)"""
data = list(flatten(args))
self.conn.send_flat("world.setBlocks", list(floorFlatten(data[:8]))+data[8:])
def getHeight(self, *args):
"""Get the height of the world (x,z) => int"""
return int(self.conn.sendReceive_flat("world.getHeight", floorFlatten(args)))
def getPlayerId(self, *args):
"""Get the id of the current player"""
a = tuple(flatten(args))
if self.playerId is not None and len(a) == 0:
return self.playerId
else:
return int(self.conn.sendReceive_flat("world.getPlayerId", flatten(args)))
def getPlayerEntityIds(self):
"""Get the entity ids of the connected players => [id:int]"""
ids = self.conn.sendReceive("world.getPlayerIds")
return map(int, ids.split("|"))
def saveCheckpoint(self):
"""Save a checkpoint that can be used for restoring the world"""
self.conn.send("world.checkpoint.save")
def restoreCheckpoint(self):
"""Restore the world state to the checkpoint"""
self.conn.send("world.checkpoint.restore")
def postToChat(self, msg):
"""Post a message to the game chat"""
self.conn.send("chat.post", msg)
def setting(self, setting, status):
"""Set a world setting (setting, status). keys: world_immutable, nametags_visible"""
self.conn.send("world.setting", setting, 1 if bool(status) else 0)
@staticmethod
def create(address = None, port = None):
return Minecraft(Connection(address, port))