Skip to content

Commit

Permalink
some stuff and hopefully fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
moddedmcplayer committed Aug 21, 2022
1 parent e77874c commit 628361e
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 13 deletions.
2 changes: 1 addition & 1 deletion hats/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void AddHat(this Player ply, Hat hat)
comp.ply = ply;
comp.schem = obj;
var gameObject = obj.gameObject;
gameObject.transform.parent = ply.GameObject.transform;
gameObject.transform.SetParent(ply.GameObject.transform);
gameObject.transform.localPosition = hat.Offset;
gameObject.transform.localRotation = hat.Rotation;
Plugin.Singleton.hats.Add(ply.UserId, comp);
Expand Down
2 changes: 1 addition & 1 deletion hats/Commands/AddHat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
}
}

if (Plugin.Singleton.hats.Keys.Any(x => x == ply.UserId))
if (Plugin.Singleton.hats.ContainsKey(ply.UserId))
{
response = "Player is already wearing a hat!";
return false;
Expand Down
50 changes: 50 additions & 0 deletions hats/Commands/HatDebug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace hats.Commands
{
using System;
using System.Linq;
using CommandSystem;
using Exiled.API.Features;
using UnityEngine;

public class HatDebug : ICommand
{
public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
{
var ply = Player.Get(sender);
if (ply is null || ply.IsHost)
{
response = "Is host.";
return false;
}

if (!Plugin.Singleton.hats.ContainsKey(ply.UserId))
{
response = "Player isnt wearing a hat!";
return false;
}

var schem = Plugin.Singleton.hats[ply.UserId];
if(schem.gameObject.IsHat(out var hat))
{
var gameObject = hat.gameObject;
Transform parent;
response = $"Name: {hat.hat.Name}" +
$"Enabled: {hat.schem.enabled}" +
$"Config rotation offset: {hat.hat.Rotation}" +
$"Config position offset: {hat.hat.Offset}" +
$"Actual local rotation: {gameObject.transform.localRotation}" +
$"Actual local position: {gameObject.transform.localPosition}" +
$"Parent GO (is player): {(parent = gameObject.transform.parent).gameObject.name} ({ply.GameObject == parent.gameObject})" +
$"Hat global position: {gameObject.transform.position}";
return true;
}

response = "Something went wrong";
return false;
}

public string Command { get; } = "Debug";
public string[] Aliases { get; } = { };
public string Description { get; } = "Print hat debug information";
}
}
2 changes: 2 additions & 0 deletions hats/Commands/Parent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public sealed override void LoadGeneratedCommands()
RegisterCommand(new List());
RegisterCommand(new AddHat());
RegisterCommand(new RemoveHat());
RegisterCommand(new ReloadHats());
RegisterCommand(new HatDebug());
}

protected override bool ExecuteParent(ArraySegment<string> args, ICommandSender sender, out string response)
Expand Down
27 changes: 27 additions & 0 deletions hats/Commands/ReloadHats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace hats.Commands
{
using System;
using CommandSystem;
using Exiled.API.Features;
using Exiled.Permissions.Extensions;

public class ReloadHats : ICommand
{
public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
{
if(!sender.CheckPermission("hats.reload"))
{
response = "Missing permission: hats.reload.";
return false;
}

API.LoadHats();
response = "reloaded hats!";
return true;
}

public string Command { get; } = "reload";
public string[] Aliases { get; } = { };
public string Description { get; } = "Reloads all hats (WILL REMOVE ALL CURRENT HATS!)";
}
}
9 changes: 3 additions & 6 deletions hats/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@ namespace hats
public class EventHandler
{
private Config cfg;
private bool _isLoaded = false;

public EventHandler(Config cfg) => this.cfg = cfg;

public void WaitingForPlayers()
{
if (API.Hats.Count != 0)
if (API.Hats.Count != 0 || _isLoaded)
return;
API.LoadHats();
_isLoaded = true;
}

public void OnLeave(LeftEventArgs ev)
{
if(ev.Player.GameObject.TryGetComponent<HatComponent>(out _))
ev.Player.RemoveHat();
}

public void EndingRound(EndingRoundEventArgs ev)
{
API.LoadHats();
}
}
}
2 changes: 1 addition & 1 deletion hats/Hat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void DestroyInstances()
List<KeyValuePair<string, HatComponent>> toRemove = new List<KeyValuePair<string, HatComponent>>();
foreach (var kvp in Plugin.Singleton.hats.Where(x => x.Value.hat == this))
{
if (kvp.Value is null || kvp.Value.gameObject.Equals(null) || !kvp.Value.schem.isActiveAndEnabled)
if (kvp.Value is null || kvp.Value.gameObject == null || !kvp.Value.schem.isActiveAndEnabled)
{
toRemove.Add(kvp);
continue;
Expand Down
5 changes: 1 addition & 4 deletions hats/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using Exiled.API.Features;
using hats.Components;
using UnityEngine;
using Player = Exiled.Events.Handlers.Player;
using Server = Exiled.Events.Handlers.Server;

Expand All @@ -12,7 +11,7 @@ public class Plugin : Plugin<Config>
{
public override string Author { get; } = "Rowpann SCP";
public override string Name { get; } = "hats";
public override Version Version { get; } = new Version(1, 1, 0);
public override Version Version { get; } = new Version(1, 2, 0);
public override Version RequiredExiledVersion { get; } = new Version(5, 2, 0);

public static Plugin Singleton;
Expand All @@ -26,7 +25,6 @@ public override void OnEnabled()
Handler = new EventHandler(Config);

Server.WaitingForPlayers += Handler.WaitingForPlayers;
Server.EndingRound += Handler.EndingRound;
Player.Left += Handler.OnLeave;

base.OnEnabled();
Expand All @@ -35,7 +33,6 @@ public override void OnEnabled()
public override void OnDisabled()
{
Server.WaitingForPlayers -= Handler.WaitingForPlayers;
Server.EndingRound -= Handler.EndingRound;
Player.Left -= Handler.OnLeave;

Singleton = null;
Expand Down
2 changes: 2 additions & 0 deletions hats/hats.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@
<ItemGroup>
<Compile Include="API.cs" />
<Compile Include="Commands\AddHat.cs" />
<Compile Include="Commands\HatDebug.cs" />
<Compile Include="Commands\List.cs" />
<Compile Include="Commands\Parent.cs" />
<Compile Include="Commands\ReloadHats.cs" />
<Compile Include="Commands\RemoveHat.cs" />
<Compile Include="Components\HatComponent.cs" />
<Compile Include="Config.cs" />
Expand Down

0 comments on commit 628361e

Please sign in to comment.