Fly no longer spawns a new thread

This commit is contained in:
UnknownShadow200 2017-05-31 23:19:28 +10:00
parent 5411ee4f97
commit 1e6cf8fe7b
4 changed files with 102 additions and 77 deletions

View File

@ -19,6 +19,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using MCGalaxy.Maths; using MCGalaxy.Maths;
using MCGalaxy.Tasks;
namespace MCGalaxy.Commands.Misc { namespace MCGalaxy.Commands.Misc {
public sealed class CmdFly : Command { public sealed class CmdFly : Command {
@ -36,29 +37,39 @@ namespace MCGalaxy.Commands.Misc {
p.isFlying = !p.isFlying; p.isFlying = !p.isFlying;
if (!p.isFlying) return; if (!p.isFlying) return;
Player.Message(p, "You are now flying. &cJump!"); Player.Message(p, "You are now flying. &cJump!");
Thread flyThread = new Thread(() => FlyThread(p)); FlyState state = new FlyState();
flyThread.Name = "MCG_Fly"; state.player = p;
flyThread.Start(); SchedulerTask task = new SchedulerTask(FlyCallback, state, TimeSpan.Zero, true);
p.CriticalTasks.Add(task);
} }
void FlyThread(Player p) { class FlyState {
Position oldpos = default(Position); public Player player;
List<Vec3U16> last = new List<Vec3U16>(), next = new List<Vec3U16>(); public Position oldPos = default(Position);
while (p.isFlying && !p.disconnected) public List<Vec3U16> last = new List<Vec3U16>();
DoFly(p, ref oldpos, last, next); public List<Vec3U16> next = new List<Vec3U16>();
}
foreach (Vec3U16 cP in last) static void FlyCallback(SchedulerTask task) {
FlyState state = (FlyState)task.State;
Player p = state.player;
if (state.player.isFlying) { DoFly(state); return; }
foreach (Vec3U16 cP in state.last) {
p.SendBlockchange(cP.X, cP.Y, cP.Z, ExtBlock.Air); p.SendBlockchange(cP.X, cP.Y, cP.Z, ExtBlock.Air);
Player.Message(p, "Stopped flying");
} }
void DoFly(Player p, ref Position old, List<Vec3U16> last, List<Vec3U16> next) { Player.Message(p, "Stopped flying");
Thread.Sleep(20); task.Repeating = false;
if (p.Pos == old) return; }
static void DoFly(FlyState state) {
Player p = state.player;
if (p.Pos == state.oldPos) return;
try {
int x = p.Pos.BlockX, z = p.Pos.BlockZ; int x = p.Pos.BlockX, z = p.Pos.BlockZ;
int y = (p.Pos.Y - 60) / 32; int y = (p.Pos.Y - 60) / 32;
ExtBlock glass = (ExtBlock)Block.glass; ExtBlock glass = (ExtBlock)Block.glass;
@ -72,25 +83,25 @@ namespace MCGalaxy.Commands.Misc {
Vec3U16 pos; Vec3U16 pos;
pos.X = offX; pos.Y = offY; pos.Z = offZ; pos.X = offX; pos.Y = offY; pos.Z = offZ;
next.Add(pos); state.next.Add(pos);
} }
foreach (Vec3U16 P in next) { foreach (Vec3U16 P in state.next) {
if (last.Contains(P)) continue; if (state.last.Contains(P)) continue;
last.Add(P); state.last.Add(P);
p.SendBlockchange(P.X, P.Y, P.Z, glass); p.SendBlockchange(P.X, P.Y, P.Z, glass);
} }
for (int i = 0; i < last.Count; i++) { for (int i = 0; i < state.last.Count; i++) {
Vec3U16 P = last[i]; Vec3U16 P = state.last[i];
if (next.Contains(P)) continue; if (state.next.Contains(P)) continue;
p.SendBlockchange(P.X, P.Y, P.Z, ExtBlock.Air); p.SendBlockchange(P.X, P.Y, P.Z, ExtBlock.Air);
last.RemoveAt(i); i--; state.last.RemoveAt(i); i--;
} }
next.Clear();
} catch (Exception ex) { Server.ErrorLog(ex); } state.next.Clear();
old = p.Pos; state.oldPos = p.Pos;
} }
public override void Help(Player p) { public override void Help(Player p) {

View File

@ -23,6 +23,7 @@ using MCGalaxy.Undo;
using MCGalaxy.Maths; using MCGalaxy.Maths;
using MCGalaxy.Events; using MCGalaxy.Events;
using MCGalaxy.Network; using MCGalaxy.Network;
using MCGalaxy.Tasks;
namespace MCGalaxy { namespace MCGalaxy {
@ -146,6 +147,7 @@ namespace MCGalaxy {
public bool staticCommands = false; public bool staticCommands = false;
public DateTime ZoneSpam; public DateTime ZoneSpam;
public VolatileArray<SchedulerTask> CriticalTasks = new VolatileArray<SchedulerTask>(false);
public bool aiming; public bool aiming;
public bool isFlying = false; public bool isFlying = false;

View File

@ -276,6 +276,7 @@ namespace MCGalaxy {
leftServer = true; leftServer = true;
if (chatMsg != null) chatMsg = Colors.EscapeColors(chatMsg); if (chatMsg != null) chatMsg = Colors.EscapeColors(chatMsg);
discMsg = Colors.EscapeColors(discMsg); discMsg = Colors.EscapeColors(discMsg);
CriticalTasks.Clear();
//Umm...fixed? //Umm...fixed?
if (name == "") { if (name == "") {

View File

@ -34,8 +34,14 @@ namespace MCGalaxy.Tasks {
for (int i = 0; i < players.Length; i++) { for (int i = 0; i < players.Length; i++) {
try { try {
Player p = players[i]; TickPlayer(players[i]);
} catch (Exception e) {
Server.ErrorLog(e);
}
}
}
static void TickPlayer(Player p) {
if (p.following != "") { if (p.following != "") {
Player who = PlayerInfo.FindExact(p.following); Player who = PlayerInfo.FindExact(p.following);
if (who == null || who.level != p.level) { if (who == null || who.level != p.level) {
@ -44,7 +50,7 @@ namespace MCGalaxy.Tasks {
p.canBuild = true; p.canBuild = true;
if (who != null && who.possess == p.name) if (who != null && who.possess == p.name)
who.possess = ""; who.possess = "";
continue; return;
} }
p.SendPos(Entities.SelfID, who.Pos, who.Rot); p.SendPos(Entities.SelfID, who.Pos, who.Rot);
@ -59,9 +65,14 @@ namespace MCGalaxy.Tasks {
p.CheckSurvival(P.X, P.Y, P.Z); p.CheckSurvival(P.X, P.Y, P.Z);
p.CheckBlock(); p.CheckBlock();
p.oldIndex = p.level.PosToInt(P.X, P.Y, P.Z); p.oldIndex = p.level.PosToInt(P.X, P.Y, P.Z);
} catch (Exception e) {
Server.ErrorLog(e); SchedulerTask[] tasks = p.CriticalTasks.Items;
} for (int i = 0; i < tasks.Length; i++) {
SchedulerTask task = tasks[i];
task.Callback(task);
if (task.Repeating) continue;
p.CriticalTasks.Remove(task);
} }
} }