using Newtonsoft.Json;
using Oxide.Core;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("SubmersiblePumpFix", "Mi4ok", "1.0.6")]
public class SubmersiblePump : RustPlugin
{
private void Init()
{
config = Config.ReadObject<PluginConfig>();
Config.WriteObject(config);
cmd.AddChatCommand(config.command, this, nameof(CraftPumpCommand));
cmd.AddConsoleCommand("givepump", this, nameof(GivePumpCommand));
if (config.requirePerm) permission.RegisterPermission("submersiblepump.use", this);
permission.RegisterPermission("submersiblepump.give", this);
}
private void OnServerInitialized()
{
LoadData();
foreach (var pump in data.pumps)
{
WaterPump pumpEntity = BaseNetworkable.serverEntities.Find((NetworkableId)pump) as WaterPump;
if (pumpEntity != null)
TerrainMeta.TopologyMap.AddTopology(pumpEntity.transform.position, 65536);
}
}
private void Unload() => SaveData();
private void OnEntityKill(WaterPump entity)
{
if (data.pumps.Contains(entity.net.ID))
{
Item item = ItemManager.CreateByName("electric.fuelgenerator.small", 1, 2593673595);
item.name = config.pumpName;
item.Drop(entity.transform.position, Vector3.zero, new Quaternion());
}
}
private bool CanPickupEntity(BasePlayer player, WaterPump entity)
{
if (entity.skinID == 2593673595)
{
if (entity.GetBuildingPrivilege() == null)
{
entity.Kill();
return false;
}
if (!entity.GetBuildingPrivilege().IsAuthed(player)) return false;
entity.Kill();
return false;
}
return true;
}
private void OnEntityBuilt(Planner plan, GameObject go)
{
if (plan.GetOwnerPlayer() == null) return;
BasePlayer player = plan.GetOwnerPlayer();
FuelGenerator generator = go.ToBaseEntity() as FuelGenerator;
if (generator == null || generator.skinID != 2593673595) return;
if (config.requirePerm && !permission.UserHasPermission(player.UserIDString, "submersiblepump.use"))
{
Item item = ItemManager.CreateByName("electric.fuelgenerator.small", 1, 2593673595);
item.name = config.pumpName;
player.inventory.GiveItem(item);
NextTick(() => generator.Kill());
SendReply(player, Lang("NoPermission", player.UserIDString));
return;
}
Vector3 heightCheck = new Vector3(generator.transform.position.x, TerrainMeta.HeightMap.GetHeight(generator.transform.position), generator.transform.position.z);
if (config.checkGround && Vector3.Distance(heightCheck, generator.transform.position) > 1.4f)
{
Item item = ItemManager.CreateByName("electric.fuelgenerator.small", 1, 2593673595);
item.name = config.pumpName;
player.inventory.GiveItem(item);
NextTick(() => generator.Kill());
SendReply(player, Lang("TooHigh", player.UserIDString));
return;
}
WaterPump pump = GameManager.server.CreateEntity("assets/prefabs/deployable/playerioents/waterpump/water.pump.deployed.prefab", generator.transform.position, generator.transform.rotation) as WaterPump;
pump.skinID = generator.skinID;
pump.OwnerID = player.userID;
NextTick(() => generator.Kill());
pump.Spawn();
data.pumps.Add(pump.net.ID);
TerrainMeta.TopologyMap.AddTopology(pump.transform.position, 65536);
}
private object canRemove(BasePlayer player, WaterPump pump)
{
if (pump.skinID == 2593673595) return false;
else return null;
}
private void CraftPumpCommand(BasePlayer player, string command, string[] args)
{
if (config.requirePerm && !permission.UserHasPermission(player.UserIDString, "submersiblepump.use"))
{
SendReply(player, Lang("NoPermission", player.UserIDString));
return;
}
if (!config.enablePumpCraft)
{
SendReply(player, Lang("NotEnabled", player.UserIDString));
return;
}
if (args.Length == 0)
ShowHelp(player);
else if (args.Length == 1 && args[0].ToLower() == "craft")
{
if (config.requireBlueprint && !player.blueprints.HasUnlocked(ItemManager.FindItemDefinition("waterpump")))
{
SendReply(player, Lang("NoBlueprint", player.UserIDString));
return;
}
if (config.requiredWorkbench > player.currentCraftLevel)
{
SendReply(player, Lang("NoWorkbench", player.UserIDString, player.currentCraftLevel, config.requiredWorkbench));
return;
}
if (!TakeResources(player))
{
SendReply(player, Lang("NoItems", player.UserIDString));
return;
}
SendReply(player, Lang("ItemCrafted", player.UserIDString));
Item item = ItemManager.CreateByName("electric.fuelgenerator.small", 1, 2593673595);
item.name = config.pumpName;
if (player.inventory.GiveItem(item))
player.SendConsoleCommand($"note.inv -1284169891 1 \"{config.pumpName}\"");
else
item.Drop(player.transform.position + new Vector3(0, 1, 0), Vector3.zero);
}
else
ShowHelp(player);
}
private void GivePumpCommand(ConsoleSystem.Arg arg)
{
BasePlayer player = arg.Player();
if (player != null && !permission.UserHasPermission(player.UserIDString, "submersiblepump.give"))
{
SendReply(arg, Lang("NoPermission", player.UserIDString));
return;
}
if (player == null && arg.Args == null)
{
SendReply(arg, Lang("PlayerNotFound"));
return;
}
if (arg.Args != null && arg.Args.Length >= 1)
{
player = BasePlayer.FindByID(Convert.ToUInt64(arg.Args[0]));
if (player == null)
{
SendReply(arg, Lang("PlayerNotFound"));
return;
}
}
Item item = ItemManager.CreateByName("electric.fuelgenerator.small", 1, 2593673595);
item.name = config.pumpName;
player.inventory.GiveItem(item);
player.SendConsoleCommand($"note.inv -1284169891 1 \"{config.pumpName}\"");
SendReply(arg, Lang("ItemCrafted"));
}
private bool TakeResources(BasePlayer player)
{
foreach (var requiredItem in config.pumpCraftCost)
{
bool haveRequired = false;
int inventoryAmount = 0;
foreach (var item in player.inventory.AllItems())
{
if (item.skin == requiredItem.skin && item.info.shortname == requiredItem.shortname)
{
inventoryAmount += item.amount;
if (inventoryAmount >= requiredItem.amount)
{
haveRequired = true;
break;
}
}
}
if (!haveRequired)
return false;
}
foreach (var requiredItem in config.pumpCraftCost)
{
ItemDefinition itemDef = ItemManager.FindItemDefinition(requiredItem.shortname);
player.SendConsoleCommand($"note.inv {itemDef.itemid} -{requiredItem.amount}");
int takenItems = 0;
foreach (var item in player.inventory.AllItems())
{
if (item.skin == requiredItem.skin && item.info.shortname == requiredItem.shortname)
{
if (takenItems < requiredItem.amount)
{
if (item.amount > requiredItem.amount - takenItems)
{
item.amount -= requiredItem.amount - takenItems;
item.MarkDirty();
break;
}
if (item.amount <= requiredItem.amount - takenItems)
{
takenItems += item.amount;
item.GetHeldEntity()?.Kill();
item.Remove();
}
}
else break;
}
}
}
return true;
}
private void ShowHelp(BasePlayer player)
{
string items = string.Empty;
if (config.requireBlueprint)
items += Lang("BlueprintRequired", player.UserIDString);
foreach (var item in config.pumpCraftCost)
items += Lang("ItemFormat", player.UserIDString, item.amount, ItemManager.FindItemDefinition(item.shortname).displayName.english);
SendReply(player, Lang("Help", player.UserIDString, config.command, config.requiredWorkbench, items));
}
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["TooHigh"] = "You are <color=#5c81ed>too far from ground</color> to place submersible pump!",
["NotEnabled"] = "Submersible Pump crafting is <color=#5c81ed>not enabled</color>!",
["NoItems"] = "You don't have <color=#5c81ed>required items</color> to craft Submersible Pump!",
["ItemCrafted"] = "You've succesfully crafted your <color=#5c81ed>Submersible Pump</color>!",
["NoBlueprint"] = "You need to learn Water Pump <color=#5c81ed>blueprint</color> first to craft Submersible Pump!",
["NoWorkbench"] = "Submersible Pump require higher <color=#5c81ed>workbench level</color>!\n(Current: {0}, Required: {1})",
["Help"] = "<color=#5c81ed>/{0} craft</color> - Craft Submersible Pump\n\nRequired Workbench Level: <color=#5c81ed>{1}</color>\nRequired Items:\n{2}",
["BlueprintRequired"] = " - Water Pump Blueprint\n",
["ItemFormat"] = " - <color=#5c81ed>x{0}</color> {1}\n",
["NoPermission"] = "You don't have permission to craft and place <color=#5c81ed>Submersible Pumps</color>!",
["PlayerNotFound"] = "We couldn't find player with this ID!"
}, this);
lang.RegisterMessages(new Dictionary<string, string>
{
["TooHigh"] = "Вы <color=#5c81ed>находитесь слишком высоко</color>, чтобы установить погружной насос!",
["NotEnabled"] = "Изготовление погружного насоса <color=#5c81ed>отключено</color>!",
["NoItems"] = "У вас <color=#5c81ed>недостаточно необходимых предметов</color> для создания погружного насоса!",
["ItemCrafted"] = "Вы успешно создали <color=#5c81ed>погружной насос</color>!",
["NoBlueprint"] = "Сначала вам нужно изучить чертеж <color=#5c81ed>водного насоса</color>, чтобы создать погружной насос!",
["NoWorkbench"] = "Для установки погружного насоса требуется более высокий <color=#5c81ed>уровень верстака</color>!\n(Текущий: {0}, Требуемый: {1})",
["Help"] = "<color=#5c81ed>/{0} craft</color> - Создать погружной насос\n\nТребуемый уровень верстака: <color=#5c81ed>{1}</color>\nНеобходимые предметы:\n{2}",
["BlueprintRequired"] = " - Чертеж водного насоса\n",
["ItemFormat"] = " - <color=#5c81ed>x{0}</color> {1}\n",
["NoPermission"] = "У вас нет разрешения на создание и установку <color=#5c81ed>погружных насосов</color>!",
["PlayerNotFound"] = "Мы не смогли найти игрока с этим ID!"
}, this, "ru");
}
private string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
private PluginConfig config;
protected override void LoadDefaultConfig()
{
Config.WriteObject(config = new PluginConfig()
{
pumpCraftCost = new List<ItemConfig>()
{
new ItemConfig() { shortname = "metal.fragments", amount = 1000, skin = 0 },
new ItemConfig() { shortname = "gears", amount = 10, skin = 0 },
new ItemConfig() { shortname = "metalpipe", amount = 20, skin = 0 }
}
}, true);
}
private class PluginConfig
{
[JsonProperty("Misc - Require Permission")]
public bool requirePerm = false;
[JsonProperty("Misc - Pump Item Name")]
public string pumpName = "Погружной насос";
[JsonProperty("Misc - Pump Ground Check")]
public bool checkGround = true;
[JsonProperty("Craft - Enable Pump Craft")]
public bool enablePumpCraft = false;
[JsonProperty("Craft - Chat Command")]
public string command = "pump";
[JsonProperty("Craft - Require Blueprint For Pump")]
public bool requireBlueprint = true;
[JsonProperty("Craft - Required Workbench Level (0-3)")]
public int requiredWorkbench = 2;
[JsonProperty("Craft - Pump Craft Cost")]
public List<ItemConfig> pumpCraftCost = new List<ItemConfig>();
}
private class ItemConfig
{
[JsonProperty("Item Shortname")]
public string shortname;
[JsonProperty("Item Amount")]
public int amount;
[JsonProperty("Item Skin")]
public ulong skin;
}
private static PluginData data = new PluginData();
private class PluginData
{
[JsonProperty("Placed Pumps")]
public List<NetworkableId> pumps = new List<NetworkableId>();
}
private void LoadData()
{
data = Interface.Oxide.DataFileSystem.ReadObject<PluginData>(this.Name);
timer.Every(Core.Random.Range(500, 700), SaveData);
if (data == null)
{
PrintError("Data file is corrupted! Generating new data file...");
Interface.Oxide.DataFileSystem.WriteObject(this.Name, new PluginData());
data = Interface.Oxide.DataFileSystem.ReadObject<PluginData>(this.Name);
}
}
private void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(this.Name, data);
}
}