Skip to content

Commit

Permalink
[BloodRefill] Removed the setblood command and moved the BloodType en…
Browse files Browse the repository at this point in the history
…um to the Shared code folder
  • Loading branch information
WhiteFang5 committed Aug 5, 2022
1 parent a280cd5 commit 4939850
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 140 deletions.
7 changes: 1 addition & 6 deletions BloodRefill/BloodRefill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Shared\CommandSystem\Command.cs" Link="Shared\Command.cs" />
<Compile Include="..\Shared\CommandSystem\CommandAttribute.cs" Link="Shared\CommandAttribute.cs" />
<Compile Include="..\Shared\CommandSystem\CommandExtensions.cs" Link="Shared\CommandExtensions.cs" />
<Compile Include="..\Shared\CommandSystem\CommandSystem.cs" Link="Shared\CommandSystem.cs" />
<Compile Include="..\Shared\CommandSystem\CommandSystemConfig.cs" Link="Shared\CommandSystemConfig.cs" />
<Compile Include="..\Shared\BloodType.cs" Link="Shared\BloodType.cs" />
<Compile Include="..\Shared\Utils.cs" Link="Shared\Utils.cs" />
<Compile Include="..\Shared\VModCharacter.cs" Link="Shared\VModCharacter.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions BloodRefill/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ public sealed override void Load()
}
Utils.Initialize(Log, PluginInfo.PLUGIN_NAME);

CommandSystemConfig.Initialize(Config);
BloodRefillConfig.Initialize(Config);

CommandSystem.Initialize();
BloodRefillSystem.Initialize();

_hooks = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly());
Expand All @@ -48,7 +46,6 @@ public sealed override bool Unload()
}
_hooks?.UnpatchSelf();
BloodRefillSystem.Deinitialize();
CommandSystem.Deinitialize();
Config.Clear();
Utils.Deinitialize();
return true;
Expand Down
69 changes: 1 addition & 68 deletions BloodRefill/Systems/BloodRefillSystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ProjectM;
using ProjectM.Network;
using System;
using System.Linq;
using Unity.Entities;
using VMods.Shared;
using Wetstone.API;
Expand Down Expand Up @@ -163,80 +162,14 @@ private static void OnDeath(DeathEvent deathEvent)
float newTotalBloodInLitres = (int)Math.Round(newTotalBlood) / 10f;
Utils.SendMessage(userEntity, $"+{refillAmountInLitres}L Blood ({newTotalBloodInLitres}L)", ServerChatMessageType.Lore);

ChangeBloodType(user, playerBloodType, playerBlood.Quality, roundedRefillAmount);
playerBloodType.ApplyToPlayer(user, playerBlood.Quality, roundedRefillAmount);
return;
}
}

Utils.SendMessage(userEntity, $"No blood gained from the enemy.", ServerChatMessageType.Lore);
}

private static void ChangeBloodType(User user, BloodType bloodType, float quality, int addAmount)
{
ChangeBloodDebugEvent bloodChangeEvent = new()
{
Source = bloodType.ToPrefabGUID(),
Quality = quality,
Amount = addAmount,
};
VWorld.Server.GetExistingSystem<DebugEventsSystem>().ChangeBloodEvent(user.Index, ref bloodChangeEvent);
}

[Command("setblood", "setblood <blood-type> <blood-quality> [<gain-amount>]", "Sets your blood type to the specified blood-type and blood-quality, and optionally adds a given amount of blood (in Litres).", true)]
private static void OnSetBloodCommand(Command command)
{
var vmodCharacter = command.VModCharacter;
var argCount = command.Args.Length;
if(argCount >= 2)
{
var searchBloodType = command.Args[0];
var validBloodTypes = BloodTypeExtensions.BloodTypeToPrefabGUIDMapping.Keys.ToList();
if(Enum.TryParse(searchBloodType.ToLowerInvariant(), true, out BloodType bloodType) && validBloodTypes.Contains(bloodType))
{
var searchBloodQuality = command.Args[1];
if(int.TryParse(searchBloodQuality.Replace("%", string.Empty), out var bloodQuality) && bloodQuality >= 1 && bloodQuality <= 100)
{
float? addBloodAmount = null;
if(argCount >= 3)
{
var searchLitres = command.Args[2];
if(float.TryParse(searchLitres.Replace("L", string.Empty), out float parsedAddBloodAmount) && parsedAddBloodAmount >= -10f && parsedAddBloodAmount <= 10f)
{
addBloodAmount = parsedAddBloodAmount;
}
else
{
vmodCharacter.SendSystemMessage($"<color=#ff0000>Invalid gain-amount '{searchBloodQuality}'. Should be between -10 and 10</color>");
}
}
else
{
addBloodAmount = 10f;
}

if(addBloodAmount.HasValue)
{
ChangeBloodType(vmodCharacter.User, bloodType, bloodQuality, (int)(addBloodAmount.Value * 10f));
vmodCharacter.SendSystemMessage($"Changed blood type to <color=#ff0000>{bloodQuality}%</color> <color=#ffffff>{searchBloodType}</color> and added <color=#ff0000>{addBloodAmount.Value}L</color>");
}
}
else
{
vmodCharacter.SendSystemMessage($"<color=#ff0000>Invalid blood-quality '{searchBloodQuality}'. Should be between 1 and 100</color>");
}
}
else
{
vmodCharacter.SendSystemMessage($"<color=#ff0000>Invalid blood-type '{searchBloodType}'. Options are: {string.Join(", ", validBloodTypes.Select(x => x.ToString()))}</color>");
}
}
else
{
CommandSystem.SendInvalidCommandMessage(command);
}
command.Use();
}

#endregion
}
}
63 changes: 0 additions & 63 deletions BloodRefill/Systems/BloodType.cs

This file was deleted.

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# VMods Change Log

## Blood Refill
### Next Release
* Moved the `!setblood` command to the Generic Chat Commands VMod
* Made the `BloodType` enum generic and usable across multiple VMods

### v1.0.0
* Initial release

Expand Down
76 changes: 76 additions & 0 deletions Shared/BloodType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using ProjectM;
using ProjectM.Network;
using System;
using System.Collections.Generic;
using Wetstone.API;

namespace VMods.Shared
{
public enum BloodType
{
Frailed = -899826404,//(UnitBloodType) BloodType_None
Creature = -77658840,//(UnitBloodType) BloodType_Creature
Warrior = -1094467405,//(UnitBloodType) BloodType_Warrior
Rogue = 793735874,//(UnitBloodType) BloodType_Rogue
Brute = 581377887,//(UnitBloodType) BloodType_Brute
Scholar = -586506765,//(UnitBloodType) BloodType_Scholar
Worker = -540707191,//(UnitBloodType) BloodType_Worker
VBlood = 1557174542,//(UnitBloodType) BloodType_VBlood
}

public static class BloodTypeExtensions
{
#region Consts

public static readonly Dictionary<BloodType, PrefabGUID> BloodTypeToPrefabGUIDMapping = new()
{
[BloodType.Creature] = new PrefabGUID(1897056612),//CHAR_Wildlife_Deer
[BloodType.Warrior] = new PrefabGUID(-1128238456),//CHAR_Bandit_Bomber
[BloodType.Rogue] = new PrefabGUID(-1030822544),//CHAR_Bandit_Deadeye
[BloodType.Brute] = new PrefabGUID(-1464869978),//CHAR_Town_Cleric
[BloodType.Scholar] = new PrefabGUID(-700632469),//CHAR_Farmlands_Nun
[BloodType.Worker] = new PrefabGUID(-1342764880),//CHAR_Farmlands_Farmer
};

#endregion

#region Public Methods

public static bool ParseBloodType(this PrefabGUID prefabGUID, out BloodType bloodType)
{
int guidHash = prefabGUID.GuidHash;
if(!Enum.IsDefined(typeof(BloodType), guidHash))
{
bloodType = BloodType.Frailed;
return false;
}
bloodType = (BloodType)guidHash;
return true;
}

public static BloodType? ToBloodType(this PrefabGUID prefabGUID)
{
int guidHash = prefabGUID.GuidHash;
if(!Enum.IsDefined(typeof(BloodType), guidHash))
{
return null;
}
return (BloodType)guidHash;
}

public static PrefabGUID ToPrefabGUID(this BloodType bloodType) => BloodTypeToPrefabGUIDMapping[bloodType];

public static void ApplyToPlayer(this BloodType bloodType, User user, float quality, int addAmount)
{
ChangeBloodDebugEvent bloodChangeEvent = new()
{
Source = bloodType.ToPrefabGUID(),
Quality = quality,
Amount = addAmount,
};
VWorld.Server.GetExistingSystem<DebugEventsSystem>().ChangeBloodEvent(user.Index, ref bloodChangeEvent);
}

#endregion
}
}

0 comments on commit 4939850

Please sign in to comment.