-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__item.cs
123 lines (97 loc) · 4.3 KB
/
__item.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//reference System.Core.dll
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MCGalaxy;
namespace NA2 {
public class Item {
static bool ValidItemName(string name) {
foreach (char c in name) {
if (!(
(c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
c == '_' ||
c == '.' ||
c == '-'
)) { return false; }
}
return true;
}
static string ItemDirectory(string playerName) {
string directory = "text/inventory/" + playerName + "/";
if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); }
return directory;
}
public static Item[] GetItemsOwnedBy(string playerName) {
DirectoryInfo info = new DirectoryInfo(ItemDirectory(playerName));
FileInfo[] allItemFiles = info.GetFiles().OrderBy(f => f.CreationTime).ToArray();
Item[] allItems = new Item[allItemFiles.Length];
for (int i = 0; i < allItems.Length; i++) {
allItems[i] = new Item(allItemFiles[i].Name);
}
return allItems;
}
public static Item MakeInstance(Player p, string itemName) {
try {
return new Item(itemName);
} catch (System.ArgumentException e) {
p.Message("&W{0}", e.Message);
return null;
}
}
public readonly string name;
public readonly string displayName;
public readonly bool isVar;
public string[] ItemDesc { get { return GetDesc(); } }
public string Color { get { return (ItemDesc.Length == 0) ? "&b" : "&6"; } }
public string ColoredName { get { return Color+displayName; } }
public Item(string givenName) {
if (String.IsNullOrWhiteSpace(givenName)) {
throw new System.ArgumentException("Item name cannot be null or blank");
}
name = givenName.ToUpper().Replace(' ', '_');
if (!ValidItemName(name)) {
throw new System.ArgumentException("Item name \""+name+"\" may only use A-Z or _ . characters.");
}
displayName = name.Replace('_', ' ');
isVar = name.StartsWith("VAR.");
}
public bool OwnedBy(string playerName) {
return File.Exists(ItemDirectory(playerName) + name);
}
public void GiveTo(Player p) {
if (OwnedBy(p.name)) { return; }
GiveToOffline(p.name);
if (isVar) { return; }
p.Message("You found {0} {1}%S!", AOrAn(), ColoredName);
p.Message("Check what stuff you have with &b/stuff%S.");
}
public void GiveToOffline(string playerName) {
// Used in contest plugin
if (OwnedBy(playerName)) { return; }
File.WriteAllText(ItemDirectory(playerName) + name + "", "");
}
public bool TakeFrom(Player p) {
if (!TakeFromOffline(p.name)) { return false; } //only send message if item was actually taken
if (!isVar) { p.Message("{0}%S was removed from your stuff.", ColoredName); }
return true;
}
public bool TakeFromOffline(string playerName) {
if (name.Contains("/") || name.Contains("\\")) { return false; } //nice try
if (!OwnedBy(playerName)) { return false; }
File.Delete(ItemDirectory(playerName) + name);
return true;
}
public string AOrAn() {
if (name.StartsWith("A") || name.StartsWith("E") || name.StartsWith("I") || name.StartsWith("O") || name.StartsWith("U")) { return "an"; }
return "a";
}
string[] GetDesc() {
const string descDirectory = "text/itemDesc/";
if (!File.Exists(descDirectory + name + ".txt")) { return new string[] { }; }
string[] desc = File.ReadAllLines(descDirectory + name + ".txt");
return desc;
}
}
}