|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Terraria; |
| 7 | +using Terraria.ModLoader; |
| 8 | +using Terraria.ModLoader.IO; |
| 9 | +using Microsoft.Xna.Framework; |
| 10 | +using Terraria.DataStructures; |
| 11 | +using System.IO; |
| 12 | +using Terraria.ID; |
| 13 | + |
| 14 | +namespace StructureHelper |
| 15 | +{ |
| 16 | + class MultiWand : ModItem |
| 17 | + { |
| 18 | + public bool SecondPoint { get; set; } |
| 19 | + public Point16 TopLeft { get; set; } |
| 20 | + public int Width { get; set; } |
| 21 | + public int Height { get; set; } |
| 22 | + internal List<TagCompound> StructureCache { get; set; } = new List<TagCompound>(); |
| 23 | + public override void SetStaticDefaults() |
| 24 | + { |
| 25 | + DisplayName.SetDefault("Multistructure Wand"); |
| 26 | + Tooltip.SetDefault("Select 2 points in the world, then right click to add a structure. Right click in your inventory when done to save."); |
| 27 | + } |
| 28 | + public override void SetDefaults() |
| 29 | + { |
| 30 | + item.useStyle = 1; |
| 31 | + item.useTime = 20; |
| 32 | + item.useAnimation = 20; |
| 33 | + item.rare = 1; |
| 34 | + } |
| 35 | + public override bool CanRightClick() => true; |
| 36 | + public override void RightClick(Player player) |
| 37 | + { |
| 38 | + item.stack++; |
| 39 | + if (StructureCache.Count > 1) SaveMeForGood(); |
| 40 | + else Main.NewText("Not enough structures! If you want to save a single structure, use the normal structure wand instead!", Color.Red); |
| 41 | + } |
| 42 | + |
| 43 | + public override bool UseItem(Player player) |
| 44 | + { |
| 45 | + if (player.altFunctionUse == 2 && !SecondPoint && TopLeft != null) |
| 46 | + { |
| 47 | + SaveStructure(new Rectangle(TopLeft.X, TopLeft.Y, Width, Height)); |
| 48 | + } |
| 49 | + |
| 50 | + else if (!SecondPoint) |
| 51 | + { |
| 52 | + TopLeft = (Main.MouseWorld / 16).ToPoint16(); |
| 53 | + Width = 0; |
| 54 | + Height = 0; |
| 55 | + Main.NewText("Select Second Point"); |
| 56 | + SecondPoint = true; |
| 57 | + } |
| 58 | + |
| 59 | + else |
| 60 | + { |
| 61 | + Point16 bottomRight = (Main.MouseWorld / 16).ToPoint16(); |
| 62 | + Width = bottomRight.X - TopLeft.X - 1; |
| 63 | + Height = bottomRight.Y - TopLeft.Y - 1; |
| 64 | + Main.NewText("Ready to add! Right click to add this structure, Right click in inventory to save all structures"); |
| 65 | + SecondPoint = false; |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | + public override bool AltFunctionUse(Player player) |
| 71 | + { |
| 72 | + return true; |
| 73 | + } |
| 74 | + internal void SaveMeForGood(string targetPath = null) |
| 75 | + { |
| 76 | + string path = ModLoader.ModPath.Replace("Mods", "SavedStructures"); |
| 77 | + if (!Directory.Exists(path)) Directory.CreateDirectory(path); |
| 78 | + string thisPath = targetPath ?? Path.Combine(path, "SavedMultiStructure_" + DateTime.Now.ToString("d-M-y----H-m-s-f")); |
| 79 | + Main.NewText("Structure saved as " + thisPath, Color.Yellow); |
| 80 | + FileStream stream = File.Create(thisPath); |
| 81 | + stream.Close(); |
| 82 | + |
| 83 | + TagCompound tag = new TagCompound(); |
| 84 | + tag.Add("Structures", StructureCache); |
| 85 | + |
| 86 | + TagIO.ToFile(tag, thisPath); |
| 87 | + |
| 88 | + StructureCache.Clear(); |
| 89 | + } |
| 90 | + public void SaveStructure(Rectangle target) |
| 91 | + { |
| 92 | + TagCompound tag = new TagCompound(); |
| 93 | + tag.Add("Width", Width); |
| 94 | + tag.Add("Height", Height); |
| 95 | + |
| 96 | + List<TileSaveData> data = new List<TileSaveData>(); |
| 97 | + for (int x = target.X; x <= target.X + target.Width; x++) |
| 98 | + { |
| 99 | + for (int y = target.Y; y <= target.Y + target.Height; y++) |
| 100 | + { |
| 101 | + Tile tile = Framing.GetTileSafely(x, y); |
| 102 | + string tileName; |
| 103 | + string wallName; |
| 104 | + string teName; |
| 105 | + if (tile.type >= TileID.Count) tileName = ModContent.GetModTile(tile.type).mod.Name + " " + ModContent.GetModTile(tile.type).Name; |
| 106 | + else tileName = tile.type.ToString(); |
| 107 | + if (tile.wall >= WallID.Count) wallName = ModContent.GetModWall(tile.wall).mod.Name + " " + ModContent.GetModWall(tile.wall).Name; |
| 108 | + else wallName = tile.wall.ToString(); |
| 109 | + |
| 110 | + TileEntity teTarget = null; //grabbing TE data |
| 111 | + TagCompound entityTag = null; |
| 112 | + |
| 113 | + if (TileEntity.ByPosition.ContainsKey(new Point16(x, y))) teTarget = TileEntity.ByPosition[new Point16(x, y)]; |
| 114 | + |
| 115 | + if (teTarget != null) |
| 116 | + { |
| 117 | + if (teTarget.type < 2) |
| 118 | + { |
| 119 | + teName = teTarget.type.ToString(); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + ModTileEntity entityTarget = ModTileEntity.GetTileEntity(teTarget.type); |
| 124 | + if (entityTarget != null) |
| 125 | + { |
| 126 | + teName = entityTarget.mod.Name + " " + entityTarget.Name; |
| 127 | + entityTag = (teTarget as ModTileEntity).Save(); |
| 128 | + } |
| 129 | + else teName = ""; |
| 130 | + } |
| 131 | + } |
| 132 | + else teName = ""; |
| 133 | + |
| 134 | + byte[] wireArray = new byte[] |
| 135 | + { |
| 136 | + (byte)tile.wire().ToInt(), |
| 137 | + (byte)tile.wire2().ToInt(), |
| 138 | + (byte)tile.wire3().ToInt(), |
| 139 | + (byte)tile.wire4().ToInt() |
| 140 | + }; |
| 141 | + data.Add(new TileSaveData(tile.active(), tileName, wallName, tile.frameX, tile.frameY, (short)tile.wallFrameX(), (short)tile.wallFrameY(), |
| 142 | + tile.slope(), tile.halfBrick(), tile.actuator(), !tile.nactive(), tile.liquid, tile.liquidType(), tile.color(), tile.wallColor(), wireArray, |
| 143 | + teName, entityTag)); |
| 144 | + } |
| 145 | + } |
| 146 | + tag.Add("TileData", data); |
| 147 | + |
| 148 | + StructureCache.Add(tag); |
| 149 | + Main.NewText("Structure added. Total structure count: " + StructureCache.Count, Color.Cyan); |
| 150 | + |
| 151 | + TopLeft = default; |
| 152 | + SecondPoint = false; |
| 153 | + Width = 0; |
| 154 | + Height = 0; |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments