public static bool PlaceIceChest(int x, int y, ushort type = 21, bool notNearOtherChests = false, int style = 0) { int num = -1; TileObject toBePlaced; if (TileObject.CanPlace(x, y, type, style, 1, out toBePlaced, false)) { bool flag = true; if (notNearOtherChests && Chest.NearOtherChests(x - 1, y - 1)) { flag = false; } if (flag) { TileObject.Place(toBePlaced); num = Chest.CreateChest(toBePlaced.xCoord, toBePlaced.yCoord, -1); } } else { num = -1; } // if (num != -1 && Main.netMode == 1) // { // NetMessage.SendData(34, -1, -1, "", 0, (float)x, (float)y, (float)style, 0, 0, 0); // } return(true); }
public static bool PlaceChest(int x, int y, ushort type = 21, bool notNearOtherChests = false, int style = 0) { bool b = false; TileObject toBePlaced; if (TileObject.CanPlace(x, y, type, style, 1, out toBePlaced, false)) { bool flag = !(notNearOtherChests && Chest.NearOtherChests(x - 1, y - 1)); if (flag) { b = TileObject.Place(toBePlaced); if (b) { Chest.CreateChest(toBePlaced.xCoord, toBePlaced.yCoord, -1); } } } return(b); /* * var chest_num = WorldGen.PlaceChest(x, y); * * if (chest_num != -1) * { * Item[] chest = Main.chest[chest_num].item; * * int itemsToPlace = Main.rand.Next(1, 4); * for (int i = 0; i < itemsToPlace; i++) * { * if (Main.rand.Next(0, 5) == 0) * { * //God Escence * chest[i].SetDefaults(ModContent.ItemType<cameraObscura>()); * } * else if (Main.rand.Next(0, 2) == 0) * { * //Broken hero's sword * chest[i].SetDefaults(ItemID.BrokenHeroSword); * } * else * { * //Broken hero's sword * chest[i].SetDefaults(ItemID.SwordStatue); * } * } * Main.chest[chest_num].item = chest; * } */ }
public static Chest FindAndCreateChest(Rectangle searchRange, int tileType) { var r = searchRange; for (int i = 0; i < r.Width; i++) { for (int j = 0; j < r.Height; j++) { if (Main.tile[r.X + i, r.Y + j].type == tileType) { return(Main.chest[Chest.CreateChest(r.X + i, r.Y + j)]); } } } return(null); }
public override void ExecuteLoadPhase(DimensionEntity <Dimension> entity) { var locationToLoad = entity.Location; var dimension = entity.Dimension; for (var i = 0; i < dimension.Chests.Length; i++) { var chest = dimension.Chests[i].CloneObject(); chest.x += locationToLoad.X; chest.y += locationToLoad.Y; var chestIndex = Chest.CreateChest(chest.x, chest.y, -1); Main.chest[chestIndex] = chest; } Recipe.FindRecipes(); }
internal static void LoadChests(IList <TagCompound> list) { foreach (var tag in list) { int x = tag.GetInt("x"); int y = tag.GetInt("y"); int chest = Chest.FindChest(x, y); if (chest < 0) { chest = Chest.CreateChest(x, y); } if (chest >= 0) { PlayerIO.LoadInventory(Main.chest[chest].item, tag.GetList <TagCompound>("items")); } } }
public static void GenerateChest(Point16 pos, TagCompound rules) { int i = Chest.CreateChest(pos.X, pos.Y); if (i == -1) { return; } Item item = new Item(); item.SetDefaults(1); Chest chest = Main.chest[i]; ChestEntity.SetChest(chest, ChestEntity.LoadChestRules(rules)); }
internal static void ReadChest(BinaryReader reader) { int x = reader.ReadInt32(); int y = reader.ReadInt32(); int chest = Chest.FindChest(x, y); if (chest < 0) { chest = Chest.CreateChest(x, y); } if (chest >= 0) { PlayerIO.ReadInventory(Main.chest[chest].item, reader, true); } else { PlayerIO.ReadInventory(new Item[40], reader, true); } }
private static void LoadLegacyChest(BinaryReader reader) { int x = reader.ReadInt32(); int y = reader.ReadInt32(); int chest = Chest.FindChest(x, y); if (chest < 0) { chest = Chest.CreateChest(x, y); } if (chest >= 0) { ItemIO.LoadLegacyInventory(Main.chest[chest].item, reader, true); } else { ItemIO.LoadLegacyInventory(new Item[40], reader, true); } }
// F*****g hell. // Manually syncs the placement of a new Universal Crafter because Terraria thinks its 2x2 and frames it weirdly. private void WorldGen_PlaceChestDirect(On.Terraria.WorldGen.orig_PlaceChestDirect orig, int x, int y, ushort type, int style, int id) { if (type != Type) { orig(x, y, type, style, id); return; } Chest.CreateChest(x, y - 2, id); for (int i = -2; i < 3; i++) { for (int j = -2; j < 1; j++) { Tile tile = Framing.GetTileSafely(x + i, y + j); // Null-checks and sets the tile to a new one. tile.active(active: true); tile.frameX = (short)(18 * (i + 2)); tile.frameY = (short)(18 * (j + 2)); tile.type = type; tile.halfBrick(halfBrick: false); } } }
public static void PlaceModChest(int rightX, int bottomY, ushort type, int style) { if (type < 0 || style < 0 || !WorldGen.InWorld(rightX, bottomY)) { return; } Chest.CreateChest(rightX - 1, bottomY - 1); for (int j = 0; j < 2; j++) { for (int i = 0; i < 2; i++) { if (Main.tile[rightX - 1 + i, bottomY - 1 + j] == null) { Main.tile[rightX - 1 + i, bottomY - 1 + j] = new Tile(); } Main.tile[rightX - 1 + i, bottomY - 1 + j].active(active: true); Main.tile[rightX - 1 + i, bottomY - 1 + j].frameY = (short)(18 * j); Main.tile[rightX - 1 + i, bottomY - 1 + j].frameX = (short)(18 * i + (36 * style)); Main.tile[rightX - 1 + i, bottomY - 1 + j].type = type; Main.tile[rightX - 1 + i, bottomY - 1 + j].halfBrick(halfBrick: false); } } }
public IChest CreateChestData(DPoint chestLocation) { int chestIndex = Chest.CreateChest(chestLocation.X, chestLocation.Y); bool isWorldFull = (chestIndex == -1 || chestIndex == ChestManager.DummyChestIndex); if (!isWorldFull) { return(new ChestAdapter(chestIndex, Main.chest[chestIndex])); } else { lock (this.WorldMetadata.ProtectorChests) { isWorldFull = (this.WorldMetadata.ProtectorChests.Count >= this.Config.MaxProtectorChests); if (isWorldFull) { throw new LimitEnforcementException(); } IChest chest = new ProtectorChestData(chestLocation); this.WorldMetadata.ProtectorChests.Add(chestLocation, (ProtectorChestData)chest); return(chest); } } }
public static void LoadWorldSection(string path) { var data = LoadWorldData(path); for (var i = 0; i < data.Width; i++) { for (var j = 0; j < data.Height; j++) { Main.tile[i + data.X, j + data.Y] = data.Tiles[i, j]; Main.tile[i + data.X, j + data.Y].skipLiquid(true); } } foreach (var sign in data.Signs) { var id = Sign.ReadSign(sign.X + data.X, sign.Y + data.Y); if (id == -1) { continue; } Sign.TextSign(id, sign.Text); } foreach (var itemFrame in data.ItemFrames) { var x = itemFrame.X + data.X; var y = itemFrame.Y + data.Y; var id = TEItemFrame.Place(x, y); if (id == -1) { continue; } WorldGen.PlaceObject(x, y, TileID.ItemFrame); var frame = (TEItemFrame)TileEntity.ByID[id]; frame.item = new Item(); frame.item.netDefaults(itemFrame.Item.NetId); frame.item.stack = itemFrame.Item.Stack; frame.item.prefix = itemFrame.Item.PrefixId; } foreach (var chest in data.Chests) { int chestX = chest.X + data.X, chestY = chest.Y + data.Y; int id; if ((id = Chest.FindChest(chestX, chestY)) == -1 && (id = Chest.CreateChest(chestX, chestY)) == -1) { continue; } WorldGen.PlaceChest(chestX, chestY); for (var index = 0; index < chest.Items.Length; index++) { var netItem = chest.Items[index]; var item = new Item(); item.netDefaults(netItem.NetId); item.stack = netItem.Stack; item.prefix = netItem.PrefixId; Main.chest[id].item[index] = item; } } ResetSection(data.X, data.Y, data.X + data.Width, data.Y + data.Height); }
public override void Execute() { var clipboardPath = Tools.GetClipboardPath(plr.User.ID); var data = Tools.LoadWorldData(clipboardPath); var width = data.Width - 1; var height = data.Height - 1; if ((alignment & 1) == 0) { x2 = x + width; } else { x2 = x; x -= width; } if ((alignment & 2) == 0) { y2 = y + height; } else { y2 = y; y -= height; } Tools.PrepareUndo(x, y, x2, y2, plr); for (var i = x; i <= x2; i++) { for (var j = y; j <= y2; j++) { if (i < 0 || j < 0 || i >= Main.maxTilesX || j >= Main.maxTilesY || expression != null && !expression.Evaluate(Main.tile[i, j])) { continue; } var index1 = i - x; var index2 = j - y; Main.tile[i, j] = data.Tiles[index1, index2]; } } foreach (var sign in data.Signs) { var id = Sign.ReadSign(sign.X + x, sign.Y + y); if (id == -1) { continue; } Sign.TextSign(id, sign.Text); } foreach (var itemFrame in data.ItemFrames) { var ifX = itemFrame.X + x; var ifY = itemFrame.Y + y; var id = TEItemFrame.Place(ifX, ifY); if (id == -1) { continue; } WorldGen.PlaceObject(ifX, ifY, TileID.ItemFrame); var frame = (TEItemFrame)TileEntity.ByID[id]; frame.item = new Item(); frame.item.netDefaults(itemFrame.Item.NetId); frame.item.stack = itemFrame.Item.Stack; frame.item.prefix = itemFrame.Item.PrefixId; } foreach (var chest in data.Chests) { int chestX = chest.X + x, chestY = chest.Y + y; int id; if ((id = Chest.FindChest(chestX, chestY)) == -1 && (id = Chest.CreateChest(chestX, chestY)) == -1) { continue; } WorldGen.PlaceChest(chestX, chestY); for (var index = 0; index < chest.Items.Length; index++) { var netItem = chest.Items[index]; var item = new Item(); item.netDefaults(netItem.NetId); item.stack = netItem.Stack; item.prefix = netItem.PrefixId; Main.chest[id].item[index] = item; } } ResetSection(); plr.SendSuccessMessage("Pasted clipboard to selection."); }
public static void LoadWorldSection(WorldSectionData Data, int?X = null, int?Y = null, bool Tiles = true) { int x = (X ?? Data.X), y = (Y ?? Data.Y); if (Tiles) { for (var i = 0; i < Data.Width; i++) { for (var j = 0; j < Data.Height; j++) { int _x = i + x, _y = j + y; if (!InMapBoundaries(_x, _y)) { continue; } Main.tile[_x, _y] = Data.Tiles[i, j]; Main.tile[_x, _y].skipLiquid(true); } } } ClearObjects(x, y, x + Data.Width, y + Data.Height); foreach (var sign in Data.Signs) { var id = Sign.ReadSign(sign.X + x, sign.Y + y); if ((id == -1) || !InMapBoundaries(sign.X, sign.Y)) { continue; } Sign.TextSign(id, sign.Text); } foreach (var itemFrame in Data.ItemFrames) { var id = TEItemFrame.Place(itemFrame.X + x, itemFrame.Y + y); if (id == -1) { continue; } var frame = (TEItemFrame)TileEntity.ByID[id]; if (!InMapBoundaries(frame.Position.X, frame.Position.Y)) { continue; } frame.item = new Item(); frame.item.netDefaults(itemFrame.Item.NetId); frame.item.stack = itemFrame.Item.Stack; frame.item.prefix = itemFrame.Item.PrefixId; } foreach (var chest in Data.Chests) { int chestX = chest.X + x, chestY = chest.Y + y; int id; if ((id = Chest.FindChest(chestX, chestY)) == -1 && (id = Chest.CreateChest(chestX, chestY)) == -1) { continue; } Chest _chest = Main.chest[id]; if (!InMapBoundaries(chest.X, chest.Y)) { continue; } for (var index = 0; index < chest.Items.Length; index++) { var netItem = chest.Items[index]; var item = new Item(); item.netDefaults(netItem.NetId); item.stack = netItem.Stack; item.prefix = netItem.PrefixId; Main.chest[id].item[index] = item; } } foreach (var logicSensor in Data.LogicSensors) { var id = TELogicSensor.Place(logicSensor.X + x, logicSensor.Y + y); if (id == -1) { continue; } var sensor = (TELogicSensor)TileEntity.ByID[id]; if (!InMapBoundaries(sensor.Position.X, sensor.Position.Y)) { continue; } sensor.logicCheck = logicSensor.Type; } foreach (var trainingDummy in Data.TrainingDummies) { var id = TETrainingDummy.Place(trainingDummy.X + x, trainingDummy.Y + y); if (id == -1) { continue; } var dummy = (TETrainingDummy)TileEntity.ByID[id]; if (!InMapBoundaries(dummy.Position.X, dummy.Position.Y)) { continue; } dummy.npc = -1; } ResetSection(x, y, x + Data.Width, y + Data.Height); }
internal static unsafe bool Generate(TagCompound tag, Point16 pos, bool ignoreNull = false) { List <TileSaveData> data = (List <TileSaveData>)tag.GetList <TileSaveData>("TileData"); if (data is null) { StructureHelper.Instance.Logger.Warn("Corrupt or Invalid structure data."); return(false); } int width = tag.GetInt("Width"); int height = tag.GetInt("Height"); for (int x = 0; x <= width; x++) { for (int y = 0; y <= height; y++) { bool isNullTile = false; bool isNullWall = false; int index = y + x * (height + 1); TileSaveData d = data[index]; Tile tile = Framing.GetTileSafely(pos.X + x, pos.Y + y); if (!int.TryParse(d.Tile, out int type)) { string[] parts = d.Tile.Split(); if (parts[0] == "StructureHelper" && parts[1] == "NullBlock" && !ignoreNull) { isNullTile = true; } else if (parts.Length > 1 && ModLoader.GetMod(parts[0]) != null && ModLoader.GetMod(parts[0]).TryFind <ModTile>(parts[1], out ModTile modTileType)) { type = modTileType.Type; } else { type = 0; } } if (!int.TryParse(d.Wall, out int wallType)) { string[] parts = d.Wall.Split(); if (parts[0] == "StructureHelper" && parts[1] == "NullWall" && !ignoreNull) { isNullWall = true; } else if (parts.Length > 1 && ModLoader.GetMod(parts[0]) != null && ModLoader.GetMod(parts[0]).TryFind <ModWall>(parts[1], out ModWall modWallType)) { wallType = modWallType.Type; } else { wallType = 0; } } if (!d.Active) { isNullTile = false; } if (!isNullTile || ignoreNull) //leave everything else about the tile alone if its a null block { tile.ClearEverything(); tile.TileType = (ushort)type; tile.TileFrameX = d.FrameX; tile.TileFrameY = d.FrameY; fixed(void *ptr = &tile.Get <TileWallWireStateData>()) { var intPtr = (int *)(ptr); intPtr++; *intPtr = d.WallWireData; } fixed(void *ptr = &tile.Get <LiquidData>()) { var shortPtr = (short *)ptr; *shortPtr = d.PackedLiquidData; } if (!d.Active) { tile.HasTile = false; } if (d.TEType != "") //place and load a tile entity { if (d.TEType != "") { if (d.TEType == "StructureHelper ChestEntity" && !ignoreNull) { GenerateChest(new Point16(pos.X + x, pos.Y + y), d.TEData); } else { int typ; if (!int.TryParse(d.TEType, out typ)) { string[] parts = d.TEType.Split(); typ = ModLoader.GetMod(parts[0]).Find <ModTileEntity>(parts[1]).Type; } TileEntity.PlaceEntityNet(pos.X + x, pos.Y + y, typ); if (d.TEData != null && typ > 2) { (TileEntity.ByPosition[new Point16(pos.X + x, pos.Y + y)] as ModTileEntity).LoadData(d.TEData); } } } } else if ((type == TileID.Containers || TileID.Sets.BasicChest[tile.TileType]) && d.FrameX % 36 == 0 && d.FrameY % 36 == 0) //generate an empty chest if there is no chest data { Chest.CreateChest(pos.X + x, pos.Y + y); } } if (!isNullWall || ignoreNull) //leave the wall alone if its a null wall { tile.WallType = (ushort)wallType; } } } return(true); }
public override void Execute() { string message; switch (_action) { case 0: { int success = 0, failure = 0; for (int i = x; i <= x2; i++) { for (int j = y; j <= y2; j++) { if ((Main.tile[i, j].type == TileID.Signs || Main.tile[i, j].type == TileID.Tombstones || Main.tile[i, j].type == TileID.AnnouncementBox) && Main.tile[i, j].frameX % 36 == 0 && Main.tile[i, j].frameY == 0 && Sign.ReadSign(i, j, false) == -1) { int sign = Sign.ReadSign(i, j); if (sign == -1) { failure++; } else { success++; } } } } message = string.Format("Activated signs. ({0}){1}", success, failure > 0 ? " Failed to activate signs. (" + failure + ")" : ""); break; } case 1: { int success = 0, failure = 0; for (int i = x; i <= x2; i++) { for (int j = y; j <= y2; j++) { if ((Main.tile[i, j].type == TileID.Containers || Main.tile[i, j].type == TileID.Containers2 || Main.tile[i, j].type == TileID.Dressers) && Main.tile[i, j].frameX % 36 == 0 && Main.tile[i, j].frameY == 0 && Chest.FindChest(i, j) == -1) { int chest = Chest.CreateChest(i, j); if (chest == -1) { failure++; } else { success++; } } } } message = string.Format("Activated chests. ({0}){1}", success, failure > 0 ? " Failed to activate chests. (" + failure + ")" : ""); break; } case 2: { int success = 0, failure = 0; for (int i = x; i <= x2; i++) { for (int j = y; j <= y2; j++) { if (Main.tile[i, j].type == TileID.ItemFrame && Main.tile[i, j].frameX % 36 == 0 && Main.tile[i, j].frameY == 0 && TEItemFrame.Find(i, j) == -1) { int frame = TEItemFrame.Place(i, j); if (frame == -1) { failure++; } else { success++; } } } } message = string.Format("Activated item frames. ({0}){1}", success, failure > 0 ? " Failed to activate item frames. (" + failure + ")" : ""); break; } default: return; } ResetSection(); plr.SendSuccessMessage(message); }
public void Generate(int x, int y) { bool chestSpawned = false; // Cleanup generation site first. for (int i = 0; i < dynastyStructureTiles.GetLength(0); i++) { for (int j = 0; j < dynastyStructureTiles.GetLength(1); j++) { int k = (x + j) - dynastyStructureTiles.GetLength(1); int l = (y + i) - dynastyStructureTiles.GetLength(0); if (WorldGen.InWorld(k, l, 40)) { Tile tile = Framing.GetTileSafely(k, l); if (dynastyStructureTiles[i, j] >= 0) { tile.ResetToType(0); } // If the spot should be empty: empty it, before actual generation begins. if (dynastyStructureTiles[i, j] == 0) { tile.active(false); } tile.liquid = 0; tile.lava(false); } } } // Start generating actual house. for (int i = 0; i < dynastyStructureTiles.GetLength(0); i++) { for (int j = 0; j < dynastyStructureTiles.GetLength(1); j++) { int k = (x + j) - dynastyStructureTiles.GetLength(1); int l = (y + i) - dynastyStructureTiles.GetLength(0); if (WorldGen.InWorld(k, l, 40)) { Tile tile = Framing.GetTileSafely(k, l); // Place tiles. switch (dynastyStructureTiles[i, j]) { case 1: // Dynasty Wood tile.active(true); tile.type = TileID.DynastyWood; break; case 2: // Red Dynasty Shingles tile.active(true); tile.type = TileID.RedDynastyShingles; break; case 3: // Blue Dynasty Shingles tile.active(true); tile.type = TileID.BlueDynastyShingles; break; case 4: // Wooden Platform tile.active(true); tile.type = TileID.Platforms; WorldGen.SquareTileFrame(k, l, true); break; case 5: // Wooden Beam tile.active(true); tile.type = TileID.WoodenBeam; break; case 6: // Dynasty Door this.Place(k, l, TileID.ClosedDoor, 28, 1); break; case 7: // Dynasty Lantern this.Place(k, l, TileID.HangingLanterns, 26, 1); break; case 8: // Large Dynasty Lantern this.Place(k, l, TileID.Chandeliers, 22, 1); break; case 9: // Dynasty Candle this.Place(k, l, TileID.Candles, 18, 1); break; case 10: // Large Dynasty Candle this.Place(k, l, TileID.Candelabras, 18, 1); break; case 11: // Chinese Lantern this.Place(k, l, TileID.ChineseLanterns, 0, 1); break; case 12: // Red Banner this.Place(k, l, TileID.Banners, 0, 1); break; case 13: // Blue Banner this.Place(k, l, TileID.Banners, 2, 1); break; case 14: // Dynasty Chair Facing Right this.Place(k, l, TileID.Chairs, 27, 1); break; case 15: // Dynasty Chair Facing Left this.Place(k, l, TileID.Chairs, 27, -1); break; case 16: // Dynasty Table this.Place(k, l, TileID.Tables, 25, 1); break; case 17: // Dynasty Workbench this.Place(k, l, TileID.WorkBenches, 18, 1); break; case 18: // Dynasty Cup this.Place(k, l, TileID.Bottles, 5, 1); break; case 19: // Dynasty Plate this.Place(k, l, TileID.Bowls, 2, 1); break; case 20: // Dynasty Mill this.Place(k, l, mod.TileType("DynastyMill_Tile"), 0, 1); break; case 21: // Dynasty Chest Main.tile[k, l].active(false); // Modify this to add items and give a chance *not* to spawn. if (!chestSpawned && WorldGen.genRand.Next(3) == 0) { this.Place(k, l, mod.TileType("LockedDynastyChest_Tile"), 0, 1); int chest = Chest.CreateChest(k, l - 1, -1); // Fill chest. chestSpawned = true; } break; } // Apply tile styles. switch (dynastyStructureStyles[i, j]) { case 1: tile.halfBrick(true); break; case 2: tile.slope(Tile.Type_SlopeDownRight); break; case 3: tile.slope(Tile.Type_SlopeDownLeft); break; case 4: tile.slope(1); break; case 5: tile.slope(Tile.Type_SlopeUpLeft); break; } } } } }
public override void Execute() { if (!CanUseCommand()) { return; } Tools.PrepareUndo(x, y, x2, y2, plr); int noMsg = 0; #region Signs if ((_action == 255) || (_action == 0)) { int success = 0, failure = 0; for (int i = x; i <= x2; i++) { for (int j = y; j <= y2; j++) { if ((Main.tile[i, j].type == TileID.Signs || Main.tile[i, j].type == TileID.Tombstones || Main.tile[i, j].type == TileID.AnnouncementBox) && Main.tile[i, j].frameX % 36 == 0 && Main.tile[i, j].frameY == 0 && Sign.ReadSign(i, j, false) == -1) { int sign = Sign.ReadSign(i, j); if (sign == -1) { failure++; } else { success++; } } } } if (success > 0 || failure > 0) { plr.SendSuccessMessage("Activated signs. ({0}){1}", success, failure > 0 ? " Failed to activate signs. (" + failure + ")" : ""); } else { noMsg++; } } #endregion #region Chests if ((_action == 255) || (_action == 1)) { int success = 0, failure = 0; for (int i = x; i <= x2; i++) { for (int j = y; j <= y2; j++) { if ((Main.tile[i, j].type == TileID.Containers || Main.tile[i, j].type == TileID.Containers2 || Main.tile[i, j].type == TileID.Dressers) && Main.tile[i, j].frameX % 36 == 0 && Main.tile[i, j].frameY == 0 && Chest.FindChest(i, j) == -1) { int chest = Chest.CreateChest(i, j); if (chest == -1) { failure++; } else { success++; } } } } if (success > 0 || failure > 0) { plr.SendSuccessMessage("Activated chests. ({0}){1}", success, failure > 0 ? " Failed to activate chests. (" + failure + ")" : ""); } else { noMsg++; } } #endregion #region ItemFrames if ((_action == 255) || (_action == 2)) { int success = 0, failure = 0; for (int i = x; i <= x2; i++) { for (int j = y; j <= y2; j++) { if (Main.tile[i, j].type == TileID.ItemFrame && Main.tile[i, j].frameX % 36 == 0 && Main.tile[i, j].frameY == 0 && TEItemFrame.Find(i, j) == -1) { int frame = TEItemFrame.Place(i, j); if (frame == -1) { failure++; } else { success++; } } } } if (success > 0 || failure > 0) { plr.SendSuccessMessage("Activated item frames. ({0}){1}", success, failure > 0 ? " Failed to activate item frames. (" + failure + ")" : ""); } else { noMsg++; } } #endregion #region LogicSensors if ((_action == 255) || (_action == 3)) { int success = 0, failure = 0; for (int i = x; i <= x2; i++) { for (int j = y; j <= y2; j++) { if (Main.tile[i, j].type == TileID.LogicSensor && TELogicSensor.Find(i, j) == -1) { int sensor = TELogicSensor.Place(i, j); if (sensor == -1) { failure++; } else { ((TELogicSensor)TELogicSensor.ByID[sensor]).logicCheck = (TELogicSensor.LogicCheckType)((Main.tile[i, j].frameY / 18) + 1); success++; } } } } if (success > 0 || failure > 0) { plr.SendSuccessMessage("Activated logic sensors. ({0}){1}", success, failure > 0 ? " Failed to activate logic sensors. (" + failure + ")" : ""); } else { noMsg++; } } #endregion #region TargetDummies if ((_action == 255) || (_action == 4)) { int success = 0, failure = 0; for (int i = x; i <= x2; i++) { for (int j = y; j <= y2; j++) { if (Main.tile[i, j].type == TileID.TargetDummy && Main.tile[i, j].frameX % 36 == 0 && Main.tile[i, j].frameY == 0 && TETrainingDummy.Find(i, j) == -1) { int dummy = TETrainingDummy.Place(i, j); if (dummy == -1) { failure++; } else { success++; } } } } if (success > 0 || failure > 0) { plr.SendSuccessMessage("Activated target dummies. ({0}){1}", success, failure > 0 ? " Failed to activate target dummies. (" + failure + ")" : ""); } else { noMsg++; } } #endregion if (noMsg == 5) { plr.SendSuccessMessage("There are no objects to activate in this area."); } ResetSection(); }
public void InfiniteChests_ChestDataImport( ProtectionManager protectionManager, out int importedChests, out int overwrittenChests, out int protectFailures ) { string sqliteDatabaseFilePath = Path.Combine(TShock.SavePath, "chests.sqlite"); if (!File.Exists(sqliteDatabaseFilePath)) { throw new FileNotFoundException("Sqlite database file not found.", sqliteDatabaseFilePath); } IDbConnection dbConnection = null; try { switch (TShock.Config.StorageType.ToLower()) { case "mysql": string[] host = TShock.Config.MySqlHost.Split(':'); dbConnection = new MySqlConnection(string.Format( "Server={0}; Port={1}; Database={2}; Uid={3}; Pwd={4};", host[0], host.Length == 1 ? "3306" : host[1], TShock.Config.MySqlDbName, TShock.Config.MySqlUsername, TShock.Config.MySqlPassword )); break; case "sqlite": dbConnection = new SqliteConnection( string.Format("uri=file://{0},Version=3", sqliteDatabaseFilePath) ); break; default: throw new NotImplementedException("Unsupported database."); } importedChests = 0; overwrittenChests = 0; protectFailures = 0; using (QueryResult reader = dbConnection.QueryReader( "SELECT X, Y, Account, Flags, Items FROM Chests WHERE WorldID = @0", Main.worldID) ) { while (reader.Read()) { int rawX = reader.Get <int>("X"); int rawY = reader.Get <int>("Y"); string rawAccount = reader.Get <string>("Account"); InfiniteChestsChestFlags rawFlags = (InfiniteChestsChestFlags)reader.Get <int>("Flags"); string rawItems = reader.Get <string>("Items"); if (!TerrariaUtils.Tiles.IsValidCoord(rawX, rawY)) { continue; } DPoint chestLocation = new DPoint(rawX, rawY); if (!TerrariaUtils.Tiles[chestLocation].active() || TerrariaUtils.Tiles[chestLocation].type != (int)BlockType.Chest) { this.PluginTrace.WriteLineWarning(string.Format( "The chest data on the location {0} could not be imported because no corresponding chest does exist in the world.", chestLocation )); continue; } // TSPlayer.All means that the chest must not be protected at all. TSPlayer owner = TSPlayer.All; if (!string.IsNullOrEmpty(rawAccount)) { User tUser = TShock.Users.GetUserByName(rawAccount); if (tUser != null) { owner = new TSPlayer(0) { UserID = tUser.ID, UserAccountName = tUser.Name, Group = TShock.Groups.GetGroupByName(tUser.Group), }; } else { // The original owner of the chest does not exist anymore, so we just protect it for the server player. owner = TSPlayer.Server; } } int chestIndex = Chest.FindChest(rawX, rawY); if (chestIndex == -1) { chestIndex = Chest.CreateChest(rawX, rawY); } else { this.PluginTrace.WriteLineWarning(string.Format("The items of the chest {0} were overwritten.", chestLocation)); overwrittenChests++; } Chest tChest = Main.chest[chestIndex]; int[] itemArgs = new int[60]; string[] itemData = rawItems.Split(','); for (int i = 0; i < 120; i++) { itemArgs[i] = int.Parse(itemData[i]); } for (int i = 0; i < 40; i++) { tChest.item[i] = new Item(); tChest.item[i].netDefaults(itemArgs[i * 3]); tChest.item[i].prefix = (byte)itemArgs[i * 3 + 2]; tChest.item[i].stack = itemArgs[i * 3 + 1]; } importedChests++; if (owner != TSPlayer.All) { try { ProtectionEntry protection = protectionManager.CreateProtection(owner, chestLocation, true, false, false); protection.IsSharedWithEveryone = (rawFlags & InfiniteChestsChestFlags.PUBLIC) != 0; if ((rawFlags & InfiniteChestsChestFlags.REFILL) != 0) { protectionManager.SetUpRefillChest(owner, chestLocation, TimeSpan.Zero); } } catch (Exception ex) { this.PluginTrace.WriteLineWarning( "Failed to create protection or define refill chest at {0}:\n{1}", chestLocation, ex ); protectFailures++; } } } } } finally { if (dbConnection != null) { dbConnection.Close(); } } }