当前位置: 首页>>代码示例>>Python>>正文


Python Connection.sendReceive方法代码示例

本文整理汇总了Python中connection.Connection.sendReceive方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.sendReceive方法的具体用法?Python Connection.sendReceive怎么用?Python Connection.sendReceive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在connection.Connection的用法示例。


在下文中一共展示了Connection.sendReceive方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import sendReceive [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"""
#.........这里部分代码省略.........
开发者ID:jprorama,项目名称:raspberryjammod,代码行数:103,代码来源:minecraft.py

示例2: __init__

# 需要导入模块: from connection import Connection [as 别名]
# 或者: from connection.Connection import sendReceive [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()

        if security.AUTHENTICATION_USERNAME and security.AUTHENTICATION_PASSWORD:
            self.conn.authenticate(security.AUTHENTICATION_USERNAME, security.AUTHENTICATION_PASSWORD)

        self.camera = CmdCamera(self.conn)
        self.entity = CmdEntity(self.conn)
        
        self.playerId = None
        
        if autoId:
            try:
                 self.playerId = int(environ['MINECRAFT_PLAYER_ID'])
                 self.player = CmdPlayer(self.conn,playerId=self.playerId)
            except:
                try:
                    self.playerId = self.getPlayerId(environ['MINECRAFT_PLAYER_NAME'])
                    self.player = CmdPlayer(self.conn,playerId=self.playerId)
                except:
                    if security.AUTHENTICATION_USERNAME:
                        try:
                            self.playerId = self.getPlayerId(security.AUTHENTICATION_USERNAME)
                            self.player = CmdPlayer(self.conn,playerId=self.playerId)
                        except:
                            self.player = CmdPlayer(self.conn)
                    else:
                        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))
        return stringToBlockWithNBT(ans)
    """
        @TODO
    """

    def fallbackGetCuboid(self, getBlock, *args):
        (x0,y0,z0,x1,y1,z1) = map(lambda x:int(math.floor(float(x))), flatten(args))
        out = []
        for y in range(min(y0,y1),max(y0,y1)+1):
            for x in range(min(x0,x1),max(x0,x1)+1):
                for z in range(min(z0,z1),max(z0,z1)+1):
                    out.append(getBlock(x,y,z))                    
        return out
        
    def fallbackGetBlocksWithData(self, *args):
        return self.fallbackGetCuboid(self.getBlockWithData, args)

    def fallbackGetBlocks(self, *args):
        return self.fallbackGetCuboid(self.getBlock, args)

    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.
#.........这里部分代码省略.........
开发者ID:arpruss,项目名称:raspberryjammod-minetest,代码行数:103,代码来源:minecraft.py


注:本文中的connection.Connection.sendReceive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。