Example #1
0
 public ChestOpenEventArgs(int x, int y, int playerid, NetItem[] items, string account, ChestFlags flags)
 {
     this.X = x;
     this.Y = y;
     this.Items = items;
     this.Account = account;
     this.Flags = flags;
     this.Who = playerid;
 }
Example #2
0
 public ChestItemEventArgs(int x, int y, int playerid, NetItem itemIn, NetItem itemOut, int slot, string account, ChestFlags flags)
 {
     this.X = x;
     this.Y = y;
     this.ItemPutIn = itemIn;
     this.ItemTakenOut = itemOut;
     this.Account = account;
     this.Flags = flags;
     this.Slot = slot;
     this.Who = playerid;
 }
Example #3
0
 public static string ToString(NetItem[] inventory)
 {
     StringBuilder items = new StringBuilder();
     for (int i = 0; i < maxNetInventory; i++)
     {
         items.Append(inventory[i].netID).Append(",");
         if (inventory[i].netID != 0)
             items.Append(inventory[i].stack).Append(",").Append(inventory[i].prefix).Append("~");
         else
             items.Append("0,0~");
     }
     return items.ToString(0, items.Length - 1);
 }
		public void SaveInventory(int UserID, string Name, NetItem[] Inventory, bool updateExisting = false)
		{
			if (!updateExisting)
			{
				Instance.Players[UserID].Inventories.Add(Name, Inventory);
				db.Query("INSERT INTO UnlimitedInventories (UserID, Name, Inventory) VALUES (@0, @1, @2);", UserID.ToString(), Name, string.Join("~", Inventory));
			}
			else
			{
				Instance.Players[UserID].Inventories[Name] = Inventory;
				db.Query("UPDATE UnlimitedInventories SET Inventory=@0 WHERE UserID=@1 AND Name=@2;", string.Join("~", Inventory), UserID.ToString(), Name);
			}
		}
		public void CreateInventory(string name)
		{
			int index;
			NetItem[] PlayerInventory = new NetItem[MaxInventory];

			TSPlayer player = FindTSPlayerByUserID(UserID);
			if (player != null)
			{
				Item[] inventory = player.TPlayer.inventory;
				Item[] armor = player.TPlayer.armor;
				Item[] dye = player.TPlayer.dye;
				Item[] miscEquips = player.TPlayer.miscEquips;
				Item[] miscDyes = player.TPlayer.miscDyes;

				for (int i = 0; i < MaxInventory; i++)
				{
					if (i < InventorySlots)
					{
						PlayerInventory[i] = (NetItem)inventory[i];
					}
					else if (i < InventorySlots + ArmorSlots)
					{
						index = i - InventorySlots;
						PlayerInventory[i] = (NetItem)armor[index];
					}
					else if (i < InventorySlots + ArmorSlots + DyeSlots)
					{
						index = i - (InventorySlots + ArmorSlots);
						PlayerInventory[i] = (NetItem)dye[index];
					}
					else if (i < InventorySlots + ArmorSlots + DyeSlots + MiscEquipSlots)
					{
						index = i - (InventorySlots + ArmorSlots + DyeSlots);
						PlayerInventory[i] = (NetItem)miscEquips[index];
					}
					else if (i < InventorySlots + ArmorSlots + DyeSlots + MiscEquipSlots + MiscDyeSlots)
					{
						index = i - (InventorySlots + ArmorSlots + DyeSlots + MiscEquipSlots);
						PlayerInventory[i] = (NetItem)miscDyes[index];
					}
				}

				Instance.Database.SaveInventory(UserID, name, PlayerInventory, HasInventory(name));
			}
		}
Example #6
0
 public static string ToString(NetItem[] inventory)
 {
     string inventoryString = "";
     for (int i = 0; i < NetItem.maxNetInventory; i++)
     {
         if (i != 0)
             inventoryString += "~";
         inventoryString += inventory[i].netID;
         if (inventory[i].netID != 0)
         {
             inventoryString += "," + inventory[i].stack;
             inventoryString += "," + inventory[i].prefix;
         }
         else
         {
             inventoryString += ",0,0";
         }
     }
     return inventoryString;
 }
Example #7
0
 public static NetItem[] Parse(string data)
 {
     NetItem[] inventory = new NetItem[NetItem.maxNetInventory];
     int i;
     for (i = 0; i < NetItem.maxNetInventory; i++)
     {
         inventory[i] = new NetItem();
     }
     string[] items = data.Split('~');
     i = 0;
     foreach (string item in items)
     {
         string[] idata = item.Split(',');
         inventory[i].netID = int.Parse(idata[0]);
         inventory[i].stack = int.Parse(idata[1]);
         inventory[i].prefix = int.Parse(idata[2]);
         i++;
     }
     return inventory;
 }
Example #8
0
        public static NetItem[] Parse(string data)
        {
            NetItem[] inventory = new NetItem[NetItem.maxNetInventory];
            int       i;

            for (i = 0; i < NetItem.maxNetInventory; i++)
            {
                inventory[i] = new NetItem();
            }
            string[] items = data.Split('~');
            i = 0;
            foreach (string item in items)
            {
                string[] idata = item.Split(',');
                inventory[i].netID  = int.Parse(idata[0]);
                inventory[i].stack  = int.Parse(idata[1]);
                inventory[i].prefix = int.Parse(idata[2]);
                i++;
            }
            return(inventory);
        }
Example #9
0
        void ModChest(int plr, byte slot, int ID, byte stack, byte prefix)
        {
            Chest chest = null;
            using (QueryResult reader = Database.QueryReader("SELECT Account, Flags, Items FROM Chests WHERE X = @0 AND Y = @1 AND WorldID = @2",
                Infos[plr].x, Infos[plr].y, Main.worldID))
            {
                if (reader.Read())
                {
                    chest = new Chest { flags = (ChestFlags)reader.Get<int>("Flags"), items = reader.Get<string>("Items"), account = reader.Get<string>("Account") };
                }
            }
            TSPlayer player = TShock.Players[plr];

            if (chest != null)
            {
                if ((chest.flags & ChestFlags.REFILL) != 0)
                {
                    if (!Timer.ContainsKey(new Point(Infos[plr].x, Infos[plr].y)))
                    {
                        Timer.Add(new Point(Infos[plr].x, Infos[plr].y), (int)chest.flags >> 3);
                    }
                }
                else
                {
                    int[] itemArgs = new int[60];
                    string[] split = chest.items.Split(',');
                    for (int i = 0; i < 60; i++)
                    {
                        itemArgs[i] = Convert.ToInt32(split[i]);
                    }
                    var itemIn = new NetItem { netID = ID, stack = stack, prefix = prefix };
                    var itemOut = new NetItem { netID = itemArgs[slot * 3], stack = itemArgs[slot * 3 + 1], prefix = itemArgs[slot * 3 + 2] };
                    if (itemIn.netID == itemOut.netID && itemIn.prefix == itemOut.prefix)
                    {
                        if (itemOut.stack >= itemIn.stack) // item beeing taken out
                        {
                            itemOut.stack -= itemIn.stack;
                            itemIn.netID = 0;
                            itemIn.prefix = 0;
                            itemIn.stack = 0;
                        }
                        else // item beeing put in
                        {
                            itemIn.stack -= itemOut.stack;
                            itemOut.netID = 0;
                            itemOut.prefix = 0;
                            itemOut.stack = 0;
                        }
                    }
                    ChestItemEventArgs args = new ChestItemEventArgs(Infos[plr].x, Infos[plr].y, plr, itemIn, itemOut, slot, chest.account, chest.flags);
                    if (ChestItem != null)
                        ChestItem(args);
                    if (args.Handled)
                    {
                        player.SendMessage("Another plugin is preventing the modification of items in this chest!", Color.Red);
                        byte[] raw2 = new byte[] { 11, 0, 0, 0, 33, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255 };
                        player.SendRawData(raw2);
                        return;
                    }
                    itemArgs[slot * 3] = ID;
                    itemArgs[slot * 3 + 1] = stack;
                    itemArgs[slot * 3 + 2] = prefix;
                    StringBuilder newItems = new StringBuilder();
                    for (int i = 0; i < 60; i++)
                    {
                        newItems.Append(itemArgs[i]);
                        if (i != 59)
                        {
                            newItems.Append(',');
                        }
                    }
                    Database.Query("UPDATE Chests SET Items = @0 WHERE X = @1 AND Y = @2 AND WorldID = @3",
                        newItems.ToString(), Infos[plr].x, Infos[plr].y, Main.worldID);

                    for (int i = 0; i < 256; i++)
                    {
                        if (Infos[i].x == Infos[plr].x && Infos[i].y == Infos[plr].y && i != plr)
                        {
                            byte[] raw = new byte[] { 8, 0, 0, 0, 32, 0, 0, slot, stack, prefix, (byte)ID, (byte)(ID >> 8) };
                            TShock.Players[i].SendRawData(raw);
                        }
                    }
                }
            }
        }
Example #10
0
        void GetChest(int X, int Y, int plr)
        {
            Chest chest = null;
            using (QueryResult reader = Database.QueryReader("SELECT Account, Flags, Items, Password FROM Chests WHERE X = @0 AND Y = @1 and WorldID = @2",
                X, Y, Main.worldID))
            {
                if (reader.Read())
                {
                    chest = new Chest
                    {
                        account = reader.Get<string>("Account"),
                        flags = (ChestFlags)reader.Get<int>("Flags"),
                        items = reader.Get<string>("Items"),
                        password = reader.Get<string>("Password")
                    };
                }
            }
            TSPlayer player = TShock.Players[plr];

            if (chest != null)
            {
                switch (Infos[plr].action)
                {
                    case ChestAction.INFO:
                        player.SendMessage(string.Format("X: {0} Y: {1} Account: {2} {3}Refill: {4} ({5} second{6}) Region: {7}",
                            X, Y, chest.account == "" ? "N/A" : chest.account, ((chest.flags & ChestFlags.PUBLIC) != 0) ? "(public) " : "",
                            (chest.flags & ChestFlags.REFILL) != 0, (int)chest.flags / 8, (int)chest.flags / 8 == 1 ? "" : "s",
                            (chest.flags & ChestFlags.REGION) != 0), Color.Yellow);
                        break;
                    case ChestAction.PROTECT:
                        if (chest.account != "")
                        {
                            player.SendMessage("This chest is already protected.", Color.Red);
                            break;
                        }
                        Database.Query("UPDATE Chests SET Account = @0 WHERE X = @1 AND Y = @2 AND WorldID = @3",
                            player.UserAccountName, X, Y, Main.worldID);
                        player.SendMessage("This chest is now protected.", Color.Green);
                        break;
                    case ChestAction.PUBLIC:
                        if (chest.account == "")
                        {
                            player.SendMessage("This chest is not protected.", Color.Red);
                            break;
                        }
                        if (chest.account != player.UserAccountName && !player.Group.HasPermission("infchests.admin.editall"))
                        {
                            player.SendMessage("This chest is not yours.", Color.Red);
                            break;
                        }
                        Database.Query("UPDATE Chests SET Flags = ((~(Flags & 1)) & (Flags | 1)) WHERE X = @0 AND Y = @1 AND WorldID = @2",
                            X, Y, Main.worldID);
                        player.SendMessage(string.Format("This chest is now {0}.",
                            (chest.flags & ChestFlags.PUBLIC) != 0 ? "private" : "public"), Color.Green);
                        break;
                    case ChestAction.REFILL:
                        if (chest.account != player.UserAccountName && chest.account != "" && !player.Group.HasPermission("infchests.admin.editall"))
                        {
                            player.SendMessage("This chest is not yours.", Color.Red);
                            break;
                        }
                        if (Infos[plr].time > 0)
                        {
                            Database.Query("UPDATE Chests SET Flags = @0 WHERE X = @1 AND Y = @2 AND WorldID = @3",
                                ((int)chest.flags & 3) + (Infos[plr].time * 8) + 4, X, Y, Main.worldID);
                            player.SendMessage(string.Format("This chest will now refill with a delay of {0} second{1}.", Infos[plr].time,
                                Infos[plr].time == 1 ? "" : "s"), Color.Green);
                        }
                        else
                        {
                            Database.Query("UPDATE Chests SET Flags = ((~(Flags & 4)) & (Flags | 4)) & 7 WHERE X = @0 AND Y = @1 AND WorldID = @2",
                                X, Y, Main.worldID);
                            player.SendMessage(string.Format("This chest will {0} refill.",
                                (chest.flags & ChestFlags.REFILL) != 0 ? "no longer" : "now"), Color.Green);
                        }
                        break;
                    case ChestAction.REGION:
                        if (chest.account == "")
                        {
                            player.SendMessage("This chest is not protected.", Color.Red);
                            break;
                        }
                        if (chest.account != player.UserAccountName && !player.Group.HasPermission("infchests.admin.editall"))
                        {
                            player.SendMessage("This chest is not yours.", Color.Red);
                            break;
                        }
                        Database.Query("UPDATE Chests SET Flags = ((~(Flags & 2)) & (Flags | 2)) WHERE X = @0 AND Y = @1 AND WorldID = @2",
                            X, Y, Main.worldID);
                        player.SendMessage(string.Format("This chest is {0} region shared.",
                            (chest.flags & ChestFlags.REGION) != 0 ? "no longer" : "now"), Color.Green);
                        break;
                    case ChestAction.SETPASS:
                        if (chest.account == "")
                        {
                            player.SendMessage("This chest is not protected.", Color.Red);
                            break;
                        }
                        if (chest.account != player.UserAccountName && !player.Group.HasPermission("infchests.admin.editall"))
                        {
                            player.SendMessage("This chest is not yours.", Color.Red);
                            break;
                        }
                        if (Infos[plr].password != "remove")
                        {
                            Database.Query("UPDATE Chests SET Password = @0 WHERE X = @1 AND Y = @2 AND WorldID = @3",
                                "", X, Y, Main.worldID);
                        }
                        else
                        {
                            Database.Query("UPDATE Chests SET Password = @0 WHERE X = @1 AND Y = @2 AND WorldID = @3",
                                TShock.Utils.HashPassword(Infos[plr].password), X, Y, Main.worldID);
                        }
                        player.SendMessage("This chest is now password protected.", Color.Green);
                        break;
                    case ChestAction.UNPROTECT:
                        if (chest.account == "")
                        {
                            player.SendMessage("This chest is not protected.", Color.Red);
                            break;
                        }
                        if (chest.account != player.UserAccountName && !player.Group.HasPermission("infchests.admin.editall"))
                        {
                            player.SendMessage("This chest is not yours.", Color.Red);
                            break;
                        }
                        Database.Query("UPDATE Chests SET Account = '' WHERE X = @0 AND Y = @1 AND WorldID = @2",
                            X, Y, Main.worldID);
                        player.SendMessage("This chest is now un-protected.", Color.Green);
                        break;
                    default:
                        bool isFree = chest.account == "";
                        bool isOwner = chest.account == player.UserAccountName || player.Group.HasPermission("infchests.admin.editall");
                        bool isPub = (chest.flags & ChestFlags.PUBLIC) == ChestFlags.PUBLIC;
                        bool isRegion = (chest.flags & ChestFlags.REGION) == ChestFlags.REGION && TShock.Regions.CanBuild(X, Y, player);
                        if (!isFree && !isOwner && !isPub && !isRegion)
                        {
                            if (String.IsNullOrEmpty(chest.password))
                            {
                                player.SendMessage("This chest is protected.", Color.Red);
                                break;
                            }
                            else if (TShock.Utils.HashPassword(Infos[plr].password) != chest.password)
                            {
                                player.SendMessage("This chest is password protected.", Color.Red);
                                break;
                            }
                            else
                            {
                                player.SendMessage("Chest unlocked.", Color.Green);
                                Infos[plr].password = "";
                            }
                        }
                        int timeLeft;
                        if (Timer.TryGetValue(new Point(X, Y), out timeLeft) && timeLeft > 0)
                        {
                            player.SendMessage(string.Format("This chest will refill in {0} second{1}.", timeLeft, timeLeft == 1 ? "" : "s"), Color.Red);
                            break;
                        }

                        int[] itemArgs = new int[60];
                        string[] split = chest.items.Split(',');
                        for (int i = 0; i < 60; i++)
                        {
                            itemArgs[i] = Convert.ToInt32(split[i]);
                        }
                        NetItem[] items = new NetItem[20];
                        for (int i = 0; i < 20; i++)
                        {
                            items[i] = new NetItem { netID = itemArgs[i * 3], stack = itemArgs[i * 3 + 1], prefix = itemArgs[i * 3 + 2] };
                        }
                        ChestOpenEventArgs args = new ChestOpenEventArgs(X, Y, plr, items, chest.account, chest.flags);
                        if (ChestOpen != null)
                            ChestOpen(args);
                        if (args.Handled)
                            return;

                        byte[] raw = new byte[] { 8, 0, 0, 0, 32, 0, 0, 255, 255, 255, 255, 255 };
                        for (int i = 0; i < 20; i++)
                        {
                            raw[7] = (byte)i;
                            raw[8] = (byte)itemArgs[i * 3 + 1];
                            raw[9] = (byte)itemArgs[i * 3 + 2];
                            raw[10] = (byte)itemArgs[i * 3];
                            raw[11] = (byte)(itemArgs[i * 3] >> 8);
                            player.SendRawData(raw);
                        }

                        byte[] raw2 = new byte[] { 11, 0, 0, 0, 33, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255 };
                        Buffer.BlockCopy(BitConverter.GetBytes(X), 0, raw2, 7, 4);
                        Buffer.BlockCopy(BitConverter.GetBytes(Y), 0, raw2, 11, 4);
                        player.SendRawData(raw2);
                        Infos[plr].x = X;
                        Infos[plr].y = Y;
                        break;
                }
                Infos[plr].action = ChestAction.NONE;
            }
        }
Example #11
0
 public static ItemData FromNetItem(NetItem netItem)
 {
     return new ItemData((ItemPrefix)netItem.PrefixId, (ItemType)netItem.NetId, netItem.Stack);
 }