Skip to content

Commit

Permalink
Close #174
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosnt committed Mar 19, 2016
1 parent aed14ba commit 1446a39
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/Compatibility/HookManager.cs
Expand Up @@ -79,9 +79,23 @@ public void RegisterHook( Type hookType )
RegisterHook( typeof (T) );
}

public Hook GetByName( string hookName )
public Optional<Hook> GetByName( string hookName )
{
return _activeHooks[hookName.ToLowerInvariant()];
hookName = hookName.ToLowerInvariant();

if ( _activeHooks.ContainsKey( hookName ) )
{
return Optional<Hook>.Of( _activeHooks[hookName] );
}

return Optional<Hook>.Empty();
}

public Optional<THookType> GetByType<THookType>() where THookType : Hook
{
return Optional<THookType>.OfNullable(
(THookType) _activeHooks.FirstOrDefault( h => h.Value is THookType ).Value
);
}
}
}
72 changes: 72 additions & 0 deletions src/Compatibility/Hooks/UconomyHook.cs
@@ -0,0 +1,72 @@
/*
* This file is part of uEssentials project.
* https://uessentials.github.io/
*
* Copyright (C) 2015-2016 Leonardosc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using Essentials.Common;
using Essentials.Common.Reflect;
using Rocket.Core;
using System.Linq;
using System.Reflection;

namespace Essentials.Compatibility.Hooks
{
public class UconomyHook : Hook
{
private MethodAccessor<decimal> _getBalanceMethod;
private MethodAccessor<decimal> _increaseBalanceMethod;

public UconomyHook() : base("economy") {}

public override void OnLoad()
{
var uconomyPlugin = R.Plugins.GetPlugins().FirstOrDefault( c => c.Name.EqualsIgnoreCase( "uconomy" ) );
var uconomyType = uconomyPlugin.GetType().Assembly.GetType( "fr34kyn01535.Uconomy.Uconomy" );
var uconomyInstance = uconomyType.GetField( "Instance", BindingFlags.Static | BindingFlags.Public ).GetValue( uconomyPlugin );

var databaseInstance = uconomyInstance.GetType().GetField( "Database" ).GetValue( uconomyInstance );
_getBalanceMethod = AccessorFactory.AccessMethod<decimal>( databaseInstance, "GetBalance" );
_increaseBalanceMethod = AccessorFactory.AccessMethod<decimal>( databaseInstance, "IncreaseBalance" );
}

public override void OnUnload()
{
}

public override bool CanBeLoaded()
{
return R.Plugins.GetPlugins().Any( c => c.Name.EqualsIgnoreCase( "uconomy" ) );
}

public decimal Withdraw( ulong playerId, decimal amount )
{
return _increaseBalanceMethod.Invoke( playerId.ToString(), -amount );
}

public decimal Deposit( ulong playerId, decimal amount )
{
return _increaseBalanceMethod.Invoke( playerId.ToString(), amount );
}

public decimal GetBalance( ulong playerId )
{
return _getBalanceMethod.Invoke( playerId.ToString() );
}
}
}
10 changes: 5 additions & 5 deletions src/Core/EssCore.cs
Expand Up @@ -202,6 +202,9 @@ select directory

CommandManager.RegisterAll( "Essentials.Commands" );

HookManager.RegisterAll();
HookManager.LoadAll();

/*
Load internal modules
*/
Expand All @@ -220,10 +223,7 @@ select type

Logger.LogInfo( "Loading modules..." );
ModuleManager.LoadAll( ModulesFolder );
Logger.LogInfo( $"Loaded {ModuleManager.RunningModules.Count(t => !(t is InternalModule))} modules" );

HookManager.RegisterAll();
HookManager.LoadAll();
Logger.LogInfo( $"Loaded {ModuleManager.RunningModules.Count( t => !(t is InternalModule) )} modules" );

if ( Config.AutoAnnouncer.Enabled )
{
Expand All @@ -232,7 +232,7 @@ select type

if ( !Config.Updater.AlertOnJoin )
{
EventManager.Unregister<EssentialsEventHandler>( "UpdaterAlertOnJoin" );
EventManager.Unregister<EssentialsEventHandler>( "UpdaterAlert" );
}

if ( Config.DisabledCommands.Count != 0 )
Expand Down
17 changes: 17 additions & 0 deletions src/InternalModules/Kit/Data/KitData.cs
Expand Up @@ -30,6 +30,8 @@
using Newtonsoft.Json.Linq;
using Rocket.Unturned.Items;
using SDG.Unturned;
using Essentials.Core;
using Essentials.Compatibility.Hooks;

namespace Essentials.InternalModules.Kit.Data
{
Expand Down Expand Up @@ -78,11 +80,26 @@ public virtual void Save( Dictionary<string, Kit> type )
);

var itemIndex = 0;
var economyHook = EssCore.Instance.HookManager.GetByType<UconomyHook>();

foreach ( var itemObj in kitObj.GetValue( "items", strCmp ).Children<JObject>() )
{
AbstractKitItem kitItem;

if ( itemObj.GetValue( "money", strCmp ) != null )
{
if ( economyHook.IsAbsent )
{
EssProvider.Logger.LogWarning(
$"Cannot add money item, no economy plugin found. Kit: {kit.Name}" );

continue;
}

kitItem = new KitItemMoney( itemObj.GetValue( "money", strCmp ).Value<decimal>() );
goto add;
}

if ( itemObj.GetValue( "xp", strCmp ) != null )
{
kitItem = new KitItemExperience( itemObj.GetValue( "xp", strCmp ).Value<uint>() );
Expand Down
52 changes: 52 additions & 0 deletions src/InternalModules/Kit/Item/KitItemMoney.cs
@@ -0,0 +1,52 @@
/*
* This file is part of uEssentials project.
* https://uessentials.github.io/
*
* Copyright (C) 2015-2016 Leonardosc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using Essentials.Api.Unturned;
using Essentials.Compatibility.Hooks;
using Essentials.Core;
using Newtonsoft.Json;

namespace Essentials.InternalModules.Kit.Item
{
public class KitItemMoney : AbstractKitItem
{
[JsonProperty( "Money" )]
public decimal Amount { get; set; }

public KitItemMoney( decimal amount )
{
Amount = amount;
}

public override bool GiveTo( UPlayer player, bool dropIfInventoryFull = true )
{
EssCore.Instance.HookManager.GetByType<UconomyHook>().IfPresent( h => {
h.Deposit( player.CSteamId.m_SteamID, Amount );
} );
return true;
}

public override string ToString()
{
return $"Money: {Amount}";
}
}
}
2 changes: 2 additions & 0 deletions uEssentials.csproj
Expand Up @@ -110,6 +110,7 @@
<Compile Include="src\Common\Util\TimeUtil.cs" />
<Compile Include="src\Compatibility\Hook.cs" />
<Compile Include="src\Compatibility\HookManager.cs" />
<Compile Include="src\Compatibility\Hooks\UconomyHook.cs" />
<Compile Include="src\Compatibility\Hooks\LPXHook.cs" />
<Compile Include="src\Core\Command\MethodCommand.cs" />
<Compile Include="src\Core\Command\CommandAdapter.cs" />
Expand Down Expand Up @@ -142,6 +143,7 @@
<Compile Include="src\Commands\CommandKillZombies.cs" />
<Compile Include="src\Commands\CommandTell.cs" />
<Compile Include="src\Commands\CommandResetPlayer.cs" />
<Compile Include="src\InternalModules\Kit\Item\KitItemMoney.cs" />
<Compile Include="src\InternalModules\Warp\Commands\CommandSetWarp.cs" />
<Compile Include="src\Commands\CommandPing.cs" />
<Compile Include="src\Commands\CommandPoll.cs" />
Expand Down

0 comments on commit 1446a39

Please sign in to comment.