Skip to content

Commit f2c4316

Browse files
Updated game object extensions and libs
1 parent 04f3589 commit f2c4316

14 files changed

+47
-19
lines changed

ShimmysAdminTools/Commands/PointToolCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public void Execute(IRocketPlayer caller, string[] command)
3434
if (Session.PointToolEnabled)
3535
{
3636
UnturnedChat.Say(caller, "PointTool_EnabledActive".Translate(Session.PointTool));
37-
Player.Player.transform.GetOrAddComponent<PointToolInputListener>();
37+
Player.Player.gameObject.AddComponent<PointToolInputListener>();
3838
}
3939
else
4040
{
4141
UnturnedChat.Say(caller, "PointTool_Disabled".Translate());
42-
Player.Player.transform.DestroyComponentIfExists<PointToolInputListener>();
42+
Player.Player.gameObject.DestroyComponentIfExists<PointToolInputListener>();
4343

4444
}
4545
}
@@ -91,7 +91,7 @@ public void Execute(IRocketPlayer caller, string[] command)
9191
{
9292
Session.PointToolEnabled = true;
9393
UnturnedChat.Say(caller, Send + "PointTool_Extension_Enabled".Translate());
94-
Player.Player.transform.GetOrAddComponent<PointToolInputListener>();
94+
Player.Player.gameObject.AddComponent<PointToolInputListener>();
9595

9696
}
9797
}
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
using Rocket.API;
2-
using Rocket.Core;
2+
using Rocket.API.Extensions;
33
using Rocket.Unturned.Player;
44
using ShimmysAdminTools.Models;
55
using ShimmysAdminTools.Modules;
66
using System;
77
using System.Collections.Generic;
8-
using System.Linq;
9-
using System.Text;
10-
using System.Threading.Tasks;
11-
using UnityEngine.Rendering;
8+
using UnityEngine;
129

1310
namespace ShimmysAdminTools.Components
1411
{
@@ -17,7 +14,7 @@ public static class Extensions
1714
public static IEnumerable<R> CastEnumeration<T, R>(this IEnumerable<T> Inp, Func<T, R> Castingmethod)
1815
{
1916
List<R> Res = new List<R>();
20-
foreach(T InpE in Inp)
17+
foreach (T InpE in Inp)
2118
{
2219
Res.Add(Castingmethod(InpE));
2320
}
@@ -28,6 +25,7 @@ public static PlayerSession GetSession(this IRocketPlayer Player)
2825
{
2926
return ((UnturnedPlayer)Player).GetSession();
3027
}
28+
3129
public static PlayerSession GetSession(this UnturnedPlayer Player)
3230
{
3331
return PlayerSessionStore.GetPlayerData(Player);
@@ -37,9 +35,35 @@ public static UnturnedPlayer UPlayer(this IRocketPlayer caller)
3735
{
3836
return (UnturnedPlayer)caller;
3937
}
38+
4039
public static string Translate(this string Translation, params object[] Args)
4140
{
4241
return AdminToolsPlugin.Instance.Translate(Translation, Args);
4342
}
43+
44+
public static T getOrAddComponent<T>(this GameObject go) where T : UnityEngine.Component
45+
{
46+
var obj = go.GetComponent<T>();
47+
48+
if (obj != null)
49+
{
50+
return obj;
51+
}
52+
53+
obj = go.AddComponent<T>();
54+
55+
return obj;
56+
}
57+
58+
public static void DestroyComponentIfExists<T>(this GameObject go) where T : UnityEngine.Component
59+
{
60+
var obj = go.GetComponent<T>();
61+
if (obj != null)
62+
{
63+
GameObject.Destroy(obj);
64+
}
65+
}
66+
67+
4468
}
45-
}
69+
}
-512 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
319 KB
Binary file not shown.
23.3 KB
Binary file not shown.
30.3 KB
Binary file not shown.

ShimmysAdminTools/Models/PlayerSession.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using ShimmysAdminTools.Modules;
44
using ShimmysAdminTools.Behaviors;
55
using UnityEngine;
6+
using Rocket.API.Extensions;
7+
using ShimmysAdminTools.Components;
68

79
namespace ShimmysAdminTools.Models
810
{
@@ -40,17 +42,17 @@ public void StartMapJumpingSession()
4042
{
4143
MapJumpingSession = new GameObject("MapJumpingSession");
4244
UnityEngine.Object.DontDestroyOnLoad(MapJumpingSession);
43-
MapJumpingSession.AddComponent<MapJumpingSession>();
45+
var c =MapJumpingSession.AddComponent<MapJumpingSession>();
4446
UnturnedPlayer pl = UnturnedPlayer.FromCSteamID(new Steamworks.CSteamID(Player));
45-
GameObjectExtension.getOrAddComponent<MapJumpingSession>(MapJumpingSession).SetPlayer(pl);
47+
c.SetPlayer(pl);
4648
}
4749
}
4850

4951
public void StartFlightSession()
5052
{
5153
if (!FlySessionActive)
5254
{
53-
FlightSession NewFly = UPlayer.Player.transform.GetOrAddComponent<FlightSession>();
55+
FlightSession NewFly = UPlayer.Player.gameObject.AddComponent<FlightSession>();
5456
NewFly.SetReady(UPlayer);
5557
FlySession = NewFly;
5658
}
@@ -62,7 +64,7 @@ public void StopFlightSession()
6264
if (FlySessionActive)
6365
{
6466
FlySession.Stop();
65-
UPlayer.Player.transform.DestroyComponentIfExists<FlightSession>();
67+
UPlayer.Player.gameObject.DestroyComponentIfExists<FlightSession>();
6668
FlySession = null;
6769
}
6870
}

0 commit comments

Comments
 (0)