Skip to content

Commit a43f7fb

Browse files
author
Zack Piispanen
committed
itembans added
1 parent 46e774d commit a43f7fb

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

FlaggedRegion.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class FlaggedRegion
1313
private Region region;
1414
private int dps = 0;
1515
private int hps = 0;
16+
private List<string> bannedItems = new List<string>();
1617

1718
public FlaggedRegion(Region r )
1819
{
@@ -94,6 +95,10 @@ public List<Flags> getFlags()
9495
{
9596
f.Add(Flags.NOPVP);
9697
}
98+
if ((flags & (int) Flags.ITEMBAN) == (int) Flags.ITEMBAN)
99+
{
100+
f.Add(Flags.ITEMBAN);
101+
}
97102
return f;
98103
}
99104

@@ -121,5 +126,15 @@ public void setHPS(int s)
121126
{
122127
hps = s;
123128
}
129+
130+
public void setBannedItems(List<string> items)
131+
{
132+
bannedItems = items;
133+
}
134+
135+
public List<String> getItembans()
136+
{
137+
return bannedItems;
138+
}
124139
}
125140
}

FlaggedRegionManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public FlaggedRegionManager()
1616
regions = new Dictionary<string, FlaggedRegion>();
1717
}
1818

19-
public void ImportRegion( string name, int flags, int d, int h )
19+
public void ImportRegion( string name, int flags, int d, int h, List<string> items )
2020
{
2121
var reg = TShock.Regions.GetRegionByName(name);
2222
if( reg == null )
@@ -27,6 +27,7 @@ public void ImportRegion( string name, int flags, int d, int h )
2727
FlaggedRegion f = new FlaggedRegion(reg, flags);
2828
f.setDPS( d );
2929
f.setHPS(h);
30+
f.setBannedItems(items);
3031
regions.Add( name, f );
3132
}
3233

@@ -85,5 +86,10 @@ public List<FlaggedRegion> InRegions( int x, int y )
8586
}
8687
return ret;
8788
}
89+
90+
public void Clear()
91+
{
92+
regions.Clear();
93+
}
8894
}
8995
}

Flags.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public enum Flags
1919
GODMOB = 256,
2020
HEAL = 512,
2121
NOPVP = 1024,
22+
ITEMBAN = 2048,
2223
}
2324
}

RegionFlags.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.IO;
5+
using System.Linq;
56
using Mono.Data.Sqlite;
67
using MySql.Data.MySqlClient;
78
using TerrariaApi.Server;
89
using TShockAPI;
910
using TShockAPI.DB;
1011
using Terraria;
12+
using TShockAPI.Hooks;
1113

1214
namespace RegionFlags
1315
{
@@ -78,6 +80,7 @@ public override void Initialize()
7880
ServerApi.Hooks.ServerLeave.Register(this, OnLeave);
7981
GetDataHandlers.NPCStrike += npchooks.OnNPCStrike;
8082
GetDataHandlers.PlayerDamage += playerhooks.OnDamage;
83+
TShockAPI.Hooks.GeneralHooks.ReloadEvent += OnReload;
8184
Database();
8285
}
8386

@@ -118,7 +121,8 @@ private void Database()
118121
new SqlColumn("Name", MySqlDbType.VarChar, 56){ Length = 56, Primary = true},
119122
new SqlColumn("Flags", MySqlDbType.Int32){ DefaultValue = "0" },
120123
new SqlColumn("Damage", MySqlDbType.Int32) { DefaultValue = "0" },
121-
new SqlColumn("Heal", MySqlDbType.Int32) { DefaultValue = "0" }
124+
new SqlColumn("Heal", MySqlDbType.Int32) { DefaultValue = "0" },
125+
new SqlColumn("BannedItems", MySqlDbType.Text) { DefaultValue = "" }
122126
);
123127
var creator = new SqlTableCreator(db,
124128
db.GetSqlType() == SqlType.Sqlite
@@ -127,6 +131,12 @@ private void Database()
127131
creator.EnsureExists(table);
128132
}
129133

134+
private void OnReload(ReloadEventArgs args)
135+
{
136+
regions.Clear();
137+
Import(new EventArgs());
138+
}
139+
130140
private void Import(EventArgs args)
131141
{
132142
String query = "SELECT * FROM Regions";
@@ -139,7 +149,10 @@ private void Import(EventArgs args)
139149
int flags = reader.Get<int>("Flags");
140150
int damage = reader.Get<int>("Damage");
141151
int heal = reader.Get<int>("Heal");
142-
regions.ImportRegion(name, flags, damage, heal);
152+
string bannedItems = reader.Get<string>("BannedItems") ?? "";
153+
154+
List<string> bannedItemsList = new List<string>(bannedItems.Split(',').ToList().Select(s => s.Trim()));
155+
regions.ImportRegion(name, flags, damage, heal, bannedItemsList);
143156
}
144157
}
145158
}

RegionPlayer.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ public void Update()
5656
lastWarned = now;
5757
}
5858
}
59+
List<string> bannedItems = new List<string>();
60+
if (flags.Contains(Flags.ITEMBAN) && InvalidInventory(reg.getItembans(), out bannedItems))
61+
{
62+
Vector2 pos = positions.getTP();
63+
Vector2 diff = pos - player.TPlayer.position;
64+
if(((diff.X*diff.X) + (diff.Y*diff.Y)) > (100*100))
65+
player.Teleport((int)pos.X, (int)pos.Y);
66+
else
67+
player.Spawn(Main.spawnTileX, Main.spawnTileY);
68+
69+
70+
if (warning)
71+
{
72+
player.SendMessage(String.Format("The following are banned in that area: {0}", string.Join(",", bannedItems)), Color.Red);
73+
lastWarned = now;
74+
}
75+
}
5976
if (flags.Contains(Flags.DEATH) && !r.HasPermissionToBuildInRegion(player))
6077
{
6178
NetMessage.SendData((int)PacketTypes.PlayerDamage, -1, -1, " died Indiana Jone's style.", player.Index, 0, 999999,
@@ -148,5 +165,22 @@ public TSPlayer GetPlayer()
148165
{
149166
return player;
150167
}
168+
169+
private bool InvalidInventory(List<string> items, out List<string> banned )
170+
{
171+
banned = new List<string>();
172+
foreach (Item i in player.TPlayer.inventory)
173+
{
174+
if (i != null && i.stack > 0)
175+
{
176+
if (items.Select(it => it.ToUpper()).Contains(i.name.ToUpper()) || items.Contains(i.netID.ToString()))
177+
{
178+
banned.Add(i.name);
179+
}
180+
}
181+
}
182+
183+
return banned.Count > 0;
184+
}
151185
}
152186
}

0 commit comments

Comments
 (0)