Fixed kick, SendData sets kill when sending packet 0x2. Also when a socket is killed players are resynced.

This commit is contained in:
high 2011-06-03 02:34:25 -04:00
parent 1e1216f090
commit cf736cbb30

View file

@ -1,301 +1,307 @@
using System; using System;
using System.Collections.Generic; using System.IO;
using System.Linq; using Terraria;
using System.Text;
using System.IO; namespace TShockAPI
using Terraria; {
class Tools
namespace TShockAPI {
{ /// <summary>
class Tools /// Provides the real IP address from a RemoteEndPoint string that contains a port and an IP
{ /// </summary>
/// <summary> /// <param name="mess">A string IPv4 address in IP:PORT form.</param>
/// Provides the real IP address from a RemoteEndPoint string that contains a port and an IP /// <returns>A string IPv4 address.</returns>
/// </summary> public static string GetRealIP(string mess)
/// <param name="mess">A string IPv4 address in IP:PORT form.</param> {
/// <returns>A string IPv4 address.</returns> return mess.Split(':')[0];
public static string GetRealIP(string mess) }
{
return mess.Split(':')[0]; /// <summary>
} /// Used for some places where a list of players might be used.
/// <summary> /// </summary>
/// Used for some places where a list of players might be used. /// <returns>String of players seperated by commas.</returns>
/// </summary> public static string GetPlayers()
/// <returns>String of players seperated by commas.</returns> {
public static string GetPlayers() string str = "";
{ for (int i = 0; i < Main.maxPlayers; i++)
string str = ""; {
for (int i = 0; i < Main.maxPlayers; i++) if (Main.player[i].active)
{ {
if (Main.player[i].active) if (str == "")
{ {
if (str == "") str = str + Main.player[i].name;
{ }
str = str + Main.player[i].name; else
} {
else str = str + ", " + Main.player[i].name;
{ }
str = str + ", " + Main.player[i].name; }
} }
} return str;
} }
return str;
} /// <summary>
/// <summary> /// It's a clamp function
/// It's a clamp function /// </summary>
/// </summary> /// <typeparam name="T"></typeparam>
/// <typeparam name="T"></typeparam> /// <param name="value">Value to clamp</param>
/// <param name="value">Value to clamp</param> /// <param name="max">Maximum bounds of the clamp</param>
/// <param name="max">Maximum bounds of the clamp</param> /// <param name="min">Minimum bounds of the clamp</param>
/// <param name="min">Minimum bounds of the clamp</param> /// <returns></returns>
/// <returns></returns> public static T Clamp<T>(T value, T max, T min)
public static T Clamp<T>(T value, T max, T min) where T : System.IComparable<T>
where T : System.IComparable<T> {
{ T result = value;
T result = value; if (value.CompareTo(max) > 0)
if (value.CompareTo(max) > 0) result = max;
result = max; if (value.CompareTo(min) < 0)
if (value.CompareTo(min) < 0) result = min;
result = min; return result;
return result; }
}
/// <summary> /// <summary>
/// Broadcasts a message to all players /// Broadcasts a message to all players
/// </summary> /// </summary>
/// <param name="msg">string message</param> /// <param name="msg">string message</param>
public static void Broadcast(string msg) public static void Broadcast(string msg)
{ {
for (int i = 0; i < Main.player.Length; i++) for (int i = 0; i < Main.player.Length; i++)
{ {
SendMessage(i, msg); SendMessage(i, msg);
} }
} }
/// <summary>
/// Sends a message out to a single player /// <summary>
/// </summary> /// Sends a message out to a single player
/// <param name="ply">int socket thingy for the player from the server socket</param> /// </summary>
/// <param name="msg">String message</param> /// <param name="ply">int socket thingy for the player from the server socket</param>
/// <param name="color">Float containing red, blue, and green color values</param> /// <param name="msg">String message</param>
public static void SendMessage(int ply, string msg, float[] color) /// <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, 255, color[0], color[1], color[2]); {
} NetMessage.SendData(0x19, ply, -1, msg, 255, color[0], color[1], color[2]);
/// <summary> }
/// Sends a green message to a player
/// </summary> /// <summary>
/// <param name="ply">int socket thingy for the player from the server socket</param> /// Sends a green message to a player
/// <param name="message">string message</param> /// </summary>
public static void SendMessage(int ply, string message) /// <param name="ply">int socket thingy for the player from the server socket</param>
{ /// <param name="message">string message</param>
NetMessage.SendData(0x19, ply, -1, message, 255, 0f, 255f, 0f); public static void SendMessage(int ply, string message)
} {
/// <summary> NetMessage.SendData(0x19, ply, -1, message, 255, 0f, 255f, 0f);
/// The number of active players on the server. }
/// </summary>
/// <returns>int playerCount</returns> /// <summary>
public static int activePlayers() /// The number of active players on the server.
{ /// </summary>
int num = 0; /// <returns>int playerCount</returns>
for (int i = 0; i < Main.maxPlayers; i++) public static int activePlayers()
{ {
if (Main.player[i].active) int num = 0;
{ for (int i = 0; i < Main.maxPlayers; i++)
num++; {
} if (Main.player[i].active)
} {
return num; num++;
} }
/// <summary> }
/// Finds the name of the player of the int given return num;
/// </summary> }
/// <param name="ply">string player name</param>
/// <returns>int player</returns> /// <summary>
public static int FindPlayer(string ply) /// Finds the name of the player of the int given
{ /// </summary>
int pl = -1; /// <param name="ply">string player name</param>
for (int i = 0; i < Main.player.Length; i++) /// <returns>int player</returns>
{ public static int FindPlayer(string ply)
if ((ply.ToLower()) == Main.player[i].name.ToLower()) {
{ int pl = -1;
pl = i; for (int i = 0; i < Main.player.Length; i++)
break; {
} if ((ply.ToLower()) == Main.player[i].name.ToLower())
} {
return pl; pl = i;
} break;
/// <summary> }
/// Gets the given player's name }
/// </summary> return pl;
/// <param name="ply">int player</param> }
/// <returns>string name</returns>
public static string FindPlayer(int ply) /// <summary>
{ /// Gets the given player's name
for (int i = 0; i < Main.player.Length; i++) /// </summary>
{ /// <param name="ply">int player</param>
if (i == ply) /// <returns>string name</returns>
{ public static string FindPlayer(int ply)
return Main.player[i].name; {
} for (int i = 0; i < Main.player.Length; i++)
} {
return "null"; if (i == ply)
} {
/// <summary> return Main.player[i].name;
/// Creates an NPC }
/// </summary> }
/// <param name="type">Type is defined in the enum NPC list</param> return "null";
/// <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> /// <summary>
public static void NewNPC(int type, int x, int y, int target) /// Creates an NPC
{ /// </summary>
/// <param name="type">Type is defined in the enum NPC list</param>
switch (type) /// <param name="x">X coord of the desired npc</param>
{ /// <param name="y">Y coord of the desired npc</param>
case 0: //World Eater /// <param name="target">int player that the npc targets</param>
WorldGen.shadowOrbSmashed = true; public static void NewNPC(int type, int x, int y, int target)
WorldGen.shadowOrbCount = 3; {
int w = NPC.NewNPC(x, y, 13, 1); switch (type)
Main.npc[w].target = target; {
break; case 0: //World Eater
case 1: //Eye WorldGen.shadowOrbSmashed = true;
Main.time = 4861; WorldGen.shadowOrbCount = 3;
Main.dayTime = false; int w = NPC.NewNPC(x, y, 13, 1);
WorldGen.spawnEye = true; Main.npc[w].target = target;
break; break;
case 2: //Skeletron case 1: //Eye
int enpeecee = NPC.NewNPC(x, y, 0x23, 0); Main.time = 4861;
Main.npc[enpeecee].netUpdate = true; Main.dayTime = false;
break; WorldGen.spawnEye = true;
break;
} case 2: //Skeletron
int enpeecee = NPC.NewNPC(x, y, 0x23, 0);
} Main.npc[enpeecee].netUpdate = true;
/// <summary> break;
/// 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> /// <summary>
public static bool IsAdmin(int ply) /// Finds a player, reads admins.txt, and determines if their IP address is on that list.
{ /// </summary>
string remoteEndPoint = Convert.ToString((Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); /// <param name="ply">int player</param>
string[] remoteEndPointIP = remoteEndPoint.Split(':'); /// <returns>true/false</returns>
TextReader tr = new StreamReader(FileTools.SaveDir + "admins.txt"); public static bool IsAdmin(int ply)
string adminlist = tr.ReadToEnd(); {
tr.Close(); string remoteEndPoint = Convert.ToString((Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
if (adminlist.Contains(remoteEndPointIP[0])) string[] remoteEndPointIP = remoteEndPoint.Split(':');
{ TextReader tr = new StreamReader(FileTools.SaveDir + "admins.txt");
return true; string adminlist = tr.ReadToEnd();
} tr.Close();
return false; if (adminlist.Contains(remoteEndPointIP[0]))
} {
/// <summary> return true;
/// Finds a player based on their name, reads admins.txt, and determines if thier IP address is on that list. }
/// </summary> return false;
/// <param name="ply"></param> }
/// <returns></returns>
public static bool IsAdmin(string ply) /// <summary>
{ /// Finds a player based on their name, reads admins.txt, and determines if thier IP address is on that list.
string remoteEndPoint = Convert.ToString((Netplay.serverSock[Tools.FindPlayer(ply)].tcpClient.Client.RemoteEndPoint)); /// </summary>
string[] remoteEndPointIP = remoteEndPoint.Split(':'); /// <param name="ply"></param>
TextReader tr = new StreamReader(FileTools.SaveDir + "admins.txt"); /// <returns></returns>
string adminlist = tr.ReadToEnd(); public static bool IsAdmin(string ply)
tr.Close(); {
if (adminlist.Contains(remoteEndPointIP[0])) string remoteEndPoint = Convert.ToString((Netplay.serverSock[Tools.FindPlayer(ply)].tcpClient.Client.RemoteEndPoint));
{ string[] remoteEndPointIP = remoteEndPoint.Split(':');
return true; TextReader tr = new StreamReader(FileTools.SaveDir + "admins.txt");
} string adminlist = tr.ReadToEnd();
return false; tr.Close();
} if (adminlist.Contains(remoteEndPointIP[0]))
/// <summary> {
/// Kicks a player from the server. return true;
/// </summary> }
/// <param name="ply">int player</param> return false;
/// <param name="reason">string reason</param> }
public static void Kick(int ply, string reason)
{ /// <summary>
NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f); /// Kicks a player from the server.
Netplay.serverSock[ply].kill = true; /// </summary>
NetMessage.syncPlayers(); /// <param name="ply">int player</param>
} /// <param name="reason">string reason</param>
public static void Kick(int ply, string reason)
/// <summary> {
/// Adds someone to cheaters.txt NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f);
/// </summary> }
/// <param name="ply">int player</param>
public static void HandleCheater(int ply) /// <summary>
{ /// Adds someone to cheaters.txt
if (!TShock.players[ply].IsAdmin()) /// </summary>
{ /// <param name="ply">int player</param>
string cheater = Tools.FindPlayer(ply); public static void HandleCheater(int ply)
string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); {
if (!TShock.players[ply].IsAdmin())
FileTools.WriteCheater(ply); {
if (!ConfigurationManager.kickCheater) { return; } string cheater = Tools.FindPlayer(ply);
Netplay.serverSock[ply].kill = true; string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
Netplay.serverSock[ply].Reset();
NetMessage.syncPlayers(); FileTools.WriteCheater(ply);
Tools.Broadcast(cheater + " was " + (ConfigurationManager.banCheater ? "banned " : "kicked ") + "for cheating."); if (!ConfigurationManager.kickCheater) { return; }
} Netplay.serverSock[ply].kill = true;
} Netplay.serverSock[ply].Reset();
NetMessage.syncPlayers();
/// <summary> Tools.Broadcast(cheater + " was " + (ConfigurationManager.banCheater ? "banned " : "kicked ") + "for cheating.");
/// Adds someone to greifers.txt }
/// </summary> }
/// <param name="ply">int player</param>
public static void HandleGreifer(int ply) /// <summary>
{ /// Adds someone to greifers.txt
if (!TShock.players[ply].IsAdmin()) /// </summary>
{ /// <param name="ply">int player</param>
string cheater = Tools.FindPlayer(ply); public static void HandleGreifer(int ply)
string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); {
if (!TShock.players[ply].IsAdmin())
FileTools.WriteGrief(ply); {
if (!ConfigurationManager.kickGriefer) { return; } string cheater = Tools.FindPlayer(ply);
Netplay.serverSock[ply].kill = true; string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint));
Netplay.serverSock[ply].Reset();
NetMessage.syncPlayers(); FileTools.WriteGrief(ply);
Tools.Broadcast(cheater + " was " + (ConfigurationManager.banCheater ? "banned " : "kicked ") + "for greifing."); if (!ConfigurationManager.kickGriefer) { return; }
} Netplay.serverSock[ply].kill = true;
} Netplay.serverSock[ply].Reset();
/// <summary> NetMessage.syncPlayers();
/// Shows a MOTD to the player Tools.Broadcast(cheater + " was " + (ConfigurationManager.banCheater ? "banned " : "kicked ") + "for greifing.");
/// </summary> }
/// <param name="ply">int player</param> }
public static void ShowMOTD(int ply)
{ /// <summary>
string foo = ""; /// Shows a MOTD to the player
TextReader tr = new StreamReader(FileTools.SaveDir + "motd.txt"); /// </summary>
while ((foo = tr.ReadLine()) != null) /// <param name="ply">int player</param>
{ public static void ShowMOTD(int ply)
foo = foo.Replace("%map%", Main.worldName); {
foo = foo.Replace("%players%", Tools.GetPlayers()); string foo = "";
if (foo.Substring(0, 1) == "%" && foo.Substring(12, 1) == "%") //Look for a beginning color code. TextReader tr = new StreamReader(FileTools.SaveDir + "motd.txt");
{ while ((foo = tr.ReadLine()) != null)
string possibleColor = foo.Substring(0, 13); {
foo = foo.Remove(0, 13); foo = foo.Replace("%map%", Main.worldName);
float[] pC = { 0, 0, 0 }; foo = foo.Replace("%players%", Tools.GetPlayers());
possibleColor = possibleColor.Replace("%", ""); if (foo.Substring(0, 1) == "%" && foo.Substring(12, 1) == "%") //Look for a beginning color code.
string[] pCc = possibleColor.Split(','); {
if (pCc.Length == 3) string possibleColor = foo.Substring(0, 13);
{ foo = foo.Remove(0, 13);
try float[] pC = { 0, 0, 0 };
{ possibleColor = possibleColor.Replace("%", "");
pC[0] = Tools.Clamp(Convert.ToInt32(pCc[0]), 255, 0); string[] pCc = possibleColor.Split(',');
pC[1] = Tools.Clamp(Convert.ToInt32(pCc[1]), 255, 0); if (pCc.Length == 3)
pC[2] = Tools.Clamp(Convert.ToInt32(pCc[2]), 255, 0); {
Tools.SendMessage(ply, foo, pC); try
continue; {
} pC[0] = Tools.Clamp(Convert.ToInt32(pCc[0]), 255, 0);
catch (Exception e) pC[1] = Tools.Clamp(Convert.ToInt32(pCc[1]), 255, 0);
{ pC[2] = Tools.Clamp(Convert.ToInt32(pCc[2]), 255, 0);
FileTools.WriteError(e.Message); Tools.SendMessage(ply, foo, pC);
} continue;
} }
} catch (Exception e)
Tools.SendMessage(ply, foo); {
} FileTools.WriteError(e.Message);
tr.Close(); }
} }
public Tools() { } }
} Tools.SendMessage(ply, foo);
} }
tr.Close();
}
public Tools() { }
}
}