forked from war3i4i/ItemDrawers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.cs
More file actions
52 lines (44 loc) · 2.05 KB
/
API.cs
File metadata and controls
52 lines (44 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using kg_ItemDrawers;
using UnityEngine;
namespace API;
// class to copy. keep in mind that AllDrawers should not be cached in any case since Prefab and Amount are applied once so it will be outdated,
// also there is no IsValid() check so next frame Drawer can be destroyed and you will get null reference exception
// best usage: call AllDrawers() every time you need it and process it in the same frame
public static class ItemDrawers_API
{
private static readonly bool _IsInstalled;
private static readonly MethodInfo MI_GetAllDrawers;
public class Drawer(ZNetView znv)
{
public string Prefab = znv.GetZDO().GetString("Prefab");
public int Amount = znv.GetZDO().GetInt("Amount");
public void Remove(int amount) { znv.ClaimOwnership(); znv.InvokeRPC("ForceRemove", amount); }
public void Withdraw(int amount) => znv.InvokeRPC("WithdrawItem_Request", amount);
public void Add(int amount) => znv.InvokeRPC("AddItem_Request", Prefab, amount);
}
public static List<Drawer> AllDrawers => _IsInstalled ?
((List<ZNetView>)MI_GetAllDrawers.Invoke(null, null)).Select(znv => new Drawer(znv)).ToList()
: new();
public static List<Drawer> AllDrawersInRange(Vector3 pos, float range) => _IsInstalled ?
((List<ZNetView>)MI_GetAllDrawers.Invoke(null, null)).Where(znv => Vector3.Distance(znv.transform.position, pos) <= range).Select(znv => new Drawer(znv)).ToList()
: new();
static ItemDrawers_API()
{
if (Type.GetType("API.ClientSide, kg_ItemDrawers") is not { } drawersAPI)
{
_IsInstalled = false;
return;
}
_IsInstalled = true;
MI_GetAllDrawers = drawersAPI.GetMethod("AllDrawers", BindingFlags.Public | BindingFlags.Static);
}
}
//do not copy
public static class ClientSide
{
public static List<ZNetView> AllDrawers() => DrawerComponent.AllDrawers.Where(d => d._znv.IsValid()).Select(d => d._znv).ToList();
}