Skip to content

Commit

Permalink
rewrite pain
Browse files Browse the repository at this point in the history
  • Loading branch information
moddedmcplayer committed Jun 17, 2022
1 parent def57d9 commit 9a3267f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 31 deletions.
44 changes: 25 additions & 19 deletions hats/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@
using System.Collections.Generic;
using System.Linq;
using Exiled.API.Features;
using hats.Components;
using MapEditorReborn.API.Features;
using MapEditorReborn.API.Features.Objects;
using MapEditorReborn.API.Features.Serializable;
using Newtonsoft.Json;
using UnityEngine;
using Object = UnityEngine.Object;

namespace hats
{
public static class API
{
public static Dictionary<string, Hat> Hats { get; private set; } = new Dictionary<string, Hat>();

public static bool IsHat(this GameObject obj, out Hat hat)
public static bool IsHat(this GameObject obj, out HatComponent hat)
{
hat = Hats.FirstOrDefault(x => x.Value.SpawnedHats.Any(x => x.gameObject == obj)).Value;
if (hat != null)
{
return true;
}
return false;
hat = Plugin.Singleton.hats.FirstOrDefault(x => x.Value.gameObject.Equals(obj)).Value;
return hat is not null;
}

public static void LoadHats()
Expand All @@ -45,10 +43,10 @@ public static void ClearHats()
{
if(Hats.Count == 0)
return;
foreach (var ply in Player.List.Where(x => x.SessionVariables.ContainsKey("HatWearer")))
foreach (var ply in Player.List)
{
((SchematicObject)ply.SessionVariables["HatWearer"]).Destroy();
ply.SessionVariables.Remove("HatWearer");
if(ply.GameObject.TryGetComponent<HatComponent>(out _))
ply.RemoveHat();
}
foreach (var kvp in Hats)
{
Expand All @@ -61,23 +59,31 @@ public static void AddHat(this Player ply, Hat hat)
{
if (hat == null)
throw new ArgumentNullException(nameof(hat));
if (ply.GameObject.TryGetComponent<HatComponent>(out _))
return;
var obj = hat.SpawnHat(ply.Position);
obj.gameObject.transform.parent = ply.GameObject.transform;
obj.gameObject.transform.localPosition = hat.Offset;
ply.SessionVariables.Add("HatWearer", obj);
var comp = ply.GameObject.AddComponent<HatComponent>();
comp.hat = hat;
comp.ply = ply;
comp.schem = obj;
var gameObject = obj.gameObject;
gameObject.transform.parent = ply.GameObject.transform;
gameObject.transform.localPosition = hat.Offset;
Plugin.Singleton.hats.Add(ply.UserId, comp);
}

public static void RemoveHat(this Player ply)
{
if (!ply.SessionVariables.ContainsKey("HatWearer"))
if (Plugin.Singleton.hats.Keys.All(x => x != ply.UserId))
{
throw new ArgumentException("Player isnt wearing a hat!");
throw new ArgumentException("Player isn't wearing a hat!");
}

if(ply.SessionVariables["HatWearer"] is SchematicObject obj && obj.gameObject.IsHat(out var hat))

var schem = Plugin.Singleton.hats[ply.UserId];
if(schem.gameObject.IsHat(out var hat))
{
hat.SpawnedHats.Remove(obj);
obj.Destroy();
hat.DoDestroy();
Plugin.Singleton.hats.Remove(ply.UserId);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion hats/Commands/AddHat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using CommandSystem;
using Exiled.API.Features;
using Exiled.Permissions.Extensions;
Expand Down Expand Up @@ -43,7 +44,7 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
}
}

if (ply.SessionVariables.ContainsKey("HatWearer"))
if (Plugin.Singleton.hats.Keys.Any(x => x == ply.UserId))
{
response = "Player is already wearing a hat!";
return false;
Expand Down
5 changes: 3 additions & 2 deletions hats/Commands/RemoveHat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using CommandSystem;
using Exiled.API.Features;
using Exiled.Permissions.Extensions;
Expand Down Expand Up @@ -31,15 +32,15 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
}
}

if (!ply.SessionVariables.ContainsKey("HatWearer"))
if (Plugin.Singleton.hats.Keys.All(x => x != ply.UserId))
{
response = "Player isn't wearing a hat!";
return false;
}

ply.RemoveHat();

response = $"Gave hat to {ply.Nickname}!";
response = $"Removed hat from {ply.Nickname}!";
return true;
}
}
21 changes: 21 additions & 0 deletions hats/Components/HatComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Exiled.API.Features;
using MapEditorReborn.API.Features.Objects;
using UnityEngine;

namespace hats.Components;

public class HatComponent : MonoBehaviour
{
public Hat hat;
public Player ply;
public SchematicObject schem;

public void DoDestroy()
{
schem.Destroy();
ply = null;
hat = null;
Destroy(this);
}
}
5 changes: 3 additions & 2 deletions hats/EventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Exiled.Events.EventArgs;
using hats.Components;
using MapEditorReborn.API.Features.Objects;

namespace hats
{
public class EventHandler
{
private Config cfg;

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

public void WaitingForPlayers()
Expand All @@ -16,7 +17,7 @@ public void WaitingForPlayers()

public void OnLeave(LeftEventArgs ev)
{
if(ev.Player.SessionVariables.ContainsKey("HatWearer"))
if(ev.Player.GameObject.TryGetComponent<HatComponent>(out _))
ev.Player.RemoveHat();
}
}
Expand Down
20 changes: 14 additions & 6 deletions hats/Hat.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using hats.Components;
using MapEditorReborn.API.Features;
using MapEditorReborn.API.Features.Objects;
using MapEditorReborn.API.Features.Serializable;
using MapEditorReborn.Commands.Scale;
using UnityEngine;
using Object = UnityEngine.Object;

namespace hats
{
public class Hat
{
public List<SchematicObject> SpawnedHats = new List<SchematicObject>();
public string Name;
public Vector3 Offset;
public SchematicObjectDataList Schematic;

public SchematicObject SpawnHat(Vector3 Position, Quaternion? Rotation = null, Vector3? scale = null)
internal SchematicObject SpawnHat(Vector3 Position, Quaternion? Rotation = null, Vector3? scale = null)
{
SchematicObject obj = ObjectSpawner.SpawnSchematic(Name, Position, null, scale, Schematic);
SpawnedHats.Append(obj);
return obj;
}

public void DestroyInstances()
{
foreach (var hat in SpawnedHats.Where(x => x != null))
if (Plugin.Singleton.hats.All(x => x.Value.hat != this))
return;
List<KeyValuePair<string, HatComponent>> toRemove = new List<KeyValuePair<string, HatComponent>>();
foreach (var kvp in Plugin.Singleton.hats.Where(x => x.Value.hat == this))
{
hat.Destroy();
kvp.Value.DoDestroy();
toRemove.Add(kvp);
}

foreach (var kvp in toRemove)
{
Plugin.Singleton.hats.Remove(kvp.Key);
}
SpawnedHats.Clear();
}

public Hat(string Name, SchematicObjectDataList data, Vector3 offset)
Expand Down
8 changes: 7 additions & 1 deletion hats/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
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 @@ -14,9 +17,11 @@ public class Plugin : Plugin<Config>

public static Plugin Singleton;
public EventHandler Handler { get; private set; }

public Dictionary<string, HatComponent> hats;

public override void OnEnabled()
{
hats = new Dictionary<string, HatComponent>();
Singleton = this;
Handler = new EventHandler(Config);

Expand All @@ -33,6 +38,7 @@ public override void OnDisabled()

Singleton = null;
Handler = null;
hats = null;
base.OnDisabled();
}
}
Expand Down
1 change: 1 addition & 0 deletions hats/hats.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="Commands\List.cs" />
<Compile Include="Commands\Parent.cs" />
<Compile Include="Commands\RemoveHat.cs" />
<Compile Include="Components\HatComponent.cs" />
<Compile Include="Config.cs" />
<Compile Include="EventHandler.cs" />
<Compile Include="Hat.cs" />
Expand Down

0 comments on commit 9a3267f

Please sign in to comment.