Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <API/ARK/Ark.h>
- #include <API/UE/Math/ColorList.h>
- #include <Logger/Logger.h>
- #include "json.hpp"
- #pragma comment(lib, "ArkApi.lib")
- nlohmann::json config;
- FString GetText(const std::string& str)
- {
- return FString(ArkApi::Tools::Utf8Decode(config["Messages"].value(str, "No message")).c_str());
- }
- void ReadConfig()
- {
- const std::string config_path = ArkApi::Tools::GetCurrentDir() + "/ArkApi/Plugins/CustomShop/config.json";
- std::ifstream file{ config_path };
- if (!file.is_open())
- throw std::runtime_error("Can't open config.json");
- file >> config;
- file.close();
- }
- bool SellItem(AShooterPlayerController* player_controller, const nlohmann::basic_json<>& item_entry, uint64 steam_id, int amount)
- {
- bool success = false;
- auto items_map = item_entry["ReqItems"];
- for (const auto& item : items_map)
- {
- const int default_amount = item["Amount"];
- const std::string blueprint = item["Blueprint"];
- const int final_amount = default_amount * amount;
- if (final_amount <= 0)
- return false;
- FString fblueprint(blueprint);
- UPrimalInventoryComponent* inventory = player_controller->GetPlayerCharacter()->MyInventoryComponentField();
- if (inventory == nullptr)
- {
- return false;
- }
- int item_count = 0;
- // Count items
- TArray<UPrimalItem*> items_for_removal;
- TArray<UPrimalItem*> items = inventory->InventoryItemsField();
- for (UPrimalItem* item : items)
- {
- if (item->ClassField() != nullptr)
- {
- const FString item_bp = ArkApi::IApiUtils::GetItemBlueprint(item);
- if (item_bp == fblueprint)
- {
- items_for_removal.Add(item);
- item_count += item->GetItemQuantity();
- if (item_count >= final_amount)
- {
- break;
- }
- }
- }
- }
- if (item_count >= final_amount)
- {
- item_count = 0;
- // Remove items
- for (UPrimalItem* item : items_for_removal)
- {
- item_count += item->GetItemQuantity();
- if (item_count > final_amount)
- {
- item->SetQuantity(item_count - final_amount, true);
- inventory->NotifyClientsItemStatus(item, false, false, true, false, false, nullptr, nullptr, false, false, true);
- }
- else
- {
- inventory->RemoveItem(&item->ItemIDField(), false, false, true, true);
- }
- }
- ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"), *GetText("SoldItems"));
- success = true;
- }
- else
- {
- ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"), *GetText("NotEnoughItems"), item_count, final_amount);
- }
- return success;
- }
- //const FString blueprint = FString(item_entry.value("Blueprint", "").c_str());
- // const int needed_amount = item_entry.value("Amount", 1) * amount;
- }
- bool Sell(AShooterPlayerController* player_controller, const FString& item_id, int amount)
- {
- if (ArkApi::IApiUtils::IsPlayerDead(player_controller))
- return false;
- if (amount <= 0)
- amount = 1;
- bool success = false;
- const uint64 steam_id = ArkApi::IApiUtils::GetSteamIdFromController(player_controller);
- auto items_list = config.value("BuyItems", nlohmann::json::object());
- auto item_entry_iter = items_list.find(item_id.ToString());
- if (item_entry_iter == items_list.end())
- {
- ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"), *GetText("WrongId"));
- return false;
- }
- auto item_entry = item_entry_iter.value();
- success = SellItem(player_controller, item_entry, steam_id, amount);
- if (success)
- {
- const std::wstring log = fmt::format(TEXT("{}({}) sold item \"{}\". Amount - {}"), *ArkApi::IApiUtils::GetSteamName(player_controller), steam_id, *item_id, amount);
- // ShopLog::GetLog()->info(ArkApi::Tools::Utf8Encode(log));
- }
- return success;
- }
- void ChatSell(AShooterPlayerController* player_controller, FString* message, EChatSendMode::Type)
- {
- TArray<FString> parsed;
- message->ParseIntoArray(parsed, L" ", true);
- if (parsed.IsValidIndex(1))
- {
- Sell(player_controller, parsed[1], 1);
- }
- else
- {
- ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"), *GetText("SellUsage"));
- }
- }
- void Load()
- {
- Log::Get().Init("CustomShop");
- try
- {
- ReadConfig();
- }
- catch (const std::exception& error)
- {
- Log::GetLog()->error(error.what());
- throw;
- }
- ArkApi::GetCommands().AddChatCommand(GetText("SellCmd"), &ChatSell);
- }
- void Unload()
- {
- }
- BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
- {
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- Load();
- break;
- case DLL_PROCESS_DETACH:
- Unload();
- break;
- }
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement