/
PLRecord.cs
175 lines (147 loc) · 6.3 KB
/
PLRecord.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
using System;
using System.IO;
using MCGalaxy;
using MCGalaxy.Tasks;
namespace MCGalaxy
{
public class PLRecord : Plugin
{
public override string creator { get { return "LJplayers"; } }
public override string MCGalaxy_Version { get { return "1.9.2.8"; } }
public override string name { get { return "PLRecord"; } }
public override void Load(bool startup)
{
Command.Register(new CmdPLR());
}
public override void Unload(bool shutdown)
{
Command.Unregister(Command.Find("PLR"));
}
}
public class CmdPLR : Command2
{
public override string name { get { return "PLR"; } }
public override string type { get { return "other"; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public override void Use(Player p, string message)
{
string[] args = message.SplitSpaces();
if (args[0] == "record")
{
if (args.Length < 3) { p.Message("%S- type /help PLR for list of cmds"); return; }
p.Message("%S-started recording");
p.cmdTimer = true;
TimerArgs targs = new TimerArgs();
targs.Message = args[1];
targs.Repeats = (Int32.Parse(args[2]) * 10);
targs.Player = p;
targs.Bot = "null";
Server.MainScheduler.QueueRepeat(TimerCallback, targs, TimeSpan.FromMilliseconds(100));
}
else if (args[0] == "play")
{
if (args.Length < 2) { p.Message("%S- type /help PLR for list of cmds"); return; }
p.cmdTimer = true;
TimerArgs targs = new TimerArgs();
targs.Message = args[1];
targs.Repeats = (-1);
targs.Player = p;
targs.Bot = "null";
Server.MainScheduler.QueueRepeat(TimerCallback, targs, TimeSpan.FromMilliseconds(100));
}
else if (args[0] == "playbot")
{
if (args.Length < 3) { p.Message("%S- type /help PLR for list of cmds"); return; }
p.cmdTimer = true;
TimerArgs targs = new TimerArgs();
targs.Message = args[1];
targs.Repeats = (-1);
targs.Player = p;
targs.Bot = args[2];
Server.MainScheduler.QueueRepeat(TimerCallback, targs, TimeSpan.FromMilliseconds(100));
}
else if (args[0] == "stop")
{
p.cmdTimer = false;
}
else if (args[0] == "all")
{
DirectoryInfo d = new DirectoryInfo("PLR/");
FileInfo[] Files = d.GetFiles("*.txt");
string str = "";
foreach (FileInfo file in Files)
{
str = str + file.Name + ", ";
}
p.Message(str);
}
else p.Message("%S- type /help PLR for list of cmds");
}
class TimerArgs
{
public string Message;
public int Repeats;
public Player Player;
public string Bot;
}
static void TimerCallback(SchedulerTask task)
{
TimerArgs targs = (TimerArgs)task.State;
Player p = targs.Player;
if (targs.Repeats == 0 || p.cmdTimer == false)
{
p.Message("Stopped recording/playing path.");
task.Repeating = false;
}
else if (targs.Repeats > 0)
{
string path = ("PLR/" + p.name + targs.Message + ".txt");
string data = (p.Pos.X.ToString() + " " + p.Pos.Y.ToString() + " " + p.Pos.Z.ToString() + " " + p.Rot.HeadX.ToString() + " " + p.Rot.RotY.ToString() + "\n");
if (!File.Exists(path)) File.WriteAllText(path, targs.Message + "\n");
File.AppendAllText(Path.Combine(path), data);
}
else if (targs.Bot != "null")
{
PlayerBot bot = Matcher.FindBots(p, targs.Bot);
string path = ("PLR/" + targs.Message + ".txt");
string[] poslist = File.ReadAllLines(path);
if (Math.Abs(targs.Repeats) >= poslist.Length)
{
p.cmdTimer = false;
task.Repeating = false;
return;
}
string[] newpos = poslist[Math.Abs(targs.Repeats)].SplitSpaces();
Position pos = new Position(Int32.Parse(newpos[0]), Int32.Parse(newpos[1]), Int32.Parse(newpos[2]));
bot.Pos = pos; bot.SetYawPitch(Byte.Parse(newpos[4]), Byte.Parse(newpos[3]));
//p.Level.Message(poslist.Length.ToString());
}
else
{
string path = ("PLR/" + targs.Message + ".txt");
string[] poslist = File.ReadAllLines(path);
if (Math.Abs(targs.Repeats) >= poslist.Length)
{
p.cmdTimer = false;
task.Repeating = false;
return;
}
string[] newpos = poslist[Math.Abs(targs.Repeats)].SplitSpaces();
Position pos = new Position(Int32.Parse(newpos[0]), Int32.Parse(newpos[1]), Int32.Parse(newpos[2]));
p.SendPosition(pos, new Orientation(Byte.Parse(newpos[4]), Byte.Parse(newpos[3])));
//p.Level.Message(poslist.Length.ToString());
}
targs.Repeats--;
}
public override void Help(Player p)
{
p.Message("%S- for recording players path and playing it back");
p.Message("%A/PLR record [filename] [runtime in secounds]");
p.Message("%A/PLR play [filename]");
p.Message("%A/PLR playbot [filename] [botname]");
p.Message("%A/PLR all %S- shows all recording file names");
p.Message("%A/PLR stop");
p.Message("%S- file names always starts with the player who made them");
}
}
}