Adds Itembans into SQL, in game commands not added yet, but can use a DB editor to add bans

This commit is contained in:
Twitchy 2011-07-10 13:08:19 +12:00
parent 243b0297b9
commit bbcf84ed4a
6 changed files with 104 additions and 106 deletions

View file

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Community.CsharpSqlite.SQLiteClient;
using TShockAPI.DB;
namespace TShockAPI.DB
{
public class ItemManager
{
private IDbConnection database;
public ItemManager(IDbConnection db)
{
database = db;
using (var com = database.CreateCommand())
{
com.CommandText =
"CREATE TABLE IF NOT EXISTS 'ItemBans' ('ItemName' TEXT UNIQUE);";
com.ExecuteNonQuery();
}
}
public void AddNewBan(string itemname = "")
{
try
{
using (var com = database.CreateCommand())
{
com.CommandText = "INSERT INTO ItemBans (ItemName) VALUES (@itemname);";
com.AddParameter("@itemname", Tools.GetItemByName(itemname)[0].name);
com.ExecuteNonQuery();
}
}
catch (SqliteExecutionException ex)
{
}
}
public void RemoveBan(string itemname)
{
try
{
using (var com = database.CreateCommand())
{
com.CommandText = "DELETE FROM ItemBans WHERE ItemName=@itemname;";
com.AddParameter("@itemname", Tools.GetItemByName(itemname)[0].name);
com.ExecuteNonQuery();
}
}
catch (SqliteExecutionException ex)
{
}
}
public bool ItemIsBanned(string name)
{
try
{
using (var com = database.CreateCommand())
{
com.CommandText = "SELECT *FROM ItemBans WHERE ItemName=@name";
com.AddParameter("@name", name);
using (var reader = com.ExecuteReader())
{
if (reader.Read())
if (reader.Get<string>("ItemName") == name)
return true;
}
}
}
catch (SqliteExecutionException ex)
{
}
return false;
}
}
}

View file

@ -1,94 +0,0 @@
/*
TShock, a server mod for Terraria
Copyright (C) 2011 The TShock Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Terraria;
namespace TShockAPI
{
class ItemManager
{
public static List<ItemBan> BannedItems = new List<ItemBan>();
public static void LoadBans()
{
try
{
if (!File.Exists(FileTools.ItemBansPath))
return;
BannedItems.Clear();
foreach (var line in File.ReadAllLines(FileTools.ItemBansPath))
{
int ID = -1;
if (Int32.TryParse(line, out ID))
{
if (ID < Main.maxItemTypes && ID > 0)
{
var item = Tools.GetItemById(ID);
BannedItems.Add(new ItemBan(ID, item.name));
Log.Info("Item: " + item.name + " is banned");
}
else
{
Log.Warn("Invalid ID " + ID);
}
}
}
}
catch (Exception e)
{
Log.Error(e.ToString());
}
}
public static bool ItemIsBanned(string ID)
{
foreach (ItemBan item in BannedItems)
{
if (ID == item.Name)
return true;
}
return false;
}
}
public class ItemBan
{
public int ID { get; set; }
public string Name { get; set; }
public ItemBan(int id, string name)
{
ID = id;
Name = name;
}
public ItemBan()
{
ID = -1;
Name = string.Empty;
}
}
}

View file

@ -49,6 +49,7 @@ namespace TShockAPI
public static BackupManager Backups;
public static GroupManager Groups;
public static UserManager Users;
public static ItemManager Itembans;
public static ConfigFile Config { get; set; }
@ -118,10 +119,10 @@ namespace TShockAPI
Regions = new RegionManager(DB);
Groups = new GroupManager(DB);
Users = new UserManager(DB);
Itembans = new ItemManager(DB);
Log.ConsoleInfo(string.Format("TShock Version {0} ({1}) now running.", Version, VersionCodename));
GameHooks.PostInitialize += OnPostInit;
GameHooks.Update += OnUpdate;
ServerHooks.Join += OnJoin;
@ -134,7 +135,6 @@ namespace TShockAPI
GetDataHandlers.InitGetDataHandler();
Commands.InitCommands();
ItemManager.LoadBans();
Log.ConsoleInfo("AutoSave " + (TShock.Config.AutoSave ? "Enabled" : "Disabled"));
Log.ConsoleInfo("Backups " + (Backups.Interval > 0 ? "Enabled" : "Disabled"));
@ -260,7 +260,7 @@ namespace TShockAPI
var inv = player.TPlayer.inventory;
for (int i = 0; i < inv.Length; i++)
{
if (inv[i] != null && ItemManager.ItemIsBanned(inv[i].name))
if (inv[i] != null && TShock.Itembans.ItemIsBanned(inv[i].name))
{
player.Disconnect("Using banned item: " + inv[i].name + ", remove it and rejoin");
break;
@ -383,6 +383,15 @@ namespace TShockAPI
if (text.StartsWith("exit"))
{
Tools.ForceKickAll("Server shutting down!");
var sb = new StringBuilder();
for (int i = 0; i < Main.maxItemTypes; i++)
{
string itemName = Main.itemName[i];
string itemID = (i).ToString();
sb.Append("ItemList.Add(\"" + itemName + "\");").AppendLine();
}
File.WriteAllText("item.txt", sb.ToString());
}
else if (text.StartsWith("playing") || text.StartsWith("/playing"))
{
@ -413,7 +422,6 @@ namespace TShockAPI
if (Commands.HandleCommand(TSPlayer.Server, text))
e.Handled = true;
}
}
private void GetData(GetDataEventArgs e)

View file

@ -77,11 +77,11 @@
<ItemGroup>
<Compile Include="BackupManager.cs" />
<Compile Include="DB\BanManager.cs" />
<Compile Include="DB\ItemManager.cs" />
<Compile Include="DB\DbExt.cs" />
<Compile Include="DB\GroupManager.cs" />
<Compile Include="DB\UserManager.cs" />
<Compile Include="IPackable.cs" />
<Compile Include="ItemManager.cs" />
<Compile Include="Commands.cs" />
<Compile Include="ConfigFile.cs" />
<Compile Include="FileTools.cs" />

View file

@ -239,12 +239,16 @@ namespace TShockAPI
var found = new List<Item>();
for (int i = 1; i < Main.maxItemTypes; i++)
{
Item item = new Item();
item.SetDefaults(i);
if (item.name.ToLower() == name.ToLower())
return new List<Item> { item };
if (item.name.ToLower().StartsWith(name.ToLower()))
found.Add(item);
try
{
Item item = new Item();
item.SetDefaults(i);
if (item.name.ToLower() == name.ToLower())
return new List<Item> { item };
if (item.name.ToLower().StartsWith(name.ToLower()))
found.Add(item);
}
catch { }
}
return found;
}

View file

@ -30,7 +30,6 @@ namespace TShockAPI
class UpdateManager
{
static string updateUrl = "http://shankshock.com/tshock-update.txt";
public static bool updateCmd;
public static DateTime lastcheck = DateTime.MinValue;
public static string[] globalChanges = {};
/// <summary>