-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCargoShipNoScientistsBugFix.cs
177 lines (148 loc) · 5.86 KB
/
CargoShipNoScientistsBugFix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using Newtonsoft.Json;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("CargoShipScientistsBugFix", "Ultra", "2.1.3")]
[Description("Cargo ship respawns if it has spawned out of livable map")]
class CargoShipNoScientistsBugFix : RustPlugin
{
bool initialized = false;
int mapLimit = 0;
Timer currentPositionLogTimer = null;
#region Hooks
void OnServerInitialized()
{
mapLimit = (ConVar.Server.worldsize / 2) * 3;
if (mapLimit > 4000) mapLimit = 4000;
initialized = true;
Log($"OnServerInitialized(): MapSize: {ConVar.Server.worldsize} / MapLimit set to {mapLimit}", logType: LogType.INFO);
}
void OnEntitySpawned(BaseEntity Entity)
{
if (!initialized) return;
if (Entity == null) return;
if (Entity is CargoShip)
{
CargoShip cargoShip = (CargoShip)Entity;
if (!IsInLivableArea(cargoShip.transform.position))
{
Log($"CargoShip spawned out liveable area", logType: LogType.WARNING);
Log($"{cargoShip.transform.position.x}|{cargoShip.transform.position.y}|{cargoShip.transform.position.z}", logType: LogType.WARNING);
timer.Once(1f, () => { cargoShip.Kill(); });
Vector3 newPostition = GetFixedPosition(cargoShip.transform.position);
timer.Once(2f, () => { SpawnCargoShip(newPostition); });
}
else
{
Log($"CargoShip spawned in liveable area properly", logType: LogType.INFO);
Log($"{cargoShip.transform.position.x}|{cargoShip.transform.position.y}|{cargoShip.transform.position.z}", logType: LogType.INFO);
}
}
}
void Unload()
{
if (currentPositionLogTimer != null)
{
currentPositionLogTimer.Destroy();
}
}
#endregion
#region Core
bool IsInLivableArea(Vector3 originalPosition)
{
if (originalPosition.x < -(mapLimit)) return false;
if (originalPosition.x > mapLimit) return false;
if (originalPosition.z < -(mapLimit)) return false;
if (originalPosition.z > mapLimit) return false;
return true;
}
Vector3 GetFixedPosition(Vector3 originalPosition)
{
Vector3 newPosition = originalPosition;
if (originalPosition.x < -(mapLimit)) newPosition.x = -(mapLimit) + 100;
if (originalPosition.x > mapLimit) newPosition.x = mapLimit - 100;
if (originalPosition.z < -(mapLimit)) newPosition.z = -(mapLimit) + 100;
if (originalPosition.z > mapLimit) newPosition.z = mapLimit - 100;
return newPosition;
}
void SpawnCargoShip(Vector3 position)
{
Unsubscribe(nameof(OnEntitySpawned));
var cargoShip = GameManager.server.CreateEntity("assets/content/vehicles/boats/cargoship/cargoshiptest.prefab") as CargoShip;
if (cargoShip == null) return;
cargoShip.transform.SetPositionAndRotation(position, new Quaternion());
cargoShip.TriggeredEventSpawn();
cargoShip.ServerPosition = position;
cargoShip.Spawn();
currentPositionLogTimer = timer.Repeat(30, 5, () => LogCurrentPosition(cargoShip));
Subscribe(nameof(OnEntitySpawned));
Log($"Standby CargoShip spawned: {cargoShip.transform.position.x}|{cargoShip.transform.position.y}|{cargoShip.transform.position.z}", logType: LogType.INFO);
}
void LogCurrentPosition(CargoShip cargoShip)
{
if (cargoShip == null || cargoShip.IsDestroyed) return;
Log($"Current position: {cargoShip.transform.position.x}|{cargoShip.transform.position.y}|{cargoShip.transform.position.z} ", logType: LogType.INFO);
}
#endregion
#region Config
private ConfigData configData;
private class ConfigData
{
[JsonProperty(PropertyName = "LogInFile")]
public bool LogInFile;
[JsonProperty(PropertyName = "LogInConsole")]
public bool LogInConsole;
}
protected override void LoadConfig()
{
try
{
base.LoadConfig();
configData = Config.ReadObject<ConfigData>();
if (configData == null)
{
LoadDefaultConfig();
}
}
catch
{
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
configData = new ConfigData()
{
LogInFile = true,
LogInConsole = true
};
}
protected override void SaveConfig()
{
Config.WriteObject(configData, true);
base.SaveConfig();
}
#endregion
#region Log
void Log(string message, bool console = false, LogType logType = LogType.INFO, string fileName = "")
{
if (configData.LogInFile)
{
LogToFile(fileName, $"[{DateTime.Now.ToString("hh:mm:ss")}] {logType} > {message}", this);
}
if (configData.LogInConsole)
{
Puts($"{message.Replace("\n", " ")}");
}
}
enum LogType
{
INFO = 0,
WARNING = 1,
ERROR = 2
}
#endregion
}
}