forked from Interverse/CustomItems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomItems.cs
183 lines (159 loc) · 11 KB
/
CustomItems.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
namespace CustomItems {
[ApiVersion (2, 1)]
public class CustomItems : TerrariaPlugin {
public override string Name => "CustomItems";
public override string Author => "Johuan";
public override string Description => "Allows you to spawn custom items";
public override Version Version => Assembly.GetExecutingAssembly().GetName().Version;
public CustomItems(Main game) : base(game) {
}
public override void Initialize() {
ServerApi.Hooks.GameInitialize.Register(this, OnInitialize);
}
protected override void Dispose(bool disposing) {
if (disposing) {
ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize);
}
base.Dispose(disposing);
}
private void OnInitialize(EventArgs args) {
Commands.ChatCommands.Add(new Command("customitem", CustomItem, "customitem", "citem")
{
HelpText = "/customitem <id/itemname> <parameters> <#> ... \nParameters: hexcolor (hc), prefix (p), stack (st), damage (d), knockback (kb), useanimation (ua), " +
"usetime (ut), shoot (s), shootspeed (ss), width (w), height (h), scale (sc), ammo (a), useammo (uam), notammo (na)."
});
Commands.ChatCommands.Add(new Command("customitem.give", GiveCustomItem, "givecustomitem", "gcitem")
{
HelpText = "/givecustomitem <name> <id/itemname> <parameters> <#> ... \nParameters: hexcolor (hc), prefix (p), stack (st), damage (d), knockback (kb), useanimation (ua), " +
"usetime (ut), shoot (s), shootspeed (ss), width (w), height (h), scale (sc), ammo (a), useammo (uam), notammo (na)."
});
}
private void CustomItem(CommandArgs args) {
List<string> parameters = args.Parameters;
int num = parameters.Count();
if (num == 0) {
args.Player.SendErrorMessage("Invalid Syntax. /customitem <id/itemname> <parameters> <#> ... \nParameters: hexcolor (hc), prefix (p), stack (st), damage (d), knockback (kb), useanimation (ua), " +
"usetime (ut), shoot (s), shootspeed (ss), width (w), height (h), scale (sc), ammo (a), useammo (uam), notammo (na).");
return;
}
List<Item> items = TShock.Utils.GetItemByIdOrName(args.Parameters[0]);
Item item = items[0];
TSPlayer player = new TSPlayer(args.Player.Index);
int itemIndex = Item.NewItem((int)player.X, (int)player.Y, item.width, item.height, item.type, item.maxStack);
Item targetItem = Main.item[itemIndex];
targetItem.playerIndexTheItemIsReservedFor = args.Player.Index;
for (int index = 1; index < num; ++index) {
string lower = parameters[index].ToLower();
if ((lower.Equals("hexcolor") || lower.Equals("hc")) && index + 1 < num) {
targetItem.color = new Color(int.Parse(args.Parameters[index + 1].Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(args.Parameters[index + 1].Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(args.Parameters[index + 1].Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
} else if ((lower.Equals("damage") || lower.Equals("d")) && index + 1 < num) {
targetItem.damage = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("knockback") || lower.Equals("kb")) && index + 1 < num) {
targetItem.knockBack = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("useanimation") || lower.Equals("ua")) && index + 1 < num) {
targetItem.useAnimation = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("usetime") || lower.Equals("ut")) && index + 1 < num) {
targetItem.useTime = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("shoot") || lower.Equals("s")) && index + 1 < num) {
targetItem.shoot = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("shootspeed") || lower.Equals("ss")) && index + 1 < num) {
targetItem.shootSpeed = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("width") || lower.Equals("w")) && index + 1 < num) {
targetItem.width = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("height") || lower.Equals("h")) && index + 1 < num) {
targetItem.height = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("scale") || lower.Equals("sc")) && index + 1 < num) {
targetItem.scale = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("ammo") || lower.Equals("a")) && index + 1 < num) {
targetItem.ammo = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("useammo") || lower.Equals("uam")) && index + 1 < num) {
targetItem.useAmmo = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("notammo") || lower.Equals("na")) && index + 1 < num) {
targetItem.notAmmo = Boolean.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("prefix") || lower.Equals("p") && index + 1 < num)) {
targetItem.prefix = (byte)TShock.Utils.GetPrefixByIdOrName(args.Parameters[index + 1])[0];
} else if ((lower.Equals("stack") || lower.Equals("st") && index + 1 < num)) {
targetItem.stack = int.Parse(args.Parameters[index + 1]);
}
++index;
}
TSPlayer.All.SendData(PacketTypes.UpdateItemDrop, null, itemIndex);
TSPlayer.All.SendData(PacketTypes.ItemOwner, null, itemIndex);
TSPlayer.All.SendData(PacketTypes.TweakItem, null, itemIndex, 255, 63);
}
private void GiveCustomItem(CommandArgs args) {
List<string> parameters = args.Parameters;
int num = parameters.Count();
if (num == 0) {
args.Player.SendErrorMessage("Invalid Syntax. /givecustomitem <name> <id/itemname> <parameters> <#> ... \nParameters: hexcolor (hc), prefix (p), stack (st), damage (d), knockback (kb), useanimation (ua), " +
"usetime (ut), shoot (s), shootspeed (ss), width (w), height (h), scale (sc), ammo (a), useammo (uam), notammo (na).");
return;
}
List<TSPlayer> players = TSPlayer.FindByNameOrID(args.Parameters[0]);
if (players.Count != 1) {
args.Player.SendErrorMessage("Failed to find player of: " + args.Parameters[0]);
return;
}
if (num == 1) {
args.Player.SendErrorMessage("Failed to provide arguments to item.");
return;
}
List<Item> items = TShock.Utils.GetItemByIdOrName(args.Parameters[1]);
Item item = items[0];
TSPlayer player = new TSPlayer(players[0].Index);
int itemIndex = Item.NewItem((int)player.X, (int)player.Y, item.width, item.height, item.type, item.maxStack);
Item targetItem = Main.item[itemIndex];
targetItem.playerIndexTheItemIsReservedFor = player.Index;
for (int index = 2; index < num; ++index) {
string lower = parameters[index].ToLower();
if ((lower.Equals("hexcolor") || lower.Equals("hc")) && index + 1 < num) {
targetItem.color = new Color(int.Parse(args.Parameters[index + 1].Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(args.Parameters[index + 1].Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
int.Parse(args.Parameters[index + 1].Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
} else if ((lower.Equals("damage") || lower.Equals("d")) && index + 1 < num) {
targetItem.damage = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("knockback") || lower.Equals("kb")) && index + 1 < num) {
targetItem.knockBack = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("useanimation") || lower.Equals("ua")) && index + 1 < num) {
targetItem.useAnimation = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("usetime") || lower.Equals("ut")) && index + 1 < num) {
targetItem.useTime = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("shoot") || lower.Equals("s")) && index + 1 < num) {
targetItem.shoot = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("shootspeed") || lower.Equals("ss")) && index + 1 < num) {
targetItem.shootSpeed = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("width") || lower.Equals("w")) && index + 1 < num) {
targetItem.width = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("height") || lower.Equals("h")) && index + 1 < num) {
targetItem.height = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("scale") || lower.Equals("sc")) && index + 1 < num) {
targetItem.scale = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("ammo") || lower.Equals("a")) && index + 1 < num) {
targetItem.ammo = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("useammo") || lower.Equals("uam")) && index + 1 < num) {
targetItem.useAmmo = int.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("notammo") || lower.Equals("na")) && index + 1 < num) {
targetItem.notAmmo = Boolean.Parse(args.Parameters[index + 1]);
} else if ((lower.Equals("prefix") || lower.Equals("p") && index + 1 < num)) {
targetItem.prefix = (byte)TShock.Utils.GetPrefixByIdOrName(args.Parameters[index + 1])[0];
} else if ((lower.Equals("stack") || lower.Equals("st") && index + 1 < num)) {
targetItem.stack = int.Parse(args.Parameters[index + 1]);
}
++index;
}
TSPlayer.All.SendData(PacketTypes.UpdateItemDrop, null, itemIndex);
TSPlayer.All.SendData(PacketTypes.ItemOwner, null, itemIndex);
TSPlayer.All.SendData(PacketTypes.TweakItem, null, itemIndex, 255, 63);
}
}
}