public override void Update()
        {
            ModTileEntity up    = ExampleTEMod.GetTileEntity(Position.X, Position.Y - 1);
            ModTileEntity down  = ExampleTEMod.GetTileEntity(Position.X, Position.Y + 1);
            ModTileEntity left  = ExampleTEMod.GetTileEntity(Position.X - 1, Position.Y);
            ModTileEntity right = ExampleTEMod.GetTileEntity(Position.X + 1, Position.Y);


            if (Main.netMode == NetmodeID.Server)
            {
                updateTimer--;
                if (updateTimer == 0)
                {
                    NetMessage.SendData(MessageID.TileEntitySharing, -1, -1, null, ID);
                    updateTimer = 3;
                }
            }
            EnergyContainer.WriteTagCompound(tag);
        }
예제 #2
0
        internal static List <TagCompound> SaveTileEntities()
        {
            List <TagCompound> list = new List <TagCompound>();

            foreach (KeyValuePair <int, TileEntity> pair in TileEntity.ByID)
            {
                if (pair.Value.type >= ModTileEntity.numVanilla)
                {
                    ModTileEntity tileEntity = (ModTileEntity)pair.Value;
                    list.Add(new TagCompound {
                        ["mod"]  = tileEntity.mod.Name,
                        ["name"] = tileEntity.Name,
                        ["X"]    = tileEntity.Position.X,
                        ["Y"]    = tileEntity.Position.Y,
                        ["data"] = tileEntity.Save()
                    });
                }
            }
            return(list);
        }
예제 #3
0
        /// <summary>
        /// Generate energy as long the generator is not full.
        /// If it's on server the timer will go on and sync the data every time it reach 0
        /// </summary>
        public override void Update()
        {
            try
            {
                if (storage.GetCurrentEnergy() != storage.MaxEnergy)
                {
                    storage.ModifyEnergy(4);
                }

                ModTileEntity up    = ExampleTEMod.GetTileEntity(Position.X, Position.Y - 1);
                ModTileEntity down  = ExampleTEMod.GetTileEntity(Position.X, Position.Y + 1);
                ModTileEntity left  = ExampleTEMod.GetTileEntity(Position.X - 1, Position.Y);
                ModTileEntity right = ExampleTEMod.GetTileEntity(Position.X + 1, Position.Y);

                long usedEnergy = 0;

                ProcessSide(up, Side.up, Side.down, ref usedEnergy);
                ProcessSide(down, Side.down, Side.up, ref usedEnergy);
                ProcessSide(left, Side.left, Side.right, ref usedEnergy);
                ProcessSide(right, Side.right, Side.left, ref usedEnergy);

                storage.TransferEnergy(usedEnergy);

                if (Main.netMode == NetmodeID.Server)
                {
                    updateTimer--;

                    if (updateTimer <= 0)
                    {
                        NetMessage.SendData(MessageID.TileEntitySharing, -1, -1, null, ID);
                        updateTimer = 3;
                    }
                }
                storage.WriteTagCompound(tag);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
예제 #4
0
        public override void PreUpdateMovement()
        {
            counter += 1;
            //If your UI is visble, for some reason SGAmod would not let me access theirs despite it existing :/
            //if (SGAmod.CustomUIMenu.visible)
            //{
            if (portalTileTE == null || (((portalTileTE.Position.ToVector2() + new Vector2(2, 0)) * 16) + new Vector2(8, 0) - player.Center).Length() > 120)
            {
                portalTileTE = null;
#if Debugstuff
                if (counter % 90 == 0)
                {
                    Main.NewText("Untoggled");
                }
#endif

                //Un-toggle UI here
            }

            //}
        }
예제 #5
0
        public static void OpenUI <T>(this ModTileEntity tileEntity) where T : BaseUI
        {
            if (Main.netMode == NetmodeID.Server)
            {
                return;
            }

            Dictionary <ModTileEntity, GUI> UIs = tileEntity.mod.GetTEUIs();

            if (UIs != null)
            {
                BaseUI ui = Activator.CreateInstance <T>();
                ((ITileEntityUI)ui).SetTileEntity(tileEntity);
                UserInterface userInterface = new UserInterface();
                ui.Activate();
                ui.visible = true;
                ui.Load();
                userInterface.SetState(ui);
                UIs.Add(tileEntity, new GUI(ui, userInterface));
            }
        }
예제 #6
0
        public static void HandleUI <T>(this Mod mod, int ID) where T : BaseUI
        {
            if (Main.netMode == NetmodeID.Server)
            {
                return;
            }

            Dictionary <ModTileEntity, GUI> UIs = mod.GetTEUIs();

            if (UIs != null && ID >= 0)
            {
                ModTileEntity tileEntity = (ModTileEntity)TileEntity.ByID[ID];
                if (!UIs.ContainsKey(tileEntity))
                {
                    tileEntity.OpenUI <T>();
                }
                else
                {
                    mod.CloseUI(ID);
                }
            }
        }
예제 #7
0
        public override void SetDefaults()
        {
            Main.tileSolidTop[Type]                    = true;
            Main.tileFrameImportant[Type]              = true;
            TileObjectData.newTile.Width               = 2;
            TileObjectData.newTile.Height              = 2;
            TileObjectData.newTile.Origin              = new Point16(1, 1);
            TileObjectData.newTile.CoordinateHeights   = new int[] { 16, 16 };
            TileObjectData.newTile.CoordinateWidth     = 16;
            TileObjectData.newTile.CoordinatePadding   = 2;
            TileObjectData.newTile.HookCheckIfCanPlace = new PlacementHook(CanPlace, -1, 0, true);
            TileObjectData.newTile.UsesCustomCanPlace  = true;
            ModifyObjectData();
            ModTileEntity tileEntity = GetTileEntity();

            if (tileEntity != null)
            {
                TileObjectData.newTile.HookPostPlaceMyPlayer = new PlacementHook(
                    delegate(int i1, int i2, int i3, int i4, int i5, int i6) {
                    return(tileEntity.Hook_AfterPlacement(i1, i2, i3, i4, i5));
                }, -1, 0, false);
            }
            else
            {
                TileObjectData.newTile.HookPostPlaceMyPlayer = new PlacementHook(
                    delegate(int i1, int i2, int i3, int i4, int i5, int i6) {
                    return(TEStorageComponent.Hook_AfterPlacement_NoEntity(i1, i2, i3, i4, i5));
                }, -1, 0, false);
            }
            TileObjectData.addTile(Type);
            ModTranslation text = CreateMapEntryName();

            text.SetDefault("Magic Storage");
            AddMapEntry(new Color(153, 107, 61), text);
            dustType                      = 7;
            disableSmartCursor            = true;
            TileID.Sets.HasOutlines[Type] = HasSmartInteract();
        }
예제 #8
0
        public override void KillMultiTile(int i, int j, int frameX, int frameY)
        {
            Item.NewItem(i * 16, j * 16, 32, 32, ItemType(frameX, frameY));
            killTile = new Point16(i, j);
            ModTileEntity tileEntity = GetTileEntity();

            if (tileEntity != null)
            {
                tileEntity.Kill(i, j);
            }
            else
            {
                if (Main.netMode == NetmodeID.MultiplayerClient)
                {
                    NetHelper.SendSearchAndRefresh(killTile.X, killTile.Y);
                }
                else
                {
                    TEStorageComponent.SearchAndRefreshNetwork(killTile);
                }
            }
            killTile = new Point16(-1, -1);
        }
예제 #9
0
 public void SetTileEntity(ModTileEntity tileEntity) => this.qeChest = (TEQEChest)tileEntity;
예제 #10
0
 public void SetTileEntity(ModTileEntity tileEntity) => dsu = (TEDSU)tileEntity;
예제 #11
0
 public void SetTileEntity(ModTileEntity tileEntity) => barrel = (TEBarrel)tileEntity;
예제 #12
0
 public void SetTileEntity(ModTileEntity tileEntity) => fan = (TEFan)tileEntity;
예제 #13
0
        public unsafe static TagCompound SaveStructure(Rectangle target)
        {
            TagCompound tag = new TagCompound();

            tag.Add("Version", StructureHelper.Instance.Version.ToString());
            tag.Add("Width", target.Width);
            tag.Add("Height", target.Height);

            List <TileSaveData> data = new List <TileSaveData>();

            for (int x = target.X; x <= target.X + target.Width; x++)
            {
                for (int y = target.Y; y <= target.Y + target.Height; y++)
                {
                    Tile   tile = Framing.GetTileSafely(x, y);
                    string tileName;
                    string wallName;
                    string teName;
                    if (tile.TileType >= TileID.Count)
                    {
                        tileName = ModContent.GetModTile(tile.TileType).Mod.Name + " " + ModContent.GetModTile(tile.TileType).Name;
                    }
                    else
                    {
                        tileName = tile.TileType.ToString();
                    }
                    if (tile.WallType >= WallID.Count)
                    {
                        wallName = ModContent.GetModWall(tile.WallType).Mod.Name + " " + ModContent.GetModWall(tile.WallType).Name;
                    }
                    else
                    {
                        wallName = tile.WallType.ToString();
                    }

                    TileEntity  teTarget  = null; //grabbing TE data
                    TagCompound entityTag = new TagCompound();

                    if (TileEntity.ByPosition.ContainsKey(new Point16(x, y)))
                    {
                        teTarget = TileEntity.ByPosition[new Point16(x, y)];
                    }

                    if (teTarget != null)
                    {
                        if (teTarget.type < 2)
                        {
                            teName = teTarget.type.ToString();
                        }
                        else
                        {
                            ModTileEntity entityTarget = teTarget as ModTileEntity;

                            if (entityTarget != null)
                            {
                                teName = entityTarget.Mod.Name + " " + entityTarget.Name;
                                (teTarget as ModTileEntity).SaveData(entityTag);
                            }
                            else
                            {
                                teName = "";
                            }
                        }
                    }
                    else
                    {
                        teName = "";
                    }

                    int   wallWireData;
                    short packedLiquidData;

                    fixed(void *ptr = &tile.Get <TileWallWireStateData>())
                    {
                        var intPtr = (int *)(ptr);

                        intPtr++;

                        wallWireData = *intPtr;
                    }

                    fixed(void *ptr = &tile.Get <LiquidData>())
                    {
                        var shortPtr = (short *)ptr;

                        packedLiquidData = *shortPtr;
                    }

                    data.Add(
                        new TileSaveData(
                            tileName,
                            wallName,
                            tile.TileFrameX,
                            tile.TileFrameY,
                            wallWireData,
                            packedLiquidData,
                            teName,
                            entityTag
                            ));
                }
            }

            tag.Add("TileData", data);
            return(tag);
        }
예제 #14
0
        public void SaveStructure(Rectangle target)
        {
            TagCompound tag = new TagCompound();

            tag.Add("Width", Width);
            tag.Add("Height", Height);

            List <TileSaveData> data = new List <TileSaveData>();

            for (int x = target.X; x <= target.X + target.Width; x++)
            {
                for (int y = target.Y; y <= target.Y + target.Height; y++)
                {
                    Tile   tile = Framing.GetTileSafely(x, y);
                    string tileName;
                    string wallName;
                    string teName;
                    if (tile.type >= TileID.Count)
                    {
                        tileName = ModContent.GetModTile(tile.type).mod.Name + " " + ModContent.GetModTile(tile.type).Name;
                    }
                    else
                    {
                        tileName = tile.type.ToString();
                    }
                    if (tile.wall >= WallID.Count)
                    {
                        wallName = ModContent.GetModWall(tile.wall).mod.Name + " " + ModContent.GetModWall(tile.wall).Name;
                    }
                    else
                    {
                        wallName = tile.wall.ToString();
                    }

                    TileEntity  teTarget  = null; //grabbing TE data
                    TagCompound entityTag = null;

                    if (TileEntity.ByPosition.ContainsKey(new Point16(x, y)))
                    {
                        teTarget = TileEntity.ByPosition[new Point16(x, y)];
                    }

                    if (teTarget != null)
                    {
                        if (teTarget.type < 2)
                        {
                            teName = teTarget.type.ToString();
                        }
                        else
                        {
                            ModTileEntity entityTarget = ModTileEntity.GetTileEntity(teTarget.type);
                            if (entityTarget != null)
                            {
                                teName    = entityTarget.mod.Name + " " + entityTarget.Name;
                                entityTag = (teTarget as ModTileEntity).Save();
                            }
                            else
                            {
                                teName = "";
                            }
                        }
                    }
                    else
                    {
                        teName = "";
                    }

                    byte[] wireArray = new byte[]
                    {
                        (byte)tile.wire().ToInt(),
                        (byte)tile.wire2().ToInt(),
                        (byte)tile.wire3().ToInt(),
                        (byte)tile.wire4().ToInt()
                    };
                    data.Add(new TileSaveData(tile.active(), tileName, wallName, tile.frameX, tile.frameY, (short)tile.wallFrameX(), (short)tile.wallFrameY(),
                                              tile.slope(), tile.halfBrick(), tile.actuator(), !tile.nactive(), tile.liquid, tile.liquidType(), tile.color(), tile.wallColor(), wireArray,
                                              teName, entityTag));
                }
            }
            tag.Add("TileData", data);

            StructureCache.Add(tag);
            Main.NewText("Structure added. Total structure count: " + StructureCache.Count, Color.Cyan);

            TopLeft     = default;
            SecondPoint = false;
            Width       = 0;
            Height      = 0;
        }
예제 #15
0
 public void SetTileEntity(ModTileEntity tileEntity) => obelisk = (TEObelisk)tileEntity;
예제 #16
0
 public void SetTileEntity(ModTileEntity tileEntity) => panel = (TESolarPanel)tileEntity;
예제 #17
0
        public void SaveStructure(Rectangle target, string targetPath = null)
        {
            string path = ModLoader.ModPath.Replace("Mods", "SavedStructures");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string thisPath = targetPath ?? Path.Combine(path, "SavedStructure_" + DateTime.Now.ToString("d-M-y----H-m-s-f"));

            Main.NewText("Structure saved as " + thisPath, Color.Yellow);
            FileStream stream = File.Create(thisPath);

            stream.Close();

            TagCompound tag = new TagCompound();

            tag.Add("Width", Width);
            tag.Add("Height", Height);

            List <TileSaveData> data = new List <TileSaveData>();

            for (int x = target.X; x <= target.X + target.Width; x++)
            {
                for (int y = target.Y; y <= target.Y + target.Height; y++)
                {
                    Tile   tile = Framing.GetTileSafely(x, y);
                    string tileName;
                    string wallName;
                    string teName;
                    if (tile.type >= TileID.Count)
                    {
                        tileName = ModContent.GetModTile(tile.type).mod.Name + " " + ModContent.GetModTile(tile.type).Name;
                    }
                    else
                    {
                        tileName = tile.type.ToString();
                    }
                    if (tile.wall >= WallID.Count)
                    {
                        wallName = ModContent.GetModWall(tile.wall).mod.Name + " " + ModContent.GetModWall(tile.wall).Name;
                    }
                    else
                    {
                        wallName = tile.wall.ToString();
                    }

                    TileEntity  teTarget  = null; //grabbing TE data
                    TagCompound entityTag = null;

                    if (TileEntity.ByPosition.ContainsKey(new Point16(x, y)))
                    {
                        teTarget = TileEntity.ByPosition[new Point16(x, y)];
                    }

                    if (teTarget != null)
                    {
                        if (teTarget.type < 2)
                        {
                            teName = teTarget.type.ToString();
                        }
                        else
                        {
                            ModTileEntity entityTarget = ModTileEntity.GetTileEntity(teTarget.type);
                            if (entityTarget != null)
                            {
                                teName    = entityTarget.mod.Name + " " + entityTarget.Name;
                                entityTag = (teTarget as ModTileEntity).Save();
                            }
                            else
                            {
                                teName = "";
                            }
                        }
                    }
                    else
                    {
                        teName = "";
                    }

                    byte[] wireArray = new byte[]
                    {
                        (byte)tile.wire().ToInt(),
                        (byte)tile.wire2().ToInt(),
                        (byte)tile.wire3().ToInt(),
                        (byte)tile.wire4().ToInt()
                    };
                    data.Add(new TileSaveData(tile.active(), tileName, wallName, tile.frameX, tile.frameY, (short)tile.wallFrameX(), (short)tile.wallFrameY(),
                                              tile.slope(), tile.halfBrick(), tile.actuator(), !tile.nactive(), tile.liquid, tile.liquidType(), tile.color(), tile.wallColor(), wireArray,
                                              teName, entityTag));
                }
            }
            tag.Add("TileData", data);

            TagIO.ToFile(tag, thisPath);
        }
예제 #18
0
 public void SetTileEntity(ModTileEntity tileEntity) => geothermalPlant = (TEGeothermalPlant)tileEntity;
        internal static void NetMessage_SendData(On.Terraria.NetMessage.orig_SendData orig, int msgType, int remoteClient, int ignoreClient, NetworkText text, int number, float number2, float number3, float number4, int number5, int number6, int number7)
        {
            //TileSection (10) doesn't set "networkSend" to true in TileEntity.Write, so this needs to be kept track of manually
            //Keeping track of this simplifies the workaround code somewhat
            EditsLoader.MessageTileEntitySyncing = msgType == MessageID.TileSection;

            orig(msgType, remoteClient, ignoreClient, text, number, number2, number3, number4, number5, number6, number7);

            EditsLoader.MessageTileEntitySyncing = false;

            //Catch any uses of IDs TileSection (10) and send the ModPacket message
            //This is to circumvent the 65535 shorts' worth of data per-message limit and, hopefully, prevent world sections from suddenly disappearing for no reason
            if (msgType == MessageID.TileSection)
            {
                ModPacket packet = MagicStorage.Instance.GetPacket();

                //Get the entities in the section.  Keep writing until the next entity written would make the size go over 65535
                int   startX = number;
                int   startY = (int)number2;
                short width  = (short)number3;
                short height = (short)number4;

                Queue <int> ids = new Queue <int>();

                //Only process tile entities from Magic Storage
                foreach (var item in TileEntity.ByPosition)
                {
                    Point16 pos = item.Key;
                    if (pos.X >= startX && pos.X < startX + width && pos.Y >= startY && pos.Y < startY + height)
                    {
                        if (ModTileEntity.GetTileEntity(item.Value.type)?.mod == MagicStorage.Instance)
                        {
                            ids.Enqueue(item.Value.ID);
                        }
                    }
                }

                MemoryStream ms = new MemoryStream();
                MemoryStream ms2 = new MemoryStream();
                BinaryWriter msWriter = new BinaryWriter(ms);
                BinaryWriter msWriter2 = new BinaryWriter(ms2);
                int          written = 0, total = 0, packetCount = 1;

                while (ids.Count > 0)
                {
                    WriteNetWorkaround(msWriter, ms, msWriter2, ms2, ids, ref written, ref total, ref packetCount, ref packet, remoteClient, ignoreClient, lastSend: false);
                }

                if (written > 0)
                {
                    //Write the remaining information
                    WriteNetWorkaround(msWriter, ms, msWriter2, ms2, ids, ref written, ref total, ref packetCount, ref packet, remoteClient, ignoreClient, lastSend: true);
                }

                /*
                 * if (Main.netMode == NetmodeID.Server && total > 0)
                 *      Console.WriteLine($"Magic Storage: Wrote {packetCount} packets for {total} entities, {(packetCount - 1) * 65535 + ms.Position} bytes written");
                 */

                msWriter.Flush();
                msWriter.Close();
                msWriter.Dispose();

                msWriter2.Flush();
                msWriter2.Close();
                msWriter2.Dispose();
            }
        }
 public void SetTileEntity(ModTileEntity tileEntity) => turbine = (TEWindTurbine)tileEntity;
예제 #21
0
        internal static void LoadTileEntities(IList <TagCompound> list)
        {
            foreach (TagCompound tag in list)
            {
                string modName = tag.GetString("mod");
                string name    = tag.GetString("name");
                var    point   = new Point16(tag.GetShort("X"), tag.GetShort("Y"));

                ModTileEntity baseModTileEntity = null;
                TileEntity    tileEntity        = null;

                //If the TE is modded
                if (modName != "Terraria")
                {
                    //Find its type, defaulting to pending.
                    if (!ModContent.TryFind(modName, name, out baseModTileEntity))
                    {
                        baseModTileEntity = ModContent.GetInstance <UnloadedTileEntity>();
                    }

                    tileEntity          = ModTileEntity.ConstructFromBase(baseModTileEntity);
                    tileEntity.type     = (byte)baseModTileEntity.Type;
                    tileEntity.Position = point;

                    (tileEntity as UnloadedTileEntity)?.SetData(tag);
                }
                //Otherwise, if the TE is vanilla, try to find its existing instance for the current coordinate.
                else if (!TileEntity.ByPosition.TryGetValue(point, out tileEntity))
                {
                    //Do not create an PendingTileEntity on failure to do so.
                    continue;
                }

                //Load TE data.
                if (tag.ContainsKey("data"))
                {
                    try {
                        tileEntity.Load(tag.GetCompound("data"));

                        if (tileEntity is ModTileEntity modTileEntity)
                        {
                            (tileEntity as UnloadedTileEntity)?.TryRestore(ref modTileEntity);

                            tileEntity = modTileEntity;
                        }
                    }
                    catch (Exception e) {
                        throw new CustomModDataException((tileEntity as ModTileEntity)?.Mod, $"Error in reading {name} tile entity data for {modName}", e);
                    }
                }

                //Check mods' TEs for being valid. If they are, register them to TE collections.
                if (baseModTileEntity != null && baseModTileEntity.ValidTile(tileEntity.Position.X, tileEntity.Position.Y))
                {
                    tileEntity.ID = TileEntity.AssignNewID();
                    TileEntity.ByID[tileEntity.ID] = tileEntity;

                    if (TileEntity.ByPosition.TryGetValue(tileEntity.Position, out TileEntity other))
                    {
                        TileEntity.ByID.Remove(other.ID);
                    }

                    TileEntity.ByPosition[tileEntity.Position] = tileEntity;
                }
            }
        }
예제 #22
0
 public void SetTileEntity(ModTileEntity tileEntity) => spawner = (TESpawner)tileEntity;
예제 #23
0
 public void SetTileEntity(ModTileEntity tileEntity) => coalPlant = (TECoalPlant)tileEntity;