ShopAPI
Exposes an easy-to-use API to add custom items to the in-game shop.
模組介紹 作者原文
Content Warning Shop API
===
Exposes an easy-to-use API to add custom items to the in-game shop in Content Warning.
See the mod's GitHub repository for detailed setup instructions and usage information.
Features
- Custom items: Opens up the in-game shop to allow custom items to be added. This includes automatically synchronising item prices between players, and registering custom ItemDataEntry types defined in your mod to interface with the game's built-in item synchroniser.
- Synchronisation: Easily synchronise any arbitrary settings between players in a lobby using Steam Lobby Metadata keys via the SynchronisedMetadata class.
- Localisation: Extends and opens up the game's localisation system to allow items to be translated to the supported locales by patching item and shop related localised methods.
Integration
DO NOT bundle the mod's DLL with your own. Ensure that no ShopAPI.dll is included with your build.
Depending on your publishing target, you should instead require it as a dependency on your publishing platform:
Steam Workshop
When publishing on the Steam Workshop, add this Workshop Item as your item's dependency via the "Add/Remove Required Items" option (on your mod's page right hand side panel).
Steam will ensure that the dependency will load before your mod when the game is launched.
(BepInEx)
When building a BepInEx plugin, add this mod as a dependency to your plugin's main file:
[BepInDependency(ShopApiPlugin.MOD_GUID)]
public class YourCustomPlugin: BaseUnityPlugin
{
//...
}
If a specific version is needed, pass ShopApiPlugin.MOD_VER after the GUID.
When publishing, add the mod's Dependency String to the manifest.json file, so mod managers can automatically fetch it.
Usage
Once added as a reference to your project, all classes are available under the ContentWarningShop namespace.
Creating items
Ideally Items should be preconfigured and packed into an AssetBundle, but you can also construct them at runtime if its easier.
Custom Items should have their persistentID, price, purchasable, Category, and icon properties set to work.
If you would like your item to have a chance to be randomly spawned in the Old World like other items, set spawnable to true, and itemType to Item.ItemType.Tool.
Registering items
Items can be registered via the RegisterItem method:
//using ContentWarningShop;
var yourCustomItem = yourAssetBundle.LoadAsset("yourItem");
Shop.RegisterItem(yourCustomItem);
Shop.RegisterCustomDataEntries();
NOTE:
Item prices are automatically synchronised between players on lobby join. The price set by the lobby's host will be used for the entire lobby.
You can check if a custom item has been already registered via the IsItemRegistered method. The list of all registered custom items is also available via the CustomItems property.
If your item uses custom ItemDataEntry types, call the RegisterCustomDataEntries method to fetch and register all custom types defined in your assembly. This will let the game automatically synchronise the items' custom state between players. (See compatibility if you run into issues)
It is enough call this method once per assembly / mod.
Updating item prices
If you need to update an item's price at any point after a lobby has been started, use the UpdateItemPrice method:
var success = Shop.UpdateItemPrice(yourCustomItem, price);
This will re-synchronise the item's price to every player. Note that only the lobby's host can update an item's price, so the method returns a boolean indicating if it was successful (if the local player had permissions).
Synchronising Settings
The SynchronisedMetadata class allows arbitrary settings to be synchronised between players through the use of Steam Lobby Metadata keys. Simply create a new instance with a specific type and key and it will be automatically updated whenever the key's value is changed.
TIP:
Consider prepending your mod's GUID to the key to ensure it won't accidentally collide with
安裝與使用 安裝:BepInEx 外掛
- 本站下載的壓縮包就是模組本體,通常是 BepInEx 外掛(解壓後得到 .dll 或外掛資料夾)。
- 建議流程:用社群模組管理器(如 r2modman)選中本遊戲後匯入該模組,管理器會自動裝好 BepInEx 前置並處理依賴。
- 手動流程:先在遊戲目錄裝好 BepInEx 載入器,再把解壓出的 .dll 或外掛資料夾放進 BepInEx/plugins/。
- 啟動遊戲,BepInEx 控制台會列印載入日誌;確認這個模組出現在日誌裡即為安裝成功。
帶「前置依賴」的模組需要一併安裝,否則不會載入。功能開關通常在遊戲內或外掛自帶的設定檔裡調整。
下面這些內容需要先安裝,否則該模組不會生效: