C#:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using Network;
using System.Linq;
///Скачано с дискорд сервера Rust Edit [PRO+]
///discord.gg/9vyTXsJyKR
namespace Oxide.Plugins
{
[Info("Backpacks", "discord.gg/9vyTXsJyKR", "1.0.81")]
public class Backpacks : RustPlugin
{
#region Hooks
void OnServerInitialized()
{
if (!config.openOnActive)
Unsubscribe("OnActiveItemChanged");
foreach (var entry in config.backpack.Keys)
{
backpackProps.Add(config.backpack[entry].skinid, config.backpack[entry].slots);
Puts($"Backpack \"{entry}\" with {config.backpack[entry].slots} slots registered.");
}
}
//saved props for looted item
private Dictionary<ulong, int> backpackProps = new Dictionary<ulong, int>();
void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
player.ClientRPCPlayer(null, player, "OnRespawnInformation");
}
void OnActiveItemChanged(BasePlayer player, Item oldItem, Item newItem)
{
try
{
if (oldItem != null && oldItem.text != null)
player.ClientRPCPlayer(null, player, "OnRespawnInformation");
if (newItem.text != null && backpackProps.ContainsKey(newItem.skin))
{
if (newItem.contents != null)
{
NextTick(() => {
OpenContainer(player, newItem.contents, config.backpack[newItem.text].slots, newItem);
});
}
}
}
catch
{
// :)
}
}
private object CanAcceptItem(ItemContainer container, Item item, int targetPos)
{
// check if player is trying to place backpack into backpack
if (container.parent != null)
{
if (container.parent.text != null && container.parent.text != "")
{
if (item.text != null && item.text != "")
return ItemContainer.CanAcceptResult.CannotAccept;
}
if (!config.canStoreNonEmpty && container.parent.text != null && container.parent.text != "")
{
if (container.parent.parent.GetOwnerPlayer() == null)
return ItemContainer.CanAcceptResult.CannotAccept;
}
}
//check if player already has a backpack
if (container.playerOwner != null && backpackProps.ContainsKey(item.skin))
{
var itemList = container.playerOwner.inventory.AllItems().ToArray();
foreach (ulong skin in backpackProps.Keys)
{
if (itemList.Any(x => x.skin == skin && x.uid != item.uid))
{
return ItemContainer.CanAcceptResult.CannotAccept;
}
}
}
if (!config.canStoreNonEmpty && container.playerOwner == null && backpackProps.ContainsKey(item.skin))
{
if (item.contents != null)
{
if (item.contents.itemList.Count > 0)
return ItemContainer.CanAcceptResult.CannotAccept;
}
}
return null;
}
object CanMoveItem(Item item, PlayerInventory playerLoot, uint targetContainer, int targetSlot, int amount)
{
if (item.parent.parent == null)
return null;
if (item.parent.parent.text != null && item.parent.parent.text != "")
{
if (!playerLoot.containerMain.IsFull())
{
item.MoveToContainer(playerLoot.containerMain);
return true;
}
if (!playerLoot.containerBelt.IsFull())
{
item.MoveToContainer(playerLoot.containerBelt);
return true;
}
}
return null;
}
object CanStackItem(Item item, Item targetItem)
{
if (backpackProps.ContainsKey(item.skin))
return false;
return null;
}
object OnItemAction(Item item, string action, BasePlayer player)
{
if (item.text != null && action == "drop")
{
player.EndLooting();
return null;
}
if (item.text != null && action == "unwrap")
{
if (player.inventory.containerMain.FindItemByUID(item.uid) == null &&
player.inventory.containerBelt.FindItemByUID(item.uid) == null)
{
SendReply(player, "You need to equip backpack first in order to open it.");
return false;
}
if (item.contents != null)
{
NextTick(() => {
OpenContainer(player, item.contents, config.backpack[item.text].slots, item);
});
}
return false;
}
if (backpackProps.ContainsKey(item.skin) && action == "unwrap")
{
if (player.inventory.containerMain.FindItemByUID(item.uid) == null &&
player.inventory.containerBelt.FindItemByUID(item.uid) == null)
{
SendReply(player, "You need to equip backpack first in order to open it.");
return false;
}
if (item.contents == null)
{
string itemtext = "";
foreach (string entry in config.backpack.Keys)
{
if (config.backpack[entry].skinid == item.skin)
{
itemtext = entry;
break;
}
}
if (itemtext == "")
{
Puts($"No backpack with skin id {item.skin} was found in config.");
return false;
}
item.text = itemtext;
item.name = config.backpack[itemtext].displayname;
item.contents = new ItemContainer();
item.contents.ServerInitialize(item, backpackProps[item.skin]);
item.contents.GiveUID();
item.MarkDirty();
}
if (item.contents != null)
{
NextTick(() => {
OpenContainer(player, item.contents, backpackProps[item.skin], item);
});
}
return false;
}
return null;
}
#endregion
#region Functions/Methods
[ConsoleCommand("givebackpack")]
private void givebackpack_cmd(ConsoleSystem.Arg arg)
{
var player = arg?.Player();
var args = arg.Args;
if (args.Length < 1) return;
if (player == null)
{
if (args.Length != 2)
{
Puts("SteamID is not present in command.");
return;
}
player = BasePlayer.FindByID(Convert.ToUInt64(args[1]));
if (player == null)
{
Puts("Player not found.");
return;
}
}
else
{
if (!player.IsAdmin)
{
SendReply(player, "You don't have permission to use this command.");
return;
}
if (args.Length == 2)
{
player = BasePlayer.FindByID(Convert.ToUInt64(args[1]));
if (player == null)
{
Puts("Player not found.");
return;
}
}
}
var backpack = config.backpack[args[0]];
var item = ItemManager.CreateByName(
backpack.shortname,
1,
backpack.skinid
);
if (item == null)
{
Puts($"{backpack.shortname} is not valid shortname");
return;
}
if (item.contents == null)
{
item.contents = new ItemContainer();
item.contents.ServerInitialize(item, backpack.slots);
item.contents.GiveUID();
}
item.name = backpack.displayname;
item.text = args[0];
item.MarkDirty();
player.GiveItem(item);
Puts($"Giving {args[0]} to {player}");
}
private void OpenContainer(BasePlayer player, ItemContainer container, int _capacity, Item _item)
{
var loot = player.inventory.loot;
if (player == null) return;
loot.Clear();
loot.PositionChecks = false;
loot.entitySource = RelationshipManager.ServerInstance;
loot.itemSource = _item;
loot.MarkDirty();
loot.AddContainer(container);
loot.SendImmediate();
player.ClientRPCPlayer(null, player, "RPC_OpenLootPanel", "generic_resizable");
PlayFx(player);
}
private void PlayFx(BasePlayer player, string fx = "assets/prefabs/deployable/locker/sound/equip_zipper.prefab")
{
if (player == null) return;
if (fx == null || !config.soundOnOpen) return;
var EffectInstance = new Effect();
EffectInstance.Init(Effect.Type.Generic, player, 0, Vector3.up, Vector3.zero);
EffectInstance.pooledstringid = StringPool.Get(fx);
NetWrite netWrite = Net.sv.StartWrite();
netWrite.PacketID(Message.Type.Effect);
EffectInstance.WriteToStream(netWrite);
netWrite.Send(new SendInfo(player.net.connection));
EffectInstance.Clear();
}
#endregion
#region [Config]
private Configuration config;
protected override void LoadConfig()
{
base.LoadConfig();
config = Config.ReadObject<Configuration>();
SaveConfig();
}
protected override void LoadDefaultConfig() => config = Configuration.CreateConfig();
protected override void SaveConfig() => Config.WriteObject(config);
class Configuration
{
[JsonProperty("Open backpack when held.")]
public bool openOnActive { get; set; }
[JsonProperty("Allow storing of non-empty backpacks in boxes.")]
public bool canStoreNonEmpty { get; set; }
[JsonProperty("Play sound on backpack open.")]
public bool soundOnOpen { get; set; }
[JsonProperty("Backpacks")]
public Dictionary<string, BackpackProps> backpack { get; set; }
public class BackpackProps
{
[JsonProperty("Display Name")]
public string displayname { get; set; }
[JsonProperty("Shortname")]
public string shortname { get; set; }
[JsonProperty("Skin Id")]
public ulong skinid { get; set; }
[JsonProperty("Number of Slots")]
public int slots { get; set; }
}
public static Configuration CreateConfig()
{
return new Configuration
{
openOnActive = true,
canStoreNonEmpty = true,
soundOnOpen = true,
backpack = new Dictionary<string, Backpacks.Configuration.BackpackProps>{
{"pouch", new Backpacks.Configuration.BackpackProps{
displayname = "Small Pouch",
shortname = "halloween.lootbag.small",
skinid = 2866538257,
slots = 6
}},
{"backpack_1", new Backpacks.Configuration.BackpackProps{
displayname = "Backpack",
shortname = "halloween.lootbag.medium",
skinid = 2787478993,
slots = 12
}},
{"backpack_2", new Backpacks.Configuration.BackpackProps{
displayname = "Tactical Backpack",
shortname = "halloween.lootbag.large",
skinid = 2866532898,
slots = 24
}},
},
};
}
}
#endregion
}
}