Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.IO;
- using TShockAPI;
- using Terraria;
- using Hooks;
- using Newtonsoft.Json;
- namespace NewbieSpawnerNS
- {
- [APIVersion(1, 12)]
- public class NewbieSpawner : TerrariaPlugin
- {
- public String SavePath = Path.Combine(TShock.SavePath, "NewbieSpawner/");
- internal static Config config;
- public override string Name
- {
- get { return "NewbieSpawner"; }
- }
- public override string Author
- {
- get { return "by InanZen"; }
- }
- public override string Description
- {
- get { return ""; }
- }
- public override Version Version
- {
- get { return new Version("0.1"); }
- }
- public NewbieSpawner(Main game)
- : base(game)
- {
- Order = 10;
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- GameHooks.Initialize -= OnInitialize;
- NetHooks.GreetPlayer -= OnJoin;
- }
- }
- public override void Initialize()
- {
- GameHooks.Initialize += OnInitialize;
- NetHooks.GreetPlayer += OnJoin;
- }
- void OnInitialize()
- {
- config = new Config();
- if (!Directory.Exists(SavePath))
- Directory.CreateDirectory(SavePath);
- ReadConfig();
- }
- void OnJoin(int who, HandledEventArgs args)
- {
- if (who >= 0 && TShock.Players[who] != null && TShock.Players[who].RealPlayer)
- {
- if (!TShock.Players[who].IsLoggedIn)
- {
- TShock.Players[who].Teleport(config.X, config.Y);
- }
- else
- {
- float distance = Vector2.Distance(new Vector2(TShock.Players[who].TileX, TShock.Players[who].TileY), new Vector2(config.X, config.Y));
- if (distance <= config.Distance)
- TShock.Players[who].Teleport(Main.spawnTileX, Main.spawnTileY);
- }
- }
- }
- internal class Config
- {
- public int X;
- public int Y;
- public int Distance;
- }
- private void CreateConfig()
- {
- string filepath = Path.Combine(SavePath, "config.json");
- try
- {
- using (var stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Write))
- {
- using (var sr = new StreamWriter(stream))
- {
- config = new Config() { X = 100, Y = 100, Distance = 30 };
- var configString = JsonConvert.SerializeObject(config, Formatting.Indented);
- sr.Write(configString);
- }
- stream.Close();
- }
- }
- catch (Exception ex)
- {
- Log.ConsoleError(ex.Message);
- config = new Config();
- }
- }
- private bool ReadConfig()
- {
- string filepath = Path.Combine(SavePath, "config.json");
- try
- {
- if (File.Exists(filepath))
- {
- using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- using (var sr = new StreamReader(stream))
- {
- var configString = sr.ReadToEnd();
- config = JsonConvert.DeserializeObject<Config>(configString);
- }
- stream.Close();
- }
- return true;
- }
- else
- {
- Log.ConsoleError("Config not found. Creating new one");
- CreateConfig();
- return false;
- }
- }
- catch (Exception ex)
- {
- Log.ConsoleError(ex.Message);
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement