internal override void Receive() { if (!Config.IsSinglePlayer) { lock (TcpClient) { base.Receive(); var bytes = ReadStream(DataLength); CommandType = (ServerCommandType)BitConverter.ToUInt16(bytes, 0); Param = BitConverter.ToSingle(bytes, sizeof(ushort)); } } switch (CommandType) { case ServerCommandType.ServerVersion: new ServerMsg(Settings.VersionDisplay, ConnectedPlayer).Send(); break; case ServerCommandType.WorldSize: string message = !File.Exists(Settings.WorldFilePath) ? string.Format("World file not found @ {0}", Settings.WorldFilePath) : string.Format("World size {0}x{1} ({2} KB)", WorldData.SizeInBlocksX, WorldData.SizeInBlocksZ, new FileInfo(Settings.WorldFilePath).Length / 1024); new ServerMsg(message, ConnectedPlayer).Send(); break; case ServerCommandType.MoveSun: if (IsAdmin) { Debug.Assert(Param >= 0 && Param <= 360, "Invalid degrees to move sun: " + Param); SkyHost.SunAngleRadians = OpenTK.MathHelper.DegreesToRadians(Param); //send a sync to each player immediately foreach (var player in Server.Controller.Players.Values) { new ServerSync(SkyHost.SunAngleRadians, player).Send(); } //send confirmation to player that made the change new ServerMsg(string.Format("Moved sun to {0} degrees.", Param), ConnectedPlayer).Send(); } else { ConnectedPlayer.SendAdminRequiredMessage(); } break; case ServerCommandType.Broadcast: if (IsAdmin) { ServerMsg.Broadcast("Player sent broadcast message."); //theres no way to send the actual message from the client to broadcast yet } else { ConnectedPlayer.SendAdminRequiredMessage(); } break; } }
internal override void Receive() { if (!Config.IsSinglePlayer) { lock (TcpClient) { base.Receive(); var bytes = ReadStream(DataLength); Option = (OptionType)BitConverter.ToUInt16(bytes, 0); Value = new byte[BitConverter.ToInt32(bytes, sizeof(ushort))]; Buffer.BlockCopy(bytes, sizeof(ushort) + sizeof(int), Value, 0, Value.Length); } } if (Config.IsServer) { switch (Option) { case OptionType.Admin: if (ConnectedPlayer.IsAdmin) { new ServerMsg("You are already an Admin.", ConnectedPlayer).Send(); } else if (System.Text.Encoding.UTF8.GetString(Value) == Server.Controller.AdminPassword) { ConnectedPlayer.IsAdmin = true; ServerMsg.Broadcast(string.Format("{0} is now an Admin", ConnectedPlayer.UserName)); } else { new ServerMsg("Invalid Admin Password.", ConnectedPlayer).Send(); return; } break; case OptionType.Creative: if (Value.Length != sizeof(int)) { throw new Exception("Invalid value length."); } if (ConnectedPlayer.IsAdmin) { ConnectedPlayer.IsCreative = (BitConverter.ToInt32(Value, 0) != 0); ServerMsg.Broadcast(string.Format("{0} is {1} in Creative mode", ConnectedPlayer.UserName, BitConverter.ToInt32(Value, 0) != 0 ? "now" : "no longer")); } else { ConnectedPlayer.SendAdminRequiredMessage(); return; } break; case OptionType.Speed: if (Value.Length != sizeof(int)) { throw new Exception("Invalid value length."); } if (ConnectedPlayer.IsAdmin) { ConnectedPlayer.MoveSpeedMultiplier = BitConverter.ToInt32(Value, 0); } else { ConnectedPlayer.SendAdminRequiredMessage(); return; } break; default: throw new Exception("Unknown PlayerOption: " + Option); } Server.Controller.UpdateServerConsolePlayerList(); new PlayerOption(Option, Value) { ConnectedPlayer = ConnectedPlayer }.Send(); } else { switch (Option) { case OptionType.Admin: if (Config.IsSinglePlayer) { Game.UiHost.AddChatMessage(new ChatMessage(ChatMessageType.SlashResult, "Command not necessary in Single Player mode.")); } break; case OptionType.Creative: if (Value.Length != sizeof(int)) { throw new Exception("Invalid value length."); } Config.CreativeMode = (BitConverter.ToInt32(Value, 0) != 0); Game.UiHost.AddChatMessage(new ChatMessage(ChatMessageType.SlashResult, string.Format("Creative Mode: {0}", BitConverter.ToInt32(Value, 0) != 0 ? "On (middle mouse button toggles flying)" : "Off"))); break; case OptionType.Speed: if (Value.Length != sizeof(int)) { throw new Exception("Invalid value length."); } Settings.MoveSpeed = Constants.MOVE_SPEED_DEFAULT * BitConverter.ToInt32(Value, 0); Settings.JumpSpeed = Constants.INITIAL_JUMP_VELOCITY * BitConverter.ToInt32(Value, 0); Game.UiHost.AddChatMessage(new ChatMessage(ChatMessageType.SlashResult, string.Format("Move speed: {0}x", BitConverter.ToInt32(Value, 0)))); break; } } }