Skip to content

Commit

Permalink
Add Regional Chest Protection via GUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
xNarnia committed Mar 1, 2019
1 parent 05d4256 commit 37793fd
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 146 deletions.
26 changes: 19 additions & 7 deletions HEROsModNetwork/DatabaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public class DatabaseRegion
public int y;
public int width;
public int height;
public Color color;
public bool chestsprotected;
public Color color;

//public byte[] permissions;
public int[] permissionsGroups;
Expand Down Expand Up @@ -508,7 +509,7 @@ public static List<Region> GetRegions()
foreach (DatabaseRegion dbRegion in currentDatabaseWorld.regions)
{
//if (dbRegion.world != Main.worldID) continue;
Region region = new Region(dbRegion.name, dbRegion.x, dbRegion.y, dbRegion.width, dbRegion.height);
Region region = new Region(dbRegion.name, dbRegion.x, dbRegion.y, dbRegion.width, dbRegion.height, dbRegion.chestsprotected);
region.ImportPermissions(dbRegion.permissionsGroups, dbRegion.permissionsPlayers);
region.ID = dbRegion.ID;

Expand All @@ -530,10 +531,11 @@ public static void AddRegion(ref Region region)
dbRegion.y = region.Y;
dbRegion.width = region.Width;
dbRegion.height = region.Height;
//byte[] colorData = new byte[] { region.Color.R, region.Color.G, region.Color.B, region.Color.A };
//int colorNum = BitConverter.ToInt32(colorData, 0);
//dbRegion.color = colorNum;
dbRegion.color = region.Color;
dbRegion.chestsprotected = region.ChestsProtected;
//byte[] colorData = new byte[] { region.Color.R, region.Color.G, region.Color.B, region.Color.A };
//int colorNum = BitConverter.ToInt32(colorData, 0);
//dbRegion.color = colorNum;
dbRegion.color = region.Color;
//dbRegion.permissions = region.ExportPermissions();
dbRegion.permissionsPlayers = region.AllowedPlayersIDs.ToArray();
dbRegion.permissionsGroups = region.AllowedGroupsIDs.ToArray();
Expand Down Expand Up @@ -564,7 +566,17 @@ public static void WriteRegionColor(Region region)
SaveSetting(jsonDatabaseFilename);
}

public static void RemoveRegion(Region region)
public static void WriteRegionChestProtection(Region region)
{
DatabaseRegion r = currentDatabaseWorld.regions.Where(x => region.ID == x.ID).FirstOrDefault();
if (r != null)
{
r.chestsprotected = region.ChestsProtected;
}
SaveSetting(jsonDatabaseFilename);
}

public static void RemoveRegion(Region region)
{
DatabaseRegion databaseRegion = currentDatabaseWorld.regions.Where(x => x.ID == region.ID).FirstOrDefault();
if (databaseRegion != null)
Expand Down
49 changes: 40 additions & 9 deletions HEROsModNetwork/GeneralMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,13 @@ public static void ProcessData(ref BinaryReader reader, int playerNumber)
case MessageType.RequestChangeRegionColor:
ProcessChangeRegionColorRequest(ref reader, playerNumber);
break;
//case MessageType.RequestToggleHardmodeEnemies:
// ProcessToggleHardmodeEnemiesRequest(playerNumber);
// break;
case MessageType.RequestGodMode:
case MessageType.RequestToChangeRegionChestProtection:
ProcessChangeRegionChestProtectionRequest(ref reader, playerNumber);
break;
//case MessageType.RequestToggleHardmodeEnemies:
// ProcessToggleHardmodeEnemiesRequest(playerNumber);
// break;
case MessageType.RequestGodMode:
ProcessGodModeRequest(playerNumber);
break;

Expand Down Expand Up @@ -933,7 +936,7 @@ public static void RequestToChangeColorOfRegion(Region region, Color color)
Network.SendDataToServer();
}

private static void ProcessChangeRegionColorRequest(ref BinaryReader reader, int playerNumber)
private static void ProcessChangeRegionColorRequest(ref BinaryReader reader, int playerNumber)
{
if (Network.Players[playerNumber].Group.IsAdmin)
{
Expand All @@ -947,7 +950,34 @@ private static void ProcessChangeRegionColorRequest(ref BinaryReader reader, int
}
}

public static void RequestRestoreTiles(int playerID, bool onlinePlayer)
public static void RequestToChangeChestProtectionOfRegion(Region region, bool protectionEnabled)
{
WriteHeader(MessageType.RequestToChangeRegionChestProtection);
Writer.Write(region.ID);
Writer.Write(protectionEnabled);
Network.SendDataToServer();
}

public static void ProcessChangeRegionChestProtectionRequest(ref BinaryReader reader, int playerNumber)
{
if (Network.Players[playerNumber].Group.IsAdmin)
{
Region region = Network.GetRegionByID(reader.ReadInt32());
if (region == null) return;
bool protectionEnabled = reader.ReadBoolean();

region.ChestsProtected = protectionEnabled;
DatabaseController.WriteRegionChestProtection(region);
SendRegionListToAllPlayers();

if(protectionEnabled)
Network.SendTextToPlayer($"Chest protection now enabled for region: {region.Name}", playerNumber, Color.Aqua);
else
Network.SendTextToPlayer($"Chest protection now disabled for region: {region.Name}", playerNumber, Color.Aqua);
}
}

public static void RequestRestoreTiles(int playerID, bool onlinePlayer)
{
WriteHeader(MessageType.RequestRestoreTiles);

Expand Down Expand Up @@ -1262,7 +1292,7 @@ internal enum MessageType : byte
RequestAddGroupToRegion,
RequestChangeRegionColor,
RequestRemoveGroupFromRegion,
RequestRestoreTiles,
RequestRestoreTiles,
RequestSetSpawnPoint,
RequestToggleGravestones,
GravestonesToggled,
Expand All @@ -1280,8 +1310,9 @@ internal enum MessageType : byte
RequestTeleport,
RequestForcedSundial,
CurrentToggles,
SyncItemNonOwner
}
SyncItemNonOwner,
RequestToChangeRegionChestProtection
}

public enum TimeChangeType
{
Expand Down
Loading

0 comments on commit 37793fd

Please sign in to comment.