Skip to content

Commit

Permalink
Move OnInfoSwap/SQLSave events into PlayerDB events namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Mar 7, 2021
1 parent 3c112ee commit 5b005ce
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 30 deletions.
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/Maintenance/CmdInfoSwap.cs
Expand Up @@ -17,7 +17,7 @@
*/
using System;
using MCGalaxy.DB;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.Events.PlayerDBEvents;
using MCGalaxy.SQL;

namespace MCGalaxy.Commands.Maintenance {
Expand Down
44 changes: 44 additions & 0 deletions MCGalaxy/Events/PlayerDBEvents.cs
@@ -0,0 +1,44 @@
/*
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;

namespace MCGalaxy.Events.PlayerDBEvents {

public delegate void OnInfoSave(Player p, ref bool cancel);
/// <summary> Called whenever the server saves player's stats to the database. </summary>
public sealed class OnInfoSaveEvent : IEvent<OnInfoSave> {

public static void Call(Player p, ref bool cancel) {
IEvent<OnInfoSave>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++) {
try { items[i].method(p, ref cancel); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}
}

public delegate void OnInfoSwap(string src, string dst);
/// <summary> Called when the information of two players is being swapped. </summary>
public sealed class OnInfoSwapEvent : IEvent<OnInfoSwap> {

public static void Call(string src, string dst) {
if (handlers.Count == 0) return;
CallCommon(pl => pl(src, dst));
}
}
}
10 changes: 0 additions & 10 deletions MCGalaxy/Events/PlayerEvents.cs
Expand Up @@ -53,16 +53,6 @@ public sealed class OnPlayerMoveEvent : IEvent<OnPlayerMove> {
}
}

public delegate void OnSQLSave(Player p);
/// <summary> This event is called whenever the server saves player's data to MySQL or SQLite </summary>
public sealed class OnSQLSaveEvent : IEvent<OnSQLSave> {

public static void Call(Player p) {
if (handlers.Count == 0) return;
CallCommon(pl => pl(p));
}
}

public delegate void OnPlayerCommand(Player p, string cmd, string args, CommandData data);
/// <summary> Called whenever a player uses a command. </summary>
/// <remarks> You must cancel this event to prevent "Unknown command!" being shown. </remarks>
Expand Down
10 changes: 0 additions & 10 deletions MCGalaxy/Events/ServerEvents.cs
Expand Up @@ -86,14 +86,4 @@ public sealed class OnChatEvent : IEvent<OnChat> {
}
}
}

public delegate void OnInfoSwap(string src, string dst);
/// <summary> Called when the information of two players is being swapped. </summary>
public sealed class OnInfoSwapEvent : IEvent<OnInfoSwap> {

public static void Call(string src, string dst) {
if (handlers.Count == 0) return;
CallCommon(pl => pl(src, dst));
}
}
}
7 changes: 5 additions & 2 deletions MCGalaxy/Games/RoundsGame/RoundsGame.Plugin.cs
Expand Up @@ -18,6 +18,7 @@
using System;
using MCGalaxy.Events.LevelEvents;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Events.PlayerDBEvents;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.Network;

Expand All @@ -28,7 +29,7 @@ public abstract partial class RoundsGame : IGame {
protected virtual void HookEventHandlers() {
OnLevelUnloadEvent.Register(HandleLevelUnload, Priority.High);
OnSendingHeartbeatEvent.Register(HandleSendingHeartbeat, Priority.High);
OnSQLSaveEvent.Register(SaveStats, Priority.High);
OnInfoSaveEvent.Register(HandleSaveStats, Priority.High);

OnPlayerActionEvent.Register(HandlePlayerAction, Priority.High);
OnPlayerDisconnectEvent.Register(HandlePlayerDisconnect, Priority.High);
Expand All @@ -37,12 +38,14 @@ public abstract partial class RoundsGame : IGame {
protected virtual void UnhookEventHandlers() {
OnLevelUnloadEvent.Unregister(HandleLevelUnload);
OnSendingHeartbeatEvent.Unregister(HandleSendingHeartbeat);
OnSQLSaveEvent.Unregister(SaveStats);
OnInfoSaveEvent.Unregister(HandleSaveStats);

OnPlayerActionEvent.Unregister(HandlePlayerAction);
OnPlayerDisconnectEvent.Unregister(HandlePlayerDisconnect);
}

void HandleSaveStats(Player p, ref bool cancel) { SaveStats(p); }

protected virtual void HandleSendingHeartbeat(Heartbeat service, ref string name) {
if (Map == null || !GetConfig().MapInHeartbeat) return;
name += " (map: " + Map.MapName + ")";
Expand Down
1 change: 1 addition & 0 deletions MCGalaxy/MCGalaxy_.csproj
Expand Up @@ -475,6 +475,7 @@
<Compile Include="Entity\ModelInfo.cs" />
<Compile Include="Entity\Structs.cs" />
<Compile Include="Entity\TabList.cs" />
<Compile Include="Events\PlayerDBEvents.cs" />
<Compile Include="Events\EconomyEvents.cs" />
<Compile Include="Events\EntityEvents.cs" />
<Compile Include="Events\GameEvents.cs" />
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Player/Player.Fields.cs
Expand Up @@ -223,7 +223,7 @@ public partial class Player : IDisposable {
bool gotSQLData;


public bool cancelcommand, cancelchat, cancelmove, cancelmysql;
public bool cancelcommand, cancelchat, cancelmove;
public bool cancelmessage, cancellogin, cancelconnecting, cancelDeath;

/// <summary> Called when a player removes or places a block.
Expand Down
10 changes: 6 additions & 4 deletions MCGalaxy/Player/Player.cs
Expand Up @@ -22,6 +22,7 @@
using MCGalaxy.Events.EconomyEvents;
using MCGalaxy.Events.EntityEvents;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Events.PlayerDBEvents;
using MCGalaxy.Games;
using MCGalaxy.Maths;
using MCGalaxy.Network;
Expand Down Expand Up @@ -156,9 +157,10 @@ public partial class Player : Entity, IDisposable {
return AABB.IntersectsSolidBlocks(bb, level);
}

public void save() {
OnSQLSaveEvent.Call(this);
if (cancelmysql) { cancelmysql = false; return; }
public void SaveStats() {
bool cancel = false;
OnInfoSaveEvent.Call(this, ref cancel);
if (cancel) return;

// Player disconnected before SQL data was retrieved
if (!gotSQLData) return;
Expand Down Expand Up @@ -270,7 +272,7 @@ public partial class Player : Entity, IDisposable {

Entities.DespawnEntities(this, false);
ShowDisconnectInChat(chatMsg, isKick);
save();
SaveStats();

PlayerInfo.Online.Remove(this);
OnPlayerDisconnectEvent.Call(this, discMsg);
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Server/Maintenance/Updater.cs
Expand Up @@ -90,7 +90,7 @@ public static class Updater {
}

Player[] players = PlayerInfo.Online.Items;
foreach (Player pl in players) pl.save();
foreach (Player pl in players) pl.SaveStats();

// Move current files to previous files (by moving instead of copying,
// can overwrite original the files without breaking the server)
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Server/Tasks/ServerTasks.cs
Expand Up @@ -161,7 +161,7 @@ internal static class ServerTasks {

Player[] players = PlayerInfo.Online.Items;
try {
foreach (Player p in players) p.save();
foreach (Player p in players) p.SaveStats();
} catch (Exception ex) {
Logger.LogError("Error auto-saving players", ex);
}
Expand Down

0 comments on commit 5b005ce

Please sign in to comment.