Beispiel #1
0
        public void SetLeaveMessage(string regionName, string message)
        {
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }

            var isNull = string.IsNullOrWhiteSpace(message);

            if (rt.LeaveMsg == message)
            {
                return;
            }

            var query = isNull
                                ? "UPDATE RtRegions SET LeaveMsg = NULL WHERE Id = @0"
                                : "UPDATE RtRegions SET LeaveMsg = @0 WHERE Id = @1";
            var args = isNull
                                ? new object[] { rt.Id }
                                : new object[] { message, rt.Id };

            if (_database.Query(query, args) == 0)
            {
                throw new Exception("Database error: No affected rows.");
            }

            rt.LeaveMsg = message;
        }
Beispiel #2
0
        public void AddItemban(string regionName, string itemName)
        {
            if (string.IsNullOrWhiteSpace(itemName))
            {
                throw new ArgumentNullException(nameof(itemName));
            }
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }
            if (rt.ItemIsBanned(itemName))
            {
                throw new Exception($"{itemName} is already banned in this region.");
            }

            var modified = new StringBuilder(rt.Itembans);

            if (modified.Length != 0)
            {
                modified.Append(',');
            }
            modified.Append(itemName);

            if (_database.Query("UPDATE RtRegions SET Itembans = @0 WHERE Id = @1", modified, rt.Id) == 0)
            {
                throw new Exception("Database error: No affected rows.");
            }

            rt.Itembans = modified.ToString();
        }
Beispiel #3
0
        public void SetMsgInterval(string regionName, int interval)
        {
            if (interval < 0)
            {
                throw new ArgumentException(@"Interval can't be lesser than zero!", nameof(interval));
            }

            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }

            if (rt.MsgInterval == interval)
            {
                return;
            }

            if (_database.Query("UPDATE RtRegions SET MessageInterval = @0 WHERE Id = @1", interval, rt.Id) == 0)
            {
                throw new Exception("Database error: No affected rows.");
            }

            rt.MsgInterval = interval;
        }
Beispiel #4
0
        public void SetTempGroup(string regionName, string tempGroup)
        {
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }

            var isNull = string.IsNullOrWhiteSpace(tempGroup);

            if (!isNull && !TShock.Groups.GroupExists(tempGroup))
            {
                throw new GroupNotExistException(tempGroup);
            }

            var group = isNull
                                ? null
                                : TShock.Utils.GetGroup(tempGroup);
            var query = isNull
                                ? "UPDATE RtRegions SET TempGroup = NULL WHERE Id = @0"
                                : "UPDATE RtRegions SET TempGroup = @0 WHERE Id = @1";
            var args = isNull
                                ? new object[] { rt.Id }
                                : new object[] { group.Name, rt.Id };

            if (_database.Query(query, args) == 0)
            {
                throw new Exception("Database error: No affected rows.");
            }

            rt.TempGroup = group;
        }
Beispiel #5
0
        public void RemoveEvents(string regionName, string events)
        {
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }

            if (string.IsNullOrWhiteSpace(events) || events.ToLower() == Events.None)
            {
                throw new ArgumentException("Invalid events!");
            }

            var originEvents = rt.Events;
            var toRemove     = Events.ValidateEventsList(events).Item1;

            toRemove.ForEach(r => {
                rt.RemoveEvent(r);
            });

            if (_database.Query("UPDATE RtRegions SET Events = @0 WHERE Id = @1", rt.Events, rt.Id) != 0)
            {
                return;
            }
            rt.Events = originEvents;
            throw new Exception("Database error: No affected rows.");
        }
Beispiel #6
0
        public void RemoveItemban(string regionName, string itemName)
        {
            if (string.IsNullOrWhiteSpace(itemName))
            {
                throw new ArgumentNullException(nameof(itemName));
            }
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }
            if (!rt.ItemIsBanned(itemName))
            {
                throw new Exception($"{itemName} is not banned in this region.");
            }
            var origin = rt.Itembans;

            if (rt.RemoveBannedItem(itemName) &&
                _database.Query("UPDATE RtRegions SET Itembans = @0 WHERE Id = @1", rt.Itembans, rt.Id) != 0)
            {
                return;
            }

            rt.Itembans = origin;
            throw new Exception("Database error: No affected rows.");
        }
        public void AddRtRegion(int regionId)
        {
            if (Regions.Any(r => r.Region.ID == regionId))
            {
                return;
            }

            var rt = new RtRegion(-1, regionId, Event.None);

            const string query = "INSERT INTO RtRegions (RegionId, Events) VALUES (@0, @1);";

            try
            {
                _database.Query(query, regionId, rt.Events);
                using (var result = _database.QueryReader("SELECT Id FROM RtRegions WHERE RegionId = @0", regionId))
                {
                    if (result.Read())
                    {
                        rt.Id = result.Get <int>("Id");
                        Regions.Add(rt);
                    }
                    else
                    {
                        throw new Exception("Database error: No affected rows.");
                    }
                }
            }
            catch (Exception e)
            {
                TShock.Log.Error(e.ToString());
            }
        }
Beispiel #8
0
        private static void OnRegionLeft(TSPlayer player, RtRegion region, RtPlayer data)
        {
            if (region.HasEvent(Event.LeaveMsg))
            {
                if (string.IsNullOrWhiteSpace(region.LeaveMsg))
                {
                    player.SendInfoMessage("You have left region {0}", region.Region.Name);
                }
                else
                {
                    player.SendMessage(region.LeaveMsg, Color.White);
                }
            }

            if (region.HasEvent(Event.TempGroup) && player.tempGroup == region.TempGroup)
            {
                player.tempGroup = null;
                player.SendInfoMessage("You are no longer in group {0}.", region.TempGroup.Name);
            }

            if (region.HasEvent(Event.Godmode))
            {
                player.GodMode = false;
                player.SendInfoMessage("You are no longer in godmode!");
            }

            if (region.HasEvent(Event.Pvp) || region.HasEvent(Event.NoPvp) || region.HasEvent(Event.InvariantPvp))
            {
                data.ForcePvP     = null;
                data.CanTogglePvP = true;
                player.SendInfoMessage("You can toggle your PvP status now.");
            }
        }
        public void SetTempGroup(RtRegion rt, string tempGroup)
        {
            var isNull = string.IsNullOrWhiteSpace(tempGroup);

            Group group = null;

            if (!isNull)
            {
                group = TShock.Groups.GetGroupByName(tempGroup);
                if (group == null)
                {
                    throw new GroupNotExistException(tempGroup);
                }
            }

            var query = isNull
                                ? "UPDATE RtRegions SET TempGroup = NULL WHERE Id = @0"
                                : "UPDATE RtRegions SET TempGroup = @0 WHERE Id = @1";
            var args = isNull
                                ? new object[] { rt.Id }
                                : new object[] { group.Name, rt.Id };

            _database.Query(query, args);

            rt.TempGroup = group;
        }
Beispiel #10
0
        public void AddEvents(string regionName, string events)
        {
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }

            if (string.IsNullOrWhiteSpace(events) || events.ToLower() == Events.None)
            {
                throw new ArgumentException("Invalid events!");
            }

            StringBuilder modified = new StringBuilder(rt.Events == Events.None ? "" : rt.Events);
            var           toAdd    = Events.ValidateEventsList(events).Item1;

            toAdd.ForEach(r => {
                if (!rt.HasEvent(r))
                {
                    modified.Append($",{r}");
                }
            });
            if (modified[0] == ',')
            {
                modified.Remove(0, 1);
            }

            if (_database.Query("UPDATE RtRegions SET Events = @0 WHERE Id = @1", modified, rt.Id) == 0)
            {
                throw new Exception("Database error: No affected rows.");
            }
            rt.Events = modified.ToString();
        }
        public void Reload()
        {
            try
            {
                using (
                    var reader =
                        _database.QueryReader(
                            "SELECT `rtregions`.* FROM `rtregions`, `regions` WHERE `rtregions`.RegionId = `regions`.Id AND `regions`.WorldID = @0",
                            Main.worldID.ToString())
                    )
                {
                    Regions.Clear();

                    while (reader.Read())
                    {
                        Regions.Add(RtRegion.FromReader(reader));
                    }
                }
            }
            catch (Exception e)
            {
                TShock.Log.ConsoleError("[RegionTrigger] Load regions failed. Check log for more information.");
                TShock.Log.Error(e.ToString());
            }
        }
Beispiel #12
0
        public void AddTileban(string regionName, short tileId)
        {
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }
            if (rt.TileIsBanned(tileId))
            {
                throw new Exception($"Tile {tileId} has been already banned in this region.");
            }

            var modified = new StringBuilder(rt.Tilebans);

            if (modified.Length != 0)
            {
                modified.Append(',');
            }
            modified.Append(tileId);

            if (_database.Query("UPDATE RtRegions SET Tilebans = @0 WHERE Id = @1", modified, rt.Id) == 0)
            {
                throw new Exception("Database error: No affected rows.");
            }

            rt.Tilebans = modified.ToString();
        }
Beispiel #13
0
        public void RemoveProjban(string regionName, short projId)
        {
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }
            var p = new Projectile();

            p.SetDefaults(projId);
            if (!rt.ProjectileIsBanned(projId))
            {
                throw new Exception($"{p.name} is not banned in this region.");
            }
            var origin = rt.Projbans;

            if (rt.RemoveBannedProjectile(projId) &&
                _database.Query("UPDATE RtRegions SET Projbans = @0 WHERE Id = @1", rt.Projbans, rt.Id) != 0)
            {
                return;
            }

            rt.Projbans = origin;
            throw new Exception("Database error: No affected rows.");
        }
Beispiel #14
0
        public void Reload()
        {
            try {
                using (
                    var reader =
                        _database.QueryReader(
                            "SELECT `rtregions`.* FROM `rtregions`, `regions` WHERE `rtregions`.RegionId = `regions`.Id AND `regions`.WorldID = @0",
                            Main.worldID.ToString())
                    ) {
                    Regions.Clear();

                    while (reader.Read())
                    {
                        var id        = reader.Get <int>("Id");
                        var regionId  = reader.Get <int>("RegionId");
                        var events    = reader.Get <string>("Events");
                        var entermsg  = reader.Get <string>("EnterMsg");
                        var leavemsg  = reader.Get <string>("LeaveMsg");
                        var msg       = reader.Get <string>("Message");
                        var msgitv    = reader.Get <int?>("MessageInterval");
                        var tempgroup = reader.Get <string>("TempGroup");
                        var itemb     = reader.Get <string>("Itembans");
                        var projb     = reader.Get <string>("Projbans");
                        var tileb     = reader.Get <string>("Tilebans");

                        var temp = TShock.Groups.GroupExists(tempgroup)
                                                        ? TShock.Utils.GetGroup(tempgroup)
                                                        : null;
                        var region = new RtRegion(id, regionId)
                        {
                            Events      = events ?? Events.None,
                            EnterMsg    = entermsg,
                            LeaveMsg    = leavemsg,
                            Message     = msg,
                            MsgInterval = msgitv ?? 0,
                            TempGroup   = temp,
                            Itembans    = itemb,
                            Projbans    = projb,
                            Tilebans    = tileb
                        };

                        if (region.HasEvent(Events.TempGroup) && region.TempGroup == null)
                        {
                            TShock.Log.ConsoleError("[RegionTrigger] TempGroup '{0}' of region '{1}' is invalid!", tempgroup, region.Region.Name);
                        }

                        Regions.Add(region);
                    }
                }
            } catch (Exception e) {
#if DEBUG
                Debug.WriteLine(e);
                Debugger.Break();
#endif
                TShock.Log.ConsoleError("[RegionTrigger] Load regions failed. Check log for more information.");
                TShock.Log.Error(e.ToString());
            }
        }
        public void DeletePermissions(RtRegion rt, List <string> permissions)
        {
            var origin = rt.Permissions;

            permissions.ForEach(rt.RemovePermission);

            if (_database.Query("UPDATE RtRegions SET Permissions = @0 WHERE Id = @1", rt.Permissions, rt.Id) != 0)
            {
                return;
            }

            rt.Permissions = origin;
            throw new Exception("Database error: No affected rows.");
        }
        public void SetMsgInterval(RtRegion rt, int interval)
        {
            if (interval < 0)
            {
                throw new ArgumentException(@"Interval can't be lesser than zero!", nameof(interval));
            }

            if (rt.MsgInterval == interval)
            {
                return;
            }

            _database.Query("UPDATE RtRegions SET MessageInterval = @0 WHERE Id = @1", interval, rt.Id);

            rt.MsgInterval = interval;
        }
        public void RemoveTileban(RtRegion rt, short tileId)
        {
            if (!rt.TileIsBanned(tileId))
            {
                return;
            }

            var origin = rt.Tilebans;

            if (rt.RemoveBannedTile(tileId) &&
                _database.Query("UPDATE RtRegions SET Tilebans = @0 WHERE Id = @1", rt.Tilebans, rt.Id) != 0)
            {
                return;
            }

            rt.Tilebans = origin;
            throw new Exception("Database error: No affected rows.");
        }
        public void RemoveItemban(RtRegion rt, string itemName)
        {
            if (!rt.ItemIsBanned(itemName))
            {
                return;
            }

            var origin = rt.Itembans;

            if (rt.RemoveBannedItem(itemName) &&
                _database.Query("UPDATE RtRegions SET Itembans = @0 WHERE Id = @1", rt.Itembans, rt.Id) != 0)
            {
                return;
            }

            rt.Itembans = origin;
            throw new Exception("Database error: No affected rows.");
        }
        public void AddTileban(RtRegion rt, short tileId)
        {
            if (rt.TileIsBanned(tileId))
            {
                return;
            }

            var modified = new StringBuilder(rt.Tilebans);

            if (modified.Length != 0)
            {
                modified.Append(',');
            }
            modified.Append(tileId);

            _database.Query("UPDATE RtRegions SET Tilebans = @0 WHERE Id = @1", modified, rt.Id);

            rt.Tilebans = modified.ToString();
        }
        public void AddItemban(RtRegion rt, string itemName)
        {
            if (rt.ItemIsBanned(itemName))
            {
                return;
            }

            var modified = new StringBuilder(rt.Itembans);

            if (modified.Length != 0)
            {
                modified.Append(',');
            }
            modified.Append(itemName);

            _database.Query("UPDATE RtRegions SET Itembans = @0 WHERE Id = @1", modified, rt.Id);

            rt.Itembans = modified.ToString();
        }
        public void SetLeaveMessage(RtRegion rt, string message)
        {
            var isNull = string.IsNullOrWhiteSpace(message);

            if (string.Equals(rt.LeaveMsg, message))
            {
                return;
            }

            var query = isNull
                                ? "UPDATE RtRegions SET LeaveMsg = NULL WHERE Id = @0"
                                : "UPDATE RtRegions SET LeaveMsg = @0 WHERE Id = @1";
            var args = isNull
                                ? new object[] { rt.Id }
                                : new object[] { message, rt.Id };

            _database.Query(query, args);

            rt.LeaveMsg = message;
        }
        public RtRegion GetTopRegion(IEnumerable <RtRegion> regions)
        {
            RtRegion ret = null;

            foreach (var r in regions)
            {
                if (ret == null)
                {
                    ret = r;
                }
                else
                {
                    if (r.Region.Z > ret.Region.Z)
                    {
                        ret = r;
                    }
                }
            }
            return(ret);
        }
Beispiel #23
0
        public void DeleteRtRegion(string regionName)
        {
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }

            try {
                if (_database.Query("DELETE FROM RtRegions WHERE RegionId = @0", rt.Region.ID) != 0 &&
                    Regions.Remove(rt))
                {
                    return;
                }
                throw new Exception("Database error: No affected rows.");
            } catch (Exception e) {
                TShock.Log.Error(e.ToString());
                throw new Exception("Database error! Check logs for more information.", e);
            }
        }
Beispiel #24
0
        public void AddFlags(string regionName, string flags)
        {
            var region = TShock.Regions.GetRegionByName(regionName);

            if (region == null)
            {
                throw new Exception($"Couldn't find region named '{regionName}'!");
            }

            RtRegion rt = GetRtRegionById(region.ID);

            if (rt == null)
            {
                throw new Exception("Region has not been defined!");
            }
            if (string.IsNullOrWhiteSpace(flags) || flags.ToLower() == Events.None)
            {
                throw new ArgumentException("Invalid events!");
            }

            var oldstr = rt.Events;
            var newevt = flags.ToLower().Split(',');

            if (oldstr.Length != 0)
            {
                oldstr += ',';
            }

            oldstr = newevt
                     .Where(en => !string.IsNullOrWhiteSpace(en) && Events.Contains(en) && !rt.HasEvent(en))
                     .Aggregate(oldstr, (current, en) => current + $"{en},");
            oldstr = oldstr.Substring(0, oldstr.Length - 1);

            if (_database.Query("UPDATE RtRegions SET Flags=@0 WHERE Id=@1", oldstr, rt.Id) == 0)
            {
                throw new Exception("Database error: No affected rows.");
            }
            rt.Events = oldstr;
        }
Beispiel #25
0
        public void AddRtRegion(string regionName, string events)
        {
            if (Regions.Any(r => r.Region.Name == regionName))
            {
                throw new RegionDefinedException(regionName);
            }

            var region = TShock.Regions.GetRegionByName(regionName);

            if (region == null)
            {
                throw new Exception($"Couldn't find region named '{regionName}'!");
            }

            var rt = new RtRegion(-1, region.ID)
            {
                Events = string.IsNullOrWhiteSpace(events) ? Events.None : events.Trim().ToLower()
            };

            string query = "INSERT INTO RtRegions (RegionId, Flags) VALUES (@0, @1);";

            try {
                _database.Query(query, region.ID, rt.Events);
                using (var result = _database.QueryReader("SELECT Id FROM RtRegions WHERE RegionId = @0")) {
                    if (result.Read())
                    {
                        rt.Id = result.Get <int>("Id");
                        Regions.Add(rt);
                    }
                    else
                    {
                        throw new Exception("Database error: No affected rows.");
                    }
                }
            } catch (Exception e) {
                TShock.Log.Error(e.ToString());
                throw new Exception("Database error! Check logs for more information.", e);
            }
        }
Beispiel #26
0
        public void RemoveTileban(string regionName, short tileId)
        {
            RtRegion rt = GetRtRegionByName(regionName);

            if (rt == null)
            {
                throw new Exception("Invalid region!");
            }
            if (!rt.TileIsBanned(tileId))
            {
                throw new Exception($"Tile {tileId} is not banned in this region.");
            }
            var origin = rt.Tilebans;

            if (rt.RemoveBannedTile(tileId) &&
                _database.Query("UPDATE RtRegions SET Tilebans = @0 WHERE Id = @1", rt.Tilebans, rt.Id) != 0)
            {
                return;
            }

            rt.Tilebans = origin;
            throw new Exception("Database error: No affected rows.");
        }
        public void RemoveEvents(RtRegion rt, Event ev)
        {
            rt.RemoveEvent(ev);

            _database.Query("UPDATE RtRegions SET Events = @0 WHERE Id = @1", rt.Events, rt.Id);
        }