Skip to content

Commit

Permalink
Do action is Task. Add commands: CommandDummyButton, CommandDummyInpu…
Browse files Browse the repository at this point in the history
…tText. Add event monitor: DummyDead. Add interacion actions: Button, Execute, Face, Gesture, InputText, Mouse. Replace action thread to schedule task. Add options dummy is admin.
  • Loading branch information
DiFFoZ committed Sep 11, 2020
1 parent 6aca02b commit ad932f0
Show file tree
Hide file tree
Showing 34 changed files with 445 additions and 112 deletions.
4 changes: 3 additions & 1 deletion Dummy/API/IAction.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;

namespace EvolutionPlugins.Dummy.API
{
public interface IAction
{
void Do(PlayerDummy dummy);
Task Do(PlayerDummy dummy);
}
}
40 changes: 40 additions & 0 deletions Dummy/Commands/Actions/CommandDummyButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using EvolutionPlugins.Dummy.API;
using EvolutionPlugins.Dummy.Extensions.Interaction.Actions;
using OpenMod.API.Commands;
using OpenMod.Core.Commands;
using Steamworks;
using System;
using System.Threading.Tasks;

namespace EvolutionPlugins.Dummy.Commands.Actions
{
[Command("button")]
[CommandSyntax("<id> <buttonName>")]
[CommandParent(typeof(CommandDummy))]
public class CommandDummyButton : Command
{
private readonly IDummyProvider m_DummyProvider;

public CommandDummyButton(IServiceProvider serviceProvider, IDummyProvider dummyProvider) : base(serviceProvider)
{
m_DummyProvider = dummyProvider;
}

protected override async Task OnExecuteAsync()
{
if (Context.Parameters.Count < 2)
{
throw new CommandWrongUsageException(Context);
}

var id = (CSteamID)await Context.Parameters.GetAsync<ulong>(0);

var dummy = await m_DummyProvider.GetPlayerDummy(id.m_SteamID);
if (dummy == null)
{
throw new UserFriendlyException($"Dummy \"{id}\" has not found!");
}
dummy.Actions.Actions.Enqueue(new ButtonAction(Context.Parameters.GetArgumentLine(1)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EvolutionPlugins.Dummy.API;
using EvolutionPlugins.Dummy.Extensions.Interaction.Actions;
using OpenMod.API.Commands;
using OpenMod.Core.Commands;
using Steamworks;
Expand All @@ -8,7 +9,7 @@
using Color = System.Drawing.Color;
using Command = OpenMod.Core.Commands.Command;

namespace EvolutionPlugins.Dummy.Commands
namespace EvolutionPlugins.Dummy.Commands.Actions
{
[Command("execute")]
[CommandDescription("Execute a command by Dummy")]
Expand Down Expand Up @@ -40,14 +41,14 @@ protected override async Task OnExecuteAsync()
{
throw new UserFriendlyException($"Dummy \"{id}\" has not found!");
}

var commandContext = await m_CommandExecutor.ExecuteAsync(dummy.Data.UnturnedUser, Context.Parameters.Skip(1).ToArray(), "");

await PrintAsync($"Dummy has {(commandContext.Exception == null ? "<color=green>successfully" : "<color=red>unsuccessfully")}</color> executed command");
if (commandContext.Exception != null && !(commandContext.Exception is UserFriendlyException))
dummy.Actions.Actions.Enqueue(new ExecuteAction(m_CommandExecutor, Context.Parameters.Skip(1).ToArray(), async e =>
{
await PrintAsync(commandContext.Exception.Message, Color.Red);
}
await PrintAsync($"Dummy has {(e == null ? "<color=green>successfully" : "<color=red>unsuccessfully")}</color> executed command");
if (e != null && !(e is UserFriendlyException))
{
await PrintAsync(e.Message, Color.Red);
}
}));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Cysharp.Threading.Tasks;
using EvolutionPlugins.Dummy.API;
using EvolutionPlugins.Dummy.Extensions.Interaction.Actions;
using OpenMod.API.Commands;
using OpenMod.Core.Commands;
using SDG.Unturned;
Expand All @@ -8,7 +9,7 @@
using System.Threading.Tasks;
using Command = OpenMod.Core.Commands.Command;

namespace EvolutionPlugins.Dummy.Commands
namespace EvolutionPlugins.Dummy.Commands.Actions
{
[Command("face")]
[CommandDescription("Send a face to dummy")]
Expand All @@ -31,10 +32,6 @@ protected override async Task OnExecuteAsync()
}

var id = (CSteamID)await Context.Parameters.GetAsync<ulong>(0);
if (!m_DummyProvider.Dummies.TryGetValue(id, out _))
{
throw new UserFriendlyException($"Dummy \"{id}\" has not found!");
}

var dummy = await m_DummyProvider.GetPlayerDummy(id.m_SteamID);
if (dummy == null)
Expand All @@ -47,11 +44,7 @@ protected override async Task OnExecuteAsync()
{
throw new UserFriendlyException($"Can't change to {faceId} because is higher {Customization.FACES_FREE + Customization.FACES_PRO}");
}
await UniTask.SwitchToMainThread();
dummy.Data.UnturnedUser.Player.Player.clothing.channel.send("tellSwapFace", ESteamCall.NOT_OWNER, ESteamPacket.UPDATE_RELIABLE_BUFFER, new object[]
{
faceId
});
dummy.Actions.Actions.Enqueue(new FaceAction(faceId));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Cysharp.Threading.Tasks;
using EvolutionPlugins.Dummy.API;
using EvolutionPlugins.Dummy.API;
using EvolutionPlugins.Dummy.Extensions.Interaction.Actions;
using OpenMod.API.Commands;
using OpenMod.Core.Commands;
using SDG.Unturned;
Expand All @@ -8,7 +8,7 @@
using System.Threading.Tasks;
using Command = OpenMod.Core.Commands.Command;

namespace EvolutionPlugins.Dummy.Commands
namespace EvolutionPlugins.Dummy.Commands.Actions
{
[Command("gesture")]
[CommandDescription("Make a dummy gesture")]
Expand Down Expand Up @@ -40,10 +40,11 @@ protected override async Task OnExecuteAsync()
var gesture = await Context.Parameters.GetAsync<string>(1);
if (!Enum.TryParse<EPlayerGesture>(gesture.ToUpper(), out var eGesture))
{
throw new UserFriendlyException($"Unable find a gesture {gesture}");
await PrintAsync($"Unable find a gesture {gesture}");
await PrintAsync($"All gestures: {string.Join(",", Enum.GetNames(typeof(EPlayerGesture)))}");
return;
}
await UniTask.SwitchToMainThread();
dummy.Data.UnturnedUser.Player.Player.animator.sendGesture(eGesture, false);
dummy.Actions.Actions.Enqueue(new GestureAction(eGesture));
}
}
}
41 changes: 41 additions & 0 deletions Dummy/Commands/Actions/CommandDummyInputText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using EvolutionPlugins.Dummy.API;
using EvolutionPlugins.Dummy.Extensions.Interaction.Actions;
using OpenMod.API.Commands;
using OpenMod.Core.Commands;
using Steamworks;
using System;
using System.Threading.Tasks;

namespace EvolutionPlugins.Dummy.Commands.Actions
{
[Command("inputfield")]
[CommandAlias("if")]
[CommandSyntax("<id> <inputFieldName> <text>")]
[CommandParent(typeof(CommandDummy))]
public class CommandDummyInputText : Command
{
private readonly IDummyProvider m_DummyProvider;

public CommandDummyInputText(IServiceProvider serviceProvider, IDummyProvider dummyProvider) : base(serviceProvider)
{
m_DummyProvider = dummyProvider;
}

protected override async Task OnExecuteAsync()
{
if (Context.Parameters.Count < 3)
{
throw new CommandWrongUsageException(Context);
}

var id = (CSteamID)await Context.Parameters.GetAsync<ulong>(0);

var dummy = await m_DummyProvider.GetPlayerDummy(id.m_SteamID);
if (dummy == null)
{
throw new UserFriendlyException($"Dummy \"{id}\" has not found!");
}
dummy.Actions.Actions.Enqueue(new InputTextAction(Context.Parameters[1], Context.Parameters[2]));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
using EvolutionPlugins.Dummy.Extensions.Movement;
using OpenMod.API.Commands;
using OpenMod.Core.Commands;
using OpenMod.Core.Helpers;
using Steamworks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dummy.Commands
namespace EvolutionPlugins.Dummy.Commands.Actions
{
[Command("jump")]
[CommandDescription("Make dummy to jump")]
[CommandSyntax("<id>")]
[CommandParent(typeof(CommandDummy))]
public class CommandDummyJump : Command
{
Expand All @@ -25,7 +25,7 @@ public CommandDummyJump(IServiceProvider serviceProvider, IDummyProvider dummyPr

protected override async Task OnExecuteAsync()
{
if (Context.Parameters.Count < 2)
if (Context.Parameters.Count < 1)
{
throw new CommandWrongUsageException(Context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Cysharp.Threading.Tasks;
using EvolutionPlugins.Dummy.API;
using EvolutionPlugins.Dummy.Extensions.Movement.Actions;
using OpenMod.API.Commands;
using OpenMod.Core.Commands;
using SDG.Unturned;
using System;
using System.Threading.Tasks;
using Command = OpenMod.Core.Commands.Command;

namespace EvolutionPlugins.Dummy.Commands
namespace EvolutionPlugins.Dummy.Commands.Actions
{
[Command("stance")]
[CommandSyntax("<id> <stance>")]
Expand All @@ -29,14 +31,18 @@ protected override async Task OnExecuteAsync()
var stance = Context.Parameters[1];

var dummy = await m_DummyProvider.GetPlayerDummy(id);
if (dummy == null)
{
throw new UserFriendlyException($"Dummy \"{id}\" has not found!");
}

if (!Enum.TryParse<EPlayerStance>(stance.ToUpper(), out var eStance))
{
await PrintAsync($"Unable to find a stance: {stance}");
await PrintAsync($"All stances: {string.Join(",", Enum.GetNames(typeof(EPlayerStance)))}");
return;
}
await UniTask.SwitchToMainThread();
dummy.Data.UnturnedUser.Player.Player.stance.checkStance(eStance, false);
dummy.Actions.Actions.Enqueue(new StanceAction(eStance));
}
}
}
5 changes: 5 additions & 0 deletions Dummy/Commands/CommandDummyTphere.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Dummy.Extensions;
using EvolutionPlugins.Dummy.API;
using OpenMod.API.Commands;
using OpenMod.Core.Commands;
using OpenMod.Unturned.Users;
using System;
Expand Down Expand Up @@ -29,6 +30,10 @@ protected override async Task OnExecuteAsync()
var id = await Context.Parameters.GetAsync<ulong>(0);

var dummy = await m_DummyProvider.GetPlayerDummy(id);
if (dummy == null)
{
throw new UserFriendlyException($"Dummy \"{id}\" has not found!");
}
await dummy.Data.UnturnedUser.TeleportToPlayerAsync((UnturnedUser)Context.Actor);
}
}
Expand Down
10 changes: 10 additions & 0 deletions Dummy/Dummy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<AssemblyName>Dummy</AssemblyName>
<RootNamespace>EvolutionPlugins.Dummy</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Patches\Patch_PlayerInput_askInput.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="config.yaml" />
</ItemGroup>

<ItemGroup>
<None Include="Patches\Patch_PlayerInput_askInput.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenMod.Unturned" Version="2.0.4" />
<PackageReference Include="OpenMod.Unturned.Redist" Version="3.20.8.2" />
Expand Down
56 changes: 56 additions & 0 deletions Dummy/Events/DummyDeadEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Cysharp.Threading.Tasks;
using EvolutionPlugins.Dummy.API;
using OpenMod.API.Eventing;
using OpenMod.API.Users;
using OpenMod.Core.Eventing;
using OpenMod.Core.Users;
using OpenMod.UnityEngine.Extensions;
using OpenMod.Unturned.Players.Events.Life;
using SDG.Unturned;
using System.Threading.Tasks;

namespace Dummy.Events
{
public class DummyDeadEvent : IEventListener<UnturnedPlayerDeadEvent>
{
private readonly IDummyProvider m_DummyProvider;
private readonly IUserManager m_UserManager;

public DummyDeadEvent(IDummyProvider dummyProvider, IUserManager userManager)
{
m_DummyProvider = dummyProvider;
m_UserManager = userManager;
}

[EventListener(Priority = EventListenerPriority.Monitor)]
public async Task HandleEventAsync(object sender, UnturnedPlayerDeadEvent @event)
{
if (m_DummyProvider.Dummies.ContainsKey(@event.Player.SteamId))
{
foreach (var owner in m_DummyProvider.Dummies[@event.Player.SteamId].Data.Owners)
{
var player = await m_UserManager.FindUserAsync(KnownActorTypes.Player, owner.ToString(), UserSearchMode.FindById);
if (player == null)
{
continue;
}
await player.PrintMessageAsync($"Dummy {@event.Player.SteamId} has died. Death reason: {@event.DeathCause.ToString().ToLower()}, killer = {@event.Instigator}. Respawning...");
}

async UniTask Revive()
{
await UniTask.Delay(1500);
if (@event.Player.IsAlive) return; // double-check
await UniTask.SwitchToMainThread();
@event.Player.Player.life.sendRevive();
@event.Player.Player.life.channel.send("tellRevive", ESteamCall.ALL, ESteamPacket.UPDATE_RELIABLE_BUFFER, new object[]
{
@event.Player.Transform.Position.ToUnityVector(),
MeasurementTool.angleToByte(@event.Player.Player.transform.rotation.eulerAngles.y)
});
}
await Revive();
}
}
}
}
22 changes: 22 additions & 0 deletions Dummy/Extensions/Interaction/Actions/ButtonAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Cysharp.Threading.Tasks;
using SDG.Unturned;
using System.Threading.Tasks;

namespace EvolutionPlugins.Dummy.Extensions.Interaction.Actions
{
public class ButtonAction : IInteractionAction
{
public ButtonAction(string buttonName)
{
ButtonName = buttonName;
}

public string ButtonName { get; }

public async Task Do(PlayerDummy dummy)
{
await UniTask.SwitchToMainThread();
EffectManager.instance.tellEffectClicked(dummy.SteamID, ButtonName);
}
}
}
Loading

0 comments on commit ad932f0

Please sign in to comment.