C#:
// Все права принадлежат дискорд сообществу https://discord.gg/VgNHPpNrz6
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
using Oxide.Game.Rust.Cui;
using Oxide.Core.Plugins;
namespace Oxide.Plugins {
[Info("VirtualQuarries", "ThePitereq", "2.3.12")]
public class VirtualQuarries : RustPlugin {
[PluginReference]
private readonly Plugin ImageLibrary, PopUpAPI, ServerRewards, Economics, IQEconomic, BankSystem, ShoppyStock;
private static VirtualQuarries _plugin;
private readonly Dictionary<ulong, CachedSurvey> cachedSurveys = new Dictionary<ulong, CachedSurvey>();
private readonly Dictionary<ulong, List<RequiredItem>> cachedRequirements = new Dictionary<ulong, List<RequiredItem>>();
private readonly Dictionary<ulong, string> cachedSort = new Dictionary<ulong, string>();
private readonly Dictionary<ulong, int> cachedPage = new Dictionary<ulong, int>();
private readonly Dictionary<ulong, Dictionary<string, int>> cachedQuarryCount = new Dictionary<ulong, Dictionary<string, int>>();
private readonly Dictionary<ulong, string> cachedSurveyType = new Dictionary<ulong, string>();
private readonly Dictionary<ulong, ulong> cachedInventories = new Dictionary<ulong, ulong>();
private static DateTime lastOperation = DateTime.Now;
private class CachedSurvey {
public string profile = string.Empty;
public List<QuarryResource> resources = new List<QuarryResource>();
}
private void Init() {
_plugin = this;
config = Config.ReadObject<PluginConfig>();
Config.WriteObject(config);
LoadData();
LoadMessages();
}
private void OnServerInitialized() {
if (config.economyPlugin == 1 && Economics == null) PrintWarning("Economics plugin not found! You will not be able to upgrade your quarries with server currency!");
else if (config.economyPlugin == 2 && ServerRewards == null) PrintWarning("ServerRewards plugin not found! You will not be able to upgrade your quarries with server currency!");
else if (config.economyPlugin == 3 && IQEconomic == null) PrintWarning("IQEconomic plugin not found! You will not be able to upgrade your quarries with server currency!");
else if (config.economyPlugin == 4 && BankSystem == null) PrintWarning("BankSystem plugin not found! You will not be able to upgrade your quarries with server currency!");
else if (config.economyPlugin == 5 && ShoppyStock == null) PrintWarning("ShoppyStock plugin not found! You will not be able to upgrade your quarries with server currency!");
if (ImageLibrary == null) PrintWarning("ImageLibrary plugin not found! It is required for all images to work!");
if (PopUpAPI == null) PrintWarning("PopUpAPI plugin not found! The Pop-Up messages will not display!");
else if (PopUpAPI.Version.Major < 2) PrintWarning("You are using old version of PopUpAPI! Update it to 2.0.0 or newer, or plugin will print errors or no pop-up messages at all!");
if (config.surveyThrow) Unsubscribe(nameof(OnExplosiveThrown));
AddImage("https://images.pvrust.eu/ui_icons/VirtualQuarries/arrow_right_0.png", "UI_VirtualQuarries_arrow_right_0", 0);
AddImage("https://images.pvrust.eu/ui_icons/VirtualQuarries/arrow_left_0.png", "UI_VirtualQuarries_arrow_left_0", 0);
AddImage("https://images.pvrust.eu/ui_icons/VirtualQuarries/shared_0.png", "UI_VirtualQuarries_shared_0", 0);
AddImage("https://images.pvrust.eu/ui_icons/VirtualQuarries/search_0.png", "UI_VirtualQuarries_search_0", 0);
if (config.requirePermission) permission.RegisterPermission("virtualquarries.use", this);
if (config.sharingRequirePermission) permission.RegisterPermission("virtualquarries.share", this);
if (config.quarryPerm) permission.RegisterPermission("virtualquarries.static.quarry", this);
if (config.pumpJackPerm) permission.RegisterPermission("virtualquarries.static.pumpjack", this);
foreach (var permissionKey in config.permissions.Keys) permission.RegisterPermission(permissionKey, this);
foreach (var permissionKey in config.surveys.Values) if (permissionKey.permission != string.Empty) permission.RegisterPermission(permissionKey.permission, this);
foreach (var permissionKey in config.quarryProfiles.Values) {
if (permissionKey.permission != string.Empty) permission.RegisterPermission(permissionKey.permission, this);
foreach (var resourceKey in permissionKey.resources.Values) if (resourceKey.permission != string.Empty) permission.RegisterPermission(resourceKey.permission, this);
}
if (config.storeContainers) timer.Every(config.containerSaveInterval, () => SaveContainers());
foreach (var command in config.commandList) cmd.AddChatCommand(command, this, nameof(VirtualQuarriesCommand));
Dictionary<string, ulong> reservedImagesLoad = new Dictionary<string, ulong>();
foreach (var profile in config.quarryProfiles.Values) {
reservedImagesLoad.TryAdd(profile.icon.shortname, profile.icon.skin);
if (profile.icon.iconUrl != "") AddImage(profile.icon.iconUrl, profile.icon.shortname, profile.icon.skin);
foreach (var resource in profile.resources) {
reservedImagesLoad.TryAdd(resource.Value.shortname, resource.Value.skin);
if (resource.Value.iconUrl != "") AddImage(resource.Value.iconUrl, resource.Value.shortname, resource.Value.skin);
foreach (var additional in resource.Value.additionalItems) {
reservedImagesLoad.TryAdd(additional.shortname, additional.skin);
if (additional.iconUrl != "") AddImage(additional.iconUrl, additional.shortname, additional.skin);
}
}
foreach (var item in profile.requiredItems) {
reservedImagesLoad.TryAdd(item.shortname, item.skin);
if (item.iconUrl != "") AddImage(item.iconUrl, item.shortname, item.skin);
}
foreach (var upgrade in profile.upgrades) foreach (var item in upgrade.requiredItems) {
reservedImagesLoad.TryAdd(item.shortname, item.skin);
if (item.iconUrl != "") AddImage(item.iconUrl, item.shortname, item.skin);
}
}
foreach (var survey in config.surveys.Values) {
reservedImagesLoad.TryAdd(survey.surveyItem.shortname, survey.surveyItem.skin);
if (survey.surveyItem.iconUrl != "") AddImage(survey.surveyItem.iconUrl, survey.surveyItem.shortname, survey.surveyItem.skin);
}
if (config.forceLoadSteamImages) ImageLibrary?.Call("LoadImageList", Name, reservedImagesLoad);
timer.Once(1f, () => SetupQuarries());
foreach (var player in BasePlayer.activePlayerList) {
playerCache.TryAdd(player.userID, player.displayName);
playerCache[player.userID] = player.displayName;
if (config.offlineQuarryOwnerKickTime == 0) continue;
DateTime now = DateTime.Now;
foreach (var quarry in data.quarries) {
if (quarry.Value.owner != player.userID) continue;
quarry.Value.lastOwnerOnline = now;
}
}
if (config.offlineQuarryOwnerKickTime > 0) CheckQuarryKicks();
}
private void SetupQuarries() {
if (config.staticQuarries) {
foreach (var quarry in BaseNetworkable.serverEntities.OfType<MiningQuarry>()) {
quarry.SetOn(false);
quarry.CancelInvoke();
quarry.engineSwitchPrefab.instance.SetFlag(BaseEntity.Flags.On, true);
quarry.SetFlag(BaseEntity.Flags.On, true);
quarry.SendNetworkUpdate();
}
foreach (var quarry in data.staticQuarries.ToList()) {
if (quarry.Key == uint.MaxValue) continue;
MiningQuarry quarryEnt = BaseNetworkable.serverEntities.Find(new NetworkableId(quarry.Key)) as MiningQuarry;
if (quarryEnt == null) {
foreach (var user in quarry.Value) {
BoxStorage storage = BaseNetworkable.serverEntities.Find(new NetworkableId(user.Value)) as BoxStorage;
storage?.Kill();
}
data.staticQuarries.Remove(quarry.Key);
continue;
}
foreach (var user in quarry.Value) {
BoxStorage storage = BaseNetworkable.serverEntities.Find(new NetworkableId(user.Value)) as BoxStorage;
if (storage == null) {
Puts($"[VirtualQuarries ERROR] Static quarry of {user.Key} is missing output container!");
continue;
}
GroundWatch gw = storage.GetComponent<GroundWatch>();
if (gw != null) GameObject.Destroy(gw);
DestroyOnGroundMissing dogm = storage.GetComponent<DestroyOnGroundMissing>();
if (dogm != null) GameObject.Destroy(dogm);
if (storage.children.Count == 0) {
Puts($"[VirtualQuarries ERROR] Static quarry of {user.Key} is missing fuel container!");
continue;
}
BoxStorage fuelStorage = storage.children[0] as BoxStorage;
if (fuelStorage != null) {
gw = fuelStorage.GetComponent<GroundWatch>();
if (gw != null) GameObject.Destroy(gw);
dogm = fuelStorage.GetComponent<DestroyOnGroundMissing>();
if (dogm != null) GameObject.Destroy(dogm);
}
VirtualQuarry vQuarry = storage.GetComponent<VirtualQuarry>();
if (vQuarry == null) {
storage.gameObject.AddComponent<VirtualQuarry>();
vQuarry = storage.GetComponent<VirtualQuarry>();
}
vQuarry.staticQuarry = quarryEnt;
NextTick(() => vQuarry.SetupQuarry());
}
}
}
else Unsubscribe(nameof(OnQuarryToggled));
if (config.excavatorQuarry) {
if (config.excavatorResources.Count < 4) {
PrintWarning("You don't have all 4 resources required to run excavator properly! Generating new ones...");
config.excavatorResources = new Dictionary<string, List<StaticQuarryOutput>>() {
{
"Stone", new List<StaticQuarryOutput>() {
new StaticQuarryOutput() {
shortname = "stones", amount = 5000
}
}
}
, {
"Metal", new List<StaticQuarryOutput>() {
new StaticQuarryOutput() {
shortname = "metal.fragments", amount = 2500
}
}
}
, {
"Sulfur", new List<StaticQuarryOutput>() {
new StaticQuarryOutput() {
shortname = "sulfur.ore", amount = 1000
}
}
}
, {
"HQM", new List<StaticQuarryOutput>() {
new StaticQuarryOutput() {
shortname = "hq.metal.ore", amount = 50
}
}
}
}
;
Config.WriteObject(config);
}
foreach (var arm in BaseNetworkable.serverEntities.OfType<ExcavatorArm>()) {
arm.SetFlag(BaseEntity.Flags.Reserved8, true);
arm.SetFlag(BaseEntity.Flags.On, true);
}
if (data.staticQuarries.ContainsKey(uint.MaxValue)) {
foreach (var user in data.staticQuarries[uint.MaxValue]) {
BoxStorage storage = BaseNetworkable.serverEntities.Find(new NetworkableId(user.Value)) as BoxStorage;
if (storage == null) continue;
GroundWatch gw = storage.GetComponent<GroundWatch>();
if (gw != null) GameObject.Destroy(gw);
DestroyOnGroundMissing dogm = storage.GetComponent<DestroyOnGroundMissing>();
if (dogm != null) GameObject.Destroy(dogm);
if (storage.children.Count == 0) {
Puts($"[VirtualQuarries ERROR] Excavator quarry of {user.Key} is missing fuel container!");
continue;
}
BoxStorage fuelStorage = storage.children[0] as BoxStorage;
if (fuelStorage != null) {
gw = fuelStorage.GetComponent<GroundWatch>();
if (gw != null) GameObject.Destroy(gw);
dogm = fuelStorage.GetComponent<DestroyOnGroundMissing>();
if (dogm != null) GameObject.Destroy(dogm);
}
VirtualQuarry vQuarry = storage.GetComponent<VirtualQuarry>();
if (vQuarry == null) {
storage.gameObject.AddComponent<VirtualQuarry>();
vQuarry = storage.GetComponent<VirtualQuarry>();
}
vQuarry.excavator = true;
NextTick(() => vQuarry.SetupQuarry());
}
}
}
else Unsubscribe(nameof(OnExcavatorResourceSet));
if (!config.staticQuarries && !config.excavatorQuarry) Unsubscribe(nameof(CanLootEntity));
if (!config.excavatorLockDrops) Unsubscribe(nameof(OnExcavatorSuppliesRequest));
foreach (var quarry in data.quarries) {
cachedQuarryCount.TryAdd(quarry.Value.owner, new Dictionary<string, int>());
cachedQuarryCount[quarry.Value.owner].TryAdd(quarry.Value.profile, 0);
cachedQuarryCount[quarry.Value.owner][quarry.Value.profile]++;
BoxStorage storage = BaseNetworkable.serverEntities.Find(new NetworkableId(quarry.Value.netId)) as BoxStorage;
VirtualQuarry vQuarry = storage?.GetComponent<VirtualQuarry>();
if (storage == null) {
Puts($"[VirtualQuarries ERROR] Quarry with ID {quarry.Key} had missing resource container! Recreating...");
SpawnQuarry(quarry.Value.owner, quarry.Key);
storage = BaseNetworkable.serverEntities.Find(new NetworkableId(quarry.Value.netId)) as BoxStorage;
}
else {
GroundWatch gw = storage.GetComponent<GroundWatch>();
if (gw != null) GameObject.Destroy(gw);
DestroyOnGroundMissing dogm = storage.GetComponent<DestroyOnGroundMissing>();
if (dogm != null) GameObject.Destroy(dogm);
BoxStorage fuelStorage = null;
List<UpgradeConfig> upgrades = config.quarryProfiles[quarry.Value.profile].upgrades;
if (storage.children.Count == 0) {
fuelStorage = GameManager.server.CreateEntity(config.storagePrefab, storage.transform.position) as BoxStorage;
fuelStorage.Spawn();
GameObject.Destroy(fuelStorage.GetComponent<GroundWatch>());
GameObject.Destroy(fuelStorage.GetComponent<DestroyOnGroundMissing>());
fuelStorage.SetParent(storage);
fuelStorage.transform.position = storage.transform.position + new Vector3(0, 0.7f, 0);
if (quarry.Value.level < upgrades.Count) fuelStorage.inventory.capacity = config.quarryProfiles[quarry.Value.profile].upgrades[quarry.Value.level].fuelCapacity;
Puts($"[VirtualQuarries ERROR] Spawning missing quarry fuel container of quarry with ID {quarry.Key}...");
}
fuelStorage = storage.children[0] as BoxStorage;
if (fuelStorage != null) {
gw = fuelStorage.GetComponent<GroundWatch>();
if (gw != null) GameObject.Destroy(gw);
dogm = fuelStorage.GetComponent<DestroyOnGroundMissing>();
if (dogm != null) GameObject.Destroy(dogm);
}
else {
fuelStorage = GameManager.server.CreateEntity(config.storagePrefab, storage.transform.position) as BoxStorage;
fuelStorage.Spawn();
GameObject.Destroy(fuelStorage.GetComponent<GroundWatch>());
GameObject.Destroy(fuelStorage.GetComponent<DestroyOnGroundMissing>());
fuelStorage.SetParent(storage);
fuelStorage.transform.position = storage.transform.position + new Vector3(0, 0.7f, 0);
if (quarry.Value.level < upgrades.Count) fuelStorage.inventory.capacity = config.quarryProfiles[quarry.Value.profile].upgrades[quarry.Value.level].fuelCapacity;
Puts($"Spawning missing quarry fuel container of quarry with ID {quarry.Key}...");
}
if (vQuarry == null) {
storage.gameObject.AddComponent<VirtualQuarry>();
vQuarry = storage.GetComponent<VirtualQuarry>();
}
if (!config.quarryProfiles.ContainsKey(quarry.Value.profile)) {
PrintWarning($"There is a quarry with profile '{quarry.Value.profile}' but it is missing in the configuration!");
continue;
}
if (quarry.Value.level < upgrades.Count) {
(storage.children[0] as BoxStorage).inventory.capacity = upgrades[quarry.Value.level].fuelCapacity;
storage.inventory.capacity = upgrades[quarry.Value.level].capacity;
}
else PrintWarning($"Quarry with ID {quarry.Key} have more upgrades than is added to config! You need to add more levels or remove this quarry from data, or plugin will print errors!");
}
vQuarry = storage.GetComponent<VirtualQuarry>();
vQuarry.dataId = quarry.Key;
if (storage != null) NextTick(() => vQuarry.SetupQuarry());
}
}
private void Unload() {
if (config.storeContainers) SaveContainers();
if (config.excavatorQuarry) {
foreach (var arm in BaseNetworkable.serverEntities.OfType<ExcavatorArm>()) {
arm.SetFlag(BaseEntity.Flags.Reserved8, false);
arm.SetFlag(BaseEntity.Flags.On, false);
}
}
if (config.staticQuarries) {
foreach (var quarry in BaseNetworkable.serverEntities.OfType<MiningQuarry>()) {
quarry.SetOn(false);
quarry.CancelInvoke();
quarry.engineSwitchPrefab.instance.SetFlag(BaseEntity.Flags.On, false);
quarry.SetFlag(BaseEntity.Flags.On, false);
quarry.SendNetworkUpdate();
}
}
SaveData();
foreach (var player in BasePlayer.activePlayerList) {
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Main");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_New");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_NewCursor");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Back");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Players");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Levels");
if (cachedInventories.ContainsKey(player.userID)) player.EndLooting();
}
foreach (var netId in data.quarries.Values) {
BoxStorage quarry = BaseNetworkable.serverEntities.Find(new NetworkableId(netId.netId)) as BoxStorage;
if (quarry != null) GameObject.Destroy(quarry.GetComponent<VirtualQuarry>());
}
foreach (var quarryId in data.staticQuarries.ToList()) {
foreach (var player in quarryId.Value) {
BoxStorage quarry = BaseNetworkable.serverEntities.Find(new NetworkableId(player.Value)) as BoxStorage;
if (quarry != null) GameObject.Destroy(quarry.GetComponent<VirtualQuarry>());
}
}
SaveData();
}
private void OnNewSave() {
config = Config.ReadObject<PluginConfig>();
Config.WriteObject(config);
if (config.wipeData) {
LoadData();
data = new PluginData();
SaveData();
if (config.consoleLogs) Puts("Plugin data has been wiped successfully!");
}
}
private void OnPlayerConnected(BasePlayer player) {
playerCache.TryAdd(player.userID, player.displayName);
playerCache[player.userID] = player.displayName;
if (config.offlineQuarryOwnerKickTime == 0) return;
DateTime now = DateTime.Now;
foreach (var quarry in data.quarries) {
if (quarry.Value.owner != player.userID) continue;
quarry.Value.lastOwnerOnline = now;
}
}
private void OnLootEntityEnd(BasePlayer player) {
if (cachedInventories.ContainsKey(player.userID)) {
NextTick(() => {
if (cachedInventories[player.userID] != 0) {
BaseEntity entity = BaseNetworkable.serverEntities.Find(new NetworkableId(cachedInventories[player.userID])) as BaseEntity;
if (entity != null && !entity.IsDestroyed) entity.Kill();
}
cachedInventories.Remove(player.userID);
}
);
}
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Back");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Levels");
}
private void OnExplosiveThrown(BasePlayer player, BaseEntity entity, ThrownWeapon item) {
if (item.ShortPrefabName.Contains("survey_charge") && item.skinID == 0) {
entity.Kill();
Item survey = ItemManager.CreateByName("surveycharge");
NextTick(() => player.GiveItem(survey));
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NoSurveyThrow", player.UserIDString, config.commandList[0]));
}
}
private object OnEntityTakeDamage(BoxStorage entity) {
if (entity.transform.position.y < -390) return false;
return null;
}
private void OnQuarryToggled(MiningQuarry quarry, BasePlayer player) {
quarry.SetOn(false);
quarry.CancelInvoke();
quarry.engineSwitchPrefab.instance.SetFlag(BaseEntity.Flags.On, true);
quarry.SetFlag(BaseEntity.Flags.On, true);
quarry.SendNetworkUpdate();
}
private object OnExcavatorResourceSet(ExcavatorArm arm, string type, BasePlayer player) {
if (!data.staticQuarries.ContainsKey(uint.MaxValue) || !data.staticQuarries[uint.MaxValue].ContainsKey(player.userID)) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("OpenStorageFirst", player.UserIDString));
return false;
}
ulong storageId = data.staticQuarries[uint.MaxValue][player.userID];
BoxStorage storage = BaseNetworkable.serverEntities.Find(new NetworkableId(storageId)) as BoxStorage;
if (storage == null) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("ErrorOccured", player.UserIDString));
return false;
}
storage.GetComponent<VirtualQuarry>().mineType = type;
if (!arm.IsOn()) {
arm.SetFlag(BaseEntity.Flags.On, true);
ExcavatorServerEffects.SetMining(true, true);
}
return false;
}
private object OnExcavatorSuppliesRequest(ExcavatorArm arm, BasePlayer player) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NoAirDrops", player.UserIDString));
return false;
}
private object CanLootEntity(BasePlayer player, StorageContainer storage) {
if (storage == null) return null;
if (config.excavatorQuarry && storage is ExcavatorOutputPile) {
ExcavatorOutputPile excavatorStorage = storage as ExcavatorOutputPile;
if (config.excavatorPerm && !permission.UserHasPermission(player.UserIDString, "virtualquarries.static.excavator")) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NoPermissionQuarry", player.UserIDString));
return false;
}
uint excavatorId = uint.MaxValue;
data.staticQuarries.TryAdd(excavatorId, new Dictionary<ulong, ulong>());
if (!data.staticQuarries[excavatorId].ContainsKey(player.userID)) SpawnStaticQuarry(excavatorId, player, false, storage);
else OpenExcavatorQuarry(player, excavatorStorage);
return false;
}
else if (config.excavatorQuarry && storage is DieselEngine) {
DieselEngine dieselStorage = storage as DieselEngine;
if (config.excavatorPerm && !permission.UserHasPermission(player.UserIDString, "virtualquarries.static.excavator")) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NoPermissionQuarry", player.UserIDString));
return false;
}
uint excavatorId = uint.MaxValue;
data.staticQuarries.TryAdd(excavatorId, new Dictionary<ulong, ulong>());
if (!data.staticQuarries[excavatorId].ContainsKey(player.userID)) SpawnStaticQuarry(excavatorId, player, false, storage);
else OpenExcavatorQuarry(player, dieselStorage);
return false;
}
else if (config.staticQuarries && storage is ResourceExtractorFuelStorage) {
ResourceExtractorFuelStorage quarryStorage = storage as ResourceExtractorFuelStorage;
MiningQuarry quarry = quarryStorage.GetParentEntity() as MiningQuarry;
if (quarry == null) return null;
if (config.quarryPerm && quarry.ShortPrefabName == "mininquarry_static" && !permission.UserHasPermission(player.UserIDString, "virtualquarries.static.quarry")) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NoPermissionQuarry", player.UserIDString));
return false;
}
else if (config.pumpJackPerm && quarry.ShortPrefabName == "pumpjack-static" && !permission.UserHasPermission(player.UserIDString, "virtualquarries.static.pumpjack")) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NoPermissionPumpJack", player.UserIDString));
return false;
}
data.staticQuarries.TryAdd(quarry.net.ID.Value, new Dictionary<ulong, ulong>());
if (!data.staticQuarries[quarry.net.ID.Value].ContainsKey(player.userID)) SpawnStaticQuarry(quarry.net.ID.Value, player, true, storage, quarry);
else OpenStaticQuarry(player, quarryStorage, quarry);
return false;
}
return null;
}
private void SpawnStaticQuarry(ulong netId, BasePlayer player, bool staticQuarry, StorageContainer storage, MiningQuarry quarry = null) {
int worldSize = Convert.ToInt32(World.Size);
float startX = -worldSize / 1.5f;
float startZ = worldSize / 1.5f;
Vector3 newLocation = new Vector3(startX + Core.Random.Range(-50, 50), -400, startZ + Core.Random.Range(-50, 50));
BoxStorage resourceStorage = GameManager.server.CreateEntity(config.storagePrefab, newLocation) as BoxStorage;
resourceStorage.Spawn();
resourceStorage.inventory.capacity = config.staticResourceSize;
data.staticQuarries[netId].TryAdd(player.userID, resourceStorage.net.ID.Value);
data.staticQuarries[netId][player.userID] = resourceStorage.net.ID.Value;
BoxStorage fuelStorage = GameManager.server.CreateEntity(config.storagePrefab, newLocation) as BoxStorage;
fuelStorage.Spawn();
UnityEngine.Object.Destroy(resourceStorage.GetComponent<GroundWatch>());
UnityEngine.Object.Destroy(resourceStorage.GetComponent<DestroyOnGroundMissing>());
UnityEngine.Object.Destroy(fuelStorage.GetComponent<GroundWatch>());
UnityEngine.Object.Destroy(fuelStorage.GetComponent<DestroyOnGroundMissing>());
fuelStorage.SetParent(resourceStorage);
fuelStorage.transform.position = newLocation + new Vector3(0, 0.7f, 0);
fuelStorage.inventory.capacity = staticQuarry ? config.staticFuelSize : config.excavatorFuelSize;
VirtualQuarry vQuarry = resourceStorage.GetComponent<VirtualQuarry>();
if (vQuarry == null) {
resourceStorage.gameObject.AddComponent<VirtualQuarry>();
vQuarry = resourceStorage.GetComponent<VirtualQuarry>();
}
if (staticQuarry) vQuarry.staticQuarry = quarry;
else vQuarry.excavator = true;
NextTick(() => {
vQuarry.SetupQuarry();
if (storage is ResourceExtractorFuelStorage) OpenStaticQuarry(player, storage as ResourceExtractorFuelStorage, quarry);
else if (storage is DieselEngine) OpenExcavatorQuarry(player, storage as DieselEngine);
else if (storage is ExcavatorOutputPile) OpenExcavatorQuarry(player, storage as ExcavatorOutputPile);
}
);
}
[ConsoleCommand("UI_VirtualQuarries")]
private void VirtualQuarriesConsoleCommand(ConsoleSystem.Arg arg) {
BasePlayer player = arg.Player();
if (config.requirePermission && !permission.UserHasPermission(player.UserIDString, "virtualquarries.use")) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NoPermission", player.UserIDString));
return;
}
switch (arg.Args[0]) {
case "close" : CuiHelper.DestroyUi(player, "VirtualQuarriesUI_NewCursor");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_New");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Main");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Players");
break;
case "new" : CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Main");
SelectSurveyTypeUI(player);
break;
case "back" : CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Main");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_NewCursor");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_New");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Back");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Players");
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Levels");
player.EndLooting();
if (arg.Args.Length == 1) ShowQuarryMenuUI(player);
else ShowQuarryMenuUI(player, 1, Convert.ToInt32(arg.Args[1]));
break;
case "throw_open" : cachedSurveyType.TryAdd(player.userID, "");
cachedSurveyType[player.userID] = arg.Args[1];
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_New");
ShowNewQuarryUI(player, true);
break;
case "throw" : TrySurveyThrow(player);
break;
case "place" : TryPlaceQuarry(player);
break;
case "view" : ShowQuarryMenuUI(player, Convert.ToInt32(arg.Args[1]), Convert.ToInt32(arg.Args[2]));
break;
case "engine" : TryStartQuarry(player, Convert.ToInt32(arg.Args[1]));
break;
case "fuel" : TryOpenFuelContainer(player, Convert.ToInt32(arg.Args[1]));
break;
case "storage" : TryOpenResourceContainer(player, Convert.ToInt32(arg.Args[1]));
break;
case "delete" : TryRemoveQuarry(player, Convert.ToInt32(arg.Args[1]));
break;
case "sortOwned" : ChangeSort(player, "owned");
break;
case "sortShared" : ChangeSort(player, "shared");
break;
case "sortDisable" : ChangeSort(player, "disable");
break;
case "page" : int page = Convert.ToInt32(arg.Args[1]);
cachedPage.TryAdd(player.userID, page);
cachedPage[player.userID] = page;
ShowQuarryMenuUI(player, page);
break;
case "action" : CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Main");
if (arg.Args.Length == 2) ManageQuarryPlayersUI(player, arg.Args[1]);
else if (arg.Args.Length == 3) ManageQuarryPlayersUI(player, arg.Args[1], Convert.ToInt32(arg.Args[2]));
else if (arg.Args.Length == 4) ManageQuarryPlayersUI(player, arg.Args[1], Convert.ToInt32(arg.Args[2]), Convert.ToInt32(arg.Args[3]));
else if (arg.Args.Length >= 5) ManageQuarryPlayersUI(player, arg.Args[1], Convert.ToInt32(arg.Args[2]), Convert.ToInt32(arg.Args[3]), arg.Args[4]);
break;
case "doAction" : ManageQuarryUser(player, arg.Args[1], Convert.ToInt32(arg.Args[2]), Convert.ToUInt64(arg.Args[3]));
break;
case "list" : ShowQuarryAccessList(player, Convert.ToInt32(arg.Args[1]));
break;
case "upgrade" : TryUpgradeQuarry(player, Convert.ToInt32(arg.Args[1]), arg.Args[2]);
break;
default : ShowQuarryMenuUI(player);
break;
}
}
private void VirtualQuarriesCommand(BasePlayer player) => ShowQuarryMenuUI(player);
private void ShowQuarryAccessList(BasePlayer player, int quarryId) {
if (data.quarries[quarryId].owner != player.userID && !data.quarries[quarryId].authPlayers.Contains(player.userID)) return;
string accessList = Lang("AccessListStart", player.UserIDString);
foreach (var playerId in data.quarries[quarryId].authPlayers) accessList += Lang("AccessListList", player.UserIDString, playerCache[playerId]);
if (accessList == Lang("AccessListStart", player.UserIDString)) accessList += Lang("AccessListNoAdded", player.UserIDString);
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, accessList, 10f, "Hud.Menu", 12);
}
private void TryUpgradeQuarry(BasePlayer player, int quarryId, string type) {
if (data.quarries[quarryId].owner != player.userID && !data.quarries[quarryId].authPlayers.Contains(player.userID)) return;
QuarryData quarry = data.quarries[quarryId];
List<UpgradeConfig> upgrades = config.quarryProfiles[quarry.profile].upgrades;
if (type == "currency") {
if (upgrades[quarry.level + 1].requiredRp == 0) return;
if (config.economyPlugin == 1) {
if (Economics.Call<double>("Balance", player.userID) >= upgrades[quarry.level + 1].requiredRp) {
Economics.Call("Withdraw", player.userID, Convert.ToDouble(upgrades[quarry.level + 1].requiredRp));
UpgradeQuarry(player, quarryId, type);
}
else PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NotEnoughCurrency", player.UserIDString));
}
else if (config.economyPlugin == 2) {
if (ServerRewards.Call<int>("CheckPoints", player.userID) >= upgrades[quarry.level + 1].requiredRp) {
ServerRewards.Call("TakePoints", player.userID, upgrades[quarry.level + 1].requiredRp);
UpgradeQuarry(player, quarryId, type);
}
else PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NotEnoughCurrency", player.UserIDString));
}
else if (config.economyPlugin == 3) {
if (IQEconomic.Call<int>("API_GET_BALANCE", player.userID) >= upgrades[quarry.level + 1].requiredRp) {
IQEconomic.Call("API_REMOVE_BALANCE", player.userID, upgrades[quarry.level + 1].requiredRp);
UpgradeQuarry(player, quarryId, type);
}
else PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NotEnoughCurrency", player.UserIDString));
}
else if (config.economyPlugin == 4) {
if (BankSystem.Call<int>("Balance", player.userID) >= upgrades[quarry.level + 1].requiredRp) {
BankSystem.Call("Withdraw", player.userID, upgrades[quarry.level + 1].requiredRp);
UpgradeQuarry(player, quarryId, type);
}
else PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NotEnoughCurrency", player.UserIDString));
}
else if (config.economyPlugin == 5) {
if (ShoppyStock.Call<int>("GetCurrencyAmount", config.economyCurrency, player.userID) >= upgrades[quarry.level + 1].requiredRp) {
ShoppyStock.Call("TakeCurrency", config.economyCurrency, player.userID, upgrades[quarry.level + 1].requiredRp);
UpgradeQuarry(player, quarryId, type);
}
else PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NotEnoughCurrency", player.UserIDString));
}
}
else {
DateTime now = DateTime.Now;
if ((now - lastOperation).TotalSeconds < 1) return;
if (!TakeResources(player, upgrades[quarry.level + 1].requiredItems)) PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NotEnoughItems", player.UserIDString));
else UpgradeQuarry(player, quarryId, type);
lastOperation = now;
}
}
private void UpgradeQuarry(BasePlayer player, int quarryId, string type) {
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Levels");
QuarryData quarry = data.quarries[quarryId];
quarry.level++;
bool typeBool = type == "currency";
quarry.upgradedForRp.TryAdd(quarry.level, typeBool);
List<UpgradeConfig> upgrades = config.quarryProfiles[quarry.profile].upgrades;
BoxStorage storage = BaseNetworkable.serverEntities.Find(new NetworkableId(quarry.netId)) as BoxStorage;
if (storage == null) return;
storage.inventory.capacity = upgrades[quarry.level].capacity;
storage.SendNetworkUpdate();
BoxStorage fuelStorage = storage.children.Count > 0 ? storage.children[0] as BoxStorage : null;
if (fuelStorage != null) {
fuelStorage.inventory.capacity = upgrades[quarry.level].fuelCapacity;
fuelStorage.SendNetworkUpdate();
}
storage.GetComponent<VirtualQuarry>().SetupQuarry();
bool anyNewResource = false;
if (upgrades[quarry.level].additionalResources.Any()) {
foreach (var resourceKey in upgrades[quarry.level].additionalResources) {
if (!config.quarryProfiles[quarry.profile].resources.ContainsKey(resourceKey)) continue;
ResourceConfig resource = config.quarryProfiles[quarry.profile].resources[resourceKey];
if (resource.permission != string.Empty && !permission.UserHasPermission(player.UserIDString, resource.permission)) continue;
quarry.resources.Add(new QuarryResource() {
configKey = resourceKey, work = Core.Random.Range(resource.outputMin, resource.outputMax)
}
);
VirtualQuarry qr = BaseNetworkable.serverEntities.Find(new NetworkableId(quarry.netId))?.GetComponent<VirtualQuarry>();
qr?.SetupQuarry();
anyNewResource = true;
}
}
if (anyNewResource) PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NewResourceDug", player.UserIDString));
Effect.server.Run("assets/prefabs/deployable/quarry/effects/mining-quarry-deploy.prefab", player.transform.position);
Effect.server.Run("assets/bundled/prefabs/fx/build/promote_toptier.prefab", player.transform.position);
Interface.CallHook("OnQuarryUpgraded", player, quarry.level, quarry.profile);
player.EndLooting();
timer.Once(0.2f, () => TryOpenResourceContainer(player, quarryId));
if (config.consoleLogs) Puts($"Player {player.displayName} ({player.userID}) upgraded his quarry with ID {quarryId} to level {quarry.level + 1}.");
}
private void ManageQuarryUser(BasePlayer player, string type, int quarryId, ulong userId) {
if (quarryId != 0 && !data.quarries.ContainsKey(quarryId)) return;
if (quarryId != 0 && data.quarries[quarryId].owner != player.userID && !data.quarries[quarryId].authPlayers.Contains(player.userID)) return;
if (config.shareClanOnly) {
if (player.currentTeam == 0 || !RelationshipManager.ServerInstance.teams[player.currentTeam].members.Contains(userId)) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("OnlyTeamShare", player.UserIDString));
return;
}
}
if (type.Contains("addAll")) {
foreach (var quarry in data.quarries.Values) if (quarry.owner == player.userID && !quarry.authPlayers.Contains(userId)) quarry.authPlayers.Add(userId);
return;
}
else if (type == "remove") data.quarries[quarryId].authPlayers.Remove(userId);
else if (type.Contains("add") && !data.quarries[quarryId].authPlayers.Contains(userId)) data.quarries[quarryId].authPlayers.Add(userId);
CuiHelper.DestroyUi(player, "VirtualQuarriesUI_Players");
ShowQuarryMenuUI(player, 1, quarryId);
}
private void ChangeSort(BasePlayer player, string type) {
cachedSort.Remove(player.userID);
if (type == "owned") cachedSort.TryAdd(player.userID, "owned");
else if (type == "shared") cachedSort.TryAdd(player.userID, "shared");
ShowQuarryMenuUI(player);
}
private void TryRemoveQuarry(BasePlayer player, int quarryId) {
if (!data.quarries.ContainsKey(quarryId)) {
Puts($"[VirtualQuarries ERROR] Player {player.displayName} ({player.UserIDString}) is trying to remove quarry more than once!");
return;
}
if (data.quarries[quarryId].owner != player.userID) return;
if (!player.serverInput.IsDown(BUTTON.SPRINT)) {
PopUpAPI?.Call("ShowPopUp", player, config.popUpPreset, Lang("NoSprintButton", player.UserIDString));
return;
}
BoxStorage quarry = GetQuarry(quarryId);
bool anyRefund = false;
if (config.refundRemove) {
anyRefund = true;
List<RequiredItem> refundItems1 = config.quarryProfiles[data.quarries[quarryId].profile].requiredItems;
foreach (var refundItem in refundItems1) {
Item item = ItemManager.CreateByName(refundItem.shortname, refundItem.amount, refundItem.skin);
if (!string.IsNullOrEmpty(refundItem.displayName)) item.name = refundItem.displayName;
if (!item.MoveToContainer(player.inventory.containerMain)) item.Drop(player.eyes.position, Vector3.zero);
}
Dictionary<string, ResourceConfig> refundItems2 = config.quarryProfiles[data.quarries[quarryId].profile].resources;
foreach (var resource in data.quarries[quarryId].resources) {
if (!refundItems2.ContainsKey(resource.configKey)) continue;
foreach (var refundItem in refundItems2[resource.configKey].additionalItems) {
Item item = ItemManager.CreateByName(refundItem.shortname, refundItem.amount, refundItem.skin);
if (!string.IsNullOrEmpty(refundItem.displayName)) item.name = refundItem.displayName;
if (!item.MoveToContainer(player.inventory.containerMain)) item.Drop(player.eyes.position, Vector3.zero);
}
}
foreach (var item in quarry.inventory.itemList.ToList()) if (!item.MoveToContainer(player.inventory.containerMain)) item.Drop(player.eyes.position, Vector3.zero);
BoxStorage fuelStorage = quarry.children[0] as BoxStorage;
if (fuelStorage != null) foreach (var item in fuelStorage.inventory.itemList.ToList()) if (!item.MoveToContainer(player.inventory.containerMain)) item.Drop(player.eyes.position, Vector3.zero);
PopUpAPI?.Ca
// ... [Code preview truncated due to length. Full source in attached file] ...