Advertisement
InanZen

NewbieSpawner

Jan 10th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using TShockAPI;
  5. using Terraria;
  6. using Hooks;
  7. using Newtonsoft.Json;
  8.  
  9. namespace NewbieSpawnerNS
  10. {
  11.     [APIVersion(1, 12)]
  12.     public class NewbieSpawner : TerrariaPlugin
  13.     {
  14.         public String SavePath = Path.Combine(TShock.SavePath, "NewbieSpawner/");
  15.         internal static Config config;
  16.         public override string Name
  17.         {
  18.             get { return "NewbieSpawner"; }
  19.         }
  20.         public override string Author
  21.         {
  22.             get { return "by InanZen"; }
  23.         }
  24.         public override string Description
  25.         {
  26.             get { return ""; }
  27.         }
  28.         public override Version Version
  29.         {
  30.             get { return new Version("0.1"); }
  31.         }
  32.         public NewbieSpawner(Main game)
  33.             : base(game)
  34.         {
  35.             Order = 10;
  36.         }
  37.         protected override void Dispose(bool disposing)
  38.         {
  39.             if (disposing)
  40.             {
  41.                 GameHooks.Initialize -= OnInitialize;
  42.                 NetHooks.GreetPlayer -= OnJoin;
  43.             }
  44.         }
  45.         public override void Initialize()
  46.         {
  47.             GameHooks.Initialize += OnInitialize;
  48.             NetHooks.GreetPlayer += OnJoin;
  49.         }
  50.         void OnInitialize()
  51.         {
  52.             config = new Config();
  53.             if (!Directory.Exists(SavePath))
  54.                 Directory.CreateDirectory(SavePath);
  55.             ReadConfig();          
  56.         }
  57.         void OnJoin(int who, HandledEventArgs args)
  58.         {
  59.             if (who >= 0 && TShock.Players[who] != null && TShock.Players[who].RealPlayer)
  60.             {
  61.                 if (!TShock.Players[who].IsLoggedIn)
  62.                 {
  63.                     TShock.Players[who].Teleport(config.X, config.Y);
  64.                 }
  65.                 else
  66.                 {
  67.                     float distance = Vector2.Distance(new Vector2(TShock.Players[who].TileX, TShock.Players[who].TileY), new Vector2(config.X, config.Y));
  68.                     if (distance <= config.Distance)
  69.                         TShock.Players[who].Teleport(Main.spawnTileX, Main.spawnTileY);
  70.                 }
  71.             }
  72.         }
  73.         internal class Config
  74.         {
  75.             public int X;
  76.             public int Y;
  77.             public int Distance;
  78.         }
  79.         private void CreateConfig()
  80.         {
  81.             string filepath = Path.Combine(SavePath, "config.json");
  82.             try
  83.             {
  84.                 using (var stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Write))
  85.                 {
  86.                     using (var sr = new StreamWriter(stream))
  87.                     {
  88.                         config = new Config() { X = 100, Y = 100, Distance = 30 };
  89.                         var configString = JsonConvert.SerializeObject(config, Formatting.Indented);
  90.                         sr.Write(configString);
  91.                     }
  92.                     stream.Close();
  93.                 }
  94.             }
  95.             catch (Exception ex)
  96.             {
  97.                 Log.ConsoleError(ex.Message);
  98.                 config = new Config();
  99.             }
  100.         }
  101.         private bool ReadConfig()
  102.         {
  103.             string filepath = Path.Combine(SavePath, "config.json");
  104.             try
  105.             {
  106.                 if (File.Exists(filepath))
  107.                 {
  108.                     using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
  109.                     {
  110.                         using (var sr = new StreamReader(stream))
  111.                         {
  112.                             var configString = sr.ReadToEnd();
  113.                             config = JsonConvert.DeserializeObject<Config>(configString);
  114.                         }
  115.                         stream.Close();
  116.                     }
  117.                     return true;
  118.                 }
  119.                 else
  120.                 {
  121.                     Log.ConsoleError("Config not found. Creating new one");
  122.                     CreateConfig();
  123.                     return false;
  124.                 }
  125.             }
  126.             catch (Exception ex)
  127.             {
  128.                 Log.ConsoleError(ex.Message);
  129.             }
  130.             return false;
  131.         }
  132.  
  133.  
  134.     }
  135.  
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement