public static InfChest GetChest(int x, int y) { string query = $"SELECT * FROM InfChests3 WHERE X = {x} AND Y = {y} AND WorldID = {Main.worldID};"; using (var reader = db.QueryReader(query)) { if (reader.Read()) { InfChest chest = new InfChest(reader.Get <int>("UserID"), x, y, Main.worldID) { id = reader.Get <int>("ID"), isPublic = reader.Get <int>("Public") == 1 ? true : false, refill = reader.Get <int>("Refill") }; chest.StringToGroups(reader.Get <string>("Groups")); chest.StringToUsers(reader.Get <string>("Users")); chest.StringToItems(reader.Get <string>("Items")); return(chest); } else { return(null); } } }
public static List <Chest> GetAllChests() { string query = "SELECT * FROM InfChests3 WHERE WorldID = " + Main.worldID + ";"; using (var reader = db.QueryReader(query)) { List <Chest> chests = new List <Chest>(); while (reader.Read()) { InfChest ichest = new InfChest(-1, reader.Get <int>("X"), reader.Get <int>("Y"), Main.worldID); ichest.StringToItems(reader.Get <string>("Items")); Chest chest = new Chest() { item = ichest.items, x = ichest.x, y = ichest.y }; chests.Add(chest); } return(chests); } }
private int InnerConvChests() { int count = 0; for (int i = 0; i < Main.chest.Length; i++) { if (Main.chest[i] != null) { InfChest chest = new InfChest(-1, Main.chest[i].x, Main.chest[i].y, Main.worldID) { groups = new List <string>(), isPublic = false, items = Main.chest[i].item, refill = -1, users = new List <int>() }; DB.AddChest(chest); Main.chest[i] = null; count++; } } TShock.Utils.SaveWorld(); return(count); }
private void OnGetData(GetDataEventArgs args) { if (args.Handled) { return; } if (!usingInfChests) { return; } int index = args.Msg.whoAmI; using (var reader = new BinaryReader(new MemoryStream(args.Msg.readBuffer, args.Index, args.Length))) { switch (args.MsgID) { case PacketTypes.ChestGetContents: if (lockChests) { TShock.Players[args.Msg.whoAmI].SendWarningMessage("Chests are currently being converted. Please wait for a few moments."); return; } var tilex = reader.ReadInt16(); var tiley = reader.ReadInt16(); #if DEBUG File.AppendAllText("debug.txt", $"[IN] 31 ChestGetContents: Tile X = {tilex} | Tile Y = {tiley}\n"); #endif args.Handled = true; #region GetChest InfChest gchest = DB.GetChest(tilex, tiley); TSPlayer gplayer = TShock.Players[index]; if (gchest == null) { gplayer.SendErrorMessage("This chest is corrupted."); WorldGen.KillTile(tilex, tiley); TSPlayer.All.SendData(PacketTypes.Tile, "", 0, tilex, tiley + 1); return; } PlayerInfo info = gplayer.GetData <PlayerInfo>(PIString); switch (info.Action) { case ChestAction.GetInfo: gplayer.SendInfoMessage($"X: {gchest.x} | Y: {gchest.y}"); string owner = gchest.userid == -1 ? "(None)" : TShock.Users.GetUserByID(gchest.userid) == null ? "(Deleted User)" : TShock.Users.GetUserByID(gchest.userid).Name; string ispublic = gchest.isPublic ? " (Public)" : ""; string isrefill = gchest.refill > -1 ? $" (Refill: {gchest.refill})" : ""; gplayer.SendInfoMessage($"Chest Owner: {owner}{ispublic}{isrefill}"); if (gchest.groups.Count > 0 && !string.IsNullOrWhiteSpace(gchest.groups[0])) { string tinfo = string.Join(", ", gchest.groups); gplayer.SendInfoMessage($"Groups Allowed: {tinfo}"); } else { gplayer.SendInfoMessage("Groups Allowed: (None)"); } if (gchest.users.Count > 0) { string tinfo = string.Join(", ", gchest.users.Select(p => TShock.Users.GetUserByID(p) == null ? "(Deleted User)" : TShock.Users.GetUserByID(p).Name)); gplayer.SendInfoMessage($"Users Allowed: {tinfo}"); } else { gplayer.SendInfoMessage("Users Allowed: (None)"); } break; case ChestAction.Protect: if (gchest.userid == gplayer.User.ID) { gplayer.SendErrorMessage("This chest is already claimed by you!"); } else if (gchest.userid != -1 && !gplayer.HasPermission("ic.edit")) { gplayer.SendErrorMessage("This chest is already claimed by someone else!"); } else { gchest.userid = gplayer.User.ID; DB.UpdateUser(gchest); gplayer.SendSuccessMessage("This chest is now claimed by you!"); } break; case ChestAction.Unprotect: if (gchest.userid != gplayer.User.ID && !gplayer.HasPermission("ic.edit")) { gplayer.SendErrorMessage("This chest is not yours!"); } else if (gchest.userid == -1) { gplayer.SendErrorMessage("This chest is not claimed!"); } else { gchest.userid = -1; DB.UpdateUser(gchest); gplayer.SendSuccessMessage("This chest is no longer claimed."); } break; case ChestAction.SetGroup: if (gchest.userid != gplayer.User.ID && !gplayer.HasPermission("ic.edit")) { gplayer.SendErrorMessage("This chest is not yours!"); } else if (gchest.userid == -1) { gplayer.SendErrorMessage("This chest is not claimed!"); } else { if (gchest.groups.Contains(info.ExtraInfo)) { gchest.groups.Remove(info.ExtraInfo); gplayer.SendSuccessMessage($"Successfully removed group access from chest."); DB.UpdateGroups(gchest); } else { gchest.groups.Add(info.ExtraInfo); gplayer.SendSuccessMessage($"Successfully added group access to chest."); DB.UpdateGroups(gchest); } } break; case ChestAction.SetRefill: if (gchest.userid != gplayer.User.ID && !gplayer.HasPermission("ic.edit")) { gplayer.SendErrorMessage("This chest is not yours!"); } else if (gchest.userid == -1) { gplayer.SendErrorMessage("This chest is not claimed!"); } else { int refilltime = int.Parse(info.ExtraInfo); gchest.refill = refilltime; DB.UpdateRefill(gchest); gplayer.SendSuccessMessage("Successfull set refill time to " + (refilltime == -1 ? "(none)." : refilltime.ToString() + ".")); } break; case ChestAction.SetUser: if (gchest.userid != gplayer.User.ID && !gplayer.HasPermission("ic.edit")) { gplayer.SendErrorMessage("This chest is not yours!"); } else if (gchest.userid == -1) { gplayer.SendErrorMessage("This chest is not claimed!"); } else { int userid = int.Parse(info.ExtraInfo); if (gchest.users.Contains(userid)) { gchest.users.Remove(userid); DB.UpdateUsers(gchest); gplayer.SendSuccessMessage("Successfully removed user access from chest."); } else { gchest.users.Add(userid); DB.UpdateUsers(gchest); gplayer.SendSuccessMessage("Successfully added user access to chest."); } } break; case ChestAction.TogglePublic: if (gchest.userid != gplayer.User.ID && !gplayer.HasPermission("ic.edit")) { gplayer.SendErrorMessage("This chest is not yours!"); } else if (gchest.userid == -1) { gplayer.SendErrorMessage("This chest is not claimed!"); } else { if (gchest.isPublic) { gchest.isPublic = false; DB.UpdatePublic(gchest); gplayer.SendSuccessMessage("Successfully set chest as private."); } else { gchest.isPublic = true; DB.UpdatePublic(gchest); gplayer.SendSuccessMessage("Successfully set chest as public."); } } break; case ChestAction.None: //check for perms if (!gplayer.HasPermission("ic.edit") && ((TShock.Config.RegionProtectChests && !TShock.Regions.CanBuild(gchest.x, gchest.y, gplayer)) || (gchest.userid != -1 && !gchest.isPublic && !gchest.groups.Contains(gplayer.Group.Name) && (!gplayer.IsLoggedIn || (gchest.userid != gplayer.User.ID && !gchest.users.Contains(gplayer.User.ID)))))) { gplayer.SendErrorMessage("This chest is protected."); break; } info.ChestIdInUse = gchest.id; Item[] items; RefillChestInfo rcinfo = GetRCInfo(!gplayer.IsLoggedIn ? -1 : gplayer.User.ID, gchest.id); //use refill items if exists, or create new refill entry, or use items directly if (gchest.isRefill && rcinfo != null && (DateTime.Now - rcinfo.TimeOpened).TotalSeconds < gchest.refill) { items = rcinfo.CurrentItems; } else if (gchest.isRefill) { if (rcinfo != null) { DeleteOldRCInfo(!gplayer.IsLoggedIn ? -1 : gplayer.User.ID, gchest.id); } RefillChestInfo newrcinfo = new RefillChestInfo() { ChestID = gchest.id, CurrentItems = gchest.items, PlayerID = !gplayer.IsLoggedIn ? -1 : gplayer.User.ID, TimeOpened = DateTime.Now }; RCInfos.Add(newrcinfo); items = newrcinfo.CurrentItems; } else { items = gchest.items; } int tempchest = GetNextChestId(); Main.chest[tempchest] = new Chest() { item = items, x = gchest.x, y = gchest.y }; for (int i = 0; i < 40; i++) { gplayer.SendData(PacketTypes.ChestItem, "", tempchest, i, gchest.items[i].stack, gchest.items[i].prefix, gchest.items[i].netID); } gplayer.SendData(PacketTypes.ChestOpen, "", tempchest, gchest.x, gchest.y); NetMessage.SendData((int)PacketTypes.SyncPlayerChestIndex, -1, index, NetworkText.Empty, index, tempchest); Main.chest[tempchest] = null; break; } info.Action = ChestAction.None; info.ExtraInfo = ""; gplayer.SetData(PIString, info); #endregion break; case PacketTypes.ChestItem: if (lockChests) { TShock.Players[args.Msg.whoAmI].SendWarningMessage("Chests are currently being converted. Please wait for a few moments."); return; } var chestid = reader.ReadInt16(); var itemslot = reader.ReadByte(); var stack = reader.ReadInt16(); var prefix = reader.ReadByte(); var netid = reader.ReadInt16(); #if DEBUG if (itemslot == 0 || itemslot == 39) { File.AppendAllText("debug.txt", $"[IN] 32 ChestItem: Chest ID = {chestid} | Item Slot = {itemslot} | Stack = {stack} | Prefix = {prefix} | Net ID = {netid}\n"); } #endif TSPlayer ciplayer = TShock.Players[index]; PlayerInfo piinfo = ciplayer.GetData <PlayerInfo>(PIString); if (piinfo.ChestIdInUse == -1) { return; } InfChest cichest = DB.GetChest(piinfo.ChestIdInUse); RefillChestInfo circinfo = GetRCInfo(!TShock.Players[index].IsLoggedIn ? -1 : TShock.Players[index].User.ID, cichest.id); if (cichest == null) { ciplayer.SendWarningMessage("This chest is corrupted. Please remove it."); return; } Item item = new Item(); item.SetDefaults(netid); item.stack = stack; item.prefix = prefix; if (ciplayer.HasPermission(Permissions.spawnmob) && Main.hardMode && (item.netID == 3092 && ciplayer.TPlayer.ZoneHoly) || (item.netID == 3091 && (ciplayer.TPlayer.ZoneCrimson || ciplayer.TPlayer.ZoneCorrupt))) { bool empty = true; foreach (var initem in cichest.items) { if (initem?.netID != 0) { empty = false; } } if (empty) { //kick player out of chest, kill chest, spawn appropriate mimic piinfo.ChestIdInUse = -1; ciplayer.SetData(PIString, piinfo); NetMessage.SendData((int)PacketTypes.SyncPlayerChestIndex, -1, index, NetworkText.Empty, index, -1); DB.DeleteChest(cichest.id); WorldGen.KillTile(cichest.x, cichest.y, noItem: true); NetMessage.SendTileSquare(ciplayer.Index, cichest.x, cichest.y, 3); int type; if (netid == 3092) { type = 475; } else if (netid == 3091 && ciplayer.TPlayer.ZoneCrimson) { type = 474; } else //if (netid == 3091 && ciplayer.TPlayer.ZoneCorrupt) { type = 473; } var npc = TShock.Utils.GetNPCById(type); TSPlayer.Server.SpawnNPC(npc.type, npc.FullName, 1, ciplayer.TileX, ciplayer.TileY, 10, 10); } } if (cichest.isRefill) { circinfo.CurrentItems[itemslot] = item; } else { cichest.items[itemslot] = item; DB.UpdateItems(cichest); } break; case PacketTypes.ChestOpen: chestid = reader.ReadInt16(); var chestx = reader.ReadInt16(); var chesty = reader.ReadInt16(); var namelength = reader.ReadByte(); string chestname = null; if (namelength > 0) { chestname = reader.ReadString(); return; } #if DEBUG File.AppendAllText("debug.txt", $"[IN] 33 ChestName: Chest ID = {chestid} | Chest X = {chestx} | Chest Y = {chesty} | Name Length = {namelength} | Chest Name = {chestname}\n"); #endif if (chestid == -1) { PlayerInfo coinfo = TShock.Players[index].GetData <PlayerInfo>(PIString); coinfo.ChestIdInUse = -1; TShock.Players[index].SetData(PIString, coinfo); NetMessage.SendData((int)PacketTypes.SyncPlayerChestIndex, -1, index, NetworkText.Empty, index, -1); } break; case PacketTypes.TileKill: if (lockChests) { TShock.Players[args.Msg.whoAmI].SendWarningMessage("Chests are currently being converted. Please wait for a few moments."); return; } args.Handled = true; var action = reader.ReadByte(); //0 placec 1 killc 2 placed 3 killd 4 placegc tilex = reader.ReadInt16(); tiley = reader.ReadInt16(); var style = reader.ReadInt16(); //21 chest //88 dresser //467 golden/crystal chest int chesttype; if (action == 0 || action == 1) { chesttype = 21; } else if (action == 2 || action == 3) { chesttype = 88; } else if (action == 4 || action == 5) { chesttype = 467; } else { throw new Exception(); } if (action == 0 || action == 2 || action == 4) { if (TShock.Regions.CanBuild(tilex, tiley, TShock.Players[index])) { Task.Factory.StartNew(() => { if (action == 2) { tilex--; } InfChest newChest = new InfChest(TShock.Players[index].HasPermission("ic.protect") ? TShock.Players[index].User.ID : -1, tilex, tiley - 1, Main.worldID); DB.AddChest(newChest); if (action == 2) { tilex++; } }); WorldGen.PlaceChest(tilex, tiley, (ushort)(chesttype), false, style); Main.chest[0] = null; NetMessage.SendData((int)PacketTypes.TileKill, -1, -1, NetworkText.Empty, action, tilex, tiley, style); } } else { if (Main.tile[tilex, tiley].type != 21 && Main.tile[tilex, tiley].type != 88 && Main.tile[tilex, tiley].type != 467) { return; } if (TShock.Regions.CanBuild(tilex, tiley, TShock.Players[index])) { if (Main.tile[tilex, tiley].frameY % 36 != 0) { tiley--; } if (Main.tile[tilex, tiley].frameX % 36 != 0) { tilex--; } #region Kill Chest Task.Factory.StartNew(() => { InfChest chest = DB.GetChest(tilex, tiley); TSPlayer player = TShock.Players[index]; //If chest exists in map but not db, something went wrong if (chest == null) { player.SendWarningMessage("This chest is corrupted."); WorldGen.KillTile(tilex, tiley); TSPlayer.All.SendData(PacketTypes.Tile, "", 0, tilex, tiley + 1); } //check for perms - chest owner, claim, edit perm else if (chest.userid != player.User.ID && chest.userid != -1 && !player.HasPermission("ic.edit")) { player.SendErrorMessage("This chest is protected."); player.SendTileSquare(tilex, tiley, 3); } //check for empty chest else if (!chest.isEmpty) { player.SendTileSquare(tilex, tiley, 3); } else { WorldGen.KillTile(tilex, tiley); DB.DeleteChest(chest.id); TSPlayer.All.SendData(PacketTypes.Tile, "", 0, tilex, tiley + 1); } }); #endregion } } #if DEBUG File.AppendAllText("debug.txt", $"[IN] 34 PlaceChest: Action = {action} | Tile X = {tilex} | Tile Y = {tiley} | Style = {style}\n"); #endif break; case PacketTypes.ChestName: chestid = reader.ReadInt16(); chestx = reader.ReadInt16(); chesty = reader.ReadInt16(); #if DEBUG File.AppendAllText("debug.txt", $"[IN] 69 GetChestName: Chest ID = {chestid} | Chest X = {chestx} | Chest Y = {chesty}\n"); #endif break; } } }
public static int TransferV2() { InfMain.lockChests = true; List <InfChest> chests = new List <InfChest>(); if (TShock.Config.StorageType == "sqlite") { // We have to open a new db connection, as these tables are in different .sqlite files try { using (var v2conn = new SqliteConnection($"uri=file://{Path.Combine(TShock.SavePath, "chests.sqlite")},Version=3")) { string query = $"SELECT * FROM InfChests WHERE WorldID = {Main.worldID};"; using (var reader = v2conn.QueryReader(query)) { while (reader.Read()) { int id = reader.Get <int>("ID"); int userid = reader.Get <int>("UserID"); int x = reader.Get <int>("X"); int y = reader.Get <int>("Y"); bool ispublic = reader.Get <int>("Public") == 1 ? true : false; List <int> users = string.IsNullOrEmpty(reader.Get <string>("Users")) ? new List <int>() : reader.Get <string>("Users").Split(',').ToList().ConvertAll(p => int.Parse(p)); List <string> groups = string.IsNullOrEmpty(reader.Get <string>("Groups")) ? new List <string>() : reader.Get <string>("Groups").Split(',').ToList(); int refill = reader.Get <int>("Refill"); InfChest chest = new InfChest(userid, x, y, Main.worldID) { id = id, groups = groups, isPublic = ispublic, items = new Item[40], refill = refill, users = users }; chests.Add(chest); } } foreach (InfChest chest in chests) { query = $"SELECT * FROM ChestItems WHERE ChestID = {chest.id};"; using (var reader = v2conn.QueryReader(query)) { while (reader.Read()) { int slot = reader.Get <int>("Slot"); int type = reader.Get <int>("Type"); int stack = reader.Get <int>("Stack"); int prefix = reader.Get <int>("Prefix"); Item item = new Item(); item.SetDefaults(type); item.stack = stack; item.prefix = (byte)prefix; chest.items[slot] = item; } } AddChest(chest); } } } catch (Exception ex) { TShock.Log.ConsoleError(ex.ToString()); } } else { string query = $"SELECT * FROM InfChests WHERE WorldID = {Main.worldID};"; using (var reader = db.QueryReader(query)) { while (reader.Read()) { int id = reader.Get <int>("ID"); int userid = reader.Get <int>("UserID"); int x = reader.Get <int>("X"); int y = reader.Get <int>("Y"); bool ispublic = reader.Get <int>("Public") == 1 ? true : false; List <int> users = string.IsNullOrEmpty(reader.Get <string>("Users")) ? new List <int>() : reader.Get <string>("Users").Split(',').ToList().ConvertAll(p => int.Parse(p)); List <string> groups = string.IsNullOrEmpty(reader.Get <string>("Groups")) ? new List <string>() : reader.Get <string>("Groups").Split(',').ToList(); int refill = reader.Get <int>("Refill"); InfChest chest = new InfChest(userid, x, y, Main.worldID) { id = id, groups = groups, isPublic = ispublic, items = new Item[40], refill = refill, users = users }; chests.Add(chest); } } foreach (InfChest chest in chests) { query = $"SELECT * FROM ChestItems WHERE ChestID = {chest.id};"; using (var reader = db.QueryReader(query)) { while (reader.Read()) { int slot = reader.Get <int>("Slot"); int type = reader.Get <int>("Type"); int stack = reader.Get <int>("Stack"); int prefix = reader.Get <int>("Prefix"); Item item = new Item(); item.SetDefaults(type); item.stack = stack; item.prefix = (byte)prefix; chest.items[slot] = item; } } AddChest(chest); } } InfMain.lockChests = false; return(chests.Count); }
public static int TransferV1() { InfMain.lockChests = true; List <InfChest> chests = new List <InfChest>(); if (TShock.Config.StorageType == "sqlite") { // We have to open a new db connection, as these tables are in different .sqlite files try { using (var v1conn = new SqliteConnection($"uri=file://{Path.Combine(TShock.SavePath, "chests.sqlite")},Version=3")) { string query = $"SELECT * FROM Chests WHERE WorldID = {Main.worldID};"; using (var reader = v1conn.QueryReader(query)) { while (reader.Read()) { int x = reader.Get <int>("X"); int y = reader.Get <int>("Y"); string account = reader.Get <string>("Account"); string rawitems = reader.Get <string>("Items"); int refill = reader.Get <int>("RefillTime"); UserAccount user = TShock.UserAccounts.GetUserAccountByName(account); InfChest chest = new InfChest(user == null ? -1 : user.ID, x, y, Main.worldID); if (refill > -1) { chest.refill = refill; } string[] items = rawitems.Split(','); for (int i = 0; i < 40; i++) { Item item = new Item(); item.SetDefaults(int.Parse(items[3 * i])); item.stack = int.Parse(items[3 * i + 1]); item.prefix = byte.Parse(items[3 * i + 2]); chest.items[i] = item; } chests.Add(chest); } } } } catch (Exception ex) { TShock.Log.ConsoleError(ex.ToString()); } } else { //Probably could just have this code once and switch db conn as needed, but #lazy string query = $"SELECT * FROM Chests WHERE WorldID = {Main.worldID};"; using (var reader = db.QueryReader(query)) { while (reader.Read()) { int x = reader.Get <int>("X"); int y = reader.Get <int>("Y"); string account = reader.Get <string>("Account"); string rawitems = reader.Get <string>("Items"); int refill = reader.Get <int>("RefillTime"); UserAccount user = TShock.UserAccounts.GetUserAccountByName(account); InfChest chest = new InfChest(user == null ? -1 : user.ID, x, y, Main.worldID); if (refill > -1) { chest.refill = refill; } string[] items = rawitems.Split(','); for (int i = 0; i < 40; i++) { Item item = new Item(); item.SetDefaults(int.Parse(items[3 * i])); item.stack = int.Parse(items[3 * i + 1]); item.prefix = byte.Parse(items[3 * i + 2]); chest.items[i] = item; } chests.Add(chest); } } } foreach (InfChest chest in chests) { AddChest(chest); } InfMain.lockChests = false; return(chests.Count); }
public static void UpdateRefill(InfChest chest) { string query = $"UPDATE InfChests3 SET Refill = {chest.refill} WHERE ID = {chest.id};"; db.Query(query); }
public static void UpdateGroups(InfChest chest) { string query = $"UPDATE InfChests3 SET Groups = '{chest.GroupsToString()}' WHERE ID = {chest.id};"; db.Query(query); }
public static void UpdatePublic(InfChest chest) { string query = $"UPDATE InfChests3 SET Public = {(chest.isPublic ? 1 : 0)} WHERE ID = {chest.id};"; db.Query(query); }
public static void UpdateUser(InfChest chest) { string query = $"UPDATE InfChests3 SET UserID = {chest.userid} WHERE ID = {chest.id};"; db.Query(query); }
public static void AddChest(InfChest chest) { string query = $"INSERT INTO InfChests3 (UserID, X, Y, Items, Public, Users, Groups, Refill, WorldID) VALUES ({chest.userid}, {chest.x}, {chest.y}, '{chest.ItemsToString()}', {(chest.isPublic ? 1 : 0)}, '{chest.UsersToString()}', '{chest.GroupsToString()}', {chest.refill}, {Main.worldID});"; db.Query(query); }