Item bans now have a group component. Adding a group to an item ban will allow that group to use the item.
Also fixed chests/items in chests being null when we truncate stack sizes. Added code to let anyone with manageregion permission to modify regions...make sense.
This commit is contained in:
parent
14e2d8ca46
commit
4c468b7f3c
4 changed files with 155 additions and 35 deletions
|
|
@ -1,22 +1,28 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Xml;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
|
using Terraria;
|
||||||
|
|
||||||
namespace TShockAPI.DB
|
namespace TShockAPI.DB
|
||||||
{
|
{
|
||||||
public class ItemManager
|
public class ItemManager
|
||||||
{
|
{
|
||||||
private IDbConnection database;
|
private IDbConnection database;
|
||||||
public List<string> ItemBans = new List<string>();
|
public List<ItemBan> ItemBans = new List<ItemBan>();
|
||||||
|
|
||||||
public ItemManager(IDbConnection db)
|
public ItemManager(IDbConnection db)
|
||||||
{
|
{
|
||||||
database = db;
|
database = db;
|
||||||
|
|
||||||
var table = new SqlTable("ItemBans",
|
var table = new SqlTable("ItemBans",
|
||||||
new SqlColumn("ItemName", MySqlDbType.VarChar, 50) { Primary = true }
|
new SqlColumn("ItemName", MySqlDbType.VarChar, 50) { Primary = true },
|
||||||
|
new SqlColumn("AllowedGroups", MySqlDbType.Text )
|
||||||
);
|
);
|
||||||
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
||||||
creator.EnsureExists(table);
|
creator.EnsureExists(table);
|
||||||
|
|
@ -33,13 +39,13 @@ namespace TShockAPI.DB
|
||||||
{
|
{
|
||||||
|
|
||||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite") ?
|
string query = (TShock.Config.StorageType.ToLower() == "sqlite") ?
|
||||||
"INSERT OR IGNORE INTO 'ItemBans' (ItemName) VALUES (@0);" :
|
"INSERT OR IGNORE INTO 'ItemBans' (ItemName, AllowedGroups) VALUES (@0, @1);" :
|
||||||
"INSERT IGNORE INTO ItemBans SET ItemName=@0;";
|
"INSERT IGNORE INTO ItemBans SET ItemName=@0,AllowedGroups=@1 ;";
|
||||||
|
|
||||||
int id = 0;
|
int id = 0;
|
||||||
int.TryParse(line, out id);
|
int.TryParse(line, out id);
|
||||||
|
|
||||||
database.Query(query, TShock.Utils.GetItemById(id).name);
|
database.Query(query, TShock.Utils.GetItemById(id).name, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -62,17 +68,22 @@ namespace TShockAPI.DB
|
||||||
|
|
||||||
using (var reader = database.QueryReader("SELECT * FROM ItemBans"))
|
using (var reader = database.QueryReader("SELECT * FROM ItemBans"))
|
||||||
{
|
{
|
||||||
while (reader != null && reader.Read())
|
while (reader != null && reader.Read())
|
||||||
ItemBans.Add(reader.Get<string>("ItemName"));
|
{
|
||||||
|
ItemBan ban = new ItemBan(reader.Get<string>("ItemName"));
|
||||||
|
ban.SetAllowedGroups( reader.Get<string>("AllowedGroups") );
|
||||||
|
ItemBans.Add(ban);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddNewBan(string itemname = "")
|
public void AddNewBan(string itemname = "")
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
database.Query("INSERT INTO ItemBans (ItemName) VALUES (@0);", TShock.Utils.GetItemByName(itemname)[0].name);
|
database.Query("INSERT INTO ItemBans (ItemName, AllowedGroups) VALUES (@0, @1);", TShock.Utils.GetItemByName(itemname)[0].name, "");
|
||||||
if (!ItemIsBanned(itemname))
|
if (!ItemIsBanned(itemname, null))
|
||||||
ItemBans.Add(itemname);
|
ItemBans.Add( new ItemBan(itemname) );
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -82,12 +93,12 @@ namespace TShockAPI.DB
|
||||||
|
|
||||||
public void RemoveBan(string itemname)
|
public void RemoveBan(string itemname)
|
||||||
{
|
{
|
||||||
if (!ItemIsBanned(itemname))
|
if (!ItemIsBanned(itemname, null))
|
||||||
return;
|
return;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
database.Query("Delete FROM 'ItemBans' WHERE ItemName=@0;", TShock.Utils.GetItemByName(itemname)[0].name);
|
database.Query("Delete FROM 'ItemBans' WHERE ItemName=@0;", TShock.Utils.GetItemByName(itemname)[0].name);
|
||||||
ItemBans.Remove(itemname);
|
ItemBans.Remove( new ItemBan(itemname) );
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -95,12 +106,118 @@ namespace TShockAPI.DB
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ItemIsBanned(string name)
|
public bool ItemIsBanned(string name, TSPlayer ply)
|
||||||
{
|
{
|
||||||
if (ItemBans.Contains(name))
|
if (ItemBans.Contains( new ItemBan(name) ) )
|
||||||
return true;
|
{
|
||||||
|
ItemBan b = GetItemBanByName(name);
|
||||||
return false;
|
Console.WriteLine("Item ban exists");
|
||||||
|
Console.WriteLine( "Can use: {0}",b.HasPermissionToUseItem(ply));
|
||||||
|
return !b.HasPermissionToUseItem(ply);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool AllowGroup(string item, string name)
|
||||||
|
{
|
||||||
|
string groupsNew = "";
|
||||||
|
ItemBan b = GetItemBanByName(item);
|
||||||
|
if (b != null)
|
||||||
|
{
|
||||||
|
groupsNew = String.Join(",", b.AllowedGroups);
|
||||||
|
if (groupsNew.Length > 0)
|
||||||
|
groupsNew += ",";
|
||||||
|
groupsNew += name;
|
||||||
|
b.SetAllowedGroups(groupsNew);
|
||||||
|
|
||||||
|
int q = database.Query("UPDATE ItemBans SET AllowedGroups=@0 WHERE ItemName=@1", groupsNew,
|
||||||
|
item);
|
||||||
|
|
||||||
|
return q > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveGroup(string item, string group)
|
||||||
|
{
|
||||||
|
ItemBan b = GetItemBanByName(item);
|
||||||
|
if (b != null)
|
||||||
|
{
|
||||||
|
b.RemoveGroup(group);
|
||||||
|
string groups = string.Join(",", b.AllowedGroups);
|
||||||
|
int q = database.Query("UPDATE ItemBans SET AllowedGroups=@0 WHERE ItemName=@1", groups,
|
||||||
|
item);
|
||||||
|
if (q > 0)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemBan GetItemBanByName(String name)
|
||||||
|
{
|
||||||
|
foreach (ItemBan b in ItemBans)
|
||||||
|
{
|
||||||
|
if (b.Name == name)
|
||||||
|
{
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ItemBan : IEquatable<ItemBan>
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public List<string> AllowedGroups { get; set; }
|
||||||
|
|
||||||
|
public ItemBan(string name)
|
||||||
|
: this()
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
AllowedGroups = new List<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemBan()
|
||||||
|
{
|
||||||
|
Name = "";
|
||||||
|
AllowedGroups = new List<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Equals(ItemBan other)
|
||||||
|
{
|
||||||
|
return Name == other.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasPermissionToUseItem(TSPlayer ply)
|
||||||
|
{
|
||||||
|
if (ply == null)
|
||||||
|
return false;
|
||||||
|
Console.WriteLine(ply.Group.Name);
|
||||||
|
return AllowedGroups.Contains(ply.Group.Name); // could add in the other permissions in this class instead of a giant if switch.
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetAllowedGroups( String groups )
|
||||||
|
{
|
||||||
|
// prevent null pointer exceptions
|
||||||
|
if (!string.IsNullOrEmpty(groups))
|
||||||
|
{
|
||||||
|
List<String> groupArr = groups.Split(',').ToList();
|
||||||
|
|
||||||
|
for (int i = 0; i < groupArr.Count; i++)
|
||||||
|
{
|
||||||
|
groupArr[i] = groupArr[i].Trim();
|
||||||
|
Console.WriteLine(groupArr[i]);
|
||||||
|
}
|
||||||
|
AllowedGroups = groupArr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool RemoveGroup(string groupName)
|
||||||
|
{
|
||||||
|
return AllowedGroups.Remove(groupName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -646,7 +646,7 @@ namespace TShockAPI.DB
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return AllowedIDs.Contains(ply.UserID) || AllowedGroups.Contains(ply.Group.Name) || Owner == ply.UserAccountName;
|
return AllowedIDs.Contains(ply.UserID) || AllowedGroups.Contains(ply.Group.Name) || Owner == ply.UserAccountName || ply.Group.HasPermission("manageregion");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAllowedIDs(String ids)
|
public void setAllowedIDs(String ids)
|
||||||
|
|
|
||||||
|
|
@ -481,7 +481,7 @@ namespace TShockAPI
|
||||||
args.Player.SendTileSquare(tileX, tileY);
|
args.Player.SendTileSquare(tileX, tileY);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (tiletype == 48 && !args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Spikes"))
|
if (tiletype == 48 && !args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Spikes", args.Player))
|
||||||
{
|
{
|
||||||
args.Player.SendTileSquare(tileX, tileY);
|
args.Player.SendTileSquare(tileX, tileY);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -492,7 +492,7 @@ namespace TShockAPI
|
||||||
args.Player.SendTileSquare(tileX, tileY);
|
args.Player.SendTileSquare(tileX, tileY);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (tiletype == 141 && !args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Explosives"))
|
if (tiletype == 141 && !args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Explosives", args.Player))
|
||||||
{
|
{
|
||||||
args.Player.SendTileSquare(tileX, tileY);
|
args.Player.SendTileSquare(tileX, tileY);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -661,7 +661,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
if ((control & 32) == 32)
|
if ((control & 32) == 32)
|
||||||
{
|
{
|
||||||
if (!args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned(args.TPlayer.inventory[item].name))
|
if (!args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned(args.TPlayer.inventory[item].name, args.Player))
|
||||||
{
|
{
|
||||||
control -= 32;
|
control -= 32;
|
||||||
args.Player.LastThreat = DateTime.UtcNow;
|
args.Player.LastThreat = DateTime.UtcNow;
|
||||||
|
|
@ -896,14 +896,14 @@ namespace TShockAPI
|
||||||
bucket = 2;
|
bucket = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lava && bucket != 2 && !args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Lava Bucket"))
|
if (lava && bucket != 2 && !args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Lava Bucket", args.Player))
|
||||||
{
|
{
|
||||||
args.Player.LastThreat = DateTime.UtcNow;
|
args.Player.LastThreat = DateTime.UtcNow;
|
||||||
args.Player.SendTileSquare(tileX, tileY);
|
args.Player.SendTileSquare(tileX, tileY);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lava && bucket != 1 && !args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Water Bucket"))
|
if (!lava && bucket != 1 && !args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Water Bucket", args.Player))
|
||||||
{
|
{
|
||||||
args.Player.LastThreat = DateTime.UtcNow;
|
args.Player.LastThreat = DateTime.UtcNow;
|
||||||
args.Player.SendTileSquare(tileX, tileY);
|
args.Player.SendTileSquare(tileX, tileY);
|
||||||
|
|
@ -1042,7 +1042,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
Item item = new Item();
|
Item item = new Item();
|
||||||
item.netDefaults(type);
|
item.netDefaults(type);
|
||||||
if (stacks > item.maxStack || TShock.Itembans.ItemIsBanned(item.name))
|
if (stacks > item.maxStack || TShock.Itembans.ItemIsBanned(item.name, args.Player))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -1175,7 +1175,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
Item item = new Item();
|
Item item = new Item();
|
||||||
item.netDefaults(type);
|
item.netDefaults(type);
|
||||||
if (stacks > item.maxStack || TShock.Itembans.ItemIsBanned(item.name))
|
if (stacks > item.maxStack || TShock.Itembans.ItemIsBanned(item.name, args.Player))
|
||||||
{
|
{
|
||||||
args.Player.SendData(PacketTypes.ItemDrop, "", id);
|
args.Player.SendData(PacketTypes.ItemDrop, "", id);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -1335,7 +1335,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
if (buff == 10)
|
if (buff == 10)
|
||||||
{
|
{
|
||||||
if (!args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Invisibility Potion"))
|
if (!args.Player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Invisibility Potion", args.Player) )
|
||||||
buff = 0;
|
buff = 0;
|
||||||
else if (TShock.Config.DisableInvisPvP && args.TPlayer.hostile)
|
else if (TShock.Config.DisableInvisPvP && args.TPlayer.hostile)
|
||||||
buff = 0;
|
buff = 0;
|
||||||
|
|
|
||||||
|
|
@ -424,11 +424,14 @@ namespace TShockAPI
|
||||||
{
|
{
|
||||||
foreach(Chest chest in Main.chest)
|
foreach(Chest chest in Main.chest)
|
||||||
{
|
{
|
||||||
foreach(Item item in chest.item)
|
if (chest != null)
|
||||||
{
|
{
|
||||||
if (item.stack > item.maxStack)
|
foreach (Item item in chest.item)
|
||||||
item.stack = item.maxStack;
|
{
|
||||||
}
|
if (item != null && item.stack > item.maxStack)
|
||||||
|
item.stack = item.maxStack;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -816,7 +819,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
void OnNpcSetDefaults(SetDefaultsEventArgs<NPC, int> e)
|
void OnNpcSetDefaults(SetDefaultsEventArgs<NPC, int> e)
|
||||||
{
|
{
|
||||||
if (TShock.Itembans.ItemIsBanned(e.Object.name))
|
if (TShock.Itembans.ItemIsBanned(e.Object.name, null) )
|
||||||
{
|
{
|
||||||
e.Object.SetDefaults(0);
|
e.Object.SetDefaults(0);
|
||||||
}
|
}
|
||||||
|
|
@ -975,12 +978,12 @@ namespace TShockAPI
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == 17 && !player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Dirt Wand")) //Dirt Wand Projectile
|
if (type == 17 && !player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Dirt Wand", player)) //Dirt Wand Projectile
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((type == 42 || type == 65 || type == 68) && !player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Sandgun")) //Sandgun Projectiles
|
if ((type == 42 || type == 65 || type == 68) && !player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned("Sandgun", player)) //Sandgun Projectiles
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -988,7 +991,7 @@ namespace TShockAPI
|
||||||
Projectile proj = new Projectile();
|
Projectile proj = new Projectile();
|
||||||
proj.SetDefaults(type);
|
proj.SetDefaults(type);
|
||||||
|
|
||||||
if (!player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned(proj.name))
|
if (!player.Group.HasPermission(Permissions.usebanneditem) && TShock.Itembans.ItemIsBanned(proj.name, player))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue