Added InventorySlotAvailable to TSPlayer

Using TSPlayer.GiveItem from /item and /give
Fixed TSPlayer.GiveItem not setting stack size
This commit is contained in:
ricky 2011-06-17 16:17:28 +10:00
parent 4dc5d7f58e
commit bf49ae7b99
3 changed files with 107 additions and 83 deletions

View file

@ -627,32 +627,18 @@ namespace TShockAPI
return; return;
} }
NPC npc; var npcs = Tools.GetNPCByIdOrName(args.Parameters[0]);
int type = -1;
if (int.TryParse(args.Parameters[0], out type))
{
npc = Tools.GetNPCById(type);
}
else
{
var npcs = Tools.GetNPCByName(args.Parameters[0]);
if (npcs.Count == 0) if (npcs.Count == 0)
{ {
args.Player.SendMessage("Invalid mob type!", Color.Red); args.Player.SendMessage("Invalid mob type!", Color.Red);
return;
} }
else if (npcs.Count > 1) else if (npcs.Count > 1)
{ {
args.Player.SendMessage(string.Format("More than one ({0}) mob matched!", npcs.Count), Color.Red); args.Player.SendMessage(string.Format("More than one ({0}) mob matched!", npcs.Count), Color.Red);
return;
} }
else else
{ {
npc = npcs[0]; var npc = npcs[0];
type = npc.type;
}
}
if (npc.type >= 1 && npc.type < Main.maxNPCTypes) if (npc.type >= 1 && npc.type < Main.maxNPCTypes)
{ {
TSPlayer.Server.SpawnNPC(npc.type, npc.name, amount, (int)args.Player.TileX, (int)args.Player.TileY, 50, 20); TSPlayer.Server.SpawnNPC(npc.type, npc.name, amount, (int)args.Player.TileX, (int)args.Player.TileY, 50, 20);
@ -661,6 +647,7 @@ namespace TShockAPI
else else
args.Player.SendMessage("Invalid mob type!", Color.Red); args.Player.SendMessage("Invalid mob type!", Color.Red);
} }
}
#endregion Cause Events and Spawn Monsters Commands #endregion Cause Events and Spawn Monsters Commands
@ -1071,40 +1058,36 @@ namespace TShockAPI
return; return;
} }
int type = -1; var items = Tools.GetItemByIdOrName(args.Parameters[0]);
if (!int.TryParse(args.Parameters[0], out type)) if (items.Count == 0)
type = TShock.GetItemID(String.Join(" ", args.Parameters));
if (type < 1 || type >= Main.maxItemTypes)
{ {
args.Player.SendMessage("Invalid item type!", Color.Red); args.Player.SendMessage("Invalid item type!", Color.Red);
return;
} }
else if (items.Count > 1)
if (!args.Player.RealPlayer)
{ {
args.Player.SendMessage("You cant get items!"); args.Player.SendMessage(string.Format("More than one ({0}) item matched!", items.Count), Color.Red);
return;
} }
else
bool flag = false;
for (int i = 0; i < 40; i++)
{ {
if (!args.TPlayer.inventory[i].active) var item = items[0];
if (item.type >= 1 && item.type < Main.maxItemTypes)
{ {
int id = Terraria.Item.NewItem(0, 0, 0, 0, type, 1, true); if (args.Player.InventorySlotAvailable)
Main.item[id].position.X = args.Player.X; {
Main.item[id].position.Y = args.Player.Y; args.Player.GiveItem(item.type, item.name, item.width, item.height, item.maxStack);
Main.item[id].stack = Main.item[id].maxStack; args.Player.SendMessage(string.Format("Got some {0}.", item.name));
NetMessage.SendData(21, -1, -1, "", id, 0f, 0f, 0f);
args.Player.SendMessage(string.Format("Got some {0}.", Main.item[id].name));
flag = true;
break;
} }
} else
if (!flag) {
args.Player.SendMessage("You don't have free slots!", Color.Red); args.Player.SendMessage("You don't have free slots!", Color.Red);
} }
}
else
{
args.Player.SendMessage("Invalid item type!", Color.Red);
}
}
}
private static void Give(CommandArgs args) private static void Give(CommandArgs args)
{ {
@ -1124,16 +1107,20 @@ namespace TShockAPI
return; return;
} }
int type = -1; var items = Tools.GetItemByIdOrName(args.Parameters[0]);
if (!int.TryParse(args.Parameters[0], out type)) if (items.Count == 0)
type = TShock.GetItemID(args.Parameters[0]);
if (type < 1 || type >= Main.maxItemTypes)
{ {
args.Player.SendMessage("Invalid item type!", Color.Red); args.Player.SendMessage("Invalid item type!", Color.Red);
return;
} }
else if (items.Count > 1)
{
args.Player.SendMessage(string.Format("More than one ({0}) item matched!", items.Count), Color.Red);
}
else
{
var item = items[0];
if (item.type >= 1 && item.type < Main.maxItemTypes)
{
string plStr = args.Parameters[1]; string plStr = args.Parameters[1];
var players = Tools.FindPlayer(plStr); var players = Tools.FindPlayer(plStr);
if (players.Count == 0) if (players.Count == 0)
@ -1147,26 +1134,24 @@ namespace TShockAPI
else else
{ {
var plr = players[0]; var plr = players[0];
bool flag = false; if (plr.InventorySlotAvailable)
for (int i = 0; i < 40; i++)
{ {
if (!plr.TPlayer.inventory[i].active) plr.GiveItem(item.type, item.name, item.width, item.height, item.maxStack);
args.Player.SendMessage(string.Format("Gave {0} some {1}.", plr.Name, item.name));
plr.SendMessage(string.Format("{0} gave you some {1}.", args.Player.Name, item.name));
}
else
{ {
int id = Terraria.Item.NewItem(0, 0, 0, 0, type, 1, true);
Main.item[id].position.X = plr.X;
Main.item[id].position.Y = plr.Y;
Main.item[id].stack = Main.item[id].maxStack;
NetMessage.SendData(21, -1, -1, "", id, 0f, 0f, 0f);
args.Player.SendMessage(string.Format("Gave {0} some {1}.", plr.Name, Main.item[id].name));
plr.SendMessage(string.Format("{0} gave you some {1}.", args.Player.Name, Main.item[id].name));
flag = true;
break;
}
}
if (!flag)
args.Player.SendMessage("Player does not have free slots!", Color.Red); args.Player.SendMessage("Player does not have free slots!", Color.Red);
} }
} }
}
else
{
args.Player.SendMessage("Invalid item type!", Color.Red);
}
}
}
private static void Heal(CommandArgs args) private static void Heal(CommandArgs args)
{ {

View file

@ -88,6 +88,22 @@ namespace TShockAPI
{ {
get { return (int)(Y / 16); } get { return (int)(Y / 16); }
} }
public bool InventorySlotAvailable
{
get
{
bool flag = false;
for (int i = 0; i < 40; i++)
{
if (!TPlayer.inventory[i].active)
{
flag = true;
break;
}
}
return flag;
}
}
public TSPlayer(int index) public TSPlayer(int index)
{ {
@ -126,6 +142,9 @@ namespace TShockAPI
int itemid = Terraria.Item.NewItem((int)X, (int)Y, width, height, type, stack, true); int itemid = Terraria.Item.NewItem((int)X, (int)Y, width, height, type, stack, true);
// This is for special pickaxe/hammers/swords etc // This is for special pickaxe/hammers/swords etc
Main.item[itemid].SetDefaults(name); Main.item[itemid].SetDefaults(name);
// The set default overrides the wet and stack set by NewItem
Main.item[itemid].wet = Collision.WetCollision(Main.item[itemid].position, Main.item[itemid].width, Main.item[itemid].height);
Main.item[itemid].stack = stack;
Main.item[itemid].owner = Index; Main.item[itemid].owner = Index;
NetMessage.SendData((int)PacketTypes.ItemDrop, -1, -1, "", itemid, 0f, 0f, 0f); NetMessage.SendData((int)PacketTypes.ItemDrop, -1, -1, "", itemid, 0f, 0f, 0f);
NetMessage.SendData((int)PacketTypes.ItemOwner, -1, -1, "", itemid, 0f, 0f, 0f); NetMessage.SendData((int)PacketTypes.ItemOwner, -1, -1, "", itemid, 0f, 0f, 0f);

View file

@ -173,7 +173,7 @@ namespace TShockAPI
tileY = startTileY + random.Next(tileYRange * -1, tileYRange); tileY = startTileY + random.Next(tileYRange * -1, tileYRange);
j++; j++;
} }
while (TileValid(tileX, tileY) && !Tools.TileClear(tileX, tileY)); while (TileValid(tileX, tileY) && !TileClear(tileX, tileY));
} }
private static bool TileValid(int tileX, int tileY) private static bool TileValid(int tileX, int tileY)
@ -186,6 +186,16 @@ namespace TShockAPI
return !Main.tile[tileX, tileY].active; return !Main.tile[tileX, tileY].active;
} }
public static List<Item> GetItemByIdOrName(string idOrName)
{
int type = -1;
if (int.TryParse(idOrName, out type))
{
return new List<Item> { GetItemById(type) };
}
return GetItemByName(idOrName);
}
public static Item GetItemById(int id) public static Item GetItemById(int id)
{ {
Item item = new Item(); Item item = new Item();
@ -215,6 +225,16 @@ namespace TShockAPI
return found; return found;
} }
public static List<NPC> GetNPCByIdOrName(string idOrName)
{
int type = -1;
if (int.TryParse(idOrName, out type))
{
return new List<NPC> { GetNPCById(type) };
}
return GetNPCByName(idOrName);
}
public static NPC GetNPCById(int id) public static NPC GetNPCById(int id)
{ {
NPC npc = new NPC(); NPC npc = new NPC();
@ -255,7 +275,7 @@ namespace TShockAPI
{ {
if (player != null && player.Active) if (player != null && player.Active)
{ {
Tools.ForceKick(player, reason); ForceKick(player, reason);
} }
} }
} }
@ -290,7 +310,7 @@ namespace TShockAPI
if (adminUserName.Length == 0) if (adminUserName.Length == 0)
Broadcast(string.Format("{0} was kicked for {1}", playerName, reason.ToLower())); Broadcast(string.Format("{0} was kicked for {1}", playerName, reason.ToLower()));
else else
Tools.Broadcast(string.Format("{0} kicked {1} for {2}", adminUserName, playerName, reason.ToLower())); Broadcast(string.Format("{0} kicked {1} for {2}", adminUserName, playerName, reason.ToLower()));
return true; return true;
} }
return false; return false;
@ -315,7 +335,7 @@ namespace TShockAPI
if (adminUserName.Length == 0) if (adminUserName.Length == 0)
Broadcast(string.Format("{0} was banned for {1}", playerName, reason.ToLower())); Broadcast(string.Format("{0} was banned for {1}", playerName, reason.ToLower()));
else else
Tools.Broadcast(string.Format("{0} banned {1} for {2}", adminUserName, playerName, reason.ToLower())); Broadcast(string.Format("{0} banned {1} for {2}", adminUserName, playerName, reason.ToLower()));
return true; return true;
} }
return false; return false;