Moved some code around
Fixed dead code
This commit is contained in:
parent
0e09801e0c
commit
734aaa2fb8
6 changed files with 578 additions and 491 deletions
33
TShockAPI/ConfigurationManager.cs
Normal file
33
TShockAPI/ConfigurationManager.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TShockAPI
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides all the stupid little variables a home away from home.
|
||||
/// </summary>
|
||||
class ConfigurationManager
|
||||
{
|
||||
public static int invasionMultiplier = 1;
|
||||
public static int defaultMaxSpawns = 4;
|
||||
public static int defaultSpawnRate = 700;
|
||||
public static int serverPort = 7777;
|
||||
public static bool enableWhitelist = false;
|
||||
public static bool infiniteInvasion = false;
|
||||
public static bool permaPvp = false;
|
||||
public static int killCount = 0;
|
||||
public static bool startedInvasion = false;
|
||||
public static bool kickCheater = true;
|
||||
public static bool banCheater = true;
|
||||
public static bool banTnt = false;
|
||||
public static bool kickTnt = false;
|
||||
public enum NPCList : int
|
||||
{
|
||||
WORLD_EATER = 0,
|
||||
EYE = 1,
|
||||
SKELETRON = 2
|
||||
}
|
||||
}
|
||||
}
|
||||
194
TShockAPI/FileTools.cs
Normal file
194
TShockAPI/FileTools.cs
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using Terraria;
|
||||
|
||||
namespace TShockAPI
|
||||
{
|
||||
class FileTools
|
||||
{
|
||||
public static string saveDir = "./tshock/";
|
||||
public static void CreateFile(string file)
|
||||
{
|
||||
using (FileStream fs = File.Create(file)) { }
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds a 'cheater' to cheaters.txt
|
||||
/// </summary>
|
||||
/// <param name="ply">You should know what this does by now.</param>
|
||||
public static void WriteCheater(int ply)
|
||||
{
|
||||
string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
|
||||
string cheaters = "";
|
||||
TextReader tr = new StreamReader(saveDir + "cheaters.txt");
|
||||
cheaters = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (cheaters.Contains(Main.player[ply].name) && cheaters.Contains(ip)) { return; }
|
||||
TextWriter sw = new StreamWriter(saveDir + "cheaters.txt", true);
|
||||
sw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]");
|
||||
sw.Close();
|
||||
}
|
||||
/// <summary>
|
||||
/// Writes a 'banned idiot' to the ban list
|
||||
/// </summary>
|
||||
/// <param name="ply"></param>
|
||||
public static void WriteBan(int ply)
|
||||
{
|
||||
string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
|
||||
TextWriter tw = new StreamWriter(saveDir + "bans.txt", true);
|
||||
tw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]");
|
||||
tw.Close();
|
||||
}
|
||||
/// <summary>
|
||||
/// Writes a tnt user to grief.txt
|
||||
/// </summary>
|
||||
/// <param name="ply">int player</param>
|
||||
public static void WriteGrief(int ply)
|
||||
{
|
||||
TextWriter tw = new StreamWriter(saveDir + "grief.txt", true);
|
||||
tw.WriteLine("[" + Main.player[ply].name + "] [" + Tools.GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()) + "]");
|
||||
tw.Close();
|
||||
}
|
||||
/// <summary>
|
||||
/// Writes an error message to errors.txt
|
||||
/// </summary>
|
||||
/// <param name="err">string message</param>
|
||||
public static void WriteError(string err)
|
||||
{
|
||||
if (System.IO.File.Exists(saveDir + "errors.txt"))
|
||||
{
|
||||
TextWriter tw = new StreamWriter(saveDir + "errors.txt", true);
|
||||
tw.WriteLine(err);
|
||||
tw.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
FileTools.CreateFile(saveDir + "errors.txt");
|
||||
TextWriter tw = new StreamWriter(saveDir + "errors.txt", true);
|
||||
tw.WriteLine(err);
|
||||
tw.Close();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Sets up the configuration file for all variables, and creates any missing files.
|
||||
/// </summary>
|
||||
public static void SetupConfig()
|
||||
{
|
||||
if (!System.IO.Directory.Exists(saveDir)) { System.IO.Directory.CreateDirectory(saveDir); }
|
||||
if (System.IO.File.Exists(saveDir + "tiles.txt"))
|
||||
{
|
||||
System.IO.File.Delete(saveDir + "tiles.txt");
|
||||
}
|
||||
if (!System.IO.File.Exists(saveDir + "motd.txt"))
|
||||
{
|
||||
FileTools.CreateFile(saveDir + "motd.txt");
|
||||
TextWriter tw = new StreamWriter(saveDir + "motd.txt");
|
||||
tw.WriteLine("This server is running TShock. Type /help for a list of commands.");
|
||||
tw.WriteLine("%255,000,000%Current map: %map%");
|
||||
tw.WriteLine("Current players: %players%");
|
||||
tw.Close();
|
||||
}
|
||||
if (!System.IO.File.Exists(saveDir + "bans.txt")) { FileTools.CreateFile(saveDir + "bans.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "cheaters.txt")) { FileTools.CreateFile(saveDir + "cheaters.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "admins.txt")) { FileTools.CreateFile(saveDir + "admins.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "grief.txt")) { FileTools.CreateFile(saveDir + "grief.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "whitelist.txt")) { FileTools.CreateFile(saveDir + "whitelist.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "config.txt"))
|
||||
{
|
||||
FileTools.CreateFile(saveDir + "config.txt");
|
||||
TextWriter tw = new StreamWriter(saveDir + "config.txt");
|
||||
tw.WriteLine("true,50,4,700,true,true,7777,false,false,false,false,false");
|
||||
tw.Close();
|
||||
}
|
||||
TextReader tr = new StreamReader(saveDir + "config.txt");
|
||||
string config = tr.ReadToEnd();
|
||||
config = config.Replace("\n", "");
|
||||
config = config.Replace("\r", "");
|
||||
config = config.Replace(" ", "");
|
||||
tr.Close();
|
||||
string[] configuration = config.Split(',');
|
||||
try
|
||||
{
|
||||
ConfigurationManager.invasionMultiplier = Convert.ToInt32(configuration[1]);
|
||||
ConfigurationManager.defaultMaxSpawns = Convert.ToInt32(configuration[2]);
|
||||
ConfigurationManager.defaultSpawnRate = Convert.ToInt32(configuration[3]);
|
||||
ConfigurationManager.kickCheater = Convert.ToBoolean(configuration[4]);
|
||||
ConfigurationManager.banCheater = Convert.ToBoolean(configuration[5]);
|
||||
ConfigurationManager.serverPort = Convert.ToInt32(configuration[6]);
|
||||
ConfigurationManager.enableWhitelist = Convert.ToBoolean(configuration[7]);
|
||||
ConfigurationManager.infiniteInvasion = Convert.ToBoolean(configuration[8]);
|
||||
ConfigurationManager.permaPvp = Convert.ToBoolean(configuration[9]);
|
||||
ConfigurationManager.kickTnt = Convert.ToBoolean(configuration[10]);
|
||||
ConfigurationManager.banTnt = Convert.ToBoolean(configuration[11]);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
FileTools.WriteError(e.Message);
|
||||
}
|
||||
|
||||
Netplay.serverPort = ConfigurationManager.serverPort;
|
||||
}
|
||||
/// <summary>
|
||||
/// Checks if a user is banned
|
||||
/// </summary>
|
||||
/// <param name="p">string ip</param>
|
||||
/// <returns>true/false</returns>
|
||||
public static bool CheckBanned(String p)
|
||||
{
|
||||
String ip = p.Split(':')[0];
|
||||
TextReader tr = new StreamReader(saveDir + "bans.txt");
|
||||
string banlist = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
banlist = banlist.Trim();
|
||||
if (banlist.Contains(ip))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Tells if a user is on the whitelist
|
||||
/// </summary>
|
||||
/// <param name="ip">string ip of the user</param>
|
||||
/// <returns>true/false</returns>
|
||||
public static bool OnWhitelist(string ip)
|
||||
{
|
||||
if (!ConfigurationManager.enableWhitelist) { return true; }
|
||||
if (!System.IO.File.Exists(saveDir + "whitelist.txt")) { FileTools.CreateFile(saveDir + "whitelist.txt"); TextWriter tw = new StreamWriter(saveDir + "whitelist.txt"); tw.WriteLine("127.0.0.1"); tw.Close(); }
|
||||
TextReader tr = new StreamReader(saveDir + "whitelist.txt");
|
||||
string whitelist = tr.ReadToEnd();
|
||||
ip = Tools.GetRealIP(ip);
|
||||
if (whitelist.Contains(ip)) { return true; } else { return false; }
|
||||
}
|
||||
/// <summary>
|
||||
/// Tells if the user is on grief.txt
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <returns></returns>
|
||||
public static bool CheckGreif(String ip)
|
||||
{
|
||||
ip = Tools.GetRealIP(ip);
|
||||
if (!ConfigurationManager.banTnt) { return false; }
|
||||
TextReader tr = new StreamReader(saveDir + "grief.txt");
|
||||
string list = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
|
||||
return list.Contains(ip);
|
||||
}
|
||||
|
||||
public static bool CheckCheat(String ip)
|
||||
{
|
||||
ip = Tools.GetRealIP(ip);
|
||||
if (!ConfigurationManager.banCheater) { return false; }
|
||||
TextReader tr = new StreamReader(saveDir + "cheaters.txt");
|
||||
string trr = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (trr.Contains(ip))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public FileTools() { }
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ using TerrariaAPI.Hooks;
|
|||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System.Net;
|
||||
using TShockAPI;
|
||||
|
||||
namespace TShockAPI
|
||||
{
|
||||
|
|
@ -22,29 +23,7 @@ namespace TShockAPI
|
|||
|
||||
public static bool shownVersion = false;
|
||||
|
||||
public static bool killGuide = true;
|
||||
public static int invasionMultiplier = 1;
|
||||
public static int defaultMaxSpawns = 4;
|
||||
public static int defaultSpawnRate = 700;
|
||||
public static bool kickCheater = true;
|
||||
public static bool banCheater = true;
|
||||
public static int serverPort = 7777;
|
||||
public static bool enableWhitelist = false;
|
||||
public static bool infiniteInvasion = false;
|
||||
public static bool permaPvp = false;
|
||||
public static int killCount = 0;
|
||||
public static bool startedInvasion = false;
|
||||
|
||||
public static string tileWhitelist = "";
|
||||
private static bool banTnt = false;
|
||||
private static bool kickTnt = false;
|
||||
|
||||
public enum NPCList : int
|
||||
{
|
||||
WORLD_EATER = 0,
|
||||
EYE = 1,
|
||||
SKELETRON = 2
|
||||
}
|
||||
|
||||
public override Version Version
|
||||
{
|
||||
|
|
@ -107,7 +86,7 @@ namespace TShockAPI
|
|||
|
||||
void NpcHooks_OnStrikeNpc(NpcStrikeEventArgs e)
|
||||
{
|
||||
if (infiniteInvasion)
|
||||
if (ConfigurationManager.infiniteInvasion)
|
||||
{
|
||||
IncrementKills();
|
||||
if (Main.invasionSize < 10)
|
||||
|
|
@ -120,7 +99,7 @@ namespace TShockAPI
|
|||
void OnPreGetData(byte id, messageBuffer msg, int idx, int length, HandledEventArgs e)
|
||||
{
|
||||
if (Main.netMode != 2) { return; }
|
||||
if (id == 0x1e && permaPvp)
|
||||
if (id == 0x1e && ConfigurationManager.permaPvp)
|
||||
{
|
||||
e.Handled = true;
|
||||
}
|
||||
|
|
@ -159,17 +138,17 @@ namespace TShockAPI
|
|||
if (Main.netMode != 2) { return; }
|
||||
int plr = who; //legacy support
|
||||
ShowUpdateReminder(who);
|
||||
ShowMOTD(who);
|
||||
Tools.ShowMOTD(who);
|
||||
if (Main.player[plr].statLifeMax > 400 || Main.player[plr].statManaMax > 200 || Main.player[plr].statLife > 400 || Main.player[plr].statMana > 200)
|
||||
{
|
||||
HandleCheater(plr);
|
||||
Tools.HandleCheater(plr);
|
||||
}
|
||||
if (permaPvp)
|
||||
if (ConfigurationManager.permaPvp)
|
||||
{
|
||||
Main.player[who].hostile = true;
|
||||
NetMessage.SendData(30, -1, -1, "", who);
|
||||
}
|
||||
if (IsAdmin(who) && infiniteInvasion && !startedInvasion)
|
||||
if (Tools.IsAdmin(who) && ConfigurationManager.infiniteInvasion && !ConfigurationManager.startedInvasion)
|
||||
{
|
||||
StartInvasion();
|
||||
}
|
||||
|
|
@ -182,15 +161,15 @@ namespace TShockAPI
|
|||
int x = (int) Main.player[ply].position.X;
|
||||
int y = (int) Main.player[ply].position.Y;
|
||||
|
||||
if (IsAdmin(ply))
|
||||
if (Tools.IsAdmin(ply))
|
||||
{
|
||||
if (msg.Length > 5 && msg.Substring(0, 5) == "/kick")
|
||||
{
|
||||
string plStr = msg.Remove(0, 5).Trim();
|
||||
if (!(FindPlayer(plStr) == -1 || plStr == ""))
|
||||
if (!(Tools.FindPlayer(plStr) == -1 || plStr == ""))
|
||||
{
|
||||
Kick(FindPlayer(plStr), "You were kicked.");
|
||||
Broadcast(plStr + " was kicked by " + FindPlayer(ply));
|
||||
Tools.Kick(Tools.FindPlayer(plStr), "You were kicked.");
|
||||
Tools.Broadcast(plStr + " was kicked by " + Tools.FindPlayer(ply));
|
||||
}
|
||||
handler.Handled = true;
|
||||
}
|
||||
|
|
@ -198,10 +177,10 @@ namespace TShockAPI
|
|||
if (msg.Length > 4 && msg.Substring(0, 4) == "/ban")
|
||||
{
|
||||
string plStr = msg.Remove(0, 4).Trim();
|
||||
if (!(FindPlayer(plStr) == -1 || plStr == ""))
|
||||
if (!(Tools.FindPlayer(plStr) == -1 || plStr == ""))
|
||||
{
|
||||
WriteBan(FindPlayer(plStr));
|
||||
Kick(FindPlayer(plStr), "You were banned.");
|
||||
FileTools.WriteBan(Tools.FindPlayer(plStr));
|
||||
Tools.Kick(Tools.FindPlayer(plStr), "You were banned.");
|
||||
}
|
||||
handler.Handled = true;
|
||||
}
|
||||
|
|
@ -214,7 +193,7 @@ namespace TShockAPI
|
|||
|
||||
if (msg == "/reload")
|
||||
{
|
||||
SetupConfig();
|
||||
FileTools.SetupConfig();
|
||||
handler.Handled = true;
|
||||
}
|
||||
|
||||
|
|
@ -243,7 +222,7 @@ namespace TShockAPI
|
|||
}
|
||||
if (msg == "/bloodmoon")
|
||||
{
|
||||
Broadcast(FindPlayer(ply) + " turned on blood moon.");
|
||||
Tools.Broadcast(Tools.FindPlayer(ply) + " turned on blood moon.");
|
||||
Main.bloodMoon = true;
|
||||
Main.time = 0;
|
||||
Main.dayTime = false;
|
||||
|
|
@ -254,34 +233,34 @@ namespace TShockAPI
|
|||
}
|
||||
if (msg == "/eater")
|
||||
{
|
||||
NewNPC((int)NPCList.WORLD_EATER, x, y, ply);
|
||||
Broadcast(FindPlayer(ply) + " has spawned an eater of worlds!");
|
||||
Tools.NewNPC((int)ConfigurationManager.NPCList.WORLD_EATER, x, y, ply);
|
||||
Tools.Broadcast(Tools.FindPlayer(ply) + " has spawned an eater of worlds!");
|
||||
handler.Handled = true;
|
||||
}
|
||||
if (msg == "/eye")
|
||||
{
|
||||
NewNPC((int)NPCList.EYE, x, y, ply);
|
||||
Broadcast(FindPlayer(ply) + " has spawned an eye!");
|
||||
Tools.NewNPC((int)ConfigurationManager.NPCList.EYE, x, y, ply);
|
||||
Tools.Broadcast(Tools.FindPlayer(ply) + " has spawned an eye!");
|
||||
handler.Handled = true;
|
||||
}
|
||||
if (msg == "/skeletron")
|
||||
{
|
||||
NewNPC((int)NPCList.SKELETRON, x, y, ply);
|
||||
Broadcast(FindPlayer(ply) + " has spawned skeletron!");
|
||||
Tools.NewNPC((int)ConfigurationManager.NPCList.SKELETRON, x, y, ply);
|
||||
Tools.Broadcast(Tools.FindPlayer(ply) + " has spawned skeletron!");
|
||||
handler.Handled = true;
|
||||
}
|
||||
if (msg == "/hardcore")
|
||||
{
|
||||
for (int i = 0; i <= 2; i++)
|
||||
{
|
||||
NewNPC(i, x, y, ply);
|
||||
Tools.NewNPC(i, x, y, ply);
|
||||
}
|
||||
Broadcast(FindPlayer(ply) + " has spawned all 3 bosses!");
|
||||
Tools.Broadcast(Tools.FindPlayer(ply) + " has spawned all 3 bosses!");
|
||||
handler.Handled = true;
|
||||
}
|
||||
if (msg == "/invade")
|
||||
{
|
||||
Broadcast(Main.player[ply].name + " started an invasion.");
|
||||
Tools.Broadcast(Main.player[ply].name + " started an invasion.");
|
||||
StartInvasion();
|
||||
handler.Handled = true;
|
||||
}
|
||||
|
|
@ -289,50 +268,50 @@ namespace TShockAPI
|
|||
{
|
||||
string passwd = msg.Remove(0, 9).Trim();
|
||||
Netplay.password = passwd;
|
||||
SendMessage(ply, "Server password changed to: " + passwd);
|
||||
Tools.SendMessage(ply, "Server password changed to: " + passwd);
|
||||
handler.Handled = true;
|
||||
}
|
||||
if (msg == "/save")
|
||||
{
|
||||
WorldGen.saveWorld();
|
||||
SendMessage(ply, "World saved.");
|
||||
Tools.SendMessage(ply, "World saved.");
|
||||
handler.Handled = true;
|
||||
}
|
||||
if (msg == "/spawn")
|
||||
{
|
||||
Teleport(ply, Main.player[ply].SpawnX * 16, Main.player[ply].SpawnY * 16);
|
||||
SendMessage(ply, "Teleported to your spawnpoint.");
|
||||
Tools.SendMessage(ply, "Teleported to your spawnpoint.");
|
||||
handler.Handled = true;
|
||||
}
|
||||
if (msg.Length > 3 && msg.Substring(0, 3) == "/tp")
|
||||
{
|
||||
string player = msg.Remove(0, 3).Trim();
|
||||
if (!(FindPlayer(player) == -1) && !(player == ""))
|
||||
if (!(Tools.FindPlayer(player) == -1) && !(player == ""))
|
||||
{
|
||||
Teleport(ply, Main.player[FindPlayer(player)].position.X, Main.player[FindPlayer(player)].position.Y);
|
||||
SendMessage(ply, "Teleported to " + player);
|
||||
Teleport(ply, Main.player[Tools.FindPlayer(player)].position.X, Main.player[Tools.FindPlayer(player)].position.Y);
|
||||
Tools.SendMessage(ply, "Teleported to " + player);
|
||||
handler.Handled = true;
|
||||
}
|
||||
}
|
||||
if (msg.Length > 7 && msg.Substring(0, 7) == "/tphere")
|
||||
{
|
||||
string player = msg.Remove(0, 7).Trim();
|
||||
if (!(FindPlayer(player) == -1) && !(player == ""))
|
||||
if (!(Tools.FindPlayer(player) == -1) && !(player == ""))
|
||||
{
|
||||
Teleport(FindPlayer(player), Main.player[ply].position.X, Main.player[ply].position.Y);
|
||||
SendMessage(FindPlayer(player), "You were teleported to " + FindPlayer(ply) + ".");
|
||||
SendMessage(ply, "You brought " + player + " here.");
|
||||
Teleport(Tools.FindPlayer(player), Main.player[ply].position.X, Main.player[ply].position.Y);
|
||||
Tools.SendMessage(Tools.FindPlayer(player), "You were teleported to " + Tools.FindPlayer(ply) + ".");
|
||||
Tools.SendMessage(ply, "You brought " + player + " here.");
|
||||
handler.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (msg == "/help")
|
||||
{
|
||||
SendMessage(ply, "TShock Commands:");
|
||||
SendMessage(ply, "/kick, /ban, /reload, /off, /dropmeteor, /invade");
|
||||
SendMessage(ply, "/star, /skeletron, /eye, /eater, /hardcore");
|
||||
SendMessage(ply, "Terraria commands:");
|
||||
SendMessage(ply, "/playing, /p, /me");
|
||||
Tools.SendMessage(ply, "TShock Commands:");
|
||||
Tools.SendMessage(ply, "/kick, /ban, /reload, /off, /dropmeteor, /invade");
|
||||
Tools.SendMessage(ply, "/star, /skeletron, /eye, /eater, /hardcore");
|
||||
Tools.SendMessage(ply, "Terraria commands:");
|
||||
Tools.SendMessage(ply, "/playing, /p, /me");
|
||||
handler.Handled = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -341,14 +320,14 @@ namespace TShockAPI
|
|||
void OnJoin(int ply, AllowEventArgs handler)
|
||||
{
|
||||
if (Main.netMode != 2) { return; }
|
||||
string ip = GetRealIP((Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)));
|
||||
if (CheckBanned(ip) || CheckCheat(ip) || CheckGreif(ip))
|
||||
string ip = Tools.GetRealIP((Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)));
|
||||
if (FileTools.CheckBanned(ip) || FileTools.CheckCheat(ip) || FileTools.CheckGreif(ip))
|
||||
{
|
||||
Kick(ply, "You are banned.");
|
||||
Tools.Kick(ply, "You are banned.");
|
||||
}
|
||||
if (!OnWhitelist(ip))
|
||||
if (!FileTools.OnWhitelist(ip))
|
||||
{
|
||||
Kick(ply, "Not on whitelist.");
|
||||
Tools.Kick(ply, "Not on whitelist.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -359,7 +338,7 @@ namespace TShockAPI
|
|||
|
||||
void OnPreInit()
|
||||
{
|
||||
SetupConfig();
|
||||
FileTools.SetupConfig();
|
||||
}
|
||||
|
||||
void OnPostInit()
|
||||
|
|
@ -376,8 +355,8 @@ namespace TShockAPI
|
|||
{
|
||||
if (Main.player[i] != null)
|
||||
{
|
||||
WriteGrief((int)i);
|
||||
Kick((int)i, "Kill tile abuse detected.");
|
||||
FileTools.WriteGrief((int)i);
|
||||
Tools.Kick((int)i, "Kill tile abuse detected.");
|
||||
}
|
||||
tileThreshold[i] = 0;
|
||||
}
|
||||
|
|
@ -396,7 +375,7 @@ namespace TShockAPI
|
|||
{
|
||||
if (!shownVersion)
|
||||
{
|
||||
if (IsAdmin(FindPlayer(ply)))
|
||||
if (Tools.IsAdmin(Tools.FindPlayer(ply)))
|
||||
{
|
||||
WebClient client = new WebClient();
|
||||
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)");
|
||||
|
|
@ -407,16 +386,16 @@ namespace TShockAPI
|
|||
float[] color = { 255, 255, 000 };
|
||||
if (Convert.ToDouble(changes[0]) > version)
|
||||
{
|
||||
SendMessage(ply, "This server is out of date.");
|
||||
Tools.SendMessage(ply, "This server is out of date.");
|
||||
for (int i = 1; i <= changes.Length; i++)
|
||||
{
|
||||
SendMessage(ply, changes[i], color);
|
||||
Tools.SendMessage(ply, changes[i], color);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(e.Message);
|
||||
FileTools.WriteError(e.Message);
|
||||
}
|
||||
shownVersion = true;
|
||||
}
|
||||
|
|
@ -444,13 +423,13 @@ namespace TShockAPI
|
|||
public static void StartInvasion()
|
||||
{
|
||||
Main.invasionType = 1;
|
||||
if (infiniteInvasion)
|
||||
if (ConfigurationManager.infiniteInvasion)
|
||||
{
|
||||
Main.invasionSize = 20000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
Main.invasionSize = 100 + (invasionMultiplier * activePlayers());
|
||||
Main.invasionSize = 100 + (ConfigurationManager.invasionMultiplier * Tools.activePlayers());
|
||||
}
|
||||
|
||||
Main.invasionWarn = 0;
|
||||
|
|
@ -466,430 +445,34 @@ namespace TShockAPI
|
|||
|
||||
public static void IncrementKills()
|
||||
{
|
||||
killCount++;
|
||||
ConfigurationManager.killCount++;
|
||||
Random r = new Random();
|
||||
int random = r.Next(5);
|
||||
if (killCount % 100 == 0)
|
||||
if (ConfigurationManager.killCount % 100 == 0)
|
||||
{
|
||||
switch (random)
|
||||
{
|
||||
case 0:
|
||||
Broadcast("You call that a lot? " + killCount + " goblins killed!");
|
||||
Tools.Broadcast("You call that a lot? " + ConfigurationManager.killCount + " goblins killed!");
|
||||
break;
|
||||
case 1:
|
||||
Broadcast("Fatality! " + killCount + " goblins killed!");
|
||||
Tools.Broadcast("Fatality! " + ConfigurationManager.killCount + " goblins killed!");
|
||||
break;
|
||||
case 2:
|
||||
Broadcast("Number of 'noobs' killed to date: " + killCount);
|
||||
Tools.Broadcast("Number of 'noobs' killed to date: " + ConfigurationManager.killCount);
|
||||
break;
|
||||
case 3:
|
||||
Broadcast("Duke Nukem would be proud. " + killCount + " goblins killed.");
|
||||
Tools.Broadcast("Duke Nukem would be proud. " + ConfigurationManager.killCount + " goblins killed.");
|
||||
break;
|
||||
case 4:
|
||||
Broadcast("You call that a lot? " + killCount + " goblins killed!");
|
||||
Tools.Broadcast("You call that a lot? " + ConfigurationManager.killCount + " goblins killed!");
|
||||
break;
|
||||
case 5:
|
||||
Broadcast(killCount + " copies of Call of Duty smashed.");
|
||||
Tools.Broadcast(ConfigurationManager.killCount + " copies of Call of Duty smashed.");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static int activePlayers()
|
||||
{
|
||||
int num = 0;
|
||||
for (int i = 0; i < Main.maxPlayers; i++)
|
||||
{
|
||||
if (Main.player[i].active)
|
||||
{
|
||||
num++;
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public static bool OnWhitelist(string ip)
|
||||
{
|
||||
if (!enableWhitelist) { return true; }
|
||||
if (!System.IO.File.Exists(saveDir + "whitelist.txt")) { CreateFile(saveDir + "whitelist.txt"); TextWriter tw = new StreamWriter(saveDir + "whitelist.txt"); tw.WriteLine("127.0.0.1"); tw.Close(); }
|
||||
TextReader tr = new StreamReader(saveDir + "whitelist.txt");
|
||||
string whitelist = tr.ReadToEnd();
|
||||
ip = GetRealIP(ip);
|
||||
if (whitelist.Contains(ip)) { return true; } else { return false; }
|
||||
}
|
||||
|
||||
public static bool CheckGreif(String ip)
|
||||
{
|
||||
ip = GetRealIP(ip);
|
||||
if (!banTnt) { return false; }
|
||||
TextReader tr = new StreamReader(saveDir + "grief.txt");
|
||||
string list = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
|
||||
return list.Contains(ip);
|
||||
}
|
||||
|
||||
public static bool CheckCheat(String ip)
|
||||
{
|
||||
ip = GetRealIP(ip);
|
||||
if (!banCheater) { return false; }
|
||||
TextReader tr = new StreamReader(saveDir + "cheaters.txt");
|
||||
string trr = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (trr.Contains(ip))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool CheckBanned(String p)
|
||||
{
|
||||
String ip = p.Split(':')[0];
|
||||
TextReader tr = new StreamReader(saveDir + "bans.txt");
|
||||
string banlist = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
banlist = banlist.Trim();
|
||||
if (banlist.Contains(ip))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void KeepTilesUpToDate()
|
||||
{
|
||||
TextReader tr = new StreamReader(saveDir + "tiles.txt");
|
||||
string file = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (!file.Contains("0x3d"))
|
||||
{
|
||||
System.IO.File.Delete(saveDir + "tiles.txt");
|
||||
CreateFile(saveDir + "tiles.txt");
|
||||
TextWriter tw = new StreamWriter(saveDir + "tiles.txt");
|
||||
tw.Write("0x03, 0x05, 0x14, 0x25, 0x18, 0x18, 0x20, 0x1b, 0x34, 0x48, 0x33, 0x3d, 0x47, 0x49, 0x4a, 0x35, 0x3d, 0x3e, 0x45, 0x47, 0x49, 0x4a,");
|
||||
tw.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetupConfig()
|
||||
{
|
||||
if (!System.IO.Directory.Exists(saveDir)) { System.IO.Directory.CreateDirectory(saveDir); }
|
||||
if (!System.IO.File.Exists(saveDir + "tiles.txt"))
|
||||
{
|
||||
CreateFile(saveDir + "tiles.txt");
|
||||
TextWriter tw = new StreamWriter(saveDir + "tiles.txt");
|
||||
tw.Write("0x03, 0x05, 0x14, 0x25, 0x18, 0x18, 0x20, 0x1b, 0x34, 0x48, 0x33, 0x3d, 0x47, 0x49, 0x4a, 0x35, 0x3d, 0x3e, 0x45, 0x47, 0x49, 0x4a,");
|
||||
tw.Close();
|
||||
}
|
||||
if (!System.IO.File.Exists(saveDir + "motd.txt"))
|
||||
{
|
||||
CreateFile(saveDir + "motd.txt");
|
||||
TextWriter tw = new StreamWriter(saveDir + "motd.txt");
|
||||
tw.WriteLine("This server is running TShock. Type /help for a list of commands.");
|
||||
tw.WriteLine("%255,000,000%Current map: %map%");
|
||||
tw.WriteLine("Current players: %players%");
|
||||
tw.Close();
|
||||
}
|
||||
if (!System.IO.File.Exists(saveDir + "bans.txt")) { CreateFile(saveDir + "bans.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "cheaters.txt")) { CreateFile(saveDir + "cheaters.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "admins.txt")) { CreateFile(saveDir + "admins.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "grief.txt")) { CreateFile(saveDir + "grief.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "whitelist.txt")) { CreateFile(saveDir + "whitelist.txt"); }
|
||||
if (!System.IO.File.Exists(saveDir + "config.txt"))
|
||||
{
|
||||
CreateFile(saveDir + "config.txt");
|
||||
TextWriter tw = new StreamWriter(saveDir + "config.txt");
|
||||
tw.WriteLine("true,50,4,700,true,true,7777,false,false,false,false,false");
|
||||
tw.Close();
|
||||
}
|
||||
KeepTilesUpToDate();
|
||||
TextReader tr = new StreamReader(saveDir + "config.txt");
|
||||
string config = tr.ReadToEnd();
|
||||
config = config.Replace("\n", "");
|
||||
config = config.Replace("\r", "");
|
||||
config = config.Replace(" ", "");
|
||||
tr.Close();
|
||||
string[] configuration = config.Split(',');
|
||||
try
|
||||
{
|
||||
killGuide = Convert.ToBoolean(configuration[0]);
|
||||
invasionMultiplier = Convert.ToInt32(configuration[1]);
|
||||
defaultMaxSpawns = Convert.ToInt32(configuration[2]);
|
||||
defaultSpawnRate = Convert.ToInt32(configuration[3]);
|
||||
kickCheater = Convert.ToBoolean(configuration[4]);
|
||||
banCheater = Convert.ToBoolean(configuration[5]);
|
||||
serverPort = Convert.ToInt32(configuration[6]);
|
||||
enableWhitelist = Convert.ToBoolean(configuration[7]);
|
||||
infiniteInvasion = Convert.ToBoolean(configuration[8]);
|
||||
permaPvp = Convert.ToBoolean(configuration[9]);
|
||||
kickTnt = Convert.ToBoolean(configuration[10]);
|
||||
banTnt = Convert.ToBoolean(configuration[11]);
|
||||
if (infiniteInvasion)
|
||||
{
|
||||
//Main.startInv();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(e.Message);
|
||||
}
|
||||
|
||||
Netplay.serverPort = serverPort;
|
||||
}
|
||||
|
||||
public static void Kick(int ply, string reason)
|
||||
{
|
||||
NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f);
|
||||
Netplay.serverSock[ply].kill = true;
|
||||
NetMessage.syncPlayers();
|
||||
}
|
||||
|
||||
public static bool IsAdmin(string ply)
|
||||
{
|
||||
string remoteEndPoint = Convert.ToString((Netplay.serverSock[FindPlayer(ply)].tcpClient.Client.RemoteEndPoint));
|
||||
string[] remoteEndPointIP = remoteEndPoint.Split(':');
|
||||
TextReader tr = new StreamReader(saveDir + "admins.txt");
|
||||
string adminlist = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (adminlist.Contains(remoteEndPointIP[0]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsAdmin(int ply)
|
||||
{
|
||||
string remoteEndPoint = Convert.ToString((Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
|
||||
string[] remoteEndPointIP = remoteEndPoint.Split(':');
|
||||
TextReader tr = new StreamReader(saveDir + "admins.txt");
|
||||
string adminlist = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (adminlist.Contains(remoteEndPointIP[0]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int FindPlayer(string ply)
|
||||
{
|
||||
int pl = -1;
|
||||
for (int i = 0; i < Main.player.Length; i++)
|
||||
{
|
||||
if ((ply.ToLower()) == Main.player[i].name.ToLower())
|
||||
{
|
||||
pl = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pl;
|
||||
}
|
||||
|
||||
public static void HandleCheater(int ply)
|
||||
{
|
||||
string cheater = FindPlayer(ply);
|
||||
string ip = GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
|
||||
|
||||
WriteGrief(ply);
|
||||
WriteCheater(ply);
|
||||
if (!kickCheater) { return; }
|
||||
Netplay.serverSock[ply].kill = true;
|
||||
Netplay.serverSock[ply].Reset();
|
||||
NetMessage.syncPlayers();
|
||||
Broadcast(cheater + " was " + (banCheater ? "banned " : "kicked ") + "for cheating.");
|
||||
|
||||
}
|
||||
|
||||
public static string FindPlayer(int ply)
|
||||
{
|
||||
for (int i = 0; i < Main.player.Length; i++)
|
||||
{
|
||||
if (i == ply)
|
||||
{
|
||||
return Main.player[i].name;
|
||||
}
|
||||
}
|
||||
return "null";
|
||||
}
|
||||
|
||||
public static void Broadcast(string msg)
|
||||
{
|
||||
for (int i = 0; i < Main.player.Length; i++)
|
||||
{
|
||||
SendMessage(i, msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetRealIP(string mess)
|
||||
{
|
||||
return mess.Split(':')[0];
|
||||
}
|
||||
|
||||
public static void SendMessage(int ply, string msg, float[] color)
|
||||
{
|
||||
NetMessage.SendData(0x19, ply, -1, msg, 8, color[0], color[1], color[2]);
|
||||
}
|
||||
|
||||
public static void SendMessage(int ply, string message)
|
||||
{
|
||||
NetMessage.SendData(0x19, ply, -1, message, 8, 0f, 255f, 0f);
|
||||
}
|
||||
|
||||
private static void WriteGrief(int ply)
|
||||
{
|
||||
TextWriter tw = new StreamWriter(saveDir + "grief.txt", true);
|
||||
tw.WriteLine("[" + Main.player[ply].name + "] [" + GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()) + "]");
|
||||
tw.Close();
|
||||
}
|
||||
|
||||
private static void WriteError(string err)
|
||||
{
|
||||
if (System.IO.File.Exists(saveDir + "errors.txt"))
|
||||
{
|
||||
TextWriter tw = new StreamWriter(saveDir + "errors.txt", true);
|
||||
tw.WriteLine(err);
|
||||
tw.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
CreateFile(saveDir + "errors.txt");
|
||||
TextWriter tw = new StreamWriter(saveDir + "errors.txt", true);
|
||||
tw.WriteLine(err);
|
||||
tw.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteBan(int ply)
|
||||
{
|
||||
string ip = GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
|
||||
TextWriter tw = new StreamWriter(saveDir + "bans.txt", true);
|
||||
tw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]");
|
||||
tw.Close();
|
||||
}
|
||||
|
||||
private static void CreateFile(string file)
|
||||
{
|
||||
using (FileStream fs = File.Create(file)) { }
|
||||
}
|
||||
|
||||
public static void ShowMOTD(int ply)
|
||||
{
|
||||
string foo = "";
|
||||
TextReader tr = new StreamReader(saveDir + "motd.txt");
|
||||
while ((foo = tr.ReadLine()) != null)
|
||||
{
|
||||
foo = foo.Replace("%map%", Main.worldName);
|
||||
foo = foo.Replace("%players%", GetPlayers());
|
||||
if (foo.Substring(0, 1) == "%" && foo.Substring(12, 1) == "%") //Look for a beginning color code.
|
||||
{
|
||||
string possibleColor = foo.Substring(0, 13);
|
||||
foo = foo.Remove(0, 13);
|
||||
float[] pC = { 0, 0, 0 };
|
||||
possibleColor = possibleColor.Replace("%", "");
|
||||
string[] pCc = possibleColor.Split(',');
|
||||
if (pCc.Length == 3)
|
||||
{
|
||||
try
|
||||
{
|
||||
pC[0] = Clamp(Convert.ToInt32(pCc[0]), 255, 0);
|
||||
pC[1] = Clamp(Convert.ToInt32(pCc[1]), 255, 0);
|
||||
pC[2] = Clamp(Convert.ToInt32(pCc[2]), 255, 0);
|
||||
SendMessage(ply, foo, pC);
|
||||
continue;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WriteError(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
SendMessage(ply, foo);
|
||||
}
|
||||
tr.Close();
|
||||
}
|
||||
|
||||
public static T Clamp<T>(T value, T max, T min)
|
||||
where T : System.IComparable<T>
|
||||
{
|
||||
T result = value;
|
||||
if (value.CompareTo(max) > 0)
|
||||
result = max;
|
||||
if (value.CompareTo(min) < 0)
|
||||
result = min;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void WriteCheater(int ply)
|
||||
{
|
||||
string ip = GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
|
||||
string cheaters = "";
|
||||
TextReader tr = new StreamReader(saveDir + "cheaters.txt");
|
||||
cheaters = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (cheaters.Contains(Main.player[ply].name) && cheaters.Contains(ip)) { return; }
|
||||
TextWriter sw = new StreamWriter(saveDir + "cheaters.txt", true);
|
||||
sw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]");
|
||||
sw.Close();
|
||||
}
|
||||
|
||||
private static string GetPlayers()
|
||||
{
|
||||
string str = "";
|
||||
for (int i = 0; i < Main.maxPlayers; i++)
|
||||
{
|
||||
if (Main.player[i].active)
|
||||
{
|
||||
if (str == "")
|
||||
{
|
||||
str = str + Main.player[i].name;
|
||||
}
|
||||
else
|
||||
{
|
||||
str = str + ", " + Main.player[i].name;
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
public static void NewNPC(int type, int x, int y, int target)
|
||||
{
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case 0: //World Eater
|
||||
WorldGen.shadowOrbSmashed = true;
|
||||
WorldGen.shadowOrbCount = 3;
|
||||
int w = NPC.NewNPC(x, y, 13, 1);
|
||||
Main.npc[w].target = target;
|
||||
break;
|
||||
case 1: //Eye
|
||||
Main.time = 4861;
|
||||
Main.dayTime = false;
|
||||
WorldGen.spawnEye = true;
|
||||
break;
|
||||
case 2: //Skeletron
|
||||
int enpeecee = NPC.NewNPC(x, y, 0x23, 0);
|
||||
Main.npc[enpeecee].netUpdate = true;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static bool TileOnWhitelist(byte tile)
|
||||
{
|
||||
int _tile = (int)tile;
|
||||
TextReader tr2 = new StreamReader(saveDir + "tiles.txt");
|
||||
tileWhitelist = tr2.ReadToEnd(); tr2.Close();
|
||||
string hexValue = _tile.ToString("X");
|
||||
if (hexValue == "0")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Console.WriteLine(hexValue);
|
||||
return tileWhitelist.Contains(hexValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -49,6 +49,9 @@
|
|||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ConfigurationManager.cs" />
|
||||
<Compile Include="FileTools.cs" />
|
||||
<Compile Include="Tools.cs" />
|
||||
<Compile Include="TShock.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
279
TShockAPI/Tools.cs
Normal file
279
TShockAPI/Tools.cs
Normal file
|
|
@ -0,0 +1,279 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using Terraria;
|
||||
|
||||
namespace TShockAPI
|
||||
{
|
||||
class Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides the real IP address from a RemoteEndPoint string that contains a port and an IP
|
||||
/// </summary>
|
||||
/// <param name="mess">A string IPv4 address in IP:PORT form.</param>
|
||||
/// <returns>A string IPv4 address.</returns>
|
||||
public static string GetRealIP(string mess)
|
||||
{
|
||||
return mess.Split(':')[0];
|
||||
}
|
||||
/// <summary>
|
||||
/// Used for some places where a list of players might be used.
|
||||
/// </summary>
|
||||
/// <returns>String of players seperated by commas.</returns>
|
||||
public static string GetPlayers()
|
||||
{
|
||||
string str = "";
|
||||
for (int i = 0; i < Main.maxPlayers; i++)
|
||||
{
|
||||
if (Main.player[i].active)
|
||||
{
|
||||
if (str == "")
|
||||
{
|
||||
str = str + Main.player[i].name;
|
||||
}
|
||||
else
|
||||
{
|
||||
str = str + ", " + Main.player[i].name;
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
/// <summary>
|
||||
/// It's a clamp function
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="value">Value to clamp</param>
|
||||
/// <param name="max">Maximum bounds of the clamp</param>
|
||||
/// <param name="min">Minimum bounds of the clamp</param>
|
||||
/// <returns></returns>
|
||||
public static T Clamp<T>(T value, T max, T min)
|
||||
where T : System.IComparable<T>
|
||||
{
|
||||
T result = value;
|
||||
if (value.CompareTo(max) > 0)
|
||||
result = max;
|
||||
if (value.CompareTo(min) < 0)
|
||||
result = min;
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Broadcasts a message to all players
|
||||
/// </summary>
|
||||
/// <param name="msg">string message</param>
|
||||
public static void Broadcast(string msg)
|
||||
{
|
||||
for (int i = 0; i < Main.player.Length; i++)
|
||||
{
|
||||
SendMessage(i, msg);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Sends a message out to a single player
|
||||
/// </summary>
|
||||
/// <param name="ply">int socket thingy for the player from the server socket</param>
|
||||
/// <param name="msg">String message</param>
|
||||
/// <param name="color">Float containing red, blue, and green color values</param>
|
||||
public static void SendMessage(int ply, string msg, float[] color)
|
||||
{
|
||||
NetMessage.SendData(0x19, ply, -1, msg, 8, color[0], color[1], color[2]);
|
||||
}
|
||||
/// <summary>
|
||||
/// Sends a green message to a player
|
||||
/// </summary>
|
||||
/// <param name="ply">int socket thingy for the player from the server socket</param>
|
||||
/// <param name="message">string message</param>
|
||||
public static void SendMessage(int ply, string message)
|
||||
{
|
||||
NetMessage.SendData(0x19, ply, -1, message, 8, 0f, 255f, 0f);
|
||||
}
|
||||
/// <summary>
|
||||
/// The number of active players on the server.
|
||||
/// </summary>
|
||||
/// <returns>int playerCount</returns>
|
||||
public static int activePlayers()
|
||||
{
|
||||
int num = 0;
|
||||
for (int i = 0; i < Main.maxPlayers; i++)
|
||||
{
|
||||
if (Main.player[i].active)
|
||||
{
|
||||
num++;
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
/// <summary>
|
||||
/// Finds the name of the player of the int given
|
||||
/// </summary>
|
||||
/// <param name="ply">string player name</param>
|
||||
/// <returns>int player</returns>
|
||||
public static int FindPlayer(string ply)
|
||||
{
|
||||
int pl = -1;
|
||||
for (int i = 0; i < Main.player.Length; i++)
|
||||
{
|
||||
if ((ply.ToLower()) == Main.player[i].name.ToLower())
|
||||
{
|
||||
pl = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pl;
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets the given player's name
|
||||
/// </summary>
|
||||
/// <param name="ply">int player</param>
|
||||
/// <returns>string name</returns>
|
||||
public static string FindPlayer(int ply)
|
||||
{
|
||||
for (int i = 0; i < Main.player.Length; i++)
|
||||
{
|
||||
if (i == ply)
|
||||
{
|
||||
return Main.player[i].name;
|
||||
}
|
||||
}
|
||||
return "null";
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates an NPC
|
||||
/// </summary>
|
||||
/// <param name="type">Type is defined in the enum NPC list</param>
|
||||
/// <param name="x">X coord of the desired npc</param>
|
||||
/// <param name="y">Y coord of the desired npc</param>
|
||||
/// <param name="target">int player that the npc targets</param>
|
||||
public static void NewNPC(int type, int x, int y, int target)
|
||||
{
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case 0: //World Eater
|
||||
WorldGen.shadowOrbSmashed = true;
|
||||
WorldGen.shadowOrbCount = 3;
|
||||
int w = NPC.NewNPC(x, y, 13, 1);
|
||||
Main.npc[w].target = target;
|
||||
break;
|
||||
case 1: //Eye
|
||||
Main.time = 4861;
|
||||
Main.dayTime = false;
|
||||
WorldGen.spawnEye = true;
|
||||
break;
|
||||
case 2: //Skeletron
|
||||
int enpeecee = NPC.NewNPC(x, y, 0x23, 0);
|
||||
Main.npc[enpeecee].netUpdate = true;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Finds a player, reads admins.txt, and determines if their IP address is on that list.
|
||||
/// </summary>
|
||||
/// <param name="ply">int player</param>
|
||||
/// <returns>true/false</returns>
|
||||
public static bool IsAdmin(int ply)
|
||||
{
|
||||
string remoteEndPoint = Convert.ToString((Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
|
||||
string[] remoteEndPointIP = remoteEndPoint.Split(':');
|
||||
TextReader tr = new StreamReader(FileTools.saveDir + "admins.txt");
|
||||
string adminlist = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (adminlist.Contains(remoteEndPointIP[0]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Finds a player based on their name, reads admins.txt, and determines if thier IP address is on that list.
|
||||
/// </summary>
|
||||
/// <param name="ply"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAdmin(string ply)
|
||||
{
|
||||
string remoteEndPoint = Convert.ToString((Netplay.serverSock[Tools.FindPlayer(ply)].tcpClient.Client.RemoteEndPoint));
|
||||
string[] remoteEndPointIP = remoteEndPoint.Split(':');
|
||||
TextReader tr = new StreamReader(FileTools.saveDir + "admins.txt");
|
||||
string adminlist = tr.ReadToEnd();
|
||||
tr.Close();
|
||||
if (adminlist.Contains(remoteEndPointIP[0]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Kicks a player from the server.
|
||||
/// </summary>
|
||||
/// <param name="ply">int player</param>
|
||||
/// <param name="reason">string reason</param>
|
||||
public static void Kick(int ply, string reason)
|
||||
{
|
||||
NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f);
|
||||
Netplay.serverSock[ply].kill = true;
|
||||
NetMessage.syncPlayers();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines what to do with someone listed in cheaters.txt
|
||||
/// </summary>
|
||||
/// <param name="ply">int player</param>
|
||||
public static void HandleCheater(int ply)
|
||||
{
|
||||
string cheater = Tools.FindPlayer(ply);
|
||||
string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
|
||||
|
||||
FileTools.WriteGrief(ply);
|
||||
FileTools.WriteCheater(ply);
|
||||
if (!ConfigurationManager.kickCheater) { return; }
|
||||
Netplay.serverSock[ply].kill = true;
|
||||
Netplay.serverSock[ply].Reset();
|
||||
NetMessage.syncPlayers();
|
||||
Tools.Broadcast(cheater + " was " + (ConfigurationManager.banCheater ? "banned " : "kicked ") + "for cheating.");
|
||||
}
|
||||
/// <summary>
|
||||
/// Shows a MOTD to the player
|
||||
/// </summary>
|
||||
/// <param name="ply">int player</param>
|
||||
public static void ShowMOTD(int ply)
|
||||
{
|
||||
string foo = "";
|
||||
TextReader tr = new StreamReader(FileTools.saveDir + "motd.txt");
|
||||
while ((foo = tr.ReadLine()) != null)
|
||||
{
|
||||
foo = foo.Replace("%map%", Main.worldName);
|
||||
foo = foo.Replace("%players%", Tools.GetPlayers());
|
||||
if (foo.Substring(0, 1) == "%" && foo.Substring(12, 1) == "%") //Look for a beginning color code.
|
||||
{
|
||||
string possibleColor = foo.Substring(0, 13);
|
||||
foo = foo.Remove(0, 13);
|
||||
float[] pC = { 0, 0, 0 };
|
||||
possibleColor = possibleColor.Replace("%", "");
|
||||
string[] pCc = possibleColor.Split(',');
|
||||
if (pCc.Length == 3)
|
||||
{
|
||||
try
|
||||
{
|
||||
pC[0] = Tools.Clamp(Convert.ToInt32(pCc[0]), 255, 0);
|
||||
pC[1] = Tools.Clamp(Convert.ToInt32(pCc[1]), 255, 0);
|
||||
pC[2] = Tools.Clamp(Convert.ToInt32(pCc[2]), 255, 0);
|
||||
Tools.SendMessage(ply, foo, pC);
|
||||
continue;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
FileTools.WriteError(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Tools.SendMessage(ply, foo);
|
||||
}
|
||||
tr.Close();
|
||||
}
|
||||
public Tools() { }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue