-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from WindFrost-CSFT/master
Add EssentialsPlus
- Loading branch information
Showing
16 changed files
with
2,063 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
|
||
namespace EssentialsPlus | ||
{ | ||
public class Config | ||
{ | ||
public string[] DisabledCommandsInPvp = new string[] | ||
{ | ||
"back" | ||
}; | ||
|
||
public int BackPositionHistory = 10; | ||
|
||
public string MySqlHost = ""; | ||
public string MySqlDbName = ""; | ||
public string MySqlUsername = ""; | ||
public string MySqlPassword = ""; | ||
|
||
public void Write(string path) | ||
{ | ||
File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented)); | ||
} | ||
|
||
public static Config Read(string path) | ||
{ | ||
return File.Exists(path) ? JsonConvert.DeserializeObject<Config>(File.ReadAllText(path)) : new Config(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace EssentialsPlus.Db | ||
{ | ||
public class Home | ||
{ | ||
public string Name { get; private set; } | ||
public int UserID { get; private set; } | ||
public float X { get; private set; } | ||
public float Y { get; private set; } | ||
|
||
public Home(int userId, string name, float x, float y) | ||
{ | ||
Name = name; | ||
UserID = userId; | ||
X = x; | ||
Y = y; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Threading.Tasks; | ||
using MySql.Data.MySqlClient; | ||
using Terraria; | ||
using TShockAPI; | ||
using TShockAPI.DB; | ||
|
||
namespace EssentialsPlus.Db | ||
{ | ||
public class HomeManager | ||
{ | ||
private IDbConnection db; | ||
private List<Home> homes = new List<Home>(); | ||
private object syncLock = new object(); | ||
|
||
public HomeManager(IDbConnection db) | ||
{ | ||
this.db = db; | ||
|
||
var sqlCreator = new SqlTableCreator(db, | ||
db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder) new SqliteQueryCreator() : new MysqlQueryCreator()); | ||
sqlCreator.EnsureTableStructure(new SqlTable("Homes", | ||
new SqlColumn("ID", MySqlDbType.Int32) {AutoIncrement = true, Primary = true}, | ||
new SqlColumn("UserID", MySqlDbType.Int32), | ||
new SqlColumn("Name", MySqlDbType.Text), | ||
new SqlColumn("X", MySqlDbType.Double), | ||
new SqlColumn("Y", MySqlDbType.Double), | ||
new SqlColumn("WorldID", MySqlDbType.Int32))); | ||
|
||
using (QueryResult result = db.QueryReader("SELECT * FROM Homes WHERE WorldID = @0", Main.worldID)) | ||
{ | ||
while (result.Read()) | ||
{ | ||
homes.Add(new Home( | ||
result.Get<int>("UserID"), | ||
result.Get<string>("Name"), | ||
result.Get<float>("X"), | ||
result.Get<float>("Y"))); | ||
} | ||
} | ||
} | ||
|
||
public async Task<bool> AddAsync(TSPlayer player, string name, float x, float y) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
try | ||
{ | ||
lock (syncLock) | ||
{ | ||
homes.Add(new Home(player.Account.ID, name, x, y)); | ||
return db.Query("INSERT INTO Homes (UserID, Name, X, Y, WorldID) VALUES (@0, @1, @2, @3, @4)", | ||
player.Account.ID, | ||
name, | ||
x, | ||
y, | ||
Main.worldID) > 0; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
public async Task<bool> DeleteAsync(TSPlayer player, string name) | ||
{ | ||
string query = db.GetSqlType() == SqlType.Mysql | ||
? "DELETE FROM Homes WHERE UserID = @0 AND Name = @1 AND WorldID = @2" | ||
: "DELETE FROM Homes WHERE UserID = @0 AND Name = @1 AND WorldID = @2 COLLATE NOCASE"; | ||
|
||
return await Task.Run(() => | ||
{ | ||
try | ||
{ | ||
lock (syncLock) | ||
{ | ||
homes.RemoveAll(h => h.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) | ||
&& h.UserID == player.Account.ID); | ||
return db.Query(query, player.Account.ID, name, Main.worldID) > 0; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
public async Task<Home> GetAsync(TSPlayer player, string name) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
lock (syncLock) | ||
{ | ||
return | ||
homes.Find(h => | ||
h.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) | ||
&& h.UserID == player.Account.ID); | ||
} | ||
}); | ||
} | ||
|
||
public async Task<List<Home>> GetAllAsync(TSPlayer player) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
lock (syncLock) | ||
{ | ||
return homes.FindAll(h => h.UserID == player.Account.ID); | ||
} | ||
}); | ||
} | ||
|
||
public async Task<bool> ReloadAsync() | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
try | ||
{ | ||
lock (syncLock) | ||
{ | ||
homes.Clear(); | ||
using (QueryResult result = db.QueryReader("SELECT * FROM Homes WHERE WorldID = @0", Main.worldID)) | ||
{ | ||
while (result.Read()) | ||
{ | ||
homes.Add(new Home( | ||
result.Get<int>("UserID"), | ||
result.Get<string>("Name"), | ||
result.Get<float>("X"), | ||
result.Get<float>("Y"))); | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
public async Task<bool> UpdateAsync(TSPlayer player, string name, float x, float y) | ||
{ | ||
string query = db.GetSqlType() == SqlType.Mysql | ||
? "UPDATE Homes SET X = @0, Y = @1 WHERE UserID = @2 AND Name = @3 AND WorldID = @4" | ||
: "UPDATE Homes SET X = @0, Y = @1 WHERE UserID = @2 AND Name = @3 AND WorldID = @4 COLLATE NOCASE"; | ||
|
||
return await Task.Run(() => | ||
{ | ||
try | ||
{ | ||
lock (syncLock) | ||
{ | ||
homes.RemoveAll(h => h.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) | ||
&& h.UserID == player.Account.ID); | ||
homes.Add(new Home(player.Account.ID, name, x, y)); | ||
return db.Query(query, x, y, player.Account.ID, name, Main.worldID) > 0; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return false; | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MySql.Data.MySqlClient; | ||
using Newtonsoft.Json; | ||
using TShockAPI; | ||
using TShockAPI.DB; | ||
|
||
namespace EssentialsPlus.Db | ||
{ | ||
public class MuteManager | ||
{ | ||
private IDbConnection db; | ||
private ReaderWriterLockSlim syncLock = new ReaderWriterLockSlim(); | ||
|
||
public MuteManager(IDbConnection db) | ||
{ | ||
this.db = db; | ||
|
||
var sqlCreator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite | ||
? (IQueryBuilder)new SqliteQueryCreator() | ||
: new MysqlQueryCreator()); | ||
|
||
sqlCreator.EnsureTableStructure(new SqlTable("Mutes", | ||
new SqlColumn("ID", MySqlDbType.Int32) { AutoIncrement = true, Primary = true }, | ||
new SqlColumn("Name", MySqlDbType.Text), | ||
new SqlColumn("UUID", MySqlDbType.Text), | ||
new SqlColumn("IP", MySqlDbType.Text), | ||
new SqlColumn("Date", MySqlDbType.Text), | ||
new SqlColumn("Expiration", MySqlDbType.Text))); | ||
} | ||
|
||
public async Task<bool> AddAsync(TSPlayer player, DateTime expiration) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
syncLock.EnterWriteLock(); | ||
|
||
try | ||
{ | ||
return db.Query("INSERT INTO Mutes VALUES (@0, @1, @2, @3, @4, @5)", | ||
null, | ||
player.Name, | ||
player.UUID, | ||
player.IP, | ||
DateTime.UtcNow.ToString("s"), | ||
expiration.ToString("s")) > 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return false; | ||
} | ||
finally | ||
{ | ||
syncLock.ExitWriteLock(); | ||
} | ||
}); | ||
} | ||
public async Task<bool> AddAsync(UserAccount user, DateTime expiration) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
syncLock.EnterWriteLock(); | ||
|
||
try | ||
{ | ||
return db.Query("INSERT INTO Mutes VALUES (@0, @1, @2, @3, @4, @5)", | ||
null, | ||
user.Name, | ||
user.UUID, | ||
JsonConvert.DeserializeObject<List<string>>(user.KnownIps)[0], | ||
DateTime.UtcNow.ToString("s"), | ||
expiration.ToString("s")) > 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return false; | ||
} | ||
finally | ||
{ | ||
syncLock.ExitWriteLock(); | ||
} | ||
}); | ||
} | ||
public async Task<bool> DeleteAsync(TSPlayer player) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
syncLock.EnterWriteLock(); | ||
string query = db.GetSqlType() == SqlType.Mysql ? | ||
"DELETE FROM Mutes WHERE UUID = @0 OR IP = @1 ORDER BY ID DESC LIMIT 1" : | ||
"DELETE FROM Mutes WHERE ID IN (SELECT ID FROM Mutes WHERE UUID = @0 OR IP = @1 ORDER BY ID DESC LIMIT 1)"; | ||
|
||
try | ||
{ | ||
return db.Query(query, player.UUID, player.IP) > 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return false; | ||
} | ||
finally | ||
{ | ||
syncLock.ExitWriteLock(); | ||
} | ||
}); | ||
} | ||
public async Task<bool> DeleteAsync(UserAccount user) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
syncLock.EnterWriteLock(); | ||
string query = db.GetSqlType() == SqlType.Mysql ? | ||
"DELETE FROM Mutes WHERE UUID = @0 OR IP = @1 ORDER BY ID DESC LIMIT 1" : | ||
"DELETE FROM Mutes WHERE ID IN (SELECT ID FROM Mutes WHERE UUID = @0 OR IP = @1 ORDER BY ID DESC LIMIT 1)"; | ||
|
||
try | ||
{ | ||
return db.Query(query, user.UUID, JsonConvert.DeserializeObject<List<string>>(user.KnownIps)[0]) > 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return false; | ||
} | ||
finally | ||
{ | ||
syncLock.ExitWriteLock(); | ||
} | ||
}); | ||
} | ||
public async Task<DateTime> GetExpirationAsync(TSPlayer player) | ||
{ | ||
return await Task.Run(() => | ||
{ | ||
syncLock.EnterReadLock(); | ||
|
||
try | ||
{ | ||
DateTime dateTime = DateTime.MinValue; | ||
using (QueryResult result = db.QueryReader("SELECT Expiration FROM Mutes WHERE UUID = @0 OR IP = @1 ORDER BY ID DESC", player.UUID, player.IP)) | ||
{ | ||
if (result.Read()) | ||
{ | ||
dateTime = DateTime.Parse(result.Get<string>("Expiration")); | ||
} | ||
} | ||
return dateTime; | ||
} | ||
catch (Exception ex) | ||
{ | ||
TShock.Log.Error(ex.ToString()); | ||
return DateTime.MinValue; | ||
} | ||
finally | ||
{ | ||
syncLock.ExitReadLock(); | ||
} | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.