### Mod Configuration Example Source: https://context7.com/creatormc/modsdksping/llms.txt Define constants such as namespaces and system paths in your mod configuration file. ```python # -*- coding: utf-8 -*- # Mod 基本信息 MOD_NAMESPACE = "TutorialMod" MOD_VERSION = "1.0.0" # 客户端系统配置 CLIENT_SYSTEM_NAME = "TutorialClientSystem" CLIENT_SYSTEM_CLS_PATH = "tutorialScripts.TutorialClientSystem.TutorialClientSystem" ``` -------------------------------- ### Component Initialization with Event Listening Source: https://github.com/creatormc/modsdksping/blob/main/modsdkspring/plugins/MODSDKSpring/core/ListenEvent.txt Overrides the __init__ method to handle client/server event listening and component registration. It ensures proper setup for both client and server systems. ```python def newInit(self, system, *args, **kwargs): if isinstance(system, ServerSystem): system.ListenForEvent(serverApi.GetEngineNamespace(), serverApi.GetEngineSystemName(), "LoadServerAddonScriptsAfter", self, self._modSDKSpringLoadFinish, 0) elif isinstance(system, ClientSystem): system.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), "LoadClientAddonScriptsAfter", self, self._modSDKSpringLoadFinish, 0) # 处理监听 ListenEvent.listenEvent(cls, system, self) logger.info("%s 组件创建成功", cls.__name__) origInit(self, system, *args, **kwargs) # 检查装饰器 ListenEvent.checkDecorator(origInit, newInit) cls.__init__ = newInit BeanFactory.componentClsDict[systemType][cls.__name__[0].lower() + cls.__name__[1:]] = [cls, targetNamespace, targetSystemName] return cls ``` -------------------------------- ### Install and Upgrade MODSDKSpring Source: https://context7.com/creatormc/modsdksping/llms.txt Use pip to install or upgrade the framework. If both Python 2 and 3 are present, use pip2 for the Python 2.7 environment. ```shell # 安装框架 pip install mc-creatormc-sdkspring # 如果同时安装了 Python2 和 Python3 pip2 install mc-creatormc-sdkspring # 升级框架 pip install --upgrade mc-creatormc-sdkspring ``` -------------------------------- ### Install CreatorMC Mod SDK Ping Source: https://github.com/creatormc/modsdksping/wiki/下载和升级 Use this command to install the CreatorMC Mod SDK Ping framework. Ensure you have Python 2.7.18 or a compatible version installed. ```shell pip install mc-creatormc-sdkspring ``` -------------------------------- ### Install CreatorMC Mod SDK Ping with pip2 Source: https://github.com/creatormc/modsdksping/wiki/下载和升级 If you have both Python 2 and Python 3 installed, use this command to specifically install the CreatorMC Mod SDK Ping framework using pip2. ```shell pip2 install mc-creatormc-sdkspring ``` -------------------------------- ### Import All Server Components in Server System Source: https://github.com/creatormc/modsdksping/blob/main/README.md Import all server components at the top of the TutorialServerSystem.py file using a wildcard import. ```python from tutorialScripts.components.server import * ``` -------------------------------- ### Import All Client Components in Client System Source: https://github.com/creatormc/modsdksping/blob/main/README.md Import all client components at the top of the TutorialClientSystem.py file using a wildcard import. ```python from tutorialScripts.components.client import * ``` -------------------------------- ### 初始化客户端系统 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 使用 @ListenEvent.InitClient 装饰器初始化客户端系统类。 ```python # -*- coding: utf-8 -*- import mod.client.extraClientApi as clientApi # 因为文件夹名称改变,所以导入路径也改变了 from tutorialComponentScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ClientSystem = clientApi.GetClientSystemCls() compFactory = clientApi.GetEngineCompFactory() @ListenEvent.InitClient class TutorialClientSystem(ClientSystem): def __init__(self, namespace, systemName): pass ``` -------------------------------- ### Initialize Client System Source: https://context7.com/creatormc/modsdksping/llms.txt Use the @ListenEvent.InitClient decorator to register a class as a client system. The framework automatically handles constructor and destroy lifecycle methods. ```python # -*- coding: utf-8 -*- import mod.client.extraClientApi as clientApi from tutorialScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ClientSystem = clientApi.GetClientSystemCls() compFactory = clientApi.GetEngineCompFactory() @ListenEvent.InitClient class TutorialClientSystem(ClientSystem): def __init__(self, namespace, systemName): # 无需调用 super().__init__(),框架自动处理 pass @ListenEvent.Client(eventName="AddEntityClientEvent") def onAddEntity(self, args): print "客户端实体添加: ", args ``` -------------------------------- ### Initialize Server System Source: https://context7.com/creatormc/modsdksping/llms.txt Use the @ListenEvent.InitServer decorator to register a class as a server system. ```python # -*- coding: utf-8 -*- import mod.server.extraServerApi as serverApi from tutorialScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ServerSystem = serverApi.GetServerSystemCls() compFactory = serverApi.GetEngineCompFactory() @ListenEvent.InitServer class TutorialServerSystem(ServerSystem): def __init__(self, namespace, systemName): pass @ListenEvent.Server(eventName="ServerChatEvent") def onServerChat(self, args): print "==== OnServerChat ==== ", args playerId = args["playerId"] comp = compFactory.CreateItem(playerId) if args["message"] == "钻石剑": comp.SpawnItemToPlayerInv({ "itemName": "minecraft:diamond_sword", "count": 1, "auxValue": 0 }, playerId) elif args["message"] == "钻石镐": comp.SpawnItemToPlayerInv({ "itemName": "minecraft:diamond_pickaxe", "count": 1, "auxValue": 0 }, playerId) ``` -------------------------------- ### Get Component Bean using getBean() Source: https://github.com/creatormc/modsdksping/wiki/框架使用 Use this method to directly retrieve a component object from the container. Specify the system type (CLIENT or SERVER) and the bean's key (typically the class name with the first letter lowercased). Avoid using this in __init__ methods. ```python from tutorialScripts.plugins.MODSDKSpring.core.BeanFactory import BeanFactory import tutorialScripts.plugins.MODSDKSpring.core.constant.SystemType as SystemType testComponentClient = BeanFactory.getBean(SystemType.CLIENT, 'testComponentClient') ``` -------------------------------- ### DB Module Initialization Source: https://github.com/creatormc/modsdksping/wiki/DB模块 How to import and initialize the ClientDB and ServerDB instances in your mod. ```APIDOC ## Initialization The module provides pre-created global instances for `ClientDB` (client-side) and `ServerDB` (server-side). You just need to import them. ### Client Initialization Example ```python # Client-side import from your_mod_path.client.ClientDB import clientDB ``` ### Server Initialization Example ```python # Server-side import from your_mod_path.server.ServerDB import serverDB ``` **Note:** These imports should be placed at the beginning of your client and server system files, respectively. The MODSDKSpring template usually includes these automatically. ### Client System Initialization Example ```python # -*- coding: utf-8 -*- import mod.client.extraClientApi as clientApi from YourModScripts.modCommon.modConfig import MOD_NAMESPACE from YourModScripts.plugins.MODSDKSpring.DB.BaseDB import DB_CHANGE_EVENT from YourModScripts.plugins.MODSDKSpring.DB.ClientDB import clientDB from YourModScripts.plugins.MODSDKSpring.Network.NotifyManage import NotifyManage, AllowNotify from YourModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ClientSystem = clientApi.GetClientSystemCls() compFactory = clientApi.GetEngineCompFactory() @ListenEvent.InitClient class TestClient(ClientSystem): # noinspection PyMissingConstructor def __init__(self, namespace, systemName): # Subscribe to data clientDB.subscribe(MOD_NAMESPACE) @ListenEvent.Client(namespace=NotifyManage.NAMESPACE, systemName=NotifyManage.CLIENT_SYSTEM_NAME, eventName=DB_CHANGE_EVENT) def dbChangeEvent(self, event): # Listen for data changes print 'Client change: ', event ``` ### Server System Initialization Example ```python # -*- coding: utf-8 -*- import mod.server.extraServerApi as serverApi from YourModScripts.modCommon.modConfig import MOD_NAMESPACE from YourModScripts.plugins.MODSDKSpring.DB.BaseDB import DB_CHANGE_EVENT from YourModScripts.plugins.MODSDKSpring.DB.ServerDB import serverDB from YourModScripts.plugins.MODSDKSpring.Network.NotifyManage import NotifyManage, NotifyToClient from YourModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ServerSystem = serverApi.GetServerSystemCls() compFactory = serverApi.GetEngineCompFactory() @ListenEvent.InitServer class TestServer(ServerSystem): # noinspection PyMissingConstructor def __init__(self, namespace, systemName): # Subscribe to data serverDB.subscribe(MOD_NAMESPACE) @ListenEvent.Server(namespace=NotifyManage.NAMESPACE, systemName=NotifyManage.SERVER_SYSTEM_NAME, eventName=DB_CHANGE_EVENT) def dbChangeEvent(self, event): # Listen for data changes print 'Server change: ', event ``` ``` -------------------------------- ### @ListenEvent.InitServer Source: https://context7.com/creatormc/modsdksping/llms.txt Initializes a server system class, enabling framework features. ```APIDOC ## @ListenEvent.InitServer ### Description Decorator applied to a server system class to register it with the framework. ### Usage ```python @ListenEvent.InitServer class TutorialServerSystem(ServerSystem): def __init__(self, namespace, systemName): pass ``` ``` -------------------------------- ### 使用命令行自动生成导入文件 Source: https://github.com/creatormc/modsdksping/wiki/高级内容 通过命令行工具自动生成 __init__.py 文件,支持指定路径参数。 ```shell modsdkspring import ``` ```shell Starting to create the __init__.py file. Successfully created the __init__.py file! ``` ```shell modsdkspring import --path "tutorialBehaviorPack/tutorialScripts/components/client" ``` -------------------------------- ### Server System Initialization and DB Subscription Source: https://github.com/creatormc/modsdksping/wiki/DB模块 Initialize the server system and subscribe to data using serverDB. This sets up the server to handle data synchronization and changes. ```python # -*- coding: utf-8 -*- import mod.server.extraServerApi as serverApi from TestModScripts.modCommon.modConfig import MOD_NAMESPACE from TestModScripts.plugins.MODSDKSpring.DB.BaseDB import DB_CHANGE_EVENT from TestModScripts.plugins.MODSDKSpring.DB.ServerDB import serverDB from TestModScripts.plugins.MODSDKSpring.Network.NotifyManage import NotifyManage, NotifyToClient from TestModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ServerSystem = serverApi.GetServerSystemCls() compFactory = serverApi.GetEngineCompFactory() @ListenEvent.InitServer class TestServer(ServerSystem): # noinspection PyMissingConstructor def __init__(self, namespace, systemName): # 订阅数据 serverDB.subscribe(MOD_NAMESPACE) @ListenEvent.Server(namespace=NotifyManage.NAMESPACE, systemName=NotifyManage.SERVER_SYSTEM_NAME, eventName=DB_CHANGE_EVENT) def dbChangeEvent(self, event): # 监听数据变化 print '服务端改变: ', event ``` -------------------------------- ### 初始化服务端系统 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 使用 @ListenEvent.InitServer 装饰器初始化服务端系统类,并确保导入了相关的组件类。 ```python # -*- coding: utf-8 -*- import mod.server.extraServerApi as serverApi # 因为文件夹名称改变,所以导入路径也改变了 from tutorialComponentScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent # 导入组件,尽管这里你没有显式使用它,也必须导入! from tutorialComponentScripts.components.server.ChatServerComponent import ChatServerComponent ServerSystem = serverApi.GetServerSystemCls() compFactory = serverApi.GetEngineCompFactory() @ListenEvent.InitServer class TutorialServerSystem(ServerSystem): def __init__(self, namespace, systemName): pass ``` -------------------------------- ### 手动配置组件导入 Source: https://github.com/creatormc/modsdksping/wiki/高级内容 在 __init__.py 中显式导入组件类,以便在系统层通过通配符导入。 ```python # -*- coding: utf-8 -*- # 此处假设模块名和模块中定义的类名相同,请根据您的实际情况进行导入 from AClientComponent import AClientComponent from BClientComponent import BClientComponent from CClientComponent import CClientComponent ``` ```python # -*- coding: utf-8 -*- # 此处假设模块名和模块中定义的类名相同,请根据您的实际情况进行导入 from AServerComponent import AServerComponent from BServerComponent import BServerComponent from CServerComponent import CServerComponent ``` -------------------------------- ### Initialize Client and Server Systems Source: https://github.com/creatormc/modsdksping/blob/main/modsdkspring/plugins/MODSDKSpring/core/ListenEvent.txt Use these decorators on classes registered in modMain.py to enable framework features for client or server systems. ```python @staticmethod def InitClient(cls): """ 添加在被 modMain.py 注册的客户端类的上方,开启框架相关功能 """ return ListenEvent.Init(cls, SystemType.CLIENT) ``` ```python @staticmethod def InitServer(cls): """ 添加在被 modMain.py 注册的服务端类的上方,开启框架相关功能 """ return ListenEvent.Init(cls, SystemType.SERVER) ``` -------------------------------- ### Initialize Server System Source: https://github.com/creatormc/modsdksping/blob/main/README.md Use the InitServer decorator on a class registered in modMain.py to enable framework features for a server system. ```python # -*- coding: utf-8 -*- import mod.server.extraServerApi as serverApi from tutorialScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ServerSystem = serverApi.GetServerSystemCls() compFactory = serverApi.GetEngineCompFactory() @ListenEvent.InitServer class TutorialServerSystem(ServerSystem): def __init__(self, namespace, systemName): pass ``` -------------------------------- ### Client System Initialization and DB Subscription Source: https://github.com/creatormc/modsdksping/wiki/DB模块 Initialize the client system and subscribe to data using clientDB. This ensures that when a player enters the game, relevant private data is automatically requested and cached locally. ```python # -*- coding: utf-8 -*- import mod.client.extraClientApi as clientApi from TestModScripts.modCommon.modConfig import MOD_NAMESPACE from TestModScripts.plugins.MODSDKSpring.DB.BaseDB import DB_CHANGE_EVENT from TestModScripts.plugins.MODSDKSpring.DB.ClientDB import clientDB from TestModScripts.plugins.MODSDKSpring.Network.NotifyManage import NotifyManage, AllowNotify from TestModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ClientSystem = clientApi.GetClientSystemCls() compFactory = clientApi.GetEngineCompFactory() @ListenEvent.InitClient class TestClient(ClientSystem): # noinspection PyMissingConstructor def __init__(self, namespace, systemName): # 订阅数据 clientDB.subscribe(MOD_NAMESPACE) @ListenEvent.Client(namespace=NotifyManage.NAMESPACE, systemName=NotifyManage.CLIENT_SYSTEM_NAME, eventName=DB_CHANGE_EVENT) def dbChangeEvent(self, event): # 监听数据变化 print '客户端改变: ', event ``` -------------------------------- ### Python 模块初始化文件 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 在 Python 2 中,每个包含 `.py` 文件的文件夹都需要一个 `__init__.py` 文件来将其识别为 Python 模块。此文件可以为空。 ```python # __init__.py ``` -------------------------------- ### Initialize Client System Source: https://github.com/creatormc/modsdksping/blob/main/README.md Use the InitClient decorator on a class registered in modMain.py to enable framework features for a client system. ```python # -*- coding: utf-8 -*- import mod.client.extraClientApi as clientApi from tutorialScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ClientSystem = clientApi.GetClientSystemCls() compFactory = clientApi.GetEngineCompFactory() @ListenEvent.InitClient class TutorialClientSystem(ClientSystem): def __init__(self, namespace, systemName): pass ``` -------------------------------- ### Import Client Components in __init__.py Source: https://github.com/creatormc/modsdksping/blob/main/README.md Import client component classes in the client folder's __init__.py file. Assumes module name matches class name. ```python # -*- coding: utf-8 -*- # 此处假设模块名和模块中定义的类名相同,请根据您的实际情况进行导入 from AClientComponent import AClientComponent from BClientComponent import BClientComponent from CClientComponent import CClientComponent ``` -------------------------------- ### Import Server Components in __init__.py Source: https://github.com/creatormc/modsdksping/blob/main/README.md Import server component classes in the server folder's __init__.py file. Assumes module name matches class name. ```python # -*- coding: utf-8 -*- # 此处假设模块名和模块中定义的类名相同,请根据您的实际情况进行导入 from AServerComponent import AServerComponent from BServerComponent import BServerComponent from CServerComponent import CServerComponent ``` -------------------------------- ### modsdkspring init Prompts Source: https://github.com/creatormc/modsdksping/wiki/框架使用 These are the expected prompts and inputs when initializing a new mod using the modsdkspring init command. Provide the requested names for the mod folder, namespace, and system classes. ```shell Start initializing your Mod... Please enter the name of the Mod folder: tutorialScripts Please enter the Mod name, which will serve as the namespace registered to the engine: TutorialMod Please enter the client system name, which will serve as the class name for the client system: TutorialClientSystem Please enter the server system name, which will serve as the class name for the server system: TutorialServerSystem Created successfully! ``` -------------------------------- ### @ListenEvent.InitClient Source: https://context7.com/creatormc/modsdksping/llms.txt Initializes a client system class, enabling framework features and automatic lifecycle management. ```APIDOC ## @ListenEvent.InitClient ### Description Decorator applied to a client system class to register it with the framework. It automatically handles parent constructor calls and Destroy method execution. ### Usage ```python @ListenEvent.InitClient class TutorialClientSystem(ClientSystem): def __init__(self, namespace, systemName): pass ``` ``` -------------------------------- ### Initialize Mod Project Structure Source: https://context7.com/creatormc/modsdksping/llms.txt Generate a standardized project directory structure for a new mod using the command line tool. ```shell # 在行为包目录下执行 modsdkspring init # 按提示输入以下信息: # Please enter the name of the Mod folder: tutorialScripts # Please enter the Mod name: TutorialMod # Please enter the client system name: TutorialClientSystem # Please enter the server system name: TutorialServerSystem # 生成的目录结构: # tutorialScripts/ # ├── components/ # │ ├── client/ # │ │ └── __init__.py # │ ├── server/ # │ │ └── __init__.py # │ └── __init__.py # ├── modCommon/ # │ ├── __init__.py # │ └── modConfig.py # ├── plugins/ # │ ├── MODSDKSpring/ # │ └── __init__.py # ├── __init__.py # ├── modMain.py # ├── TutorialClientSystem.py # └── TutorialServerSystem.py ``` -------------------------------- ### Implement Mod Entry Point Source: https://context7.com/creatormc/modsdksping/llms.txt The main Mod class using @Mod.Binding to register server and client systems during initialization. ```python # -*- coding: utf-8 -*- from mod.common.mod import Mod import mod.client.extraClientApi as clientApi import mod.server.extraServerApi as serverApi from tutorialScripts.modCommon import modConfig @Mod.Binding(name=modConfig.MOD_NAMESPACE, version=modConfig.MOD_VERSION) class TutorialMod(object): def __init__(self): print "===== init %s mod =====" % modConfig.MOD_NAMESPACE @Mod.InitServer() def serverInit(self): serverApi.RegisterSystem( modConfig.MOD_NAMESPACE, modConfig.SERVER_SYSTEM_NAME, modConfig.SERVER_SYSTEM_CLS_PATH ) @Mod.DestroyServer() def serverDestroy(self): print "===== destroy server =====" @Mod.InitClient() def clientInit(self): clientApi.RegisterSystem( modConfig.MOD_NAMESPACE, modConfig.CLIENT_SYSTEM_NAME, modConfig.CLIENT_SYSTEM_CLS_PATH ) @Mod.DestroyClient() def clientDestroy(self): print "===== destroy client =====" ``` -------------------------------- ### Initialize Client Component Source: https://github.com/creatormc/modsdksping/blob/main/README.md Register a custom client component class with the framework. ```python # -*- coding: utf-8 -*- from particleModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent @ListenEvent.InitComponentClient class HurtEntityClientComponent(object): def __init__(self, client): self.client = client ``` -------------------------------- ### 配置 VS Code 自动化任务 Source: https://github.com/creatormc/modsdksping/wiki/高级内容 在 settings.json 中配置 Save and Run 扩展,实现保存文件时自动执行导入命令。 ```json "saveAndRun": { "commands": [ { "match": ".*\\\\components\\\\(client|server).*\\.py", "cmd": "modsdkspring import --path \"${fileDirname}\"", "useShortcut": false, "silent": false } ] } ``` -------------------------------- ### Importing Client and Server Components Source: https://context7.com/creatormc/modsdksping/llms.txt Use these imports at the top of your system files to access all available client or server components. ```python # 在客户端系统文件顶部一行导入所有客户端组件 from tutorialScripts.components.client import * # 在服务端系统文件顶部一行导入所有服务端组件 from tutorialScripts.components.server import * ``` -------------------------------- ### Internal Initialization Methods Source: https://github.com/creatormc/modsdksping/blob/main/modsdkspring/plugins/MODSDKSpring/core/ListenEvent.txt These methods handle the core logic for system initialization, event registration, and dependency injection. They are intended for internal framework use. ```python @staticmethod def Init(cls, systemType): """ 添加在被 modMain.py 注册的类的上方,开启框架相关功能 (不能直接使用) Args: systemType (str): 系统的类型,请使用常量 SystemType.CLIENT 或 SystemType.SERVER """ # 记录原本的构造方法 origInit = cls.__init__ # 记录原本的 Destroy 方法 origDestroy = cls.Destroy # 定义 Mod 在客户端和服务端加载完成时触发的函数,用于框架在运行时动态导入模块 def _modSDKSpringLoadFinish(self, event): className, suffix = ('NotifyServer', '.Network.server.NotifyServer') if SystemType.SERVER == systemType else ('NotifyClient', '.Network.client.NotifyClient') ListenEvent._registerNotifySystem(className, suffix, systemType) ListenEvent._registerNotifyFunction(self, systemType) # 给客户端和服务端系统类添加方法 cls._modSDKSpringLoadFinish = _modSDKSpringLoadFinish def newInit(self, namespace, systemName, *args, **kwargs): if isinstance(self, ServerSystem): ServerSystem.__init__(self, namespace, systemName) self.ListenForEvent(serverApi.GetEngineNamespace(), serverApi.GetEngineSystemName(), "LoadServerAddonScriptsAfter", self, self._modSDKSpringLoadFinish, 0) elif isinstance(self, ClientSystem): ClientSystem.__init__(self, namespace, systemName) self.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), "LoadClientAddonScriptsAfter", self, self._modSDKSpringLoadFinish, 0) # 处理监听 ListenEvent.listenEvent(cls, self, self) # 处理带 @InitComponent 的 Bean 的创建和依赖注入 BeanFactory.createBean(self, systemType, namespace, systemName) # 处理当前自己的依赖注入 dependenceList = [namespace, systemName] dependenceList.extend(BeanFactory.createBeanWithInit(cls, self, systemType, namespace, systemName)) # 最后一次依赖注入 (解决循环依赖对象的注入) BeanFactory.dependenceInjectLast(systemType) # 清理不需要的变量,以节省内存 BeanFactory.clearNoNeedVariable(systemType) logger.info("%s 系统创建成功", cls.__name__) # 执行原来的初始化方法 origInit(self, *dependenceList) def newDestroy(self): self.UnListenAllEvents() origDestroy(self) # 检查装饰器 ListenEvent.checkDecorator(origInit, newInit) # 用新的方法覆盖原来的方法 cls.__init__ = newInit cls.Destroy = newDestroy return cls ``` ```python @staticmethod def InitComponent(cls, systemType, targetNamespace, targetSystemName): """ 添加到自定义的 Component 类的上方,开启框架相关功能 (不能直接使用) Args: systemType (str): 系统的类型,请使用常量 SystemType.CLIENT 或 SystemType.SERVER targetNamespace (str): 注入的系统的命名空间,如果值为 Target.DEFAULT 则不判断注入的系统对象的命名空间 targetSystemName (str): 注入的系统的系统名称,如果值为 Target.DEFAULT 则不判断注入的系统对象的系统名称 """ origInit = cls.__init__ ``` -------------------------------- ### ListenEvent.InitComponentServer Source: https://github.com/creatormc/modsdksping/blob/main/README.md Decorator to be placed above custom server Component classes to enable framework functionalities. ```APIDOC ## @ListenEvent.InitComponentServer ### Description Adds to the top of custom server Component classes to enable framework-related functionalities. ### Parameters #### Path Parameters - **namespace** (str) - Optional - The namespace of the injected server system. This parameter can be omitted if only one server is registered in modMain.py. - **systemName** (str) - Optional - The system name of the injected server system. This parameter can be omitted if only one server is registered in modMain.py. ### Request Example ```python # -*- coding: utf-8 -*- from xxx.plugins.MODSDKSpring.core.ListenEvent import ListenEvent @ListenEvent.InitComponentServer class HurtEntityServerComponent(object): def __init__(self, server): self.server = server ``` ``` -------------------------------- ### Register Multiple Mods with Client/Server Systems Source: https://github.com/creatormc/modsdksping/wiki/高级内容 This snippet shows how to bind and initialize multiple mods, registering both client and server systems for each. Ensure correct namespace and version are used for each mod binding. ```python # -*- coding: utf-8 -*- from mod.common.mod import Mod import mod.client.extraClientApi as clientApi import mod.server.extraServerApi as serverApi from modCommon import modConfig from mod_log import logger @Mod.Binding(name=modConfig.MOD_NAMESPACE, version=modConfig.MOD_VERSION) class TMSMultipleMod(object): def __init__(self): logger.info("===== init %s mod =====", modConfig.MOD_NAMESPACE) @Mod.InitServer() def TMSMultipleModServerInit(self): logger.info("===== init %s server =====", modConfig.SERVER_SYSTEM_NAME) serverApi.RegisterSystem(modConfig.MOD_NAMESPACE, modConfig.SERVER_SYSTEM_NAME, modConfig.SERVER_SYSTEM_CLS_PATH) @Mod.DestroyServer() def TMSMultipleModServerDestroy(self): logger.info("===== destroy %s server =====", modConfig.SERVER_SYSTEM_NAME) @Mod.InitClient() def TMSMultipleModClientInit(self): logger.info("===== init %s client =====", modConfig.CLIENT_SYSTEM_NAME) clientApi.RegisterSystem(modConfig.MOD_NAMESPACE, modConfig.CLIENT_SYSTEM_NAME, modConfig.CLIENT_SYSTEM_CLS_PATH) @Mod.DestroyClient() def TMSMultipleModClientDestroy(self): logger.info("===== destroy %s client =====", modConfig.CLIENT_SYSTEM_NAME) @Mod.Binding(name=modConfig.MOD_NAMESPACE_1, version=modConfig.MOD_VERSION_1) class TMSCopyMod(object): def __init__(self): logger.info("===== init %s mod =====", modConfig.MOD_NAMESPACE_1) @Mod.InitServer() def TMSCopyModServerInit(self): logger.info("===== init %s server =====", modConfig.SERVER_SYSTEM_NAME_1) serverApi.RegisterSystem(modConfig.MOD_NAMESPACE_1, modConfig.SERVER_SYSTEM_NAME_1, modConfig.SERVER_SYSTEM_CLS_PATH_1) @Mod.DestroyServer() def TMSCopyModServerDestroy(self): logger.info("===== destroy %s server =====", modConfig.SERVER_SYSTEM_NAME_1) @Mod.InitClient() def TMSCopyModClientInit(self): logger.info("===== init %s client =====", modConfig.CLIENT_SYSTEM_NAME_1) clientApi.RegisterSystem(modConfig.MOD_NAMESPACE_1, modConfig.CLIENT_SYSTEM_NAME_1, modConfig.CLIENT_SYSTEM_CLS_PATH_1) @Mod.DestroyClient() def TMSCopyModClientDestroy(self): logger.info("===== destroy %s client =====", modConfig.CLIENT_SYSTEM_NAME_1) ``` -------------------------------- ### 项目目录结构示例 Source: https://github.com/creatormc/modsdksping/wiki/高级内容 展示了包含组件和脚本的典型 Addon 文件结构。 ```txt TutorialMod └── tutorialBehaviorPack ├── entities ├── tutorialScripts │ ├── components │ │ ├── client │ │ │ ├── __init__.py │ │ │ ├── AClientComponent.py │ │ │ ├── BClientComponent.py │ │ │ └── CClientComponent.py │ │ ├── server │ │ │ ├── __init__.py │ │ │ ├── AServerComponent.py │ │ │ ├── BServerComponent.py │ │ │ └── CServerComponent.py │ │ └── __init__.py │ ├── modCommon │ │ ├── __init__.py │ │ └── modConfig.py │ ├── plugins │ │ ├── MODSDKSpring │ │ │ └── ... │ │ └── __init__.py │ ├── __init__.py │ ├── modMain.py │ ├── TutorialClientSystem.py │ └── TutorialServerSystem.py └── manifest.json ``` -------------------------------- ### Initialize Custom Components Source: https://github.com/creatormc/modsdksping/blob/main/modsdkspring/plugins/MODSDKSpring/core/ListenEvent.txt Use these decorators on custom Component classes to enable dependency injection and framework integration. Specify namespace and systemName if multiple systems are registered. ```python @staticmethod def InitComponentClient(cls=None, namespace=Target.DEFAULT, systemName=Target.DEFAULT): """ 添加到自定义的客户端 Component 类的上方,开启框架相关功能 Args: namespace (str, optional): 注入的客户端系统的命名空间,当 modMain.py 中只注册了一个客户端时不用填写此参数 systemName (str, optional): 注入的客户端系统的系统名称,当 modMain.py 中只注册了一个客户端时不用填写此参数 """ if cls: return ListenEvent.InitComponent(cls, SystemType.CLIENT, namespace, systemName) def wrapper(clazz): return ListenEvent.InitComponent(clazz, SystemType.CLIENT, namespace, systemName) return wrapper ``` ```python @staticmethod def InitComponentServer(cls=None, namespace=Target.DEFAULT, systemName=Target.DEFAULT): """ 添加到自定义的服务端 Component 类的上方,开启框架相关功能 Args: namespace (str, optional): 注入的服务端系统的命名空间,当 modMain.py 中只注册了一个服务端时不用填写此参数 systemName (str, optional): 注入的服务端系统的系统名称,当 modMain.py 中只注册了一个服务端时不用填写此参数 """ if cls: return ListenEvent.InitComponent(cls, SystemType.SERVER, namespace, systemName) def wrapper(clazz): ListenEvent.InitComponent(clazz, SystemType.SERVER, namespace, systemName) return wrapper ``` -------------------------------- ### 设置 Mod 名称和命名空间 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 在 modsdkspring init 命令执行过程中,按照提示输入 Mod 的名称,该名称将作为注册到引擎的命名空间。 ```shell TutorialMod ``` -------------------------------- ### Python __init__.py File Source: https://github.com/creatormc/modsdksping/wiki/框架使用 This file is required in Python 2 directories containing .py files to be recognized as packages. It can be empty. ```python ``` -------------------------------- ### Server Component Initialization Source: https://github.com/creatormc/modsdksping/blob/main/README.md This snippet demonstrates how to define and initialize a server component using the @ListenEvent.InitComponentServer decorator. It requires specifying the namespace and system name for component injection. ```APIDOC ## @ListenEvent.InitComponentServer ### Description Adds to the top of a custom server Component class, enabling framework functionalities. ### Parameters - **namespace** (str) - Required - The namespace of the server system to inject. This parameter can be omitted if only one server is registered in modMain.py. - **systemName** (str) - Required - The system name of the server system to inject. This parameter can be omitted if only one server is registered in modMain.py. ### Request Example ```python # -*- coding: utf-8 -*- from multipleModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent from multipleModScripts.modCommon import modConfig from multipleModScripts.plugins.MODSDKSpring.core.log.Log import logger @ListenEvent.InitComponentServer(namespace=modConfig.MOD_NAMESPACE_1, systemName=modConfig.SERVER_SYSTEM_NAME_1) class ACopyServerComponent(object): def __init__(self, server): self.server = server logger.info("ACopyServerComponent 获取到的系统:" + str(server)) ``` ``` -------------------------------- ### 设置 Mod 文件夹名称 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 在 modsdkspring init 命令执行过程中,按照提示输入 Mod 的文件夹名称。此名称将用于创建 Mod 的主目录。 ```shell tutorialScripts ``` -------------------------------- ### Manually Import Server Component Source: https://github.com/creatormc/modsdksping/blob/main/README.md Due to restrictions on the 'os' module in NetEase's Minecraft, you must manually import server components in TutorialServerSystem.py. This ensures that the MODSDKSpring framework can recognize and load your components. ```python from tutorialComponentScripts.components.server.ChatServerComponent import ChatServerComponent ``` -------------------------------- ### Initialize Mod Structure with modsdkspring Source: https://github.com/creatormc/modsdksping/wiki/框架使用 Use this command in the terminal within your behavior pack directory to automatically generate the initial mod structure. Follow the prompts to name your mod and its components. ```shell modsdkspring init ``` -------------------------------- ### Initialize Server Component with @ListenEvent.InitComponentServer Source: https://github.com/creatormc/modsdksping/blob/main/README.md Apply this decorator to a custom server component class to enable framework functionality. ```python # -*- coding: utf-8 -*- from particleModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent @ListenEvent.InitComponentServer class HurtEntityServerComponent(object): def __init__(self, server): self.server = server ``` -------------------------------- ### 创建服务端组件 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 定义一个服务端组件类,使用 @ListenEvent.InitComponentServer 装饰器注册,并监听 ServerChatEvent 事件以处理玩家聊天指令。 ```python # -*- coding: utf-8 -*- import mod.server.extraServerApi as serverApi from tutorialComponentScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent ServerSystem = serverApi.GetServerSystemCls() compFactory = serverApi.GetEngineCompFactory() @ListenEvent.InitComponentServer class ChatServerComponent(object): def __init__(self, server): # 获取服务端系统对象,即 TutorialServerSystem 的对象 self.server = server # 监听ServerChatEvent的回调函数 @ListenEvent.Server(eventName="ServerChatEvent") def OnServerChat(self, args): print "==== OnServerChat ==== ", args # 生成掉落物品 # 当我们输入的信息等于右边这个值时,创建相应的物品 # 创建Component,用来完成特定的功能,这里是为了创建Item物品 playerId = args["playerId"] comp = compFactory.CreateItem(playerId) if args["message"] == "钻石剑": # 调用SpawnItemToPlayerInv接口生成物品到玩家背包,参数参考《MODSDK文档》 comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_sword", "count":1, 'auxValue': 0}, playerId) elif args["message"] == "钻石镐": comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_pickaxe", "count":1, 'auxValue': 0}, playerId) elif args["message"] == "钻石头盔": comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_helmet", "count":1, 'auxValue': 0}, playerId) elif args["message"] == "钻石胸甲": comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_chestplate", "count":1, 'auxValue': 0}, playerId) elif args["message"] == "钻石护腿": comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_leggings", "count":1, 'auxValue': 0}, playerId) elif args["message"] == "钻石靴子": comp.SpawnItemToPlayerInv({"itemName":"minecraft:diamond_boots", "count":1, 'auxValue': 0}, playerId) else: print "==== Sorry man ====" ``` -------------------------------- ### 创建行为包文件夹结构 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 这是创建 MODSDKSpring 行为包所需的文件夹结构。确保 manifest.json 文件位于正确的目录下。 ```txt TutorialMod └── tutorialBehaviorPack ├── entities └── manifest.json ``` -------------------------------- ### 设置服务端系统类名 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 在 modsdkspring init 命令执行过程中,按照提示输入服务端系统的类名。此名称将作为服务端系统的类名。 ```shell TutorialServerSystem ``` -------------------------------- ### 设置客户端系统类名 Source: https://github.com/creatormc/modsdksping/blob/main/README.md 在 modsdkspring init 命令执行过程中,按照提示输入客户端系统的类名。此名称将作为客户端系统的类名。 ```shell TutorialClientSystem ``` -------------------------------- ### Import ClientDB and ServerDB Source: https://github.com/creatormc/modsdksping/wiki/DB模块 Import the necessary DB instances for client and server-side operations. These should be imported at the beginning of your client and server systems. ```python # 客户端 from 你的模组路径.client.ClientDB import clientDB # 服务端 from 你的模组路径.server.ServerDB import serverDB ``` -------------------------------- ### Client Component Initialization Source: https://github.com/creatormc/modsdksping/blob/main/README.md This snippet shows how to define and initialize a client component using the @ListenEvent.InitComponentClient decorator. It specifies the namespace and system name for component injection. ```APIDOC ## @ListenEvent.InitComponentClient ### Description Adds to the top of a custom client Component class, enabling framework functionalities. ### Parameters - **namespace** (str) - Required - The namespace of the client system to inject. This parameter can be omitted if only one client is registered in modMain.py. - **systemName** (str) - Required - The system name of the client system to inject. This parameter can be omitted if only one client is registered in modMain.py. ### Request Example ```python # -*- coding: utf-8 -*- from multipleModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent from multipleModScripts.modCommon import modConfig from multipleModScripts.plugins.MODSDKSpring.core.log.Log import logger @ListenEvent.InitComponentClient(namespace=modConfig.MOD_NAMESPACE_1, systemName=modConfig.CLIENT_SYSTEM_NAME_1) class ACopyClientComponent(object): def __init__(self, client): self.client = client logger.info("ACopyClientComponent 获取到的系统:" + str(client)) ``` ``` -------------------------------- ### Define a Client Component Source: https://github.com/creatormc/modsdksping/blob/main/README.md Use the InitComponentClient decorator to register a component to a specific client system. ```python # -*- coding: utf-8 -*- from multipleModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent from multipleModScripts.modCommon import modConfig from multipleModScripts.plugins.MODSDKSpring.core.log.Log import logger @ListenEvent.InitComponentClient(namespace=modConfig.MOD_NAMESPACE_1, systemName=modConfig.CLIENT_SYSTEM_NAME_1) class ACopyClientComponent(object): def __init__(self, client): self.client = client logger.info("ACopyClientComponent 获取到的系统:" + str(client)) ``` -------------------------------- ### Subscribe to Data Prefixes Source: https://github.com/creatormc/modsdksping/wiki/DB模块 Subscribes to a key prefix to automatically push global data to clients upon loading. Recommended for use in the system's __init__ method. ```python class MyServerSystem(ServerSystem): def __init__(self, namespace, systemName): serverDB.subscribe("my_mod_global") ``` -------------------------------- ### Update Server System Configuration Source: https://github.com/creatormc/modsdksping/wiki/框架使用 Update the server system file, ensuring the import path for ListenEvent is correct and the newly created ChatServerComponent is imported. ```python # -*- coding: utf-8 -*- import mod.server.extraServerApi as serverApi # 因为文件夹名称改变,所以导入路径也改变了 from tutorialComponentScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent # 导入组件,尽管这里你没有显式使用它,也必须导入! from tutorialComponentScripts.components.server.ChatServerComponent import ChatServerComponent ServerSystem = serverApi.GetServerSystemCls() compFactory = serverApi.GetEngineCompFactory() @ListenEvent.InitServer class TutorialServerSystem(ServerSystem): def __init__(self, namespace, systemName): pass ``` -------------------------------- ### Define a Server Component Source: https://github.com/creatormc/modsdksping/blob/main/README.md Use the InitComponentServer decorator to register a component to a specific server system. ```python # -*- coding: utf-8 -*- from multipleModScripts.plugins.MODSDKSpring.core.ListenEvent import ListenEvent from multipleModScripts.modCommon import modConfig from multipleModScripts.plugins.MODSDKSpring.core.log.Log import logger @ListenEvent.InitComponentServer(namespace=modConfig.MOD_NAMESPACE_1, systemName=modConfig.SERVER_SYSTEM_NAME_1) class ACopyServerComponent(object): def __init__(self, server): self.server = server logger.info("ACopyServerComponent 获取到的系统:" + str(server)) ``` -------------------------------- ### Define Server System Constants Source: https://context7.com/creatormc/modsdksping/llms.txt Configuration constants for the server system name and class path. ```python SERVER_SYSTEM_NAME = "TutorialServerSystem" SERVER_SYSTEM_CLS_PATH = "tutorialScripts.TutorialServerSystem.TutorialServerSystem" ```