最佳實踐
有許多方法可以創建插件,並有許多陷阱等著粗心的開發者。
Plugin Development Guidelines
The following guidelines have been prepared to aid Sponge plugin developers. It is not a definitive or comprehensive list, merely an attempt to detail some issues that are likely to arise during plugin development, and propose our best solutions.
備註
We reserve the use of Sponge for official SpongePowered works. Please don't use Sponge as part of your plugin name, unless (1) your plugin primarily concerns the Minecraft block "Sponge", or (2) your plugin also has versions for other APIs (in which case you may append "for Sponge" to the title).
Economy API
經濟API用於連結經濟插件與其他利用經濟的插件(例如商店)。你可以參閱經濟API章節 here,它列出了所有你需要知道的細節。
Packets
任何攔截數據包或加入自定義物品、方塊、實體......的行為*不被計畫*成為SpongeAPI的一部分。須注意攔截數據包可能是用錯解決問題的方法,而現有的SpongeAPI可能已經包含解決方案。在一些狀況中,可以在SpongeAPI加入任何所需要的;或者相反地,使用Forge API並創建一個Mod。
Using Forge or NMS Classes
我們不建議和Forge或Minecraft元程式一起作業,除非其能提供和SpongeAPI的相容性。大部分NMS(net.minecraft.server)代碼中的插件沒有很好的錯誤處理,造成除錯非常困難。相較於使用SpongeAPI,維護NMS模組也更加困難。加入SpongeAPI程式碼內部的模組必須寫作特定API來使其可被Sponge插件調用,而並不依賴於底層的Minecraft程式碼。但插件可以創建成能在不同相融環境下載入以和底層實作 (SpongeForge or SpongeVanilla)互動。
Plugins using implementation-specific code are very likely to break between versions, and should be clearly labelled as such wherever they are hosted. These may more appropriately labelled as "Mods".
Mixins
Mixins are specifically for transforming classes before other mods/plugins start. ForgeModLoader calls these mods “Coremods”. SpongeForge is a Coremod, and deploys mixins on startup. Mixins can be used by plugins, but be aware of the additional complexities involved.
Hybrid Mods
Sponge plugins which leverage mixins may also be a considered core mods, based on content.
To use mixins in FML, it must be a coremod. The jar may also contain a Sponge plugin, so most properly the container is a "hybrid mod".
To use mixins in SpongeVanilla, intentions must be declared in the manifest. SpongeVanilla then injects the mixins.
Having both types in a single jar is entirely possible. (Indeed, a single jar could easily contain a tweaker, FML mod, coremod, bukkit plugin, sponge plugin, and/or litemod.)
Some definitions may be helpful here.
- Tweak Mod (aka Tweaker)
一個次系統級的模組透過LaunchWrapper直接繫結遊戲,通常模組系統(如LiteLoader、FML)和獨立模組(如Optifine),可以直接和任何遊戲環境互動。基本上跨越每個版本。
- Core Mod
has almost equivalent power of a Tweak Mod but must be bootstrapped by a ModSystem. Can interact with any aspect of the game environment directly. Generally breaks on every new Game version.
- Mod
interacts with the game only via a ModSystem, the mod is exposed to game objects directly but will generally only hook into the game via hooks provided by the ModSystem. Generally breaks every major version of the Game (depending on features used). The term mod is also used as an umbrella term for anything which modifies the game, though for the sake of clarity we'll use this definition.
- 插件
interacts with the game only via an API, does not interact with game objects directly in any way, only leverages objects exposed by the API. Generally breaks only when the API is revised (and sometimes not even then).
It's also important to distinguish the container from the contents. Issues with terminology tends to arise because a jar containing a mod tends to get referred to as a "mod". Any plugin which is not fully decoupled via the API puts itself into the category of Mod. This type of "plugin" may be prevalent where there are shortcomings in an API.
Advantages of Hybrid Mods
A hybrid mod leverages both a plugin component which interacts via the API, and a mod (or even coremod) in the same package. This has the disadvantages of a mod (breaks every version) but also the power of a mod (can interact with the game directly) coupled with some of the benefits of a Plugin (high-level abstract access to the game, and can also interact with other plugins as a peer).
The primary benefit of this system is that the maintenance burden is reduced when updating the mod, because any features accessed via the API are likely to be much more stable.
這種模組可以用於實作需要超出API能力的插件
Unlike NMS-exploiting "plugins", a hybrid mod makes its nature clear.